Exemple #1
0
 def test_get_context_no_overwrite(self):
     # If there is already a context in the cache creating another context
     # should not overwrite it.
     ctx1 = context.RequestContext('111',
                                   '222',
                                   overwrite=True)
     context.get_context()
     self.assertIs(ctx1, o_context.get_current())
 def test_get_context_no_overwrite(self):
     # If there is already a context in the cache creating another context
     # should not overwrite it.
     ctx1 = context.RequestContext('111',
                                   '222',
                                   overwrite=True)
     context.get_context()
     self.assertIs(ctx1, o_context.get_current())
Exemple #3
0
    def test_scatter_gather_cells(self, mock_get_inst, mock_target_cell):
        ctxt = context.get_context()
        mapping = objects.CellMapping(database_connection='fake://db',
                                      transport_url='fake://mq',
                                      uuid=uuids.cell)
        mappings = objects.CellMappingList(objects=[mapping])

        # Use a mock manager to assert call order across mocks.
        manager = mock.Mock()
        manager.attach_mock(mock_get_inst, 'get_inst')
        manager.attach_mock(mock_target_cell, 'target_cell')

        filters = {'deleted': False}
        context.scatter_gather_cells(
            ctxt, mappings, 60, objects.InstanceList.get_by_filters, filters,
            sort_dir='foo')

        # NOTE(melwitt): This only works without the SpawnIsSynchronous fixture
        # because when the spawn is treated as synchronous and the thread
        # function is called immediately, it will occur inside the target_cell
        # context manager scope when it wouldn't with a real spawn.

        # Assert that InstanceList.get_by_filters was called before the
        # target_cell context manager exited.
        get_inst_call = mock.call.get_inst(
            mock_target_cell.return_value.__enter__.return_value, filters,
            sort_dir='foo')
        expected_calls = [get_inst_call,
                          mock.call.target_cell().__exit__(None, None, None)]
        manager.assert_has_calls(expected_calls)
 def test_list_ports_with_resource_request_non_admin_client(self):
     ctxt = context.get_context()
     client = neutron_api.get_client(ctxt)
     ports = client.list_ports(ctxt)['ports']
     port_id = self.neutron.port_with_resource_request['id']
     ports = [port for port in ports if port_id == port['id']]
     self.assertIsNone(ports[0]['resource_request'])
Exemple #5
0
    def _setup_cells(self):
        """Setup a normal cellsv2 environment.

        This sets up the CellDatabase fixture with two cells, one cell0
        and one normal cell. CellMappings are created for both so that
        cells-aware code can find those two databases.
        """
        celldbs = nova_fixtures.CellDatabases()
        celldbs.add_cell_database(objects.CellMapping.CELL0_UUID)
        celldbs.add_cell_database(uuids.cell1, default=True)
        self.useFixture(celldbs)

        ctxt = context.get_context()
        fake_transport = 'fake://nowhere/'

        c0 = objects.CellMapping(
            context=ctxt,
            uuid=objects.CellMapping.CELL0_UUID,
            name='cell0',
            transport_url=fake_transport,
            database_connection=objects.CellMapping.CELL0_UUID)
        c0.create()

        c1 = objects.CellMapping(
            context=ctxt,
            uuid=uuids.cell1,
            name=CELL1_NAME,
            transport_url=fake_transport,
            database_connection=uuids.cell1)
        c1.create()

        self.cell_mappings = {cm.name: cm for cm in (c0, c1)}
Exemple #6
0
    def start_service(self, name, host=None, **kwargs):
        cell = None
        # if the host is None then the CONF.host remains defaulted to
        # 'fake-mini' (originally done in ConfFixture)
        if host is not None:
            # Make sure that CONF.host is relevant to the right hostname
            self.useFixture(nova_fixtures.ConfPatcher(host=host))

        if name == 'compute' and self.USES_DB:
            # NOTE(danms): We need to create the HostMapping first, because
            # otherwise we'll fail to update the scheduler while running
            # the compute node startup routines below.
            ctxt = context.get_context()
            cell_name = kwargs.pop('cell', CELL1_NAME) or CELL1_NAME
            cell = self.cell_mappings[cell_name]
            if (host or name) not in self.host_mappings:
                # NOTE(gibi): If the HostMapping does not exists then this is
                # the first start of the service so we create the mapping.
                hm = objects.HostMapping(context=ctxt,
                                         host=host or name,
                                         cell_mapping=cell)
                hm.create()
                self.host_mappings[hm.host] = hm
        svc = self.useFixture(
            nova_fixtures.ServiceFixture(name, host, cell=cell, **kwargs))

        return svc.service
