def test_get_hosts(self):
        instance1 = tests_utils.get_test_instance(self.context,
                flavor=flavors.get_default_flavor(), obj=True)
        instance1.host = 'hostA'
        instance1.save()
        instance2 = tests_utils.get_test_instance(self.context,
                flavor=flavors.get_default_flavor(), obj=True)
        instance2.host = 'hostB'
        instance2.save()
        instance3 = tests_utils.get_test_instance(self.context,
                flavor=flavors.get_default_flavor(), obj=True)
        instance3.host = 'hostB'
        instance3.save()

        instance_ids = [instance1.uuid, instance2.uuid, instance3.uuid]
        values = self._get_default_values()
        group = self._create_instance_group(self.context, values)
        instance_group.InstanceGroup.add_members(self.context, group.uuid,
                instance_ids)

        group = instance_group.InstanceGroup.get_by_uuid(self.context,
                group.uuid)
        hosts = group.get_hosts(self.context)
        self.assertEqual(2, len(hosts))
        self.assertIn('hostA', hosts)
        self.assertIn('hostB', hosts)
        hosts = group.get_hosts(self.context, exclude=[instance1.uuid])
        self.assertEqual(1, len(hosts))
        self.assertIn('hostB', hosts)
    def test_count_members_by_user(self):
        instance1 = tests_utils.get_test_instance(self.context,
                flavor=flavors.get_default_flavor(), obj=True)
        instance1.user_id = 'user1'
        instance1.save()
        instance2 = tests_utils.get_test_instance(self.context,
                flavor=flavors.get_default_flavor(), obj=True)
        instance2.user_id = 'user2'
        instance2.save()
        instance3 = tests_utils.get_test_instance(self.context,
                flavor=flavors.get_default_flavor(), obj=True)
        instance3.user_id = 'user2'
        instance3.save()

        instance_ids = [instance1.uuid, instance2.uuid, instance3.uuid]
        values = self._get_default_values()
        group = self._create_instance_group(self.context, values)
        instance_group.InstanceGroup.add_members(self.context, group.uuid,
                instance_ids)

        group = instance_group.InstanceGroup.get_by_uuid(self.context,
                group.uuid)
        count_user1 = group.count_members_by_user(self.context, 'user1')
        count_user2 = group.count_members_by_user(self.context, 'user2')
        count_user3 = group.count_members_by_user(self.context, 'user3')
        self.assertEqual(1, count_user1)
        self.assertEqual(2, count_user2)
        self.assertEqual(0, count_user3)
Exemplo n.º 3
0
    def setUp(self):
        super(ServerActionsSampleJsonTest, self).setUp()
        self.actions = fake_server_actions.FAKE_ACTIONS
        self.events = fake_server_actions.FAKE_EVENTS
        self.instance = test_utils.get_test_instance()

        def fake_instance_action_get_by_request_id(context, uuid, request_id):
            return copy.deepcopy(self.actions[uuid][request_id])

        def fake_server_actions_get(context, uuid):
            return [copy.deepcopy(value) for value in
                    self.actions[uuid].itervalues()]

        def fake_instance_action_events_get(context, action_id):
            return copy.deepcopy(self.events[action_id])

        def fake_instance_get_by_uuid(context, instance_id):
            return self.instance

        def fake_get(self, context, instance_uuid, **kwargs):
            return {'uuid': instance_uuid}

        self.stubs.Set(db, 'action_get_by_request_id',
                       fake_instance_action_get_by_request_id)
        self.stubs.Set(db, 'actions_get', fake_server_actions_get)
        self.stubs.Set(db, 'action_events_get',
                       fake_instance_action_events_get)
        self.stubs.Set(db, 'instance_get_by_uuid', fake_instance_get_by_uuid)
        self.stubs.Set(compute_api.API, 'get', fake_get)
