Beispiel #1
0
    def test_dns_resolve(self):
        """Test lazy resolve logic."""
        # missing search base.
        # TODO: renable this test once we can firgure out why ctx0.ldap.conn is
        # mocked when running with nosetest and Train
        # ctx0 = context.Context()
        # self.assertRaises(context.ContextError, ctx0.resolve, 'somecell')

        ctx1 = context.Context()
        ctx1.ldap_suffix = 'dc=test'
        ctx1.dns_domain = 'x'
        ctx1.cell = 'somecell'

        treadmill.dnsutils.txt.return_value = [
            'zookeeper://tmtest@xxx:123,yyy:345/treadmill/somecell',
        ]
        treadmill.dnsutils.srv.return_value = [('ldaphost1', 1234, 10, 10),
                                               ('ldaphost2', 2345, 10, 10)]
        ctx1.get('zk_url')
        self.assertEqual(
            'zookeeper://tmtest@xxx:123,yyy:345/treadmill/somecell',
            ctx1.zk.url)
        self.assertEqual(['ldap://*****:*****@xxx:123,yyy:345/treadmill/somecell',
            ctx2.zk.url)
Beispiel #2
0
    def test_dns_resolve(self):
        """Test lazy resolve logic."""
        ctx = context.Context()
        # missing search base.
        self.assertRaises(context.ContextError, ctx.resolve, 'somecell')
        ctx.ldap.search_base = 'ou=treadmill,ou=test'

        treadmill.dnsutils.txt.return_value = [
            'zookeeper://tmtest@xxx:123,yyy:345/treadmill/somecell',
        ]
        treadmill.dnsutils.srv.return_value = [('ldaphost', 1234, 10, 10)]
        ctx.resolve('somecell')
        self.assertEquals(
            'zookeeper://tmtest@xxx:123,yyy:345/treadmill/somecell',
            ctx.zk.url)
        self.assertEquals('ldap://*****:*****@xxx:123,yyy:345/treadmill/somecell',
            ctx_1.zk.url)
Beispiel #3
0
    def test_ldap_resolve(self, cell_factory):
        """Test lazy resolve logic."""
        cell_admin = cell_factory.return_value
        # missing search base.
        # TODO: renable this test once we can firgure out why ctx0.ldap.conn is
        # mocked when running with nosetest and Train
        # ctx0 = context.Context()
        # self.assertRaises(context.ContextError, ctx0.resolve, 'somecell')

        # Missing ldap url
        ctx1 = context.Context()
        ctx1.ldap_suffix = 'dc=test'
        # TODO: renable this test once we can firgure out why ctx0.ldap.conn is
        # mocked when running with nosetest and Train
        # self.assertRaises(context.ContextError, ctx1.resolve, 'somecell')

        # Cell not defined in LDAP.
        ctx2 = context.Context()
        ctx2.cell = 'somecell'
        ctx2.ldap_suffix = 'dc=test'
        ctx2.ldap.url = 'ldap://*****:*****@xxx:123,yyy:345/treadmill/somecell',
            ctx3.zk.url)
Beispiel #4
0
    def test_api_resolve(self):
        """Tests resolving API from DNS srv records."""
        treadmill.dnsutils.srv.return_value = [('xxx', 123, 1, 2),
                                               ('yyy', 234, 3, 4)]

        ctx = context.Context()
        ctx.dns_domain = 'a'
        ctx.profile['api_scope'] = ['ny.campus', 'na.region']
        ctx.cell = 'b'
        self.assertEqual(set(['http://xxx:123', 'http://yyy:234']),
                         set(ctx.cell_api()))
        treadmill.dnsutils.srv.assert_called_with(
            '_http._tcp.cellapi.b.cell.a', mock.ANY)

        self.assertEqual(['x:8080'], ctx.cell_api('x:8080'))

        ctx.cell = None
        self.assertRaises(context.ContextError, ctx.cell_api)
        self.assertEqual(['x:8080'], ctx.cell_api('x:8080'))

        ctx.cell = 'a'
        ctx.dns_domain = None
        self.assertRaises(context.ContextError, ctx.cell_api)
        self.assertEqual(['x:8080'], ctx.cell_api('x:8080'))

        ctx.dns_domain = 'a.com'
        treadmill.dnsutils.srv.return_value = []
        self.assertRaises(context.ContextError, ctx.admin_api)
        treadmill.dnsutils.srv.assert_has_calls([
            mock.call('_http._tcp.adminapi.ny.campus.a.com', mock.ANY),
            mock.call('_http._tcp.adminapi.na.region.a.com', mock.ANY),
        ])