Exemple #7
0
    def test_scatter_gather_cells(self, mock_get_inst, mock_target_cell):
        ctxt = context.get_context()
        mapping = objects.CellMapping(database_connection='fake://db',
                                      transport_url='fake://mq',
                                      uuid=uuids.cell)
        mappings = objects.CellMappingList(objects=[mapping])

        # Use a mock manager to assert call order across mocks.
        manager = mock.Mock()
        manager.attach_mock(mock_get_inst, 'get_inst')
        manager.attach_mock(mock_target_cell, 'target_cell')

        filters = {'deleted': False}
        context.scatter_gather_cells(
            ctxt, mappings, 60, objects.InstanceList.get_by_filters, filters,
            sort_dir='foo')

        # NOTE(melwitt): This only works without the SpawnIsSynchronous fixture
        # because when the spawn is treated as synchronous and the thread
        # function is called immediately, it will occur inside the target_cell
        # context manager scope when it wouldn't with a real spawn.

        # Assert that InstanceList.get_by_filters was called before the
        # target_cell context manager exited.
        get_inst_call = mock.call.get_inst(
            mock_target_cell.return_value.__enter__.return_value, filters,
            sort_dir='foo')
        expected_calls = [get_inst_call,
                          mock.call.target_cell().__exit__(None, None, None)]
        manager.assert_has_calls(expected_calls)
Exemple #8
0
    def test_scatter_gather_cells_all_timeout(self, mock_get_inst,
                                              mock_get_result, mock_timeout,
                                              mock_log_warning):
        """This is a regression test for bug 1847131.
        test_scatter_gather_cells_timeout did not catch the issue because it
        yields a result which sets the cell_uuid variable in scope before the
        CellTimeout is processed and logged. In this test we only raise the
        CellTimeout so cell_uuid will not be in scope for the log message.
        """
        # This is needed because we're mocking get_by_filters.
        self.useFixture(nova_fixtures.SpawnIsSynchronousFixture())
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)
        mappings = objects.CellMappingList(objects=[mapping0])

        # Simulate cell0 not responding.
        mock_get_result.side_effect = exception.CellTimeout()

        results = context.scatter_gather_cells(
            ctxt, mappings, 30, objects.InstanceList.get_by_filters, {})
        self.assertEqual(1, len(results))
        self.assertIn(context.did_not_respond_sentinel, results.values())
        mock_timeout.assert_called_once_with(30, exception.CellTimeout)
        mock_log_warning.assert_called_once_with(
            'Timed out waiting for response from cell', exc_info=True)
Exemple #9
0
 def test_fallback(self):
     # Convert RequestContext, should get a dict.
     primitive = rpc.JsonPayloadSerializer.fallback(context.get_context())
     self.assertIsInstance(primitive, dict)
     # Convert anything else, should get a string.
     primitive = rpc.JsonPayloadSerializer.fallback(mock.sentinel.entity)
     self.assertIsInstance(primitive, six.text_type)
Exemple #10
0
    def setUp(self):
        super(TestDeleteWhileBooting, self).setUp()
        self.useFixture(policy_fixture.RealPolicyFixture())
        self.useFixture(nova_fixtures.NeutronFixture(self))
        self.useFixture(nova_fixtures.GlanceFixture(self))

        api_fixture = self.useFixture(nova_fixtures.OSAPIFixture(
            api_version='v2.1'))
        self.api = api_fixture.api

        self.ctxt = nova_context.get_context()

        # We intentionally do not start a conductor or scheduler service, since
        # our goal is to simulate an instance that has not been scheduled yet.

        # Kick off a server create request and move on once it's in the BUILD
        # state. Since we have no conductor or scheduler service running, the
        # server will "hang" in an unscheduled state for testing.
        self.server = self._create_server(expected_state='BUILD')
        # Simulate that a different request has deleted the build request
        # record after this delete request has begun processing. (The first
        # lookup of the build request occurs in the servers API to get the
        # instance object in order to delete it).
        # We need to get the build request now before we mock the method.
        self.br = objects.BuildRequest.get_by_instance_uuid(
            self.ctxt, self.server['id'])
Exemple #11
0
    def test_scatter_gather_cells_timeout(self, mock_get_inst, mock_get_result,
                                          mock_timeout, mock_log_warning):
        # This is needed because we're mocking get_by_filters.
        self.useFixture(nova_fixtures.SpawnIsSynchronousFixture())
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)
        mapping1 = objects.CellMapping(database_connection='fake://db1',
                                       transport_url='fake://mq1',
                                       uuid=uuids.cell1)
        mappings = objects.CellMappingList(objects=[mapping0, mapping1])

        # Simulate cell1 not responding.
        mock_get_result.side_effect = [(mapping0.uuid,
                                        mock.sentinel.instances),
                                       exception.CellTimeout()]

        results = context.scatter_gather_cells(
            ctxt, mappings, 30, objects.InstanceList.get_by_filters)
        self.assertEqual(2, len(results))
        self.assertIn(mock.sentinel.instances, results.values())
        self.assertIn(context.did_not_respond_sentinel, results.values())
        mock_timeout.assert_called_once_with(30, exception.CellTimeout)
        self.assertTrue(mock_log_warning.called)