Exemplo n.º 4
0
    def setUp(self):
        super(ServerActionsSampleJsonTest, self).setUp()
        self.api.microversion = self.microversion
        self.actions = fake_server_actions.FAKE_ACTIONS
        self.events = fake_server_actions.FAKE_EVENTS
        self.instance = test_utils.get_test_instance(obj=True)

        def _fake_get(stub_self, context, instance_uuid, expected_attrs=None, want_objects=True):
            return fake_instance.fake_instance_obj(None, **{"uuid": instance_uuid})

        def fake_instance_action_get_by_request_id(context, uuid, request_id):
            return copy.deepcopy(self.actions[uuid][request_id])

        def fake_server_actions_get(context, uuid):
            return [copy.deepcopy(value) for value in six.itervalues(self.actions[uuid])]

        def fake_instance_action_events_get(context, action_id):
            return copy.deepcopy(self.events[action_id])

        def fake_instance_get_by_uuid(context, instance_id):
            return self.instance

        self.stub_out("nova.db.action_get_by_request_id", fake_instance_action_get_by_request_id)
        self.stub_out("nova.db.actions_get", fake_server_actions_get)
        self.stub_out("nova.db.action_events_get", fake_instance_action_events_get)
        self.stub_out("nova.db.instance_get_by_uuid", fake_instance_get_by_uuid)
        self.stub_out("nova.compute.api.API.get", _fake_get)
Exemplo n.º 5
0
 def test_snapshot_not_running(self):
     instance_ref = test_utils.get_test_instance()
     img_ref = self.image_service.create(self.ctxt, {'name': 'snap-1'})
     self.assertRaises(exception.InstanceNotRunning,
                       self.connection.snapshot,
                       self.ctxt, instance_ref, img_ref['id'],
                       lambda *args, **kwargs: None)
Exemplo n.º 6
0
    def setUp(self):
        super(ServerActionsSampleJsonTest, self).setUp()
        self.api.microversion = self.microversion
        self.actions = fake_server_actions.FAKE_ACTIONS
        self.events = fake_server_actions.FAKE_EVENTS
        self.instance = test_utils.get_test_instance(obj=True)

        def fake_instance_action_get_by_request_id(context, uuid, request_id):
            return copy.deepcopy(self.actions[uuid][request_id])

        def fake_server_actions_get(context, uuid):
            return [copy.deepcopy(value) for value in
                    six.itervalues(self.actions[uuid])]

        def fake_instance_action_events_get(context, action_id):
            return copy.deepcopy(self.events[action_id])

        def fake_instance_get_by_uuid(context, instance_id):
            return self.instance

        self.stub_out('nova.db.action_get_by_request_id',
                      fake_instance_action_get_by_request_id)
        self.stub_out('nova.db.actions_get', fake_server_actions_get)
        self.stub_out('nova.db.action_events_get',
                      fake_instance_action_events_get)
        self.stub_out('nova.db.instance_get_by_uuid',
                      fake_instance_get_by_uuid)
        self.stub_out('nova.compute.api.API.get', self._fake_get)
    def setUp(self):
        super(ServerActionsSampleJsonTest, self).setUp()
        self.actions = fake_server_actions.FAKE_ACTIONS
        self.events = fake_server_actions.FAKE_EVENTS
        self.instance = test_utils.get_test_instance(obj=True)

        def fake_instance_action_get_by_request_id(context, uuid, request_id):
            return copy.deepcopy(self.actions[uuid][request_id])

        def fake_server_actions_get(context, uuid):
            return [copy.deepcopy(value) for value in
                    six.itervalues(self.actions[uuid])]

        def fake_instance_action_events_get(context, action_id):
            return copy.deepcopy(self.events[action_id])

        def fake_instance_get_by_uuid(context, instance_id):
            return self.instance

        def fake_get(self, context, instance_uuid, expected_attrs=None,
                     want_objects=True):
            return fake_instance.fake_instance_obj(
                None, **{'uuid': instance_uuid})

        self.stubs.Set(db, 'action_get_by_request_id',
                       fake_instance_action_get_by_request_id)
        self.stubs.Set(db, 'actions_get', fake_server_actions_get)
        self.stubs.Set(db, 'action_events_get',
                       fake_instance_action_events_get)
        self.stubs.Set(db, 'instance_get_by_uuid', fake_instance_get_by_uuid)
        self.stubs.Set(compute_api.API, 'get', fake_get)
Exemplo n.º 8
0
 def test_spawn_container(self, image_info=None, instance_href=None,
                           network_info=None):
     instance_href = utils.get_test_instance()
     image_info = utils.get_test_image_info(None, instance_href)
     network_info = utils.get_test_network_info()
     self.driver.spawn(self.ctxt, instance_href, image_info,
                       'fake_files', 'fake_password', network_info=network_info)