def _count(cell, appname):
    """Get number of instances scheduled/running on the cell."""
    try:
        ctx = context.Context()
        ctx.cell = cell
        ctx.dns_domain = context.GLOBAL.dns_domain

        stateapi = ctx.state_api()
        url = '/state/?' + urllib.urlencode([('match', appname)])

        response = restclient.get(stateapi, url)
        state = response.json()

        for instance in state:
            _LOGGER.info('cell: %s - %s %s %s', cell,
                         instance['name'],
                         instance['state'],
                         instance['host'])

        return len([instance for instance in state
                    if instance['state'] == 'running'])

    except Exception:  # pylint: disable=W0703
        _LOGGER.exception('Unable to get instance count for cell %s, app: %s',
                          cell, appname)
        return 0
Beispiel #6
0
    def test_ldap_resolve(self):
        """Test lazy resolve logic."""
        # missing search base.
        # TODO: renable this test once we can firgure out why ctx0.ldap.conn is
        # mocked when running with nosetest and Train
        # ctx0 = context.Context()
        # self.assertRaises(context.ContextError, ctx0.resolve, 'somecell')

        # Missing ldap url
        ctx1 = context.Context()
        ctx1.ldap.search_base = 'ou=treadmill,ou=test'
        # TODO: renable this test once we can firgure out why ctx0.ldap.conn is
        # mocked when running with nosetest and Train
        # self.assertRaises(context.ContextError, ctx1.resolve, 'somecell')

        # Cell not defined in LDAP.
        ctx2 = context.Context()
        ctx2.ldap.search_base = 'ou=treadmill,ou=test'
        ctx2.ldap.url = 'ldap://*****:*****@xxx:123,yyy:345/treadmill/somecell',
            ctx3.zk.url)
Beispiel #7
0
 def test_api_resolve_override(self):
     """Test DNS resolution of the api with override."""
     ctx = context.Context()
     ctx.dns_domain = 'a.com'
     ctx.cell = 'y'
     treadmill.dnsutils.srv.return_value = [('xxx', 123, 1, 2),
                                            ('yyy', 234, 3, 4)]
     ctx.admin_api()
     ctx.cell_api()
     ctx.ws_api()
     ctx.state_api()
     treadmill.dnsutils.srv.assert_has_calls([
         mock.call('_http._tcp.foo.admin.a.com', mock.ANY),
         mock.call('_http._tcp.foo.cell.a.com', mock.ANY),
         mock.call('_http._tcp.foo.ws.a.com', mock.ANY),
         mock.call('_http._tcp.foo.state.a.com', mock.ANY),
     ])
Beispiel #8
0
    def test_api_resolve(self):
        """Tests resolving API from DNS srv records."""
        treadmill.dnsutils.srv.return_value = [('xxx', 123, 1, 2),
                                               ('yyy', 234, 3, 4)]

        ctx = context.Context()
        ctx.dns_domain = 'a'
        ctx.admin_api_scope = 'na.region'
        ctx.cell = 'b'
        self.assertEquals(['http://xxx:123', 'http://yyy:234'], ctx.cell_api())
        treadmill.dnsutils.srv.assert_called_with(
            '_http._tcp.cellapi.b.cell.a')

        self.assertEquals(['x:8080'], ctx.cell_api('x:8080'))

        ctx.cell = None
        self.assertRaises(context.ContextError, ctx.cell_api)
        self.assertEquals(['x:8080'], ctx.cell_api('x:8080'))

        ctx.cell = 'a'
        ctx.dns_domain = None
        self.assertRaises(context.ContextError, ctx.cell_api)
        self.assertEquals(['x:8080'], ctx.cell_api('x:8080'))
Beispiel #9
0
    def test_ldap_resolve(self):
        """Test lazy resolve logic."""
        ctx = context.Context()
        # missing search base.
        self.assertRaises(context.ContextError, ctx.resolve, 'somecell')
        ctx.ldap.search_base = 'ou=treadmill,ou=test'

        # Missing ldap url
        self.assertRaises(context.ContextError, ctx.resolve, 'somecell')

        ctx.ldap.url = 'ldap://*****:*****@xxx:123,yyy:345/treadmill/somecell',
            ctx.zk.url)
Beispiel #10
0
 def test_default_domain(self):
     """Test default resoltion of dns domain."""
     ctx = context.Context()
     self.assertEqual(ctx.get('dns_domain'), 'b.c.d')