示例#1
0
class TestJonesClient(KazooTestCase):

    def setUp(self):
        super(TestJonesClient, self).setUp()
        self.config = None
        self.service = 'testservice'
        self.hostname = '127.0.0.2'
        self.ev = threading.Event()

        self.jones = Jones(self.service, self.client)
        fixture.init_tree(self.jones)
        self.jones_client = JonesClient(self.client, self.service,
                                        self.default_cb, self.hostname)

    def default_cb(self, config):
        self.config = config
        self.ev.set()

    def test_gets_config(self):
        self.ev.wait(MAGIC_NUMBER)
        self.assertEquals(self.jones_client, fixture.CHILD1)
        self.assertEquals(self.config, fixture.CHILD1)

    def test_default_value(self):
        self.ev.wait(MAGIC_NUMBER)
        self.assertEquals(self.jones_client.get('a'), fixture.CHILD1['a'])
        self.assertEquals(self.jones_client.get('notinhere', 1), 1)

    def test_raises_with_no_environment(self):
        self.assertRaises(
            EnvironmentNotFoundException,
            JonesClient,
            self.client, 'newservice'
        )

    def test_responds_to_remap(self):
        """test that changing the associations updates config properly."""

        self.ev.clear()
        self.jones.assoc_host(self.hostname, Env('parent'))
        self.ev.wait(MAGIC_NUMBER)
        self.assertEquals(self.config, fixture.PARENT)

    def test_defaults_to_root(self):
        """
        If a hostname doesn't map to anything,
        make sure to default to the root.
        That way we don't have to add every
        host under our control to zk.
        """

        ev = threading.Event()
        hostname = '0.0.0.0'
        self.assertTrue(hostname not in fixture.ASSOCIATIONS.keys())

        def cb(config):
            ev.set()

        client = JonesClient(self.client, self.service, cb, hostname)
        ev.wait(MAGIC_NUMBER)
        self.assertEquals(client.config, fixture.CONFIG['root'])
        self.assertTrue(ev.isSet())

        ev.clear()
        self.jones.assoc_host(hostname, Env('parent'))
        ev.wait(MAGIC_NUMBER)
        self.assertEquals(client.config, fixture.PARENT)
        self.assertTrue(ev.isSet())

    def test_works_if_zk_down(self):
        self.assertEquals(self.config, fixture.CHILD1)
        self.expire_session()
        self.assertEquals(self.config, fixture.CHILD1)

    def test_resets_watches(self):
        def test(fixt):
            self.ev.clear()
            self.jones.set_config(Env('parent'), {'k': fixt}, -1)
            self.ev.wait(MAGIC_NUMBER)
            self.assertEquals(self.config['k'], fixt)

        for fixt in ('a', 'b', 'c'):
            test(fixt)
示例#2
0
class TestJonesClient(KazooTestCase):
    def setUp(self):
        super(TestJonesClient, self).setUp()
        self.config = None
        self.service = 'testservice'
        self.hostname = '127.0.0.2'
        self.ev = threading.Event()

        self.jones = Jones(self.service, self.client)
        fixture.init_tree(self.jones)
        self.jones_client = JonesClient(self.client, self.service,
                                        self.default_cb, self.hostname)

    def default_cb(self, config):
        self.config = config
        self.ev.set()

    def test_gets_config(self):
        self.ev.wait(MAGIC_NUMBER)
        self.assertEquals(self.jones_client, fixture.CHILD1)
        self.assertEquals(self.config, fixture.CHILD1)

    def test_default_value(self):
        self.ev.wait(MAGIC_NUMBER)
        self.assertEquals(self.jones_client.get('a'), fixture.CHILD1['a'])
        self.assertEquals(self.jones_client.get('notinhere', 1), 1)

    def test_raises_with_no_environment(self):
        self.assertRaises(EnvironmentNotFoundException, JonesClient,
                          self.client, 'newservice')

    def test_responds_to_remap(self):
        """test that changing the associations updates config properly."""

        self.ev.clear()
        self.jones.assoc_host(self.hostname, Env('parent'))
        self.ev.wait(MAGIC_NUMBER)
        self.assertEquals(self.config, fixture.PARENT)

    def test_defaults_to_root(self):
        """
        If a hostname doesn't map to anything,
        make sure to default to the root.
        That way we don't have to add every
        host under our control to zk.
        """

        ev = threading.Event()
        hostname = '0.0.0.0'
        self.assertTrue(hostname not in fixture.ASSOCIATIONS.keys())

        def cb(config):
            ev.set()

        client = JonesClient(self.client, self.service, cb, hostname)
        ev.wait(MAGIC_NUMBER)
        self.assertEquals(client.config, fixture.CONFIG['root'])
        self.assertTrue(ev.isSet())

        ev.clear()
        self.jones.assoc_host(hostname, Env('parent'))
        ev.wait(MAGIC_NUMBER)
        self.assertEquals(client.config, fixture.PARENT)
        self.assertTrue(ev.isSet())

    def test_works_if_zk_down(self):
        self.assertEquals(self.config, fixture.CHILD1)
        self.expire_session()
        self.assertEquals(self.config, fixture.CHILD1)

    def test_resets_watches(self):
        def test(fixt):
            self.ev.clear()
            self.jones.set_config(Env('parent'), {'k': fixt}, -1)
            self.ev.wait(MAGIC_NUMBER)
            self.assertEquals(self.config['k'], fixt)

        for fixt in ('a', 'b', 'c'):
            test(fixt)