Exemple #12
0
    def test_scatter_gather_cells_timeout(self, mock_get_inst,
                                          mock_get_result, mock_timeout,
                                          mock_log_warning):
        # This is needed because we're mocking get_by_filters.
        self.useFixture(nova_fixtures.SpawnIsSynchronousFixture())
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)
        mapping1 = objects.CellMapping(database_connection='fake://db1',
                                       transport_url='fake://mq1',
                                       uuid=uuids.cell1)
        mappings = objects.CellMappingList(objects=[mapping0, mapping1])

        # Simulate cell1 not responding.
        mock_get_result.side_effect = [(mapping0.uuid,
                                        mock.sentinel.instances),
                                       exception.CellTimeout()]

        results = context.scatter_gather_cells(
            ctxt, mappings, 30, objects.InstanceList.get_by_filters)
        self.assertEqual(2, len(results))
        self.assertIn(mock.sentinel.instances, results.values())
        self.assertIn(context.did_not_respond_sentinel, results.values())
        mock_timeout.assert_called_once_with(30, exception.CellTimeout)
        self.assertTrue(mock_log_warning.called)
Exemple #13
0
    def test_scatter_gather_cells_exception(self, mock_get_inst,
                                            mock_log_exception):
        # This is needed because we're mocking get_by_filters.
        self.useFixture(nova_fixtures.SpawnIsSynchronousFixture())
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)
        mapping1 = objects.CellMapping(database_connection='fake://db1',
                                       transport_url='fake://mq1',
                                       uuid=uuids.cell1)
        mappings = objects.CellMappingList(objects=[mapping0, mapping1])

        # Simulate cell1 raising an exception.
        mock_get_inst.side_effect = [
            mock.sentinel.instances,
            test.TestingException()
        ]

        results = context.scatter_gather_cells(
            ctxt, mappings, 30, objects.InstanceList.get_by_filters)
        self.assertEqual(2, len(results))
        self.assertIn(mock.sentinel.instances, results.values())
        self.assertIn(context.raised_exception_sentinel, results.values())
        self.assertTrue(mock_log_exception.called)
Exemple #14
0
    def _setup_cells(self):
        """Setup a normal cellsv2 environment.

        This sets up the CellDatabase fixture with two cells, one cell0
        and one normal cell. CellMappings are created for both so that
        cells-aware code can find those two databases.
        """
        celldbs = nova_fixtures.CellDatabases()
        celldbs.add_cell_database(objects.CellMapping.CELL0_UUID)
        celldbs.add_cell_database(uuids.cell1, default=True)
        self.useFixture(celldbs)

        ctxt = context.get_context()
        fake_transport = 'fake://nowhere/'

        c0 = objects.CellMapping(
            context=ctxt,
            uuid=objects.CellMapping.CELL0_UUID,
            name='cell0',
            transport_url=fake_transport,
            database_connection=objects.CellMapping.CELL0_UUID)
        c0.create()

        c1 = objects.CellMapping(
            context=ctxt,
            uuid=uuids.cell1,
            name=CELL1_NAME,
            transport_url=fake_transport,
            database_connection=uuids.cell1)
        c1.create()

        self.cell_mappings = {cm.name: cm for cm in (c0, c1)}
Exemple #15
0
 def test_fallback(self):
     # Convert RequestContext, should get a dict.
     primitive = rpc.JsonPayloadSerializer.fallback(context.get_context())
     self.assertIsInstance(primitive, dict)
     # Convert anything else, should get a string.
     primitive = rpc.JsonPayloadSerializer.fallback(mock.sentinel.entity)
     self.assertIsInstance(primitive, str)
    def test_new_websocket_client_db(self,
                                     mock_ca_check,
                                     mock_validate_port,
                                     mock_inst_get,
                                     mock_validate,
                                     internal_access_path=None,
                                     instance_not_found=False):

        db_obj = self._fake_console_db(
            host='node1',
            port=10000,
            console_type='novnc',
            access_url_base='https://example.net:6080',
            internal_access_path=internal_access_path,
            instance_uuid=uuids.instance,
            # This is set by ConsoleAuthToken.validate
            token='123-456-789')
        ctxt = nova_context.get_context()
        obj = nova.objects.ConsoleAuthToken._from_db_object(
            ctxt, nova.objects.ConsoleAuthToken(), db_obj)
        mock_validate.return_value = obj

        if instance_not_found:
            mock_inst_get.side_effect = exception.InstanceNotFound(
                instance_id=uuids.instance)

        if internal_access_path is None:
            self.wh.socket.return_value = '<socket>'
        else:
            tsock = mock.MagicMock()
            tsock.recv.return_value = "HTTP/1.1 200 OK\r\n\r\n"
            self.wh.socket.return_value = tsock

        self.wh.path = "http://127.0.0.1/?token=123-456-789"
        self.wh.headers = self.fake_header

        if instance_not_found:
            self.assertRaises(exception.InvalidToken,
                              self.wh.new_websocket_client)
        else:
            with mock.patch('nova.context.get_admin_context',
                            return_value=ctxt):
                self.wh.new_websocket_client()

            mock_validate.called_once_with(ctxt, '123-456-789')
            mock_validate_port.assert_called_once_with(
                ctxt, mock_inst_get.return_value, str(db_obj['port']),
                db_obj['console_type'])
            mock_ca_check.assert_not_called()

            self.wh.socket.assert_called_with('node1', 10000, connect=True)

            if internal_access_path is None:
                self.wh.do_proxy.assert_called_with('<socket>')
            else:
                self.wh.do_proxy.assert_called_with(tsock)
 def setUp(self):
     super(CrossCellMigrationTaskTestCase, self).setUp()
     source_context = nova_context.get_context()
     host_selection = objects.Selection(cell_uuid=uuids.cell_uuid)
     migration = objects.Migration(id=1, cross_cell_move=False)
     self.task = cross_cell_migrate.CrossCellMigrationTask(
         source_context, mock.sentinel.instance, mock.sentinel.flavor,
         mock.sentinel.request_spec, migration,
         mock.sentinel.compute_rpcapi, host_selection,
         mock.sentinel.alternate_hosts)