Exemplo n.º 9
0
 def _get_running_instance(self, obj=True):
     instance_ref = test_utils.get_test_instance(obj=obj)
     network_info = test_utils.get_test_network_info()
     network_info[0]["network"]["subnets"][0]["meta"]["dhcp_server"] = "1.1.1.1"
     image_info = test_utils.get_test_image_info(None, instance_ref)
     self.connection.spawn(self.ctxt, instance_ref, image_info, [], "herp", network_info=network_info)
     return instance_ref, network_info
Exemplo n.º 10
0
 def test_create_container_wrong_image(self):
     instance_href = utils.get_test_instance()
     image_info = utils.get_test_image_object(None, instance_href)
     image_info.disk_format = 'raw'
     image_info.container_format = 'invalid_format'
     self.assertRaises(exception.InstanceDeployFailure,
                       self.test_create_container,
                       image_info, instance_href)
Exemplo n.º 11
0
    def setUp(self):
        super(FixedIpTest, self).setUp()

        instance = dict(test_utils.get_test_instance(), hostname="openstack", host="host")
        fake_fixed_ips = [
            {
                "id": 1,
                "address": "192.168.1.1",
                "network_id": 1,
                "virtual_interface_id": 1,
                "instance_uuid": "1",
                "allocated": False,
                "leased": False,
                "reserved": False,
                "created_at": None,
                "deleted_at": None,
                "updated_at": None,
                "deleted": None,
                "instance": instance,
                "network": test_network.fake_network,
                "host": None,
            },
            {
                "id": 2,
                "address": "192.168.1.2",
                "network_id": 1,
                "virtual_interface_id": 2,
                "instance_uuid": "2",
                "allocated": False,
                "leased": False,
                "reserved": False,
                "created_at": None,
                "deleted_at": None,
                "updated_at": None,
                "deleted": None,
                "instance": instance,
                "network": test_network.fake_network,
                "host": None,
            },
        ]

        def fake_fixed_ip_get_by_address(context, address, columns_to_join=None):
            for fixed_ip in fake_fixed_ips:
                if fixed_ip["address"] == address:
                    return fixed_ip
            raise exception.FixedIpNotFoundForAddress(address=address)

        def fake_fixed_ip_update(context, address, values):
            fixed_ip = fake_fixed_ip_get_by_address(context, address)
            if fixed_ip is None:
                raise exception.FixedIpNotFoundForAddress(address=address)
            else:
                for key in values:
                    fixed_ip[key] = values[key]

        self.stubs.Set(db, "fixed_ip_get_by_address", fake_fixed_ip_get_by_address)
        self.stubs.Set(db, "fixed_ip_update", fake_fixed_ip_update)
Exemplo n.º 12
0
 def _get_running_instance(self, obj=True):
     instance_ref = test_utils.get_test_instance(obj=obj)
     network_info = test_utils.get_test_network_info()
     network_info[0]['network']['subnets'][0]['meta']['dhcp_server'] = \
         '1.1.1.1'
     image_info = test_utils.get_test_image_info(None, instance_ref)
     self.connection.spawn(self.ctxt, instance_ref, image_info,
                           [], 'herp', network_info=network_info)
     return instance_ref, network_info
Exemplo n.º 13
0
 def test_find_container_by_uuid(self):
     instance_href = utils.get_test_instance()
     image_info = utils.get_test_image_object(None, instance_href)
     image_info.disk_format = 'raw'
     image_info.container_format = 'docker'
     self.connection.spawn(self.context, instance_href, image_info,
                           'fake_files', 'fake_password')
     info = self.connection._find_container_by_uuid(instance_href['uuid'])
     self.assertEqual(instance_href['name'], info['Config'].get('Hostname'))
Exemplo n.º 14
0
    def test_list_instances(self):
        instance_href = utils.get_test_instance()
        image_info = utils.get_test_image_object(None, instance_href)
        image_info.disk_format = 'raw'
        image_info.container_format = 'docker'
        self.connection.spawn(self.context, instance_href, image_info,
                              'fake_files', 'fake_password')

        instances = self.connection.list_instances()
        self.assertIn(instance_href.name, instances)
Exemplo n.º 15
0
    def test_get_image_name(self):
        instance_ref = utils.get_test_instance()
        image_info = utils.get_test_image_object(None, instance_ref)
        image_info.container_format = 'docker'
        image_info.name = 'MiXeDcAsE-image'
        repo = self.connection._get_image_name(self.context,
                                               instance_ref, image_info)

        # image_name = repo.split("/")[1]
        self.assertEqual(image_info.name, repo)