示例#3
0
class TestJones(KazooTestCase):

    def setUp(self):
        super(TestJones, self).setUp()

        self.jones = Jones('testservice', self.client)

    def test_creates_root(self):

        fixt = {'xy': 'z'}
        self.jones.create_config(Env.Root, fixt)
        print json.loads(self.client.get(self.jones.view_path)[0])
        self.assertEquals(
            json.loads(self.client.get(self.jones.view_path)[0]),
            fixt
        )

    def test_jones(self):

        fixture.init_tree(self.jones)
        self.assertEquals(
            fixture.CHILD1,
            self.jones.get_config('127.0.0.2')
        )

    def test_overwrites(self):
        self.jones.create_config(Env.Root, {"foo": "bar"})
        self.jones.set_config(Env.Root, {"foo": "baz"}, -1)

        self.assertEquals(
            self.jones._get(self.jones.conf_path)[1]['foo'],
            'baz'
        )

    def test_parent_changed(self):
        fixture.init_tree(self.jones)
        parent = dict(fixture.CONFIG['parent'])
        parent['new'] = 'key'
        self.jones.set_config(Env('parent'), parent, 0)
        #self.client.print_tree('/services')

        for i in ('127.0.0.1', '127.0.0.2'):
            config = self.jones.get_config(i)
            self.assertEquals(config.get('b'), [1, 2, 3],
                              "Host %s didn't inherit properly." % i)
            self.assertEquals(config.get('new'), 'key',
                              "Host %s not updated." % i)

    def test_conflicts(self):

        self.jones.create_config(Env.Root, {"foo": "bar"})
        self.jones.set_config(Env.Root, {"foo": "baz"}, 0)

        self.assertEquals(
            self.jones._get(self.jones.conf_path)[1]['foo'],
            'baz'
        )

        self.assertRaises(
            BadVersionException,
            self.jones.set_config,
            Env.Root, {"foo": "bag"}, 4,
        )

    def test_delete_config(self):
        fixture.init_tree(self.jones)
        env = Env('parent/child1')
        self.jones.delete_config(env, -1)

        self.assertRaises(
            NoNodeException,
            self.jones.get_config,
            '127.0.0.2'
        )

        self.assertRaises(
            NoNodeException,
            self.jones.get_config_by_env,
            env
        )

        self.assertRaises(
            NoNodeException,
            self.jones.get_view_by_env,
            env
        )

    def test_conf_is_mapping(self):
        """Make sure create_config only allows collections.Mapping types"""

        self.assertRaises(
            ValueError,
            self.jones.create_config,
            Env.Root, 'hello'
        )

    def test_get_associations(self):
        fixture.init_tree(self.jones)
        assocs = self.jones.associations.get_all()
        for host in fixture.ASSOCIATIONS:
            self.assertEquals(
                assocs[host],
                self.jones._get_view_path(fixture.ASSOCIATIONS[host])
            )

    def test_delete_association(self):
        fixture.init_tree(self.jones)
        self.jones.delete_association('127.0.0.3')
        self.assertRaises(
            KeyError,
            self.jones.get_config,
            '127.0.0.3'
        )
        self.assertTrue(len(self.jones.associations.get_all()) > 0)

    def test_create_service(self):
        """Test that creating a service creates stub conf/view/nodemaps."""

        env = Env.Root
        self.jones.create_config(env, {})
        self.assertEquals(self.jones.get_associations(env), None)
        self.assertEquals(self.jones.get_view_by_env(env), {})
        self.assertEquals(self.jones.get_config_by_env(env)[1], {})
        self.assertEquals(self.jones.get_child_envs(env), [''])

    def test_exists_reflectes_creation(self):
        self.assertFalse(self.jones.exists())
        self.jones.create_config(Env.Root, {})
        self.assertTrue(self.jones.exists())

    def test_delete_service(self):
        """Test that deleting a service removes all sub-nodes."""

        env = Env.Root
        self.jones.create_config(env, {})

        self.jones.delete_all()
        self.assertRaises(
            NoNodeException,
            self.client.get,
            self.jones.root
        )