Exemple #18
0
    def test_new_websocket_client_db(
            self, mock_ca_check, mock_validate_port, mock_inst_get,
            mock_validate, internal_access_path=None,
            instance_not_found=False):

        db_obj = self._fake_console_db(
            host='node1',
            port=10000,
            console_type='novnc',
            access_url_base='https://example.net:6080',
            internal_access_path=internal_access_path,
            instance_uuid=uuids.instance,
            # This is set by ConsoleAuthToken.validate
            token='123-456-789'
        )
        ctxt = nova_context.get_context()
        obj = nova.objects.ConsoleAuthToken._from_db_object(
            ctxt, nova.objects.ConsoleAuthToken(), db_obj)
        mock_validate.return_value = obj

        if instance_not_found:
            mock_inst_get.side_effect = exception.InstanceNotFound(
                instance_id=uuids.instance)

        if internal_access_path is None:
            self.wh.socket.return_value = '<socket>'
        else:
            tsock = mock.MagicMock()
            tsock.recv.return_value = "HTTP/1.1 200 OK\r\n\r\n"
            self.wh.socket.return_value = tsock

        self.wh.path = "http://127.0.0.1/?token=123-456-789"
        self.wh.headers = self.fake_header

        if instance_not_found:
            self.assertRaises(exception.InvalidToken,
                              self.wh.new_websocket_client)
        else:
            with mock.patch('nova.context.get_admin_context',
                            return_value=ctxt):
                self.wh.new_websocket_client()

            mock_validate.called_once_with(ctxt, '123-456-789')
            mock_validate_port.assert_called_once_with(
                ctxt, mock_inst_get.return_value, str(db_obj['port']),
                db_obj['console_type'])
            mock_ca_check.assert_not_called()

            self.wh.socket.assert_called_with('node1', 10000, connect=True)

            if internal_access_path is None:
                self.wh.do_proxy.assert_called_with('<socket>')
            else:
                self.wh.do_proxy.assert_called_with(tsock)
Exemple #19
0
 def test_cinderclient_unsupported_v2(self, get_api_version):
     """Tests that we fail if trying to use Cinder v2."""
     self.flags(catalog_info='volumev2:cinderv2:publicURL', group='cinder')
     # setup mocks
     get_endpoint = mock.Mock(
         return_value='http://localhost:8776/v2/%(project_id)s')
     fake_session = mock.Mock(get_endpoint=get_endpoint)
     ctxt = context.get_context()
     with mock.patch.object(cinder, '_SESSION', fake_session):
         self.assertRaises(exception.UnsupportedCinderAPIVersion,
                           cinder.cinderclient, ctxt)
     get_api_version.assert_called_once_with(get_endpoint.return_value)
Exemple #20
0
 def test_cinderclient_unsupported_v2(self, get_api_version):
     """Tests that we fail if trying to use Cinder v2."""
     self.flags(catalog_info='volumev2:cinderv2:publicURL', group='cinder')
     # setup mocks
     get_endpoint = mock.Mock(
         return_value='http://localhost:8776/v2/%(project_id)s')
     fake_session = mock.Mock(get_endpoint=get_endpoint)
     ctxt = context.get_context()
     with mock.patch.object(cinder, '_SESSION', fake_session):
         self.assertRaises(exception.UnsupportedCinderAPIVersion,
                           cinder.cinderclient, ctxt)
     get_api_version.assert_called_once_with(get_endpoint.return_value)
 def setUp(self):
     super(PrepResizeAtSourceTaskTestCase, self).setUp()
     self.task = cross_cell_migrate.PrepResizeAtSourceTask(
         nova_context.get_context(),
         objects.Instance(uuid=uuids.instance,
                          vm_state=vm_states.ACTIVE,
                          display_name='fake-server',
                          system_metadata={},
                          host='source.host.com'),
         objects.Migration(),
         objects.RequestSpec(),
         compute_rpcapi=mock.Mock(),
         image_api=mock.Mock())
 def setUp(self):
     super(CrossCellMigrationTaskTestCase, self).setUp()
     source_context = nova_context.get_context()
     host_selection = objects.Selection(service_host='target.host.com',
                                        cell_uuid=uuids.cell_uuid)
     migration = objects.Migration(id=1,
                                   cross_cell_move=False,
                                   source_compute='source.host.com')
     instance = objects.Instance()
     self.task = cross_cell_migrate.CrossCellMigrationTask(
         source_context, instance, objects.Flavor(),
         mock.sentinel.request_spec, migration,
         mock.sentinel.compute_rpcapi, host_selection,
         mock.sentinel.alternate_hosts)
    def test_scatter_gather_single_cell(self, mock_scatter):
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)

        filters = {'deleted': False}
        context.scatter_gather_single_cell(ctxt, mapping0,
            objects.InstanceList.get_by_filters, filters, sort_dir='foo')

        mock_scatter.assert_called_once_with(
            ctxt, [mapping0], context.CELL_TIMEOUT,
            objects.InstanceList.get_by_filters, filters,
            sort_dir='foo')
