def setup_mocks(self): self.mox.StubOutClassWithMocks(frontend_wrappers, 'RetryingAFE') self.afe = frontend_wrappers.RetryingAFE(server=mox.IgnoreArg(), delay_sec=mox.IgnoreArg(), timeout_min=mox.IgnoreArg())
def _do_test_create_volume_backed_image(self, extra_properties): def _fake_id(x): return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12) body = dict(createImage=dict(name='snapshot_of_volume_backed')) if extra_properties: body['createImage']['metadata'] = extra_properties image_service = glance.get_default_image_service() bdm = [ dict(volume_id=_fake_id('a'), volume_size=1, device_name='vda', delete_on_termination=False) ] props = dict(kernel_id=_fake_id('b'), ramdisk_id=_fake_id('c'), root_device_name='/dev/vda', block_device_mapping=bdm) original_image = dict(properties=props, container_format='ami', status='active', is_public=True) image_service.create(None, original_image) def fake_block_device_mapping_get_all_by_instance( context, inst_id, use_slave=False): return [ fake_block_device.FakeDbBlockDeviceDict({ 'volume_id': _fake_id('a'), 'source_type': 'snapshot', 'destination_type': 'volume', 'volume_size': 1, 'device_name': 'vda', 'snapshot_id': 1, 'boot_index': 0, 'delete_on_termination': False, 'no_device': None }) ] self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', fake_block_device_mapping_get_all_by_instance) instance = fakes.fake_instance_get(image_ref=original_image['id'], vm_state=vm_states.ACTIVE, root_device_name='/dev/vda') self.stubs.Set(db, 'instance_get_by_uuid', instance) volume = dict(id=_fake_id('a'), size=1, host='fake', display_description='fake') snapshot = dict(id=_fake_id('d')) self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api') volume_api = self.controller.compute_api.volume_api volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume) volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'], mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot) self.mox.ReplayAll() req = fakes.HTTPRequestV3.blank(self.url) response = self.controller._action_create_image(req, FAKE_UUID, body) location = response.headers['Location'] image_id = location.replace(glance.generate_image_url(''), '') image = image_service.show(None, image_id) self.assertEqual(image['name'], 'snapshot_of_volume_backed') properties = image['properties'] self.assertEqual(properties['kernel_id'], _fake_id('b')) self.assertEqual(properties['ramdisk_id'], _fake_id('c')) self.assertEqual(properties['root_device_name'], '/dev/vda') self.assertEqual(properties['bdm_v2'], True) bdms = properties['block_device_mapping'] self.assertEqual(len(bdms), 1) self.assertEqual(bdms[0]['boot_index'], 0) self.assertEqual(bdms[0]['source_type'], 'snapshot') self.assertEqual(bdms[0]['destination_type'], 'volume') self.assertEqual(bdms[0]['snapshot_id'], snapshot['id']) for fld in ('connection_info', 'id', 'instance_uuid', 'device_name'): self.assertNotIn(fld, bdms[0]) for k in extra_properties.keys(): self.assertEqual(properties[k], extra_properties[k])
def test_list_nets_pagination(self): cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self.mox.StubOutWithMock(network.ListNetwork, "extend_list") network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg()) self._test_list_resources_with_pagination("networks", cmd)
def create_configuration(): configuration = mox.MockObject(conf.Configuration) configuration.append_config_values(mox.IgnoreArg()) return configuration
def test_basic_schedule_run_instances_anti_affinity(self): filter_properties = {'scheduler_hints': {'group': 'cats'}} # Request spec 1 instance_opts1 = { 'project_id': 1, 'os_type': 'Linux', 'memory_mb': 512, 'root_gb': 512, 'ephemeral_gb': 0, 'vcpus': 1, 'system_metadata': { 'system': 'metadata' } } request_spec1 = { 'instance_uuids': ['fake-uuid1-1', 'fake-uuid1-2'], 'instance_properties': instance_opts1, 'instance_type': { 'memory_mb': 512, 'root_gb': 512, 'ephemeral_gb': 0, 'vcpus': 1 } } self.next_weight = 1.0 def _fake_weigh_objects(_self, functions, hosts, options): self.next_weight += 2.0 host_state = hosts[0] return [weights.WeighedHost(host_state, self.next_weight)] sched = fakes.FakeFilterScheduler() fake_context = context.RequestContext('user', 'project', is_admin=True) self.stubs.Set(sched.host_manager, 'get_filtered_hosts', fake_get_group_filtered_hosts) self.stubs.Set(weights.HostWeightHandler, 'get_weighed_objects', _fake_weigh_objects) fakes.mox_host_manager_db_calls(self.mox, fake_context) self.mox.StubOutWithMock(driver, 'instance_update_db') self.mox.StubOutWithMock(compute_rpcapi.ComputeAPI, 'run_instance') self.mox.StubOutWithMock(sched, 'group_hosts') instance1_1 = {'uuid': 'fake-uuid1-1'} instance1_2 = {'uuid': 'fake-uuid1-2'} sched.group_hosts(mox.IgnoreArg(), 'cats').AndReturn([]) def inc_launch_index1(*args, **kwargs): request_spec1['instance_properties']['launch_index'] = ( request_spec1['instance_properties']['launch_index'] + 1) expected_metadata = { 'system_metadata': { 'system': 'metadata', 'group': 'cats' } } driver.instance_update_db( fake_context, instance1_1['uuid'], extra_values=expected_metadata).WithSideEffects( inc_launch_index1).AndReturn(instance1_1) compute_rpcapi.ComputeAPI.run_instance( fake_context, host='host3', instance=instance1_1, requested_networks=None, injected_files=None, admin_password=None, is_first_time=None, request_spec=request_spec1, filter_properties=mox.IgnoreArg(), node='node3') driver.instance_update_db( fake_context, instance1_2['uuid'], extra_values=expected_metadata).WithSideEffects( inc_launch_index1).AndReturn(instance1_2) compute_rpcapi.ComputeAPI.run_instance( fake_context, host='host4', instance=instance1_2, requested_networks=None, injected_files=None, admin_password=None, is_first_time=None, request_spec=request_spec1, filter_properties=mox.IgnoreArg(), node='node4') self.mox.ReplayAll() sched.schedule_run_instance(fake_context, request_spec1, None, None, None, None, filter_properties)
def test_no_admin_pass(self): # This is here to avoid masking errors, it shouldn't be used normally self.useFixture( fixtures.MonkeyPatch('nova.virt.xenapi.vm_utils.destroy_vdi', _fake_noop)) # Mocks instance = {} self.mox.StubOutWithMock(vm_utils, 'safe_find_sr') vm_utils.safe_find_sr('session').AndReturn('sr_ref') self.mox.StubOutWithMock(vm_utils, 'create_vdi') vm_utils.create_vdi('session', 'sr_ref', instance, 'config-2', 'configdrive', 64 * 1024 * 1024).AndReturn('vdi_ref') self.mox.StubOutWithMock(vm_utils, 'vdi_attached_here') vm_utils.vdi_attached_here('session', 'vdi_ref', read_only=False).AndReturn( contextified('mounted_dev')) class FakeInstanceMetadata(object): def __init__(self, instance, content=None, extra_md=None): pass def metadata_for_config_drive(self): return [] self.useFixture( fixtures.MonkeyPatch('nova.api.metadata.base.InstanceMetadata', FakeInstanceMetadata)) self.mox.StubOutWithMock(utils, 'execute') utils.execute('genisoimage', '-o', mox.IgnoreArg(), '-ldots', '-allow-lowercase', '-allow-multidot', '-l', '-publisher', mox.IgnoreArg(), '-quiet', '-J', '-r', '-V', 'config-2', mox.IgnoreArg(), attempts=1, run_as_root=False).AndReturn(None) utils.execute('dd', mox.IgnoreArg(), mox.IgnoreArg(), run_as_root=True).AndReturn(None) self.mox.StubOutWithMock(vm_utils, 'create_vbd') vm_utils.create_vbd('session', 'vm_ref', 'vdi_ref', mox.IgnoreArg(), bootable=False, read_only=True).AndReturn(None) self.mox.ReplayAll() # And the actual call we're testing vm_utils.generate_configdrive('session', instance, 'vm_ref', 'userdevice')
def test_update_dhcp_for_nw01(self): self.flags(use_single_default_gateway=True) self.mox.StubOutWithMock(self.driver, 'write_to_file') self.mox.StubOutWithMock(self.driver, 'ensure_path') self.mox.StubOutWithMock(os, 'chmod') self.driver.write_to_file(mox.IgnoreArg(), mox.IgnoreArg()) self.driver.write_to_file(mox.IgnoreArg(), mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) self.driver.ensure_path(mox.IgnoreArg()) os.chmod(mox.IgnoreArg(), mox.IgnoreArg()) os.chmod(mox.IgnoreArg(), mox.IgnoreArg()) self.mox.ReplayAll() self.driver.update_dhcp(self.context, "eth0", networks[0])
def test_unshelve(self): db_instance = jsonutils.to_primitive(self._create_fake_instance()) instance = objects.Instance.get_by_uuid( self.context, db_instance['uuid'], expected_attrs=['metadata', 'system_metadata']) instance.task_state = task_states.UNSHELVING instance.save() image = {'id': 'fake_id'} host = 'fake-mini' node = test_compute.NODENAME limits = {} filter_properties = {'limits': limits} cur_time = timeutils.utcnow() cur_time_tz = cur_time.replace(tzinfo=iso8601.iso8601.Utc()) timeutils.set_time_override(cur_time) sys_meta = dict(instance.system_metadata) sys_meta['shelved_at'] = timeutils.strtime(at=cur_time) sys_meta['shelved_image_id'] = image['id'] sys_meta['shelved_host'] = host self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage') self.mox.StubOutWithMock(self.compute, '_prep_block_device') self.mox.StubOutWithMock(self.compute.driver, 'spawn') self.mox.StubOutWithMock(self.compute, '_get_power_state') self.mox.StubOutWithMock(self.rt, 'instance_claim') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.mox.StubOutWithMock(self.compute.network_api, 'migrate_instance_finish') self.deleted_image_id = None def fake_delete(self2, ctxt, image_id): self.deleted_image_id = image_id def fake_claim(context, instance, limits): instance.host = self.compute.host return claims.Claim(db_instance, self.rt, _fake_resources()) fake_image.stub_out_image_service(self.stubs) self.stubs.Set(fake_image._FakeImageService, 'delete', fake_delete) self.compute._notify_about_instance_usage(self.context, instance, 'unshelve.start') db.instance_update_and_get_original( self.context, instance['uuid'], { 'task_state': task_states.SPAWNING }, update_cells=False, columns_to_join=['metadata', 'system_metadata'], ).AndReturn((db_instance, db_instance)) self.compute._prep_block_device( self.context, instance, mox.IgnoreArg(), do_check_attach=False).AndReturn('fake_bdm') db_instance['key_data'] = None db_instance['auto_disk_config'] = None self.compute.network_api.migrate_instance_finish( self.context, instance, { 'source_compute': '', 'dest_compute': self.compute.host }) self.compute.driver.spawn(self.context, instance, image, injected_files=[], admin_password=None, network_info=[], block_device_info='fake_bdm') self.compute._get_power_state(self.context, instance).AndReturn(123) db.instance_update_and_get_original( self.context, instance['uuid'], { 'power_state': 123, 'vm_state': vm_states.ACTIVE, 'task_state': None, 'image_ref': instance['image_ref'], 'key_data': None, 'host': self.compute.host, # rt.instance_claim set this 'auto_disk_config': False, 'expected_task_state': task_states.SPAWNING, 'launched_at': cur_time_tz }, update_cells=False, columns_to_join=['metadata', 'system_metadata']).AndReturn( (db_instance, dict(db_instance, host=self.compute.host))) self.compute._notify_about_instance_usage(self.context, instance, 'unshelve.end') self.mox.ReplayAll() with mock.patch.object(self.rt, 'instance_claim', side_effect=fake_claim): self.compute.unshelve_instance(self.context, instance, image=image, filter_properties=filter_properties, node=node) self.assertEqual(image['id'], self.deleted_image_id) self.assertEqual(instance.host, self.compute.host)
def test_unshelve_volume_backed(self): db_instance = jsonutils.to_primitive(self._create_fake_instance()) node = test_compute.NODENAME limits = {} filter_properties = {'limits': limits} cur_time = timeutils.utcnow() cur_time_tz = cur_time.replace(tzinfo=iso8601.iso8601.Utc()) timeutils.set_time_override(cur_time) instance = objects.Instance.get_by_uuid( self.context, db_instance['uuid'], expected_attrs=['metadata', 'system_metadata']) instance.task_state = task_states.UNSHELVING instance.save() self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage') self.mox.StubOutWithMock(self.compute, '_prep_block_device') self.mox.StubOutWithMock(self.compute.driver, 'spawn') self.mox.StubOutWithMock(self.compute, '_get_power_state') self.mox.StubOutWithMock(self.rt, 'instance_claim') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.mox.StubOutWithMock(self.compute.network_api, 'migrate_instance_finish') self.compute._notify_about_instance_usage(self.context, instance, 'unshelve.start') db.instance_update_and_get_original( self.context, instance['uuid'], { 'task_state': task_states.SPAWNING }, update_cells=False, columns_to_join=['metadata', 'system_metadata']).AndReturn( (db_instance, db_instance)) self.compute._prep_block_device( self.context, instance, mox.IgnoreArg(), do_check_attach=False).AndReturn('fake_bdm') db_instance['key_data'] = None db_instance['auto_disk_config'] = None self.compute.network_api.migrate_instance_finish( self.context, instance, { 'source_compute': '', 'dest_compute': self.compute.host }) self.rt.instance_claim(self.context, instance, limits).AndReturn( claims.Claim(db_instance, self.rt, _fake_resources())) self.compute.driver.spawn(self.context, instance, None, injected_files=[], admin_password=None, network_info=[], block_device_info='fake_bdm') self.compute._get_power_state(self.context, instance).AndReturn(123) db.instance_update_and_get_original( self.context, instance['uuid'], { 'power_state': 123, 'vm_state': vm_states.ACTIVE, 'task_state': None, 'key_data': None, 'auto_disk_config': False, 'expected_task_state': task_states.SPAWNING, 'launched_at': cur_time_tz }, update_cells=False, columns_to_join=['metadata', 'system_metadata']).AndReturn( (db_instance, db_instance)) self.compute._notify_about_instance_usage(self.context, instance, 'unshelve.end') self.mox.ReplayAll() self.compute.unshelve_instance(self.context, instance, image=None, filter_properties=filter_properties, node=node)
def get_configured_driver(server='ignore_server', path='ignore_path'): configuration = mox.MockObject(conf.Configuration) configuration.xenapi_nfs_server = server configuration.xenapi_nfs_serverpath = path configuration.append_config_values(mox.IgnoreArg()) return driver.XenAPINFSDriver(configuration=configuration)
def test_get_resources(self): self.mock.StubOutWithMock(contrib, 'standard_extensions') contrib.standard_extensions(mox.IgnoreArg()).AndReturn(None) self.assertNotEqual( Healthnmon(FakeExtensionManager()).get_resources(), None)
def mox_host_manager_db_calls(mock, context): mock.StubOutWithMock(db, 'compute_node_get_all') db.compute_node_get_all(mox.IgnoreArg()).AndReturn(COMPUTE_NODES)
def test_shelve(self): CONF.shelved_offload_time = -1 db_instance = jsonutils.to_primitive(self._create_fake_instance()) self.compute.run_instance(self.context, instance=db_instance) instance = instance_obj.Instance.get_by_uuid( self.context, db_instance['uuid'], expected_attrs=['metadata', 'system_metadata']) image_id = 'fake_image_id' host = 'fake-mini' cur_time = timeutils.utcnow() timeutils.set_time_override(cur_time) instance.task_state = task_states.SHELVING instance.save() sys_meta = dict(instance.system_metadata) sys_meta['shelved_at'] = timeutils.strtime(at=cur_time) sys_meta['shelved_image_id'] = image_id sys_meta['shelved_host'] = host db_instance['system_metadata'] = utils.dict_to_metadata(sys_meta) self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage') self.mox.StubOutWithMock(self.compute.driver, 'snapshot') self.mox.StubOutWithMock(self.compute.driver, 'power_off') self.mox.StubOutWithMock(self.compute, '_get_power_state') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.compute._notify_about_instance_usage(self.context, instance, 'shelve.start') self.compute.driver.power_off(instance) self.compute._get_power_state(self.context, instance).AndReturn(123) self.compute.driver.snapshot(self.context, instance, 'fake_image_id', mox.IgnoreArg()) db.instance_update_and_get_original( self.context, instance['uuid'], { 'power_state': 123, 'vm_state': vm_states.SHELVED, 'task_state': None, 'expected_task_state': [task_states.SHELVING, task_states.SHELVING_IMAGE_UPLOADING], 'system_metadata': sys_meta }, update_cells=False, columns_to_join=['metadata', 'system_metadata'], ).AndReturn((db_instance, db_instance)) self.compute._notify_about_instance_usage(self.context, instance, 'shelve.end') self.mox.ReplayAll() self.compute.shelve_instance(self.context, instance, image_id=image_id) self.mox.VerifyAll() self.mox.UnsetStubs() self.compute.terminate_instance(self.context, instance=instance)
def test_unshelve_volume_backed(self): db_instance = jsonutils.to_primitive(self._create_fake_instance()) host = 'fake-mini' cur_time = timeutils.utcnow() cur_time_tz = cur_time.replace(tzinfo=iso8601.iso8601.Utc()) timeutils.set_time_override(cur_time) self.compute.run_instance(self.context, instance=db_instance) instance = instance_obj.Instance.get_by_uuid( self.context, db_instance['uuid'], expected_attrs=['metadata', 'system_metadata']) instance.task_state = task_states.UNSHELVING instance.save() sys_meta = dict(instance.system_metadata) sys_meta['shelved_at'] = timeutils.strtime(at=cur_time) sys_meta['shelved_image_id'] = None sys_meta['shelved_host'] = host hypervisor_hostname = 'fake_hypervisor_hostname' fake_compute_info = {'hypervisor_hostname': hypervisor_hostname} self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage') self.mox.StubOutWithMock(self.compute, '_prep_block_device') self.mox.StubOutWithMock(self.compute.driver, 'spawn') self.mox.StubOutWithMock(self.compute, '_get_power_state') self.mox.StubOutWithMock(self.compute, '_get_compute_info') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.compute._notify_about_instance_usage(self.context, instance, 'unshelve.start') self.compute._get_compute_info( mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(fake_compute_info) db.instance_update_and_get_original( self.context, instance['uuid'], { 'task_state': task_states.SPAWNING, 'host': host, 'node': hypervisor_hostname }, update_cells=False, columns_to_join=['metadata', 'system_metadata']).AndReturn( (db_instance, db_instance)) self.compute._prep_block_device(self.context, instance, []).AndReturn('fake_bdm') db_instance['key_data'] = None db_instance['auto_disk_config'] = None self.compute.driver.spawn(self.context, instance, None, injected_files=[], admin_password=None, network_info=[], block_device_info='fake_bdm') self.compute._get_power_state(self.context, instance).AndReturn(123) db.instance_update_and_get_original( self.context, instance['uuid'], { 'power_state': 123, 'vm_state': vm_states.ACTIVE, 'task_state': None, 'key_data': None, 'auto_disk_config': False, 'expected_task_state': task_states.SPAWNING, 'launched_at': cur_time_tz }, update_cells=False, columns_to_join=['metadata', 'system_metadata']).AndReturn( (db_instance, db_instance)) self.compute._notify_about_instance_usage(self.context, instance, 'unshelve.end') self.mox.ReplayAll() self.compute.unshelve_instance(self.context, instance, image=None) self.mox.VerifyAll() self.mox.UnsetStubs() self.compute.terminate_instance(self.context, instance=instance)
def _writeToMuxFileHelper(self): mux_file = self.mox.CreateMockAnything() open(FAKE_MUX_FILE_PATH, 'w').AndReturn(mux_file) mux_file.__enter__().AndReturn(mux_file) mux_file.write(MUX_MODE) mux_file.__exit__(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg())
def _shelve_instance(self, shelved_offload_time, clean_shutdown=True): CONF.set_override('shelved_offload_time', shelved_offload_time) db_instance = jsonutils.to_primitive(self._create_fake_instance()) instance = objects.Instance.get_by_uuid( self.context, db_instance['uuid'], expected_attrs=['metadata', 'system_metadata']) image_id = 'fake_image_id' host = 'fake-mini' cur_time = timeutils.utcnow() timeutils.set_time_override(cur_time) instance.task_state = task_states.SHELVING instance.save() sys_meta = dict(instance.system_metadata) sys_meta['shelved_at'] = timeutils.strtime(at=cur_time) sys_meta['shelved_image_id'] = image_id sys_meta['shelved_host'] = host db_instance['system_metadata'] = utils.dict_to_metadata(sys_meta) self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage') self.mox.StubOutWithMock(self.compute.driver, 'snapshot') self.mox.StubOutWithMock(self.compute.driver, 'power_off') self.mox.StubOutWithMock(self.compute, '_get_power_state') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.compute._notify_about_instance_usage(self.context, instance, 'shelve.start') if clean_shutdown: self.compute.driver.power_off(instance, CONF.shutdown_timeout, self.compute.SHUTDOWN_RETRY_INTERVAL) else: self.compute.driver.power_off(instance, 0, 0) self.compute._get_power_state(self.context, instance).AndReturn(123) self.compute.driver.snapshot(self.context, instance, 'fake_image_id', mox.IgnoreArg()) update_values = { 'power_state': 123, 'vm_state': vm_states.SHELVED, 'task_state': None, 'expected_task_state': [task_states.SHELVING, task_states.SHELVING_IMAGE_UPLOADING], 'system_metadata': sys_meta } if CONF.shelved_offload_time == 0: update_values['task_state'] = task_states.SHELVING_OFFLOADING db.instance_update_and_get_original( self.context, instance['uuid'], update_values, update_cells=False, columns_to_join=['metadata', 'system_metadata'], ).AndReturn((db_instance, db_instance)) self.compute._notify_about_instance_usage(self.context, instance, 'shelve.end') if CONF.shelved_offload_time == 0: self.compute._notify_about_instance_usage(self.context, instance, 'shelve_offload.start') self.compute.driver.power_off(instance) self.compute._get_power_state(self.context, instance).AndReturn(123) db.instance_update_and_get_original( self.context, instance['uuid'], { 'power_state': 123, 'host': None, 'node': None, 'vm_state': vm_states.SHELVED_OFFLOADED, 'task_state': None, 'expected_task_state': [task_states.SHELVING, task_states.SHELVING_OFFLOADING] }, update_cells=False, columns_to_join=['metadata', 'system_metadata'], ).AndReturn((db_instance, db_instance)) self.compute._notify_about_instance_usage(self.context, instance, 'shelve_offload.end') self.mox.ReplayAll() self.compute.shelve_instance(self.context, instance, image_id=image_id, clean_shutdown=clean_shutdown)
def setup_mock_log(self, name=None): if name is None: stacklog.get_logger(name=mox.IgnoreArg()).AndReturn(self.log) else: stacklog.get_logger(name=name).AndReturn(self.log)
def SetUpIndexIssues(self): search.Index(name=settings.search_index_name_format % 1).AndReturn( self.mock_index) self.mock_index.put(mox.IgnoreArg()).WithSideEffects(self.RecordDocs)
def _stub_lb_reload(self, num=1, setup=True): if setup: self.m.StubOutWithMock(lb.LoadBalancer, 'handle_update') for i in range(num): lb.LoadBalancer.handle_update(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(None)
def SetUpCreateIssueSearchDocuments(self): self.mox.StubOutWithMock(tracker_fulltext, '_IndexDocsInShard') tracker_fulltext._IndexDocsInShard(1, mox.IgnoreArg()).WithSideEffects( lambda shard_id, docs: self.RecordDocs(docs))
def create_configuration(): configuration = mox.MockObject(conf.Configuration) configuration.append_config_values(mox.IgnoreArg()) configuration.nfs_mount_point_base = '/mnt/test' configuration.nfs_mount_options = None return configuration
def setUp(self): super(TestQuantumv2, self).setUp() self.mox.StubOutWithMock(quantumv2, 'get_client') self.moxed_client = self.mox.CreateMock(client.Client) quantumv2.get_client(mox.IgnoreArg()).MultipleTimes().AndReturn( self.moxed_client) self.context = context.RequestContext('userid', 'my_tenantid') setattr(self.context, 'auth_token', 'bff4a5a6b9eb4ea2a6efec6eefb77936') self.instance = { 'project_id': '9d049e4b60b64716978ab415e6fbd5c0', 'uuid': str(uuid.uuid4()), 'display_name': 'test_instance', 'availability_zone': 'nova', 'security_groups': [] } self.nets1 = [{ 'id': 'my_netid1', 'name': 'my_netname1', 'tenant_id': 'my_tenantid' }] self.nets2 = [] self.nets2.append(self.nets1[0]) self.nets2.append({ 'id': 'my_netid2', 'name': 'my_netname2', 'tenant_id': 'my_tenantid' }) self.nets3 = self.nets2 + [{ 'id': 'my_netid3', 'name': 'my_netname3', 'tenant_id': 'my_tenantid' }] self.nets4 = [{ 'id': 'his_netid4', 'name': 'his_netname4', 'tenant_id': 'his_tenantid' }] self.nets = [self.nets1, self.nets2, self.nets3, self.nets4] self.port_address = '10.0.1.2' self.port_data1 = [{ 'network_id': 'my_netid1', 'device_id': 'device_id1', 'device_owner': 'compute:nova', 'id': 'my_portid1', 'fixed_ips': [{ 'ip_address': self.port_address, 'subnet_id': 'my_subid1' }], 'mac_address': 'my_mac1', }] self.dhcp_port_data1 = [{ 'fixed_ips': [{ 'ip_address': '10.0.1.9', 'subnet_id': 'my_subid1' }] }] self.port_data2 = [] self.port_data2.append(self.port_data1[0]) self.port_data2.append({ 'network_id': 'my_netid2', 'device_id': 'device_id2', 'device_owner': 'compute:nova', 'id': 'my_portid2', 'fixed_ips': [{ 'ip_address': '10.0.2.2', 'subnet_id': 'my_subid2' }], 'mac_address': 'my_mac2', }) self.port_data3 = [{ 'network_id': 'my_netid1', 'device_id': 'device_id3', 'device_owner': 'compute:nova', 'id': 'my_portid3', 'fixed_ips': [], # no fixed ip 'mac_address': 'my_mac3', }] self.subnet_data1 = [{ 'id': 'my_subid1', 'cidr': '10.0.1.0/24', 'network_id': 'my_netid1', 'gateway_ip': '10.0.1.1', 'dns_nameservers': ['8.8.1.1', '8.8.1.2'] }] self.subnet_data2 = [] self.subnet_data2.append({ 'id': 'my_subid2', 'cidr': '10.0.2.0/24', 'network_id': 'my_netid2', 'gateway_ip': '10.0.2.1', 'dns_nameservers': ['8.8.2.1', '8.8.2.2'] }) self.fip_pool = { 'id': '4fdbfd74-eaf8-4884-90d9-00bd6f10c2d3', 'name': 'ext_net', 'router:external': True, 'tenant_id': 'admin_tenantid' } self.fip_pool_nova = { 'id': '435e20c3-d9f1-4f1b-bee5-4611a1dd07db', 'name': 'nova', 'router:external': True, 'tenant_id': 'admin_tenantid' } self.fip_unassociated = { 'tenant_id': 'my_tenantid', 'id': 'fip_id1', 'floating_ip_address': '172.24.4.227', 'floating_network_id': self.fip_pool['id'], 'port_id': None, 'fixed_ip_address': None, 'router_id': None } fixed_ip_address = self.port_data2[1]['fixed_ips'][0]['ip_address'] self.fip_associated = { 'tenant_id': 'my_tenantid', 'id': 'fip_id2', 'floating_ip_address': '172.24.4.228', 'floating_network_id': self.fip_pool['id'], 'port_id': self.port_data2[1]['id'], 'fixed_ip_address': fixed_ip_address, 'router_id': 'router_id1' }
def _test_network_api(self, method, rpc_method, **kwargs): ctxt = context.RequestContext('fake_user', 'fake_project') rpcapi = network_rpcapi.NetworkAPI() self.assertIsNotNone(rpcapi.client) self.assertEqual(rpcapi.client.target.topic, CONF.network_topic) expected_retval = 'foo' if rpc_method == 'call' else None expected_version = kwargs.pop('version', None) expected_fanout = kwargs.pop('fanout', None) expected_kwargs = kwargs.copy() for k, v in expected_kwargs.items(): if isinstance(v, self.DefaultArg): expected_kwargs[k] = v.value kwargs.pop(k) prepare_kwargs = {} if expected_version: prepare_kwargs['version'] = expected_version if expected_fanout: prepare_kwargs['fanout'] = True if 'source_compute' in expected_kwargs: # Fix up for migrate_instance_* calls. expected_kwargs['source'] = expected_kwargs.pop('source_compute') expected_kwargs['dest'] = expected_kwargs.pop('dest_compute') targeted_methods = [ 'lease_fixed_ip', 'release_fixed_ip', 'rpc_setup_network_on_host', '_rpc_allocate_fixed_ip', 'deallocate_fixed_ip', 'update_dns', '_associate_floating_ip', '_disassociate_floating_ip', 'lease_fixed_ip', 'release_fixed_ip', 'migrate_instance_start', 'migrate_instance_finish', 'allocate_for_instance', 'deallocate_for_instance', ] targeted_by_instance = ['deallocate_for_instance'] if method in targeted_methods and ('host' in expected_kwargs or 'instance' in expected_kwargs): if method in targeted_by_instance: host = expected_kwargs['instance']['host'] else: host = expected_kwargs['host'] if method not in ['allocate_for_instance', 'deallocate_fixed_ip']: expected_kwargs.pop('host') if CONF.multi_host: prepare_kwargs['server'] = host self.mox.StubOutWithMock(rpcapi, 'client') version_check = [ 'deallocate_for_instance', 'deallocate_fixed_ip', 'allocate_for_instance', ] if method in version_check: rpcapi.client.can_send_version(mox.IgnoreArg()).AndReturn(True) if prepare_kwargs: rpcapi.client.prepare(**prepare_kwargs).AndReturn(rpcapi.client) rpc_method = getattr(rpcapi.client, rpc_method) rpc_method(ctxt, method, **expected_kwargs).AndReturn('foo') self.mox.ReplayAll() retval = getattr(rpcapi, method)(ctxt, **kwargs) self.assertEqual(retval, expected_retval)
def test_get_instance_nw_info_2(self): # Test to get one port in each of two networks and subnets. quantumv2.get_client(mox.IgnoreArg(), admin=True).MultipleTimes().AndReturn( self.moxed_client) self._get_instance_nw_info(2)
def _test_create_volume_backed_image_with_metadata_from_volume( self, extra_metadata=None): def _fake_id(x): return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12) body = dict(createImage=dict(name='snapshot_of_volume_backed')) if extra_metadata: body['createImage']['metadata'] = extra_metadata image_service = glance.get_default_image_service() def fake_block_device_mapping_get_all_by_instance( context, inst_id, use_slave=False): return [ fake_block_device.FakeDbBlockDeviceDict({ 'volume_id': _fake_id('a'), 'source_type': 'snapshot', 'destination_type': 'volume', 'volume_size': 1, 'device_name': 'vda', 'snapshot_id': 1, 'boot_index': 0, 'delete_on_termination': False, 'no_device': None }) ] self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', fake_block_device_mapping_get_all_by_instance) instance = fakes.fake_instance_get(image_ref='', vm_state=vm_states.ACTIVE, root_device_name='/dev/vda') self.stubs.Set(db, 'instance_get_by_uuid', instance) fake_metadata = { 'test_key1': 'test_value1', 'test_key2': 'test_value2' } volume = dict(id=_fake_id('a'), size=1, host='fake', display_description='fake', volume_image_metadata=fake_metadata) snapshot = dict(id=_fake_id('d')) self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api') volume_api = self.controller.compute_api.volume_api volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume) volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume) volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'], mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot) req = fakes.HTTPRequestV3.blank(self.url) self.mox.ReplayAll() response = self.controller._action_create_image(req, FAKE_UUID, body) location = response.headers['Location'] image_id = location.replace('http://localhost:9292/images/', '') image = image_service.show(None, image_id) properties = image['properties'] self.assertEqual(properties['test_key1'], 'test_value1') self.assertEqual(properties['test_key2'], 'test_value2') if extra_metadata: for key, val in extra_metadata.items(): self.assertEqual(properties[key], val)
def _stub_allocate_for_instance(self, net_idx=1, **kwargs): api = quantumapi.API() self.mox.StubOutWithMock(api, 'get_instance_nw_info') # Net idx is 1-based for compatibility with existing unit tests nets = self.nets[net_idx - 1] ports = {} fixed_ips = {} macs = kwargs.get('macs') if macs: macs = set(macs) req_net_ids = [] if 'requested_networks' in kwargs: for id, fixed_ip, port_id in kwargs['requested_networks']: if port_id: self.moxed_client.show_port(port_id).AndReturn({ 'port': { 'id': 'my_portid1', 'network_id': 'my_netid1', 'mac_address': 'my_mac1' } }) ports['my_netid1'] = self.port_data1[0] id = 'my_netid1' if macs is not None: macs.discard('my_mac1') else: fixed_ips[id] = fixed_ip req_net_ids.append(id) expected_network_order = req_net_ids else: expected_network_order = [n['id'] for n in nets] if kwargs.get('_break') == 'pre_list_networks': self.mox.ReplayAll() return api search_ids = [net['id'] for net in nets if net['id'] in req_net_ids] mox_list_network_params = dict(tenant_id=self.instance['project_id'], shared=False) if search_ids: mox_list_network_params['id'] = mox.SameElementsAs(search_ids) self.moxed_client.list_networks(**mox_list_network_params).AndReturn( {'networks': nets}) mox_list_network_params = dict(shared=True) if search_ids: mox_list_network_params['id'] = mox.SameElementsAs(search_ids) self.moxed_client.list_networks(**mox_list_network_params).AndReturn( {'networks': []}) for net_id in expected_network_order: if kwargs.get('_break') == 'net_id2': self.mox.ReplayAll() return api port_req_body = { 'port': { 'device_id': self.instance['uuid'], 'device_owner': 'compute:nova', }, } port = ports.get(net_id, None) if port: port_id = port['id'] self.moxed_client.update_port( port_id, MyComparator(port_req_body)).AndReturn({'port': port}) else: fixed_ip = fixed_ips.get(net_id) if fixed_ip: port_req_body['port']['fixed_ips'] = [{ 'ip_address': fixed_ip }] port_req_body['port']['network_id'] = net_id port_req_body['port']['admin_state_up'] = True port_req_body['port']['tenant_id'] = \ self.instance['project_id'] if macs: port_req_body['port']['mac_address'] = macs.pop() res_port = {'port': {'id': 'fake'}} self.moxed_client.create_port( MyComparator(port_req_body)).AndReturn(res_port) if kwargs.get('_break') == 'pre_get_instance_nw_info': self.mox.ReplayAll() return api api.get_instance_nw_info(mox.IgnoreArg(), self.instance, networks=nets).AndReturn(None) self.mox.ReplayAll() return api
def _save_test_helper(self, cell_type, save_kwargs): """Common code for testing save() for cells/non-cells.""" if cell_type: self.flags(enable=True, cell_type=cell_type, group='cells') else: self.flags(enable=False, group='cells') old_ref = dict(self.fake_instance, host='oldhost', user_data='old', vm_state='old', task_state='old') fake_uuid = old_ref['uuid'] expected_updates = dict(vm_state='meow', task_state='wuff', user_data='new') new_ref = dict(old_ref, host='newhost', **expected_updates) exp_vm_state = save_kwargs.get('expected_vm_state') exp_task_state = save_kwargs.get('expected_task_state') admin_reset = save_kwargs.get('admin_state_reset', False) if exp_vm_state: expected_updates['expected_vm_state'] = exp_vm_state if exp_task_state: if (exp_task_state == 'image_snapshot' and 'instance_version' in save_kwargs and save_kwargs['instance_version'] == '1.9'): expected_updates['expected_task_state'] = [ 'image_snapshot', 'image_snapshot_pending' ] else: expected_updates['expected_task_state'] = exp_task_state self.mox.StubOutWithMock(db, 'instance_get_by_uuid') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.mox.StubOutWithMock(db, 'instance_info_cache_update') cells_api_mock = self.mox.CreateMock(cells_rpcapi.CellsAPI) self.mox.StubOutWithMock(cells_api_mock, 'instance_update_at_top') self.mox.StubOutWithMock(cells_api_mock, 'instance_update_from_api') self.mox.StubOutWithMock(cells_rpcapi, 'CellsAPI', use_mock_anything=True) self.mox.StubOutWithMock(notifications, 'send_update') db.instance_get_by_uuid( self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'], use_slave=False).AndReturn(old_ref) db.instance_update_and_get_original(self.context, fake_uuid, expected_updates, update_cells=False, columns_to_join=[ 'info_cache', 'security_groups', 'system_metadata' ]).AndReturn((old_ref, new_ref)) if cell_type == 'api': cells_rpcapi.CellsAPI().AndReturn(cells_api_mock) cells_api_mock.instance_update_from_api(self.context, mox.IsA(instance.Instance), exp_vm_state, exp_task_state, admin_reset) elif cell_type == 'compute': cells_rpcapi.CellsAPI().AndReturn(cells_api_mock) cells_api_mock.instance_update_at_top(self.context, new_ref) notifications.send_update(self.context, mox.IgnoreArg(), mox.IgnoreArg()) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, old_ref['uuid']) if 'instance_version' in save_kwargs: inst.VERSION = save_kwargs.pop('instance_version') self.assertEqual('old', inst.task_state) self.assertEqual('old', inst.vm_state) self.assertEqual('old', inst.user_data) inst.vm_state = 'meow' inst.task_state = 'wuff' inst.user_data = 'new' inst.save(**save_kwargs) self.assertEqual('newhost', inst.host) self.assertEqual('meow', inst.vm_state) self.assertEqual('wuff', inst.task_state) self.assertEqual('new', inst.user_data) self.assertEqual(set([]), inst.obj_what_changed())
def _test_create_gateway_device(self, name, connector_type, connector_ip, client_certificate=None, client_certificate_file=None, must_raise=False): cmd = nwgw.CreateGatewayDevice(test_cli20.MyApp(sys.stdout), None) myid = 'myid' extra_body = { 'connector_type': connector_type, 'connector_ip': connector_ip, 'client_certificate': client_certificate } self.mox.StubOutWithMock(nwgw, 'read_cert_file') if client_certificate_file: nwgw.read_cert_file(mox.IgnoreArg()).AndReturn('xyz') extra_body['client_certificate'] = 'xyz' self.mox.ReplayAll() position_names = [ 'name', ] position_values = [ name, ] args = [] for (k, v) in extra_body.iteritems(): if (k == 'client_certificate' and client_certificate_file): v = client_certificate_file k = 'client_certificate_file' # Append argument only if value for it was specified if v: args.extend(['--%s' % k.replace('_', '-'), v]) # The following is just for verifying the call fails as expected when # both certificate and certificate file are specified. The extra # argument added is client-certificate since the loop above added # client-certificate-file if client_certificate_file and client_certificate: args.extend(['--client-certificate', client_certificate_file]) args.append(name) if must_raise: self.assertRaises(SystemExit, self._test_create_resource, self.dev_resource, cmd, name, myid, args, position_names, position_values, extra_body=extra_body) else: self._test_create_resource(self.dev_resource, cmd, name, myid, args, position_names, position_values, extra_body=extra_body) self.mox.UnsetStubs()
def _test_list_nets_columns(self, cmd, returned_body, args=['-f', 'json']): resources = 'networks' self.mox.StubOutWithMock(network.ListNetwork, "extend_list") network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg()) self._test_list_columns(cmd, resources, returned_body, args=args)
def testHeartbeat(self): """Trigger heartbeat, verify RPCs and persisting of the responses.""" self.setup_mocks() global_config.global_config.override_config_value( 'SHARD', 'shard_hostname', 'host1') self.expect_heartbeat( return_hosts=[self._get_sample_serialized_host()], return_jobs=[self._get_sample_serialized_job()], return_suite_keyvals=[self._get_sample_serialized_suite_keyvals()]) modified_sample_host = self._get_sample_serialized_host() modified_sample_host['hostname'] = 'host2' self.expect_heartbeat( return_hosts=[modified_sample_host], known_host_ids=[modified_sample_host['id']], known_host_statuses=[modified_sample_host['status']], known_job_ids=[1]) def verify_upload_jobs_and_hqes(name, shard_hostname, jobs, hqes, known_host_ids, known_host_statuses, known_job_ids): self.assertEqual(len(jobs), 1) self.assertEqual(len(hqes), 1) job, hqe = jobs[0], hqes[0] self.assertEqual(hqe['status'], 'Completed') self.expect_heartbeat( jobs=mox.IgnoreArg(), hqes=mox.IgnoreArg(), known_host_ids=[modified_sample_host['id']], known_host_statuses=[modified_sample_host['status']], known_job_ids=[], side_effect=verify_upload_jobs_and_hqes) self.mox.ReplayAll() sut = shard_client.get_shard_client() sut.do_heartbeat() # Check if dummy object was saved to DB host = models.Host.objects.get(id=2) self.assertEqual(host.hostname, 'host1') # Check if suite keyval was saved to DB suite_keyval = models.JobKeyval.objects.filter(job_id=0)[0] self.assertEqual(suite_keyval.key, 'test_key') sut.do_heartbeat() # Ensure it wasn't overwritten host = models.Host.objects.get(id=2) self.assertEqual(host.hostname, 'host1') job = models.Job.objects.all()[0] job.shard = None job.save() hqe = job.hostqueueentry_set.all()[0] hqe.status = 'Completed' hqe.save() sut.do_heartbeat() self.mox.VerifyAll()