Back to index

obnam  1.1
clientlist_tests.py
Go to the documentation of this file.
00001 # Copyright 2010  Lars Wirzenius
00002 # 
00003 # This program is free software: you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation, either version 3 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 
00017 import shutil
00018 import tempfile
00019 import unittest
00020 
00021 import obnamlib
00022 
00023 
00024 class ClientListTests(unittest.TestCase):
00025 
00026     def setUp(self):
00027         self.tempdir = tempfile.mkdtemp()
00028         fs = obnamlib.LocalFS(self.tempdir)
00029         self.hooks = obnamlib.HookManager()
00030         self.hooks.new('repository-toplevel-init')
00031         self.list = obnamlib.ClientList(fs, 
00032                                         obnamlib.DEFAULT_NODE_SIZE,
00033                                         obnamlib.DEFAULT_UPLOAD_QUEUE_SIZE,
00034                                         obnamlib.DEFAULT_LRU_SIZE, self)
00035 
00036     def tearDown(self):
00037         shutil.rmtree(self.tempdir)
00038 
00039     def test_key_bytes_is_correct_length(self):
00040         self.assertEqual(self.list.key_bytes, 
00041                          len(self.list.key('foo', 12765, 0)))
00042 
00043     def test_unkey_unpacks_key_correctly(self):
00044         key = self.list.key('client name', 12765, 42)
00045         client_hash, client_id, subkey = self.list.unkey(key)
00046         self.assertEqual(client_id, 12765)
00047         self.assertEqual(subkey, 42)
00048 
00049     def test_reports_none_as_id_for_nonexistent_client(self):
00050         self.assertEqual(self.list.get_client_id('foo'), None)
00051         
00052     def test_lists_no_clients_when_tree_does_not_exist(self):
00053         self.assertEqual(self.list.list_clients(), [])
00054 
00055     def test_added_client_has_integer_id(self):
00056         self.list.add_client('foo')
00057         self.assert_(type(self.list.get_client_id('foo')) in [int, long])
00058 
00059     def test_added_client_is_listed(self):
00060         self.list.add_client('foo')
00061         self.list.set_client_keyid('foo', 'cafebeef')
00062         self.assertEqual(self.list.list_clients(), ['foo'])
00063 
00064     def test_removed_client_has_none_id(self):
00065         self.list.add_client('foo')
00066         self.list.remove_client('foo')
00067         self.assertEqual(self.list.get_client_id('foo'), None)
00068         
00069     def test_removed_client_has_no_keys(self):
00070         self.list.add_client('foo')
00071         client_id = self.list.get_client_id('foo')
00072         self.list.remove_client('foo')
00073         minkey = self.list.key('foo', client_id, 0)
00074         maxkey = self.list.key('foo', client_id, self.list.SUBKEY_MAX)
00075         pairs = list(self.list.tree.lookup_range(minkey, maxkey))
00076         self.assertEqual(pairs, [])
00077 
00078     def test_twice_added_client_exists_only_once(self):
00079         self.list.add_client('foo')
00080         self.list.add_client('foo')
00081         self.assertEqual(self.list.list_clients(), ['foo'])
00082 
00083     def test_adding_handles_hash_collision(self):
00084         def bad_hash(string):
00085             return '0' * 16
00086         self.list.hashfunc = bad_hash
00087         self.list.add_client('foo')
00088         self.list.add_client('bar')
00089         self.assertEqual(sorted(self.list.list_clients()), ['bar', 'foo'])
00090         self.assertNotEqual(self.list.get_client_id('bar'),
00091                             self.list.get_client_id('foo'))
00092 
00093     def test_client_has_no_public_key_initially(self):
00094         self.list.add_client('foo')
00095         self.assertEqual(self.list.get_client_keyid('foo'), None)
00096 
00097     def test_sets_client_keyid(self):
00098         self.list.add_client('foo')
00099         self.list.set_client_keyid('foo', 'cafebeef')
00100         self.assertEqual(self.list.get_client_keyid('foo'), 'cafebeef')
00101 
00102     def test_remove_client_keyid(self):
00103         self.list.add_client('foo')
00104         self.list.set_client_keyid('foo', 'cafebeef')
00105         self.list.set_client_keyid('foo', None)
00106         self.assertEqual(self.list.get_client_keyid('foo'), None)
00107