Exemple #24
0
    def test_scatter_gather_single_cell(self, mock_scatter):
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)

        filters = {'deleted': False}
        context.scatter_gather_single_cell(ctxt, mapping0,
            objects.InstanceList.get_by_filters, filters, sort_dir='foo')

        mock_scatter.assert_called_once_with(
            ctxt, [mapping0], context.CELL_TIMEOUT,
            objects.InstanceList.get_by_filters, filters,
            sort_dir='foo')
Exemple #25
0
    def start_service(self, name, host=None, **kwargs):
        svc = self.useFixture(
            nova_fixtures.ServiceFixture(name, host, **kwargs))

        if name == 'compute' and self.USES_DB:
            ctxt = context.get_context()
            cell = self.cell_mappings[kwargs.pop('cell', CELL1_NAME)]
            hm = objects.HostMapping(context=ctxt,
                                     host=svc.service.host,
                                     cell_mapping=cell)
            hm.create()
            self.host_mappings[hm.host] = hm

        return svc.service
Exemple #26
0
    def start_service(self, name, host=None, **kwargs):
        svc = self.useFixture(
            nova_fixtures.ServiceFixture(name, host, **kwargs))

        if name == 'compute' and self.USES_DB:
            ctxt = context.get_context()
            cell = self.cell_mappings[kwargs.pop('cell', CELL1_NAME)]
            hm = objects.HostMapping(context=ctxt,
                                     host=svc.service.host,
                                     cell_mapping=cell)
            hm.create()
            self.host_mappings[hm.host] = hm

        return svc.service
 def test_target_cell_caching(self, mock_create_cm):
     mock_create_cm.return_value = mock.sentinel.db_conn_obj
     ctxt = context.get_context()
     mapping = objects.CellMapping(database_connection='fake://db',
                                   transport_url='fake://mq',
                                   uuid=uuids.cell)
     # First call should create new connection objects.
     with context.target_cell(ctxt, mapping):
         self.assertEqual(mock.sentinel.db_conn_obj, ctxt.db_connection)
     mock_create_cm.assert_called_once_with('fake://db')
     # Second call should use cached objects.
     mock_create_cm.reset_mock()
     with context.target_cell(ctxt, mapping):
         self.assertEqual(mock.sentinel.db_conn_obj, ctxt.db_connection)
     mock_create_cm.assert_not_called()
 def setUp(self):
     super(PrepResizeAtDestTaskTestCase, self).setUp()
     host_selection = objects.Selection(service_host='fake-host',
                                        nodename='fake-host',
                                        limits=objects.SchedulerLimits())
     self.task = cross_cell_migrate.PrepResizeAtDestTask(
         nova_context.get_context(),
         objects.Instance(uuid=uuids.instance),
         objects.Flavor(),
         objects.Migration(),
         objects.RequestSpec(),
         compute_rpcapi=mock.Mock(),
         host_selection=host_selection,
         network_api=mock.Mock(),
         volume_api=mock.Mock())
    def test_scatter_gather_cells(self, mock_get_inst, mock_target_cell):
        self.useFixture(nova_fixtures.SpawnIsSynchronousFixture())
        ctxt = context.get_context()
        mapping = objects.CellMapping(database_connection='fake://db',
                                      transport_url='fake://mq',
                                      uuid=uuids.cell)
        mappings = objects.CellMappingList(objects=[mapping])

        filters = {'deleted': False}
        context.scatter_gather_cells(
            ctxt, mappings, 60, objects.InstanceList.get_by_filters, filters,
            sort_dir='foo')

        mock_get_inst.assert_called_once_with(
            mock_target_cell.return_value.__enter__.return_value, filters,
            sort_dir='foo')
Exemple #30
0
    def test_scatter_gather_cells(self, mock_get_inst, mock_target_cell):
        self.useFixture(nova_fixtures.SpawnIsSynchronousFixture())
        ctxt = context.get_context()
        mapping = objects.CellMapping(database_connection='fake://db',
                                      transport_url='fake://mq',
                                      uuid=uuids.cell)
        mappings = objects.CellMappingList(objects=[mapping])

        filters = {'deleted': False}
        context.scatter_gather_cells(
            ctxt, mappings, 60, objects.InstanceList.get_by_filters, filters,
            sort_dir='foo')

        mock_get_inst.assert_called_once_with(
            mock_target_cell.return_value.__enter__.return_value, filters,
            sort_dir='foo')