示例#4
0
class TestJones(KazooTestCase):
    def setUp(self):
        super(TestJones, self).setUp()

        self.jones = Jones('testservice', self.client)

    def test_creates_root(self):

        fixt = {'xy': 'z'}
        self.jones.create_config(Env.Root, fixt)
        print json.loads(self.client.get(self.jones.view_path)[0])
        self.assertEquals(json.loads(self.client.get(self.jones.view_path)[0]),
                          fixt)

    def test_jones(self):

        fixture.init_tree(self.jones)
        self.assertEquals(fixture.CHILD1, self.jones.get_config('127.0.0.2'))

    def test_overwrites(self):
        self.jones.create_config(Env.Root, {"foo": "bar"})
        self.jones.set_config(Env.Root, {"foo": "baz"}, -1)

        self.assertEquals(
            self.jones._get(self.jones.conf_path)[1]['foo'], 'baz')

    def test_parent_changed(self):
        fixture.init_tree(self.jones)
        parent = dict(fixture.CONFIG['parent'])
        parent['new'] = 'key'
        self.jones.set_config(Env('parent'), parent, 0)
        #self.client.print_tree('/services')

        for i in ('127.0.0.1', '127.0.0.2'):
            config = self.jones.get_config(i)
            self.assertEquals(config.get('b'), [1, 2, 3],
                              "Host %s didn't inherit properly." % i)
            self.assertEquals(config.get('new'), 'key',
                              "Host %s not updated." % i)

    def test_conflicts(self):

        self.jones.create_config(Env.Root, {"foo": "bar"})
        self.jones.set_config(Env.Root, {"foo": "baz"}, 0)

        self.assertEquals(
            self.jones._get(self.jones.conf_path)[1]['foo'], 'baz')

        self.assertRaises(
            BadVersionException,
            self.jones.set_config,
            Env.Root,
            {"foo": "bag"},
            4,
        )

    def test_delete_config(self):
        fixture.init_tree(self.jones)
        env = Env('parent/child1')
        self.jones.delete_config(env, -1)

        self.assertRaises(NoNodeException, self.jones.get_config, '127.0.0.2')

        self.assertRaises(NoNodeException, self.jones.get_config_by_env, env)

        self.assertRaises(NoNodeException, self.jones.get_view_by_env, env)

    def test_conf_is_mapping(self):
        """Make sure create_config only allows collections.Mapping types"""

        self.assertRaises(ValueError, self.jones.create_config, Env.Root,
                          'hello')

    def test_get_associations(self):
        fixture.init_tree(self.jones)
        assocs = self.jones.associations.get_all()
        for host in fixture.ASSOCIATIONS:
            self.assertEquals(
                assocs[host],
                self.jones._get_view_path(fixture.ASSOCIATIONS[host]))

    def test_delete_association(self):
        fixture.init_tree(self.jones)
        self.jones.delete_association('127.0.0.3')
        self.assertRaises(KeyError, self.jones.get_config, '127.0.0.3')
        self.assertTrue(len(self.jones.associations.get_all()) > 0)

    def test_create_service(self):
        """Test that creating a service creates stub conf/view/nodemaps."""

        env = Env.Root
        self.jones.create_config(env, {})
        self.assertEquals(self.jones.get_associations(env), None)
        self.assertEquals(self.jones.get_view_by_env(env), {})
        self.assertEquals(self.jones.get_config_by_env(env)[1], {})
        self.assertEquals(self.jones.get_child_envs(env), [''])

    def test_exists_reflectes_creation(self):
        self.assertFalse(self.jones.exists())
        self.jones.create_config(Env.Root, {})
        self.assertTrue(self.jones.exists())

    def test_delete_service(self):
        """Test that deleting a service removes all sub-nodes."""

        env = Env.Root
        self.jones.create_config(env, {})

        self.jones.delete_all()
        self.assertRaises(NoNodeException, self.client.get, self.jones.root)