Exemplo n.º 16
0
 def test_reboot_container(self, image_info=None, instance_href=None,
                            network_info=None):
     reboot_type = "SOFT"
     instance_href = utils.get_test_instance()
     image_info = utils.get_test_image_info(None, instance_href)
     network_info = utils.get_test_network_info()
     self.driver.spawn(self.ctxt, instance_href, image_info,
                       'fake_files', 'fake_password', network_info=network_info)
     self.driver.reboot(self.ctxt, instance_href, network_info,
                        reboot_type)
Exemplo n.º 17
0
    def setUp(self):
        super(FixedIpTest, self).setUp()

        instance = dict(test_utils.get_test_instance(),
                        hostname='openstack', host='host')
        fake_fixed_ips = [{'id': 1,
                   'address': '192.168.1.1',
                   'network_id': 1,
                   'virtual_interface_id': 1,
                   'instance_uuid': '1',
                   'allocated': False,
                   'leased': False,
                   'reserved': False,
                   'created_at': None,
                   'deleted_at': None,
                   'updated_at': None,
                   'deleted': None,
                   'instance': instance,
                   'network': test_network.fake_network,
                   'host': None},
                  {'id': 2,
                   'address': '192.168.1.2',
                   'network_id': 1,
                   'virtual_interface_id': 2,
                   'instance_uuid': '2',
                   'allocated': False,
                   'leased': False,
                   'reserved': False,
                   'created_at': None,
                   'deleted_at': None,
                   'updated_at': None,
                   'deleted': None,
                   'instance': instance,
                   'network': test_network.fake_network,
                   'host': None},
                  ]

        def fake_fixed_ip_get_by_address(context, address,
                                         columns_to_join=None):
            for fixed_ip in fake_fixed_ips:
                if fixed_ip['address'] == address:
                    return fixed_ip
            raise exception.FixedIpNotFoundForAddress(address=address)

        def fake_fixed_ip_update(context, address, values):
            fixed_ip = fake_fixed_ip_get_by_address(context, address)
            if fixed_ip is None:
                raise exception.FixedIpNotFoundForAddress(address=address)
            else:
                for key in values:
                    fixed_ip[key] = values[key]

        self.stubs.Set(db, "fixed_ip_get_by_address",
                       fake_fixed_ip_get_by_address)
        self.stubs.Set(db, "fixed_ip_update", fake_fixed_ip_update)
Exemplo n.º 18
0
 def _get_running_instance(self, obj=True):
     instance_ref = utils.get_test_instance(obj=obj)
     network_info = utils.get_test_network_info()
     network_info[0]['network']['subnets'][0]['meta']['dhcp_server'] = \
         '1.1.1.1'
     image_info = utils.get_test_image_object(None, instance_ref)
     image_info.disk_format = 'raw'
     image_info.container_format = 'docker'
     self.connection.spawn(self.ctxt, instance_ref, image_info,
                           [], 'herp', network_info=network_info)
     return instance_ref, network_info
Exemplo n.º 19
0
    def test_list_instances_no_inspect_race(self):
        """Assures containers that cannot be inspected are not listed."""
        instance_href = utils.get_test_instance()
        image_info = utils.get_test_image_object(None, instance_href)
        image_info.disk_format = 'raw'
        image_info.container_format = 'docker'
        self.connection.spawn(self.context, instance_href, image_info,
                              'fake_files', 'fake_password')

        with mock.patch.object(self.mock_client, 'inspect_container',
                               return_value={}):
            instances = self.connection.list_instances()
            self.assertFalse(instances)
Exemplo n.º 20
0
 def test_create_container_empty_cmd(self, image_info=None,
                                     instance_href=None,
                                     network_info=None):
     if instance_href is None:
         instance_href = utils.get_test_instance()
     if image_info is None:
         image_info = utils.get_test_image_object(None, instance_href)
         image_info.disk_format = 'raw'
         image_info.container_format = 'docker'
     with mock.patch.object(self.mock_client, 'create_container') as mc:
         self.connection.spawn(self.context, instance_href, image_info,
                               'fake_files', 'fake_password',
                               network_info=network_info)
         self.assertIsNone(mc.call_args[1].get('command'))
