Example #1
0
    def test_get_file_contents(self):
        pointer_zoo_watcher = ZookeeperWatcher(hosts=self.hosts,
                                               filepath=self.pointer_path,
                                               pointer=True,
                                               ensure=False,
                                               valid_init=True)

        self.assertEqual(self.zoo_watcher.get_file_contents(), self.file_data)
        self.assertEqual(pointer_zoo_watcher.get_file_contents(),
                         self.file_data)
        self.assertEqual(pointer_zoo_watcher.get_file_contents(True),
                         self.pointer_data)

        pointer_zoo_watcher.close()
Example #2
0
class TestZookeeperWatcher(TestCase):
    def __init__(self, name, hosts):
        self.hosts = hosts
        self.file_path = '/test_path'
        self.file_data = 'test_data'
        self.pointer_path = '/test_pointer'
        self.pointer_data = self.file_path
        TestCase.__init__(self, name)

    def setUp(self):
        self.zoo_client = KazooClient(hosts=self.hosts)
        self.zoo_client.start()
        # prepare data
        self.zoo_client.ensure_path(self.file_path)
        self.zoo_client.set(self.file_path, self.file_data.encode('utf-8'))
        self.zoo_client.ensure_path(self.pointer_path)
        self.zoo_client.set(self.pointer_path,
                            self.pointer_data.encode('utf-8'))

        self.zoo_watcher = ZookeeperWatcher(hosts=self.hosts,
                                            filepath=self.file_path,
                                            pointer=False,
                                            ensure=False,
                                            valid_init=True)

    def test_ping(self):
        self.assertTrue(self.zoo_watcher.ping())

    def test_get_file_contents(self):
        pointer_zoo_watcher = ZookeeperWatcher(hosts=self.hosts,
                                               filepath=self.pointer_path,
                                               pointer=True,
                                               ensure=False,
                                               valid_init=True)

        self.assertEqual(self.zoo_watcher.get_file_contents(), self.file_data)
        self.assertEqual(pointer_zoo_watcher.get_file_contents(),
                         self.file_data)
        self.assertEqual(pointer_zoo_watcher.get_file_contents(True),
                         self.pointer_data)

        pointer_zoo_watcher.close()

    def tearDown(self):
        self.zoo_watcher.close()

        self.zoo_client.ensure_path(self.file_path)
        self.zoo_client.delete(self.file_path)
        self.zoo_client.ensure_path(self.pointer_path)
        self.zoo_client.delete(self.pointer_path)
        self.zoo_client.stop()
        self.zoo_client.close()
Example #3
0
class TestZookeeperWatcher(TestCase):

    def setUp(self):

        zoo_client = MagicMock()
        zoo_client.get = MagicMock(return_value=('data', 'blah'))

        with patch('scutils.zookeeper_watcher.KazooClient') as k:
            k.return_value = zoo_client
            self.zoo_watcher = ZookeeperWatcher(
                                hosts='localhost',
                                filepath='/mypath',
                                pointer=False, ensure=True,
                                valid_init=True)

    def test_ping(self):
        self.zoo_watcher.zoo_client.server_version = MagicMock()
        self.assertTrue(self.zoo_watcher.ping())
        self.zoo_watcher.zoo_client.server_version = MagicMock(side_effect=KazooException)
        self.assertFalse(self.zoo_watcher.ping())

    def test_get_file_contents(self):
        self.zoo_watcher.old_pointed = 'old_pointed'
        self.zoo_watcher.old_data = 'old_data'

        self.zoo_watcher.pointer = False
        self.assertEquals(self.zoo_watcher.get_file_contents(), 'old_data')

        self.zoo_watcher.pointer = True
        self.assertEquals(self.zoo_watcher.get_file_contents(), 'old_data')

        self.zoo_watcher.pointer = True
        self.assertEquals(self.zoo_watcher.get_file_contents(True), 'old_pointed')

    def test_compare_pointer(self):
        self.zoo_watcher.old_pointed = '/path1'

        self.assertTrue(self.zoo_watcher.compare_pointer('/path2'))

        self.zoo_watcher.old_pointed = '/path1'

        self.assertFalse(self.zoo_watcher.compare_pointer('/path1'))

    def test_compare_data(self):
        self.zoo_watcher.old_data = 'old_data'

        self.assertTrue(self.zoo_watcher.compare_data('new_data'))

        self.zoo_watcher.old_data = 'same_data'
        self.assertFalse(self.zoo_watcher.compare_data('same_data'))

    def test_set_valid(self):
        self.zoo_watcher.is_valid = MagicMock(return_value=True)
        self.zoo_watcher.valid_handler = MagicMock()
        self.zoo_watcher.set_valid(False)

        self.zoo_watcher.valid_handler.assert_called_once_with(True)

    def test_call_valid(self):
        self.the_bool = False
        def the_set(state):
            self.the_bool = True

        self.zoo_watcher.valid_handler = the_set
        self.zoo_watcher.call_valid(True)

        self.assertTrue(self.the_bool)

    def test_call_config(self):
        self.the_bool = False
        def the_set(state):
            self.the_bool = True

        self.zoo_watcher.config_handler = the_set
        self.zoo_watcher.call_config(True)

        self.assertTrue(self.the_bool)

    def test_call_error(self):
        self.the_bool = False
        def the_set(state):
            self.the_bool = True

        self.zoo_watcher.error_handler = the_set
        self.zoo_watcher.call_error(True)

        self.assertTrue(self.the_bool)
