Beispiel #1
0
    def test_ipv6_supported(self):
        self.assertIn(test_utils.is_ipv6_supported(), (False, True))

        def fake_open(path):
            raise IOError

        def fake_socket_fail(x, y):
            e = socket.error()
            e.errno = errno.EAFNOSUPPORT
            raise e

        def fake_socket_ok(x, y):
            return tempfile.TemporaryFile()

        with fixtures.MonkeyPatch('socket.socket', fake_socket_fail):
            self.assertFalse(test_utils.is_ipv6_supported())

        with fixtures.MonkeyPatch('socket.socket', fake_socket_ok):
            with fixtures.MonkeyPatch('sys.platform', 'windows'):
                self.assertTrue(test_utils.is_ipv6_supported())

            with fixtures.MonkeyPatch('sys.platform', 'linux2'):
                with fixtures.MonkeyPatch('six.moves.builtins.open',
                                          fake_open):
                    self.assertFalse(test_utils.is_ipv6_supported())
Beispiel #2
0
class TestWSGIServerWithSSL(test.NoDBTestCase):
    """WSGI server with SSL tests."""
    def setUp(self):
        super(TestWSGIServerWithSSL, self).setUp()
        self.flags(enabled_ssl_apis=['fake_ssl'],
                   ssl_cert_file=os.path.join(SSL_CERT_DIR, 'certificate.crt'),
                   ssl_key_file=os.path.join(SSL_CERT_DIR, 'privatekey.key'))

    def test_ssl_server(self):
        def test_app(env, start_response):
            start_response('200 OK', {})
            return ['PONG']

        fake_ssl_server = nova.wsgi.Server("fake_ssl",
                                           test_app,
                                           host="127.0.0.1",
                                           port=0,
                                           use_ssl=True)
        fake_ssl_server.start()
        self.assertNotEqual(0, fake_ssl_server.port)

        response = requests.post('https://127.0.0.1:%s/' %
                                 fake_ssl_server.port,
                                 verify=os.path.join(SSL_CERT_DIR, 'ca.crt'),
                                 data='PING')
        self.assertEqual(response.text, 'PONG')

        fake_ssl_server.stop()
        fake_ssl_server.wait()

    def test_two_servers(self):
        def test_app(env, start_response):
            start_response('200 OK', {})
            return ['PONG']

        fake_ssl_server = nova.wsgi.Server("fake_ssl",
                                           test_app,
                                           host="127.0.0.1",
                                           port=0,
                                           use_ssl=True)
        fake_ssl_server.start()
        self.assertNotEqual(0, fake_ssl_server.port)

        fake_server = nova.wsgi.Server("fake",
                                       test_app,
                                       host="127.0.0.1",
                                       port=0)
        fake_server.start()
        self.assertNotEqual(0, fake_server.port)

        response = requests.post('https://127.0.0.1:%s/' %
                                 fake_ssl_server.port,
                                 verify=os.path.join(SSL_CERT_DIR, 'ca.crt'),
                                 data='PING')
        self.assertEqual(response.text, 'PONG')

        response = requests.post('http://127.0.0.1:%s/' % fake_server.port,
                                 data='PING')
        self.assertEqual(response.text, 'PONG')

        fake_ssl_server.stop()
        fake_ssl_server.wait()
        fake_server.stop()
        fake_server.wait()

    @testtools.skipIf(utils.is_osx(), 'SO_REUSEADDR behaves differently'
                      ' on OSX, see bug 1436895')
    def test_socket_options_for_ssl_server(self):
        # test normal socket options has set properly
        self.flags(tcp_keepidle=500)
        server = nova.wsgi.Server("test_socket_options",
                                  None,
                                  host="127.0.0.1",
                                  port=0,
                                  use_ssl=True)
        server.start()
        sock = server._socket
        self.assertEqual(
            1, sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR))
        self.assertEqual(
            1, sock.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE))
        if hasattr(socket, 'TCP_KEEPIDLE'):
            self.assertEqual(
                CONF.tcp_keepidle,
                sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE))
        server.stop()
        server.wait()

    @testtools.skipIf(not utils.is_ipv6_supported(), "no ipv6 support")
    def test_app_using_ipv6_and_ssl(self):
        greetings = 'Hello, World!!!'

        @webob.dec.wsgify
        def hello_world(req):
            return greetings

        server = nova.wsgi.Server("fake_ssl",
                                  hello_world,
                                  host="::1",
                                  port=0,
                                  use_ssl=True)

        server.start()

        response = requests.get('https://[::1]:%d/' % server.port,
                                verify=os.path.join(SSL_CERT_DIR, 'ca.crt'))
        self.assertEqual(greetings, response.text)

        server.stop()
        server.wait()
Beispiel #3
0
class TestWSGIServer(test.NoDBTestCase):
    """WSGI server tests."""
    def test_no_app(self):
        server = nova.wsgi.Server("test_app", None)
        self.assertEqual("test_app", server.name)

    def test_custom_max_header_line(self):
        self.flags(max_header_line=4096)  # Default value is 16384.
        nova.wsgi.Server("test_custom_max_header_line", None)
        self.assertEqual(CONF.max_header_line, eventlet.wsgi.MAX_HEADER_LINE)

    def test_start_random_port(self):
        server = nova.wsgi.Server("test_random_port",
                                  None,
                                  host="127.0.0.1",
                                  port=0)
        server.start()
        self.assertNotEqual(0, server.port)
        server.stop()
        server.wait()

    @testtools.skipIf(not utils.is_ipv6_supported(), "no ipv6 support")
    def test_start_random_port_with_ipv6(self):
        server = nova.wsgi.Server("test_random_port", None, host="::1", port=0)
        server.start()
        self.assertEqual("::1", server.host)
        self.assertNotEqual(0, server.port)
        server.stop()
        server.wait()

    @testtools.skipIf(utils.is_osx(), 'SO_REUSEADDR behaves differently'
                      ' on OSX, see bug 1436895')
    def test_socket_options_for_simple_server(self):
        # test normal socket options has set properly
        self.flags(tcp_keepidle=500)
        server = nova.wsgi.Server("test_socket_options",
                                  None,
                                  host="127.0.0.1",
                                  port=0)
        server.start()
        sock = server._socket
        self.assertEqual(
            1, sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR))
        self.assertEqual(
            1, sock.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE))
        if hasattr(socket, 'TCP_KEEPIDLE'):
            self.assertEqual(
                CONF.tcp_keepidle,
                sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE))
        server.stop()
        server.wait()

    def test_server_pool_waitall(self):
        # test pools waitall method gets called while stopping server
        server = nova.wsgi.Server("test_server", None, host="127.0.0.1")
        server.start()
        with mock.patch.object(server._pool, 'waitall') as mock_waitall:
            server.stop()
            server.wait()
            mock_waitall.assert_called_once_with()

    def test_uri_length_limit(self):
        server = nova.wsgi.Server("test_uri_length_limit",
                                  None,
                                  host="127.0.0.1",
                                  max_url_len=16384)
        server.start()

        uri = "http://127.0.0.1:%d/%s" % (server.port, 10000 * 'x')
        resp = requests.get(uri, proxies={"http": ""})
        eventlet.sleep(0)
        self.assertNotEqual(resp.status_code,
                            requests.codes.REQUEST_URI_TOO_LARGE)

        uri = "http://127.0.0.1:%d/%s" % (server.port, 20000 * 'x')
        resp = requests.get(uri, proxies={"http": ""})
        eventlet.sleep(0)
        self.assertEqual(resp.status_code,
                         requests.codes.REQUEST_URI_TOO_LARGE)
        server.stop()
        server.wait()

    def test_reset_pool_size_to_default(self):
        server = nova.wsgi.Server("test_resize",
                                  None,
                                  host="127.0.0.1",
                                  max_url_len=16384)
        server.start()

        # Stopping the server, which in turn sets pool size to 0
        server.stop()
        self.assertEqual(server._pool.size, 0)

        # Resetting pool size to default
        server.reset()
        server.start()
        self.assertEqual(server._pool.size, CONF.wsgi_default_pool_size)

    def test_client_socket_timeout(self):
        self.flags(client_socket_timeout=5)

        # mocking eventlet spawn method to check it is called with
        # configured 'client_socket_timeout' value.
        with mock.patch.object(eventlet, 'spawn') as mock_spawn:
            server = nova.wsgi.Server("test_app",
                                      None,
                                      host="127.0.0.1",
                                      port=0)
            server.start()
            _, kwargs = mock_spawn.call_args
            self.assertEqual(CONF.client_socket_timeout,
                             kwargs['socket_timeout'])
            server.stop()

    def test_wsgi_keep_alive(self):
        self.flags(wsgi_keep_alive=False)

        # mocking eventlet spawn method to check it is called with
        # configured 'wsgi_keep_alive' value.
        with mock.patch.object(eventlet, 'spawn') as mock_spawn:
            server = nova.wsgi.Server("test_app",
                                      None,
                                      host="127.0.0.1",
                                      port=0)
            server.start()
            _, kwargs = mock_spawn.call_args
            self.assertEqual(CONF.wsgi_keep_alive, kwargs['keepalive'])
            server.stop()
Beispiel #4
0
class TestWSGIService(test.NoDBTestCase):
    def setUp(self):
        super(TestWSGIService, self).setUp()
        self.stub_out('nova.wsgi.Loader.load_app',
                      lambda *a, **kw: mock.MagicMock())

    @mock.patch('nova.objects.Service.get_by_host_and_binary')
    @mock.patch('nova.objects.Service.create')
    def test_service_start_creates_record(self, mock_create, mock_get):
        mock_get.return_value = None
        test_service = service.WSGIService("test_service")
        test_service.start()
        self.assertTrue(mock_create.called)

    @mock.patch('nova.objects.Service.get_by_host_and_binary')
    @mock.patch('nova.objects.Service.create')
    def test_service_start_does_not_create_record(self, mock_create, mock_get):
        test_service = service.WSGIService("test_service")
        test_service.start()
        self.assertFalse(mock_create.called)

    @mock.patch('nova.objects.Service.get_by_host_and_binary')
    def test_service_random_port(self, mock_get):
        test_service = service.WSGIService("test_service")
        test_service.start()
        self.assertNotEqual(0, test_service.port)
        test_service.stop()

    def test_workers_set_default(self):
        test_service = service.WSGIService("osapi_compute")
        self.assertEqual(test_service.workers, processutils.get_worker_count())

    def test_workers_set_good_user_setting(self):
        CONF.set_override('osapi_compute_workers', 8)
        test_service = service.WSGIService("osapi_compute")
        self.assertEqual(test_service.workers, 8)

    def test_openstack_compute_api_workers_set_default(self):
        test_service = service.WSGIService("openstack_compute_api_v2")
        self.assertEqual(test_service.workers, processutils.get_worker_count())

    def test_openstack_compute_api_workers_set_good_user_setting(self):
        CONF.set_override('osapi_compute_workers', 8)
        test_service = service.WSGIService("openstack_compute_api_v2")
        self.assertEqual(test_service.workers, 8)

    @testtools.skipIf(not utils.is_ipv6_supported(), "no ipv6 support")
    @mock.patch('nova.objects.Service.get_by_host_and_binary')
    def test_service_random_port_with_ipv6(self, mock_get):
        CONF.set_default("test_service_listen", "::1")
        test_service = service.WSGIService("test_service")
        test_service.start()
        self.assertEqual("::1", test_service.host)
        self.assertNotEqual(0, test_service.port)
        test_service.stop()

    @mock.patch('nova.objects.Service.get_by_host_and_binary')
    def test_reset_pool_size_to_default(self, mock_get):
        test_service = service.WSGIService("test_service")
        test_service.start()

        # Stopping the service, which in turn sets pool size to 0
        test_service.stop()
        self.assertEqual(test_service.server._pool.size, 0)

        # Resetting pool size to default
        test_service.reset()
        test_service.start()
        self.assertEqual(test_service.server._pool.size,
                         CONF.wsgi.default_pool_size)