Exemplo n.º 21
0
 def _get_running_instance(self, obj=False, image_name=None, flavor=None):
     instance_ref = utils.get_test_instance(obj=obj, flavor=flavor)
     network_info = utils.get_test_network_info()
     network_info[0]['network']['subnets'][0]['meta']['dhcp_server'] = (
         '1.1.1.1')
     image_info = utils.get_test_image_info(None, instance_ref)
     image_info['disk_format'] = 'raw'
     image_info['container_format'] = 'docker'
     if image_name:
         image_info['name'] = image_name
     self.connection.spawn(self.ctxt, jsonutils.to_primitive(instance_ref),
                           image_info, [], 'herp',
                           network_info=network_info)
     return instance_ref, network_info
Exemplo n.º 22
0
 def test_create_container(self, image_info=None, instance_href=None,
                           network_info=None):
     if instance_href is None:
         instance_href = utils.get_test_instance()
     if image_info is None:
         image_info = utils.get_test_image_object(None, instance_href)
         image_info.disk_format = 'raw'
         image_info.container_format = 'docker'
     self.connection.spawn(self.context, instance_href, image_info,
                           'fake_files', 'fake_password',
                           network_info=network_info)
     self._assert_cpu_shares(instance_href)
     self.assertEqual(self.mock_client.name, "nova-{0}".format(
         instance_href['uuid']))
Exemplo n.º 23
0
 def test_pull_missing_image_with_shared_dir(self, mock_os):
     self.fixture.config(shared_directory='/fake_dir', group='docker')
     instance_ref = utils.get_test_instance()
     image_info = utils.get_test_image_object(None, instance_ref)
     image_info.name = 'fake_name'
     image_info.id = 'fake_id'
     with mock.patch.object(self.mock_client, 'load_repository_file') as f:
         with mock.patch.object(self.mock_client, 'inspect_image') as i:
             i.return_value = 'fake_image'
             image = self.connection._pull_missing_image(self.context,
                                                         image_info,
                                                         instance_ref)
             f.assert_called_once_with('fake_name', '/fake_dir/fake_id')
             i.assert_called_once_with('fake_name')
             self.assertEqual('fake_image', image)
Exemplo n.º 24
0
 def test_create_container_inject_key(self, mock_inject_key):
     self.fixture.config(inject_key=True, group='docker')
     instance_href = utils.get_test_instance()
     instance_href.key_data = 'my_key'
     image_info = utils.get_test_image_info(None, instance_href)
     image_info['disk_format'] = 'raw'
     image_info['container_format'] = 'docker'
     with mock.patch.object(self.mock_client, 'create_container'):
         with mock.patch.object(self.mock_client, 'start') as ms:
             self.connection.spawn(self.context, instance_href, image_info,
                                   'fake_files', 'fake_password',
                                   network_info=None)
             command = ms.call_args[1]
             expected = {'binds': {'/tmp/.ssh':
                         {'bind': '/root/.ssh', 'ro': True}}}
             self.assertEqual(expected, command)
Exemplo n.º 25
0
 def test_create_container_glance_cmd(self, image_info=None,
                                      instance_href=None,
                                      network_info=None):
     if instance_href is None:
         instance_href = utils.get_test_instance()
     if image_info is None:
         image_info = utils.get_test_image_object(None, instance_href)
         image_info.disk_format = 'raw'
         image_info.container_format = 'docker'
         image_info.properties.os_command_line = 'uname'
     with mock.patch.object(self.mock_client, 'create_container') as mc:
         self.connection.spawn(self.context, instance_href, image_info,
                               'fake_files', 'fake_password',
                               network_info=network_info)
         command = mc.call_args[1]['command']
         self.assertEqual('uname', command)
Exemplo n.º 26
0
 def test_create_container_vcpus_2(self, image_info=None):
     flavor = utils.get_test_flavor(options={
         'name': 'vcpu_2',
         'flavorid': 'vcpu_2',
         'vcpus': 2
     })
     instance_href = utils.get_test_instance(flavor=flavor)
     if image_info is None:
         image_info = utils.get_test_image_object(None, instance_href)
         image_info.disk_format = 'raw'
         image_info.container_format = 'docker'
     self.connection.spawn(self.context, instance_href, image_info,
                           'fake_files', 'fake_password')
     self._assert_cpu_shares(instance_href, vcpus=2)
     self.assertEqual(self.mock_client.name, "nova-{0}".format(
         instance_href['uuid']))