Exemple #31
0
    def start_service(self, name, host=None, **kwargs):
        if name == 'compute' and self.USES_DB:
            # NOTE(danms): We need to create the HostMapping first, because
            # otherwise we'll fail to update the scheduler while running
            # the compute node startup routines below.
            ctxt = context.get_context()
            cell = self.cell_mappings[kwargs.pop('cell', CELL1_NAME)]
            hm = objects.HostMapping(context=ctxt,
                                     host=host or name,
                                     cell_mapping=cell)
            hm.create()
            self.host_mappings[hm.host] = hm

        svc = self.useFixture(
            nova_fixtures.ServiceFixture(name, host, **kwargs))

        return svc.service
Exemple #32
0
    def start_service(self, name, host=None, **kwargs):
        if name == 'compute' and self.USES_DB:
            # NOTE(danms): We need to create the HostMapping first, because
            # otherwise we'll fail to update the scheduler while running
            # the compute node startup routines below.
            ctxt = context.get_context()
            cell = self.cell_mappings[kwargs.pop('cell', CELL1_NAME)]
            hm = objects.HostMapping(context=ctxt,
                                     host=host or name,
                                     cell_mapping=cell)
            hm.create()
            self.host_mappings[hm.host] = hm

        svc = self.useFixture(
            nova_fixtures.ServiceFixture(name, host, **kwargs))

        return svc.service
Exemple #33
0
    def start_service(self, name, host=None, cell_name=None, **kwargs):
        # Disallow starting multiple scheduler services
        if name == 'scheduler' and self._service_fixture_count[name]:
            raise TestingException("Duplicate start_service(%s)!" % name)

        cell = None
        # if the host is None then the CONF.host remains defaulted to
        # 'fake-mini' (originally done in ConfFixture)
        if host is not None:
            # Make sure that CONF.host is relevant to the right hostname
            self.useFixture(nova_fixtures.ConfPatcher(host=host))

        if name == 'compute' and self.USES_DB:
            # NOTE(danms): We need to create the HostMapping first, because
            # otherwise we'll fail to update the scheduler while running
            # the compute node startup routines below.
            ctxt = context.get_context()
            cell_name = cell_name or CELL1_NAME
            cell = self.cell_mappings[cell_name]
            if (host or name) not in self.host_mappings:
                # NOTE(gibi): If the HostMapping does not exists then this is
                # the first start of the service so we create the mapping.
                hm = objects.HostMapping(context=ctxt,
                                         host=host or name,
                                         cell_mapping=cell)
                hm.create()
                self.host_mappings[hm.host] = hm
        svc = self.useFixture(
            nova_fixtures.ServiceFixture(name, host, cell=cell, **kwargs))

        # Keep track of how many instances of this service are running.
        self._service_fixture_count[name] += 1
        real_stop = svc.service.stop

        # Make sure stopping the service decrements the active count, so that
        # start,stop,start doesn't trigger the "Duplicate start_service"
        # exception.
        def patch_stop(*a, **k):
            self._service_fixture_count[name] -= 1
            return real_stop(*a, **k)

        self.useFixture(
            fixtures.MockPatchObject(svc.service, 'stop', patch_stop))

        return svc.service
Exemple #34
0
    def test_scatter_gather_skip_cell0(self, mock_get_all, mock_scatter):
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)
        mapping1 = objects.CellMapping(database_connection='fake://db1',
                                       transport_url='fake://mq1',
                                       uuid=uuids.cell1)
        mock_get_all.return_value = objects.CellMappingList(
            objects=[mapping0, mapping1])

        filters = {'deleted': False}
        context.scatter_gather_skip_cell0(
            ctxt, objects.InstanceList.get_by_filters, filters, sort_dir='foo')

        mock_scatter.assert_called_once_with(
            ctxt, [mapping1], 60, objects.InstanceList.get_by_filters, filters,
            sort_dir='foo')
    def test_scatter_gather_skip_cell0(self, mock_get_all, mock_scatter):
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)
        mapping1 = objects.CellMapping(database_connection='fake://db1',
                                       transport_url='fake://mq1',
                                       uuid=uuids.cell1)
        mock_get_all.return_value = objects.CellMappingList(
            objects=[mapping0, mapping1])

        filters = {'deleted': False}
        context.scatter_gather_skip_cell0(
            ctxt, objects.InstanceList.get_by_filters, filters, sort_dir='foo')

        mock_scatter.assert_called_once_with(
            ctxt, [mapping1], 60, objects.InstanceList.get_by_filters, filters,
            sort_dir='foo')
Exemple #36
0
    def test_scatter_gather_cells_exception(self, mock_get_inst,
                                            mock_log_exception):
        # This is needed because we're mocking get_by_filters.
        self.useFixture(nova_fixtures.SpawnIsSynchronousFixture())
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)
        mapping1 = objects.CellMapping(database_connection='fake://db1',
                                       transport_url='fake://mq1',
                                       uuid=uuids.cell1)
        mappings = objects.CellMappingList(objects=[mapping0, mapping1])

        # Simulate cell1 raising an exception.
        mock_get_inst.side_effect = [
            mock.sentinel.instances,
            test.TestingException()
        ]

        filters = {'deleted': False}
        results = context.scatter_gather_cells(
            ctxt, mappings, 30, objects.InstanceList.get_by_filters, filters)
        self.assertEqual(2, len(results))
        self.assertIn(mock.sentinel.instances, results.values())
        self.assertIsInstance(results[mapping1.uuid], Exception)
        # non-NovaException gets logged
        self.assertTrue(mock_log_exception.called)

        # Now run it again with a NovaException to see it's not logged.
        mock_log_exception.reset_mock()
        mock_get_inst.side_effect = [
            mock.sentinel.instances,
            exception.NotFound()
        ]

        results = context.scatter_gather_cells(
            ctxt, mappings, 30, objects.InstanceList.get_by_filters, filters)
        self.assertEqual(2, len(results))
        self.assertIn(mock.sentinel.instances, results.values())
        self.assertIsInstance(results[mapping1.uuid], exception.NovaException)
        # NovaExceptions are not logged, the caller should handle them.
        mock_log_exception.assert_not_called()
Exemple #37
0
 def test_target_cell_caching(self, mock_create_cm, mock_create_tport):
     mock_create_cm.return_value = mock.sentinel.db_conn_obj
     mock_create_tport.return_value = mock.sentinel.mq_conn_obj
     ctxt = context.get_context()
     mapping = objects.CellMapping(database_connection='fake://db',
                                   transport_url='fake://mq',
                                   uuid=uuids.cell)
     # First call should create new connection objects.
     with context.target_cell(ctxt, mapping) as cctxt:
         self.assertEqual(mock.sentinel.db_conn_obj, cctxt.db_connection)
         self.assertEqual(mock.sentinel.mq_conn_obj, cctxt.mq_connection)
     mock_create_cm.assert_called_once_with('fake://db')
     mock_create_tport.assert_called_once_with('fake://mq')
     # Second call should use cached objects.
     mock_create_cm.reset_mock()
     mock_create_tport.reset_mock()
     with context.target_cell(ctxt, mapping) as cctxt:
         self.assertEqual(mock.sentinel.db_conn_obj, cctxt.db_connection)
         self.assertEqual(mock.sentinel.mq_conn_obj, cctxt.mq_connection)
     mock_create_cm.assert_not_called()
     mock_create_tport.assert_not_called()
Exemple #38
0
    def start_service(self, name, host=None, **kwargs):
        cell = None
        if name == 'compute' and self.USES_DB:
            # NOTE(danms): We need to create the HostMapping first, because
            # otherwise we'll fail to update the scheduler while running
            # the compute node startup routines below.
            ctxt = context.get_context()
            cell_name = kwargs.pop('cell', CELL1_NAME) or CELL1_NAME
            cell = self.cell_mappings[cell_name]
            hm = objects.HostMapping(context=ctxt,
                                     host=host or name,
                                     cell_mapping=cell)
            hm.create()
            self.host_mappings[hm.host] = hm
            if host is not None:
                # Make sure that CONF.host is relevant to the right hostname
                self.useFixture(nova_fixtures.ConfPatcher(host=host))
        svc = self.useFixture(
            nova_fixtures.ServiceFixture(name, host, cell=cell, **kwargs))

        return svc.service
Exemple #39
0
    def start_service(self, name, host=None, **kwargs):
        cell = None
        if name == 'compute' and self.USES_DB:
            # NOTE(danms): We need to create the HostMapping first, because
            # otherwise we'll fail to update the scheduler while running
            # the compute node startup routines below.
            ctxt = context.get_context()
            cell_name = kwargs.pop('cell', CELL1_NAME) or CELL1_NAME
            cell = self.cell_mappings[cell_name]
            hm = objects.HostMapping(context=ctxt,
                                     host=host or name,
                                     cell_mapping=cell)
            hm.create()
            self.host_mappings[hm.host] = hm
            if host is not None:
                # Make sure that CONF.host is relevant to the right hostname
                self.useFixture(nova_fixtures.ConfPatcher(host=host))
        svc = self.useFixture(
            nova_fixtures.ServiceFixture(name, host, cell=cell, **kwargs))

        return svc.service
    def test_scatter_gather_cells_exception(self, mock_get_inst,
                                            mock_log_exception):
        # This is needed because we're mocking get_by_filters.
        self.useFixture(nova_fixtures.SpawnIsSynchronousFixture())
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)
        mapping1 = objects.CellMapping(database_connection='fake://db1',
                                       transport_url='fake://mq1',
                                       uuid=uuids.cell1)
        mappings = objects.CellMappingList(objects=[mapping0, mapping1])

        # Simulate cell1 raising an exception.
        mock_get_inst.side_effect = [mock.sentinel.instances,
                                     test.TestingException()]

        results = context.scatter_gather_cells(
            ctxt, mappings, 30, objects.InstanceList.get_by_filters)
        self.assertEqual(2, len(results))
        self.assertIn(mock.sentinel.instances, results.values())
        self.assertIn(context.raised_exception_sentinel, results.values())
        self.assertTrue(mock_log_exception.called)
Exemple #41
0
    def test_scatter_gather_cells_exception(self, mock_get_inst,
                                            mock_log_exception):
        # This is needed because we're mocking get_by_filters.
        self.useFixture(nova_fixtures.SpawnIsSynchronousFixture())
        ctxt = context.get_context()
        mapping0 = objects.CellMapping(database_connection='fake://db0',
                                       transport_url='none:///',
                                       uuid=objects.CellMapping.CELL0_UUID)
        mapping1 = objects.CellMapping(database_connection='fake://db1',
                                       transport_url='fake://mq1',
                                       uuid=uuids.cell1)
        mappings = objects.CellMappingList(objects=[mapping0, mapping1])

        # Simulate cell1 raising an exception.
        mock_get_inst.side_effect = [mock.sentinel.instances,
                                     test.TestingException()]

        results = context.scatter_gather_cells(
            ctxt, mappings, 30, objects.InstanceList.get_by_filters)
        self.assertEqual(2, len(results))
        self.assertIn(mock.sentinel.instances, results.values())
        self.assertIsInstance(results[mapping1.uuid], Exception)
        # non-NovaException gets logged
        self.assertTrue(mock_log_exception.called)

        # Now run it again with a NovaException to see it's not logged.
        mock_log_exception.reset_mock()
        mock_get_inst.side_effect = [mock.sentinel.instances,
                                     exception.NotFound()]

        results = context.scatter_gather_cells(
            ctxt, mappings, 30, objects.InstanceList.get_by_filters)
        self.assertEqual(2, len(results))
        self.assertIn(mock.sentinel.instances, results.values())
        self.assertIsInstance(results[mapping1.uuid], exception.NovaException)
        # NovaExceptions are not logged, the caller should handle them.
        mock_log_exception.assert_not_called()
Exemple #42
0
    def _setup_cells(self):
        """Setup a normal cellsv2 environment.

        This sets up the CellDatabase fixture with two cells, one cell0
        and one normal cell. CellMappings are created for both so that
        cells-aware code can find those two databases.
        """
        celldbs = nova_fixtures.CellDatabases()

        ctxt = context.get_context()
        fake_transport = 'fake://nowhere/'

        c0 = objects.CellMapping(
            context=ctxt,
            uuid=objects.CellMapping.CELL0_UUID,
            name='cell0',
            transport_url=fake_transport,
            database_connection=objects.CellMapping.CELL0_UUID)
        c0.create()
        self.cell_mappings[c0.name] = c0
        celldbs.add_cell_database(objects.CellMapping.CELL0_UUID)

        for x in range(self.NUMBER_OF_CELLS):
            name = 'cell%i' % (x + 1)
            uuid = getattr(uuids, name)
            cell = objects.CellMapping(
                context=ctxt,
                uuid=uuid,
                name=name,
                transport_url=fake_transport,
                database_connection=uuid)
            cell.create()
            self.cell_mappings[name] = cell
            # cell1 is the default cell
            celldbs.add_cell_database(uuid, default=(x == 0))

        self.useFixture(celldbs)
Exemple #43
0
    def _setup_cells(self):
        """Setup a normal cellsv2 environment.

        This sets up the CellDatabase fixture with two cells, one cell0
        and one normal cell. CellMappings are created for both so that
        cells-aware code can find those two databases.
        """
        celldbs = nova_fixtures.CellDatabases()

        ctxt = context.get_context()
        fake_transport = 'fake://nowhere/'

        c0 = objects.CellMapping(
            context=ctxt,
            uuid=objects.CellMapping.CELL0_UUID,
            name='cell0',
            transport_url=fake_transport,
            database_connection=objects.CellMapping.CELL0_UUID)
        c0.create()
        self.cell_mappings[c0.name] = c0
        celldbs.add_cell_database(objects.CellMapping.CELL0_UUID)

        for x in range(self.NUMBER_OF_CELLS):
            name = 'cell%i' % (x + 1)
            uuid = getattr(uuids, name)
            cell = objects.CellMapping(
                context=ctxt,
                uuid=uuid,
                name=name,
                transport_url=fake_transport,
                database_connection=uuid)
            cell.create()
            self.cell_mappings[name] = cell
            # cell1 is the default cell
            celldbs.add_cell_database(uuid, default=(x == 0))

        self.useFixture(celldbs)
 def setUp(self):
     super(SchedulerUtilsTestCase, self).setUp()
     self.context = nova_context.get_context()
Exemple #45
0
 def setUp(self):
     super(SchedulerUtilsTestCase, self).setUp()
     self.context = nova_context.get_context()
 def test_get_context(self):
     ctxt = context.get_context()
     self.assertIsNone(ctxt.user_id)
     self.assertIsNone(ctxt.project_id)
     self.assertFalse(ctxt.is_admin)
Exemple #47
0
 def test_get_context(self):
     ctxt = context.get_context()
     self.assertIsNone(ctxt.user_id)
     self.assertIsNone(ctxt.project_id)
     self.assertFalse(ctxt.is_admin)