Example #4
0
    elif event:
        zoo_watcher = ZookeeperWatcher(hosts,
                                       file,
                                       valid_handler=valid_file,
                                       config_handler=change_file,
                                       error_handler=error_file,
                                       pointer=True,
                                       ensure=True,
                                       valid_init=valid)
else:
    if poll:
        zoo_watcher = ZookeeperWatcher(hosts, file, ensure=True)
    elif event:
        zoo_watcher = ZookeeperWatcher(hosts,
                                       file,
                                       valid_handler=valid_file,
                                       config_handler=change_file,
                                       error_handler=error_file,
                                       valid_init=valid,
                                       ensure=True)

print "Use a keyboard interrupt to shut down the process."
try:
    while True:
        if poll:
            print "Valid File?", zoo_watcher.is_valid()
            print "Contents:", zoo_watcher.get_file_contents()
        sleep(sleep_time)
except:
    pass
zoo_watcher.close()
Example #5
0
class TestZookeeperWatcher(TestCase):
    def setUp(self):

        zoo_client = MagicMock()
        zoo_client.get = MagicMock(return_value=('data', 'blah'))

        with patch('scutils.zookeeper_watcher.KazooClient') as k:
            k.return_value = zoo_client
            self.zoo_watcher = ZookeeperWatcher(hosts='localhost',
                                                filepath='/mypath',
                                                pointer=False,
                                                ensure=True,
                                                valid_init=True)

    def test_ping(self):
        self.zoo_watcher.zoo_client.server_version = MagicMock()
        self.assertTrue(self.zoo_watcher.ping())
        self.zoo_watcher.zoo_client.server_version = MagicMock(
            side_effect=KazooException)
        self.assertFalse(self.zoo_watcher.ping())

    def test_get_file_contents(self):
        self.zoo_watcher.old_pointed = 'old_pointed'
        self.zoo_watcher.old_data = 'old_data'

        self.zoo_watcher.pointer = False
        self.assertEquals(self.zoo_watcher.get_file_contents(), 'old_data')

        self.zoo_watcher.pointer = True
        self.assertEquals(self.zoo_watcher.get_file_contents(), 'old_data')

        self.zoo_watcher.pointer = True
        self.assertEquals(self.zoo_watcher.get_file_contents(True),
                          'old_pointed')

    def test_compare_pointer(self):
        self.zoo_watcher.old_pointed = '/path1'

        self.assertTrue(self.zoo_watcher.compare_pointer('/path2'))

        self.zoo_watcher.old_pointed = '/path1'

        self.assertFalse(self.zoo_watcher.compare_pointer('/path1'))

    def test_compare_data(self):
        self.zoo_watcher.old_data = 'old_data'

        self.assertTrue(self.zoo_watcher.compare_data('new_data'))

        self.zoo_watcher.old_data = 'same_data'
        self.assertFalse(self.zoo_watcher.compare_data('same_data'))

    def test_set_valid(self):
        self.zoo_watcher.is_valid = MagicMock(return_value=True)
        self.zoo_watcher.valid_handler = MagicMock()
        self.zoo_watcher.set_valid(False)

        self.zoo_watcher.valid_handler.assert_called_once_with(True)

    def test_call_valid(self):
        self.the_bool = False

        def the_set(state):
            self.the_bool = True

        self.zoo_watcher.valid_handler = the_set
        self.zoo_watcher.call_valid(True)

        self.assertTrue(self.the_bool)

    def test_call_config(self):
        self.the_bool = False

        def the_set(state):
            self.the_bool = True

        self.zoo_watcher.config_handler = the_set
        self.zoo_watcher.call_config(True)

        self.assertTrue(self.the_bool)

    def test_call_error(self):
        self.the_bool = False

        def the_set(state):
            self.the_bool = True

        self.zoo_watcher.error_handler = the_set
        self.zoo_watcher.call_error(True)

        self.assertTrue(self.the_bool)
Example #6
0
# You can use any or all of these, polling + handlers, some handlers, etc
if pointer:
    if poll:
        zoo_watcher = ZookeeperWatcher(hosts, file, ensure=True,pointer=True)
    elif event:
        zoo_watcher = ZookeeperWatcher(hosts, file,
                                       valid_handler=valid_file,
                                       config_handler=change_file,
                                       error_handler=error_file,
                                       pointer=True, ensure=True, valid_init=valid)
else:
    if poll:
        zoo_watcher = ZookeeperWatcher(hosts, file, ensure=True)
    elif event:
        zoo_watcher = ZookeeperWatcher(hosts, file,
                                       valid_handler=valid_file,
                                       config_handler=change_file,
                                       error_handler=error_file,
                                       valid_init=valid, ensure=True)

print "Use a keyboard interrupt to shut down the process."
try:
    while True:
        if poll:
            print "Valid File?", zoo_watcher.is_valid()
            print "Contents:", zoo_watcher.get_file_contents()
        sleep(sleep_time)
except:
    pass
zoo_watcher.close()