Exemplo n.º 27
0
    def test_soft_delete_restore_container(self):
        instance_href = utils.get_test_instance()
        image_info = utils.get_test_image_object(None, instance_href)
        image_info.disk_format = 'raw'
        image_info.container_format = 'docker'

        self.connection.spawn(self.context, instance_href, image_info,
                              'fake_files', 'fake_password')
        container_id = self.connection._find_container_by_uuid(
            instance_href['uuid']).get('id')

        self.connection.soft_delete(instance_href)
        info = self.connection.docker.inspect_container(container_id)
        self.assertFalse(info['State']['Running'])

        self.connection.restore(instance_href)
        info = self.connection.docker.inspect_container(container_id)
        self.assertTrue(info['State']['Running'])
Exemplo n.º 28
0
    def test_snapshot(self, byuuid_mock):
        # Use mix-case to test that mixed-case image names succeed.
        snapshot_name = 'tEsT-SnAp'

        expected_calls = [
            {'args': (),
             'kwargs':
             {'task_state': task_states.IMAGE_PENDING_UPLOAD}},
            {'args': (),
             'kwargs':
             {'task_state': task_states.IMAGE_UPLOADING,
              'expected_state': task_states.IMAGE_PENDING_UPLOAD}}]
        func_call_matcher = matchers.FunctionCallMatcher(expected_calls)

        instance_ref = utils.get_test_instance()
        properties = {'instance_id': instance_ref['id'],
                      'user_id': str(self.context.user_id)}
        sent_meta = {'name': snapshot_name, 'is_public': False,
                     'status': 'creating', 'properties': properties}

        # Because the docker driver doesn't push directly into Glance, we
        # cannot check that the images are correctly configured in the
        # fake image service, but we can ensuring naming and other
        # conventions are accurate.
        image_service = nova.tests.unit.image.fake.FakeImageService()
        recv_meta = image_service.create(context, sent_meta)

        with mock.patch.object(self.mock_client, 'load_image'):
            with mock.patch.object(self.mock_client, 'get_image'):
                self.connection.snapshot(self.context, instance_ref,
                                         recv_meta['id'],
                                         func_call_matcher.call)

                snapshot = image_service.show(context, recv_meta['id'])
                self.assertEqual(snapshot['properties']['image_state'],
                                 'available')
                self.assertEqual(snapshot['status'], 'active')
                self.assertEqual(snapshot['disk_format'], 'raw')
                self.assertEqual(snapshot['container_format'], 'docker')
                self.assertEqual(snapshot['name'], snapshot_name)
Exemplo n.º 29
0
 def test_unfilter_instance(self):
     instance_ref = test_utils.get_test_instance()
     network_info = test_utils.get_test_network_info()
     self.connection.unfilter_instance(instance_ref, network_info)
Exemplo n.º 30
0
 def test_snapshot_not_running(self):
     instance_ref = test_utils.get_test_instance()
     img_ref = self.image_service.create(self.ctxt, {'name': 'snap-1'})
     self.assertRaises(exception.InstanceNotRunning,
                       self.connection.snapshot, self.ctxt, instance_ref,
                       img_ref['id'], lambda *args, **kwargs: None)
Exemplo n.º 31
0
 def test_get_memory_limit_from_sys_meta_in_db_instance(self):
     instance = utils.get_test_instance(obj=False)
     limit = self.connection._get_memory_limit_bytes(instance)
     self.assertEqual(2048 * units.Mi, limit)
Exemplo n.º 32
0
 def test_cleanup_container(self, byname_mock, unplug_mock, teardown_mock):
     instance = utils.get_test_instance()
     self.connection.cleanup(self.context, instance, 'fake_networkinfo')
     byname_mock.assert_called_with(instance['name'])
     teardown_mock.assert_called_with('fake_id')
Exemplo n.º 33
0
 def test_get_test_instance(self):
     # get_test_instance's return value looks like an instance_ref.
     instance_ref = test_utils.get_test_instance()
     ctxt = test_utils.get_test_admin_context()
     db.instance_get(ctxt, instance_ref['id'])
Exemplo n.º 34
0
    def _test_start_container_with_network_events(self, neutron_failure=None):
        generated_events = []

        def wait_timeout():
            event = mock.MagicMock()
            if neutron_failure == 'timeout':
                raise eventlet.timeout.Timeout()
            elif neutron_failure == 'error':
                event.status = 'failed'
            else:
                event.status = 'completed'
            return event

        def fake_prepare(instance, event_name):
            m = mock.MagicMock()
            m.instance = instance
            m.event_name = event_name
            m.wait.side_effect = wait_timeout
            generated_events.append(m)
            return m

        virtapi = manager.ComputeVirtAPI(mock.MagicMock())
        prepare = virtapi._compute.instance_events.prepare_for_instance_event
        prepare.side_effect = fake_prepare
        drvr = novadocker.virt.docker.driver.DockerDriver(virtapi)

        instance_href = utils.get_test_instance()
        container_id = self.connection._find_container_by_uuid(
            instance_href['uuid']).get('id')

        vifs = [{
            'id': 'vif1',
            'active': False
        }, {
            'id': 'vif2',
            'active': False
        }]

        @mock.patch.object(drvr, '_extract_dns_entries')
        @mock.patch.object(drvr, 'plug_vifs')
        @mock.patch.object(drvr, '_attach_vifs')
        @mock.patch.object(self.mock_client, 'start')
        @mock.patch.object(self.mock_client, 'kill')
        @mock.patch.object(self.mock_client, 'remove_container')
        def test_start(remove_container, kill, start, attach_vif, plug_vifs,
                       extract_dns_entries):
            drvr._start_container(container_id, instance_href, vifs)
            plug_vifs.assert_called_with(instance_href, vifs)
            attach_vif.assert_called_with(instance_href, vifs)

            if neutron_failure and cfg.CONF.vif_plugging_is_fatal:
                kill.assert_called_once_with(container_id)
                remove_container.assert_called_once_with(container_id,
                                                         Force=True)

        test_start()

        if nova.utils.is_neutron() and cfg.CONF.vif_plugging_timeout:
            prepare.assert_has_calls([
                mock.call(instance_href, 'network-vif-plugged-vif1'),
                mock.call(instance_href, 'network-vif-plugged-vif2')
            ])
            for event in generated_events:
                if neutron_failure and generated_events.index(event) != 0:
                    self.assertEqual(0, event.call_count)
        else:
            self.assertEqual(0, prepare.call_count)
Exemplo n.º 35
0
 def test_cleanup_container_notfound(self, byuuid_mock, unplug_mock):
     instance = utils.get_test_instance()
     self.connection.cleanup(self.context, instance, 'fake_networkinfo')
     byuuid_mock.assert_called_with(instance['uuid'])
     unplug_mock.assert_called_once_with(instance, 'fake_networkinfo')
Exemplo n.º 36
0
 def test_get_memory_limit_from_sys_meta_in_object(self):
     instance = utils.get_test_instance(obj=True)
     limit = self.connection._get_memory_limit_bytes(instance)
     self.assertEqual(512 * units.Mi, limit)
Exemplo n.º 37
0
 def test_get_info_for_unknown_instance(self):
     fake_instance = test_utils.get_test_instance(obj=True)
     self.assertRaises(exception.NotFound, self.connection.get_info,
                       fake_instance)
Exemplo n.º 38
0
 def test_destroy_container(self, byname_mock, cleanup_mock):
     instance = utils.get_test_instance()
     self.connection.destroy(self.context, instance, 'fake_networkinfo')
     cleanup_mock.assert_called_with(self.context, instance,
                                     'fake_networkinfo', None, True)
Exemplo n.º 39
0
 def test_ensure_filtering_for_instance(self):
     instance = test_utils.get_test_instance(obj=True)
     network_info = test_utils.get_test_network_info()
     self.connection.ensure_filtering_rules_for_instance(
         instance, network_info)
Exemplo n.º 40
0
 def test_get_memory_limit_from_sys_meta_in_db_instance(self):
     instance = utils.get_test_instance(obj=False)
     limit = self.connection._get_memory_limit_bytes(instance)
     self.assertEqual(2048 * units.Mi, limit)
Exemplo n.º 41
0
 def test_cleanup_container_notfound(self, byuuid_mock, unplug_mock):
     instance = utils.get_test_instance()
     self.connection.cleanup(self.context, instance, 'fake_networkinfo')
     byuuid_mock.assert_called_with(instance['uuid'])
     unplug_mock.assert_called_once_with(instance, 'fake_networkinfo')
Exemplo n.º 42
0
    def setUp(self):
        super(FixedIpTest, self).setUp()

        instance = dict(test_utils.get_test_instance(),
                        hostname='openstack',
                        host='host')
        fake_fixed_ips = [
            {
                'id': 1,
                'address': '192.168.1.1',
                'network_id': 1,
                'virtual_interface_id': 1,
                'instance_uuid': '1',
                'allocated': False,
                'leased': False,
                'reserved': False,
                'created_at': None,
                'deleted_at': None,
                'updated_at': None,
                'deleted': None,
                'instance': instance,
                'network': test_network.fake_network,
                'host': None
            },
            {
                'id': 2,
                'address': '192.168.1.2',
                'network_id': 1,
                'virtual_interface_id': 2,
                'instance_uuid': '2',
                'allocated': False,
                'leased': False,
                'reserved': False,
                'created_at': None,
                'deleted_at': None,
                'updated_at': None,
                'deleted': None,
                'instance': instance,
                'network': test_network.fake_network,
                'host': None
            },
        ]

        def fake_fixed_ip_get_by_address(context,
                                         address,
                                         columns_to_join=None):
            for fixed_ip in fake_fixed_ips:
                if fixed_ip['address'] == address:
                    return fixed_ip
            raise exception.FixedIpNotFoundForAddress(address=address)

        def fake_fixed_ip_get_by_address_detailed(context, address):
            network = {'id': 1, 'cidr': "192.168.1.0/24"}
            host = {'host': "host", 'hostname': 'openstack'}
            for fixed_ip in fake_fixed_ips:
                if fixed_ip['address'] == address:
                    return (fixed_ip, network, host)
            raise exception.FixedIpNotFoundForAddress(address=address)

        def fake_fixed_ip_update(context, address, values):
            fixed_ip = fake_fixed_ip_get_by_address(context, address)
            if fixed_ip is None:
                raise exception.FixedIpNotFoundForAddress(address=address)
            else:
                for key in values:
                    fixed_ip[key] = values[key]

        self.stubs.Set(db, "fixed_ip_get_by_address",
                       fake_fixed_ip_get_by_address)
        self.stubs.Set(db, "fixed_ip_get_by_address_detailed",
                       fake_fixed_ip_get_by_address_detailed)
        self.stubs.Set(db, "fixed_ip_update", fake_fixed_ip_update)
Exemplo n.º 43
0
 def test_destroy_instance_nonexistent(self):
     fake_instance = test_utils.get_test_instance(obj=True)
     network_info = test_utils.get_test_network_info()
     self.connection.destroy(self.ctxt, fake_instance, network_info)
Exemplo n.º 44
0
    def setUp(self):
        super(FixedIpTest, self).setUp()
        self.api.microversion = self.microversion
        instance = dict(test_utils.get_test_instance(),
                        hostname='compute.host.pvt',
                        host='host')
        fake_fixed_ips = [
            {
                'id': 1,
                'address': '192.168.1.1',
                'network_id': 1,
                'virtual_interface_id': 1,
                'instance_uuid': uuids.instance_1,
                'allocated': False,
                'leased': False,
                'reserved': False,
                'created_at': None,
                'deleted_at': None,
                'updated_at': None,
                'deleted': None,
                'instance': instance,
                'network': test_network.fake_network,
                'host': None
            },
            {
                'id': 2,
                'address': '192.168.1.2',
                'network_id': 1,
                'virtual_interface_id': 2,
                'instance_uuid': uuids.instance_2,
                'allocated': False,
                'leased': False,
                'reserved': False,
                'created_at': None,
                'deleted_at': None,
                'updated_at': None,
                'deleted': None,
                'instance': instance,
                'network': test_network.fake_network,
                'host': None
            },
        ]

        def fake_fixed_ip_get_by_address(context,
                                         address,
                                         columns_to_join=None):
            for fixed_ip in fake_fixed_ips:
                if fixed_ip['address'] == address:
                    return fixed_ip
            raise exception.FixedIpNotFoundForAddress(address=address)

        def fake_fixed_ip_update(context, address, values):
            fixed_ip = fake_fixed_ip_get_by_address(context, address)
            if fixed_ip is None:
                raise exception.FixedIpNotFoundForAddress(address=address)
            else:
                for key in values:
                    fixed_ip[key] = values[key]

        self.stub_out("nova.db.fixed_ip_get_by_address",
                      fake_fixed_ip_get_by_address)
        self.stub_out("nova.db.fixed_ip_update", fake_fixed_ip_update)