Exemplo n.º 1
0
 def test_cname_configured_with_empty_storage_domain(self):
     app = cname_lookup.CNAMELookupMiddleware(FakeApp(),
                                              {'storage_domain': '',
                                               'lookup_depth': 2})
     req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
                         headers={'Host': 'c.a.example.com'})
     resp = app(req.environ, start_response)
     self.assertEqual(resp, ['FAKE APP'])
Exemplo n.º 2
0
    def test_resolution_to_storage_domain_exactly(self):
        conf = {'storage_domain': 'example.com', 'lookup_depth': 1}
        app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)

        req = Request.blank('/',
                            environ={'REQUEST_METHOD': 'GET'},
                            headers={'Host': 'mysite.com'})
        module = 'swift.common.middleware.cname_lookup.lookup_cname'
        with mock.patch(module, lambda d, r: (0, 'example.com')):
            resp = app(req.environ, start_response)
            self.assertEqual(resp, ['FAKE APP'])
Exemplo n.º 3
0
    def test_redirect(self):
        app = cname_lookup.CNAMELookupMiddleware(RedirectSlashApp(), {})

        module = 'swift.common.middleware.cname_lookup.lookup_cname'
        with mock.patch(module, lambda x: (0, 'cont.acct.example.com')):
            req = Request.blank('/test', environ={'REQUEST_METHOD': 'GET'},
                                headers={'Host': 'mysite.com'})
            resp = req.get_response(app)
            self.assertEqual(resp.status_int, 301)
            self.assertEqual(resp.headers.get('Location'),
                             'http://mysite.com/test/')
Exemplo n.º 4
0
    def test_host_is_storage_domain(self):
        conf = {'storage_domain': 'storage.example.com', 'lookup_depth': 2}
        app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)

        def do_test(host):
            req = Request.blank('/',
                                environ={'REQUEST_METHOD': 'GET'},
                                headers={'Host': host})
            return app(req.environ, start_response)

        bad_domain = ['CNAME lookup failed to resolve to a valid domain']
        resp = do_test('c.badtest.com')
        self.assertEqual(resp, bad_domain)

        resp = do_test('storage.example.com')
        self.assertEqual(resp, ['FAKE APP'])
Exemplo n.º 5
0
    def test_multiple_storage_domains(self):
        conf = {'storage_domain': 'storage1.com, storage2.com',
                'lookup_depth': 2}
        app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)

        def do_test(lookup_back):
            req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
                                headers={'Host': 'c.a.example.com'})
            module = 'swift.common.middleware.cname_lookup.lookup_cname'
            with mock.patch(module, lambda x: (0, lookup_back)):
                return app(req.environ, start_response)

        resp = do_test('c.storage1.com')
        self.assertEqual(resp, ['FAKE APP'])

        resp = do_test('c.storage2.com')
        self.assertEqual(resp, ['FAKE APP'])

        bad_domain = ['CNAME lookup failed to resolve to a valid domain']
        resp = do_test('c.badtest.com')
        self.assertEqual(resp, bad_domain)
Exemplo n.º 6
0
 def setUp(self):
     self.app = cname_lookup.CNAMELookupMiddleware(FakeApp(),
                                                   {'lookup_depth': 2})
Exemplo n.º 7
0
    def test_configured_nameservers(self):
        class MockedResolver(object):
            def __init__(self):
                self.nameservers = None
                self.nameserver_ports = None

            def query(self, *args, **kwargs):
                raise Exception('Stop processing')

            def reset(self):
                self.nameservers = None
                self.nameserver_ports = None

        mocked_resolver = MockedResolver()
        dns_module = 'dns.resolver.Resolver'

        # If no nameservers provided in conf, resolver nameservers is unset
        for conf in [{}, {'nameservers': ''}]:
            mocked_resolver.reset()
            with mock.patch(dns_module, return_value=mocked_resolver):
                app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)
            self.assertIs(app.resolver, mocked_resolver)
            self.assertIsNone(mocked_resolver.nameservers)

        # If invalid nameservers provided, resolver nameservers is unset
        mocked_resolver.reset()
        conf = {'nameservers': '127.0.0.1, 127.0.0.2, a.b.c.d'}
        with mock.patch(dns_module, return_value=mocked_resolver):
            with self.assertRaises(ValueError) as exc_mgr:
                app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)
            self.assertIn('Invalid cname_lookup/nameservers configuration',
                          str(exc_mgr.exception))

        # If nameservers provided in conf, resolver nameservers is set
        mocked_resolver.reset()
        conf = {'nameservers': '127.0.0.1'}
        with mock.patch(dns_module, return_value=mocked_resolver):
            app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)
        self.assertIs(app.resolver, mocked_resolver)
        self.assertEqual(mocked_resolver.nameservers, ['127.0.0.1'])
        self.assertEqual(mocked_resolver.nameserver_ports, {})

        # IPv6 is OK
        mocked_resolver.reset()
        conf = {'nameservers': '[::1]'}
        with mock.patch(dns_module, return_value=mocked_resolver):
            app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)
        self.assertIs(app.resolver, mocked_resolver)
        self.assertEqual(mocked_resolver.nameservers, ['::1'])
        self.assertEqual(mocked_resolver.nameserver_ports, {})

        # As are port overrides
        mocked_resolver.reset()
        conf = {'nameservers': '127.0.0.1:5354'}
        with mock.patch(dns_module, return_value=mocked_resolver):
            app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)
        self.assertIs(app.resolver, mocked_resolver)
        self.assertEqual(mocked_resolver.nameservers, ['127.0.0.1'])
        self.assertEqual(mocked_resolver.nameserver_ports, {'127.0.0.1': 5354})

        # And IPv6 with port overrides
        mocked_resolver.reset()
        conf = {'nameservers': '[2001:db8::ff00:42:8329]:1234'}
        with mock.patch(dns_module, return_value=mocked_resolver):
            app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)
        self.assertIs(app.resolver, mocked_resolver)
        self.assertEqual(mocked_resolver.nameservers,
                         ['2001:db8::ff00:42:8329'])
        self.assertEqual(mocked_resolver.nameserver_ports,
                         {'2001:db8::ff00:42:8329': 1234})

        # Also accept lists, and bring it all together
        mocked_resolver.reset()
        conf = {
            'nameservers':
            '[::1], 127.0.0.1:5354, '
            '[2001:db8::ff00:42:8329]:1234'
        }
        with mock.patch(dns_module, return_value=mocked_resolver):
            app = cname_lookup.CNAMELookupMiddleware(FakeApp(), conf)
        self.assertIs(app.resolver, mocked_resolver)
        self.assertEqual(mocked_resolver.nameservers,
                         ['::1', '127.0.0.1', '2001:db8::ff00:42:8329'])
        self.assertEqual(mocked_resolver.nameserver_ports, {
            '127.0.0.1': 5354,
            '2001:db8::ff00:42:8329': 1234
        })
Exemplo n.º 8
0
 def setUp(self):
     if skip:
         raise SkipTest
     self.app = cname_lookup.CNAMELookupMiddleware(FakeApp(),
                                                   {'lookup_depth': 2})