|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +import tempfile |
| 14 | + |
| 15 | +import fixtures |
| 16 | +import mock |
| 17 | +from oslo_config import fixture as config_fixture |
| 18 | +from oslotest import base |
| 19 | + |
| 20 | +from nova.api.openstack import wsgi_app |
| 21 | +from nova import test |
| 22 | +from nova.tests import fixtures as nova_fixtures |
| 23 | + |
| 24 | + |
| 25 | +class WSGIAppTest(base.BaseTestCase): |
| 26 | + |
| 27 | + _paste_config = """ |
| 28 | +[app:nova-api] |
| 29 | +use = egg:Paste#static |
| 30 | +document_root = /tmp |
| 31 | + """ |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + # Ensure BaseTestCase's ConfigureLogging fixture is disabled since |
| 35 | + # we're using our own (StandardLogging). |
| 36 | + with fixtures.EnvironmentVariable('OS_LOG_CAPTURE', '0'): |
| 37 | + super(WSGIAppTest, self).setUp() |
| 38 | + self.stdlog = self.useFixture(nova_fixtures.StandardLogging()) |
| 39 | + self.conf = tempfile.NamedTemporaryFile(mode='w+t') |
| 40 | + self.conf.write(self._paste_config.lstrip()) |
| 41 | + self.conf.seek(0) |
| 42 | + self.conf.flush() |
| 43 | + self.addCleanup(self.conf.close) |
| 44 | + # Use of this fixture takes care of cleaning up config settings for |
| 45 | + # subsequent tests. |
| 46 | + self.useFixture(config_fixture.Config()) |
| 47 | + |
| 48 | + @mock.patch('sys.argv', return_value=mock.sentinel.argv) |
| 49 | + @mock.patch('nova.db.sqlalchemy.api.configure') |
| 50 | + @mock.patch('nova.api.openstack.wsgi_app._setup_service') |
| 51 | + @mock.patch('nova.api.openstack.wsgi_app._get_config_files') |
| 52 | + def test_init_application_called_twice(self, mock_get_files, mock_setup, |
| 53 | + mock_db_configure, mock_argv): |
| 54 | + """Test that init_application can tolerate being called twice in a |
| 55 | + single python interpreter instance. |
| 56 | +
|
| 57 | + When nova-api is run via mod_wsgi, if any exception is raised during |
| 58 | + init_application, mod_wsgi will re-run the WSGI script without |
| 59 | + restarting the daemon process even when configured for Daemon Mode. |
| 60 | +
|
| 61 | + We access the database as part of init_application, so if nova-api |
| 62 | + starts up before the database is up, we'll get, for example, a |
| 63 | + DBConnectionError raised during init_application and our WSGI script |
| 64 | + will get reloaded/re-run by mod_wsgi. |
| 65 | + """ |
| 66 | + mock_get_files.return_value = [self.conf.name] |
| 67 | + mock_setup.side_effect = [test.TestingException, None] |
| 68 | + # We need to mock the global database configure() method, else we will |
| 69 | + # be affected by global database state altered by other tests that ran |
| 70 | + # before this test, causing this test to fail with |
| 71 | + # oslo_db.sqlalchemy.enginefacade.AlreadyStartedError. We can instead |
| 72 | + # mock the method to raise an exception if it's called a second time in |
| 73 | + # this test to simulate the fact that the database does not tolerate |
| 74 | + # re-init [after a database query has been made]. |
| 75 | + mock_db_configure.side_effect = [None, test.TestingException] |
| 76 | + # Run init_application the first time, simulating an exception being |
| 77 | + # raised during it. |
| 78 | + self.assertRaises(test.TestingException, wsgi_app.init_application, |
| 79 | + 'nova-api') |
| 80 | + # Now run init_application a second time, it should succeed since no |
| 81 | + # exception is being raised (the init of global data should not be |
| 82 | + # re-attempted). |
| 83 | + wsgi_app.init_application('nova-api') |
| 84 | + self.assertIn('Global data already initialized, not re-initializing.', |
| 85 | + self.stdlog.logger.output) |
0 commit comments