def _shelve_instance(self, shelved_offload_time, clean_shutdown=True): CONF.set_override('shelved_offload_time', shelved_offload_time) host = 'fake-mini' instance = self._create_fake_instance_obj(params={'host': host}) image_id = 'fake_image_id' host = 'fake-mini' self.useFixture(utils_fixture.TimeFixture()) instance.task_state = task_states.SHELVING instance.save() 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(self.compute.network_api, 'cleanup_instance_network_on_host') 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) if CONF.shelved_offload_time == 0: self.compute.network_api.cleanup_instance_network_on_host( self.context, instance, instance.host) self.compute.driver.snapshot(self.context, instance, 'fake_image_id', mox.IgnoreArg()) tracking = {'last_state': instance.vm_state} def check_save(expected_task_state=None): self.assertEqual(123, instance.power_state) if tracking['last_state'] == vm_states.ACTIVE: if CONF.shelved_offload_time == 0: self.assertEqual(task_states.SHELVING_OFFLOADING, instance.task_state) else: self.assertIsNone(instance.task_state) self.assertEqual(vm_states.SHELVED, instance.vm_state) self.assertEqual([ task_states.SHELVING, task_states.SHELVING_IMAGE_UPLOADING ], expected_task_state) self.assertIn('shelved_at', instance.system_metadata) self.assertEqual(image_id, instance.system_metadata['shelved_image_id']) self.assertEqual(host, instance.system_metadata['shelved_host']) tracking['last_state'] = instance.vm_state elif (tracking['last_state'] == vm_states.SHELVED and CONF.shelved_offload_time == 0): self.assertIsNone(instance.task_state) self.assertEqual(vm_states.SHELVED_OFFLOADED, instance.vm_state) self.assertEqual( [task_states.SHELVING, task_states.SHELVING_OFFLOADING], expected_task_state) tracking['last_state'] = instance.vm_state elif (tracking['last_state'] == vm_states.SHELVED_OFFLOADED and CONF.shelved_offload_time == 0): self.assertIsNone(instance.host) self.assertIsNone(instance.node) self.assertIsNone(expected_task_state) else: self.fail('Unexpected save!') 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, 0, 0) self.compute._get_power_state(self.context, instance).AndReturn(123) self.compute._notify_about_instance_usage(self.context, instance, 'shelve_offload.end') self.mox.ReplayAll() with mock.patch.object(instance, 'save') as mock_save: mock_save.side_effect = check_save self.compute.shelve_instance(self.context, instance, image_id=image_id, clean_shutdown=clean_shutdown)
def test_create_vm_with_config_drive_verify_method_invocation(self): self.test_instance.config_drive = 'True' self.mox.StubOutWithMock(vmops.VMwareVMOps, '_create_config_drive') self.mox.StubOutWithMock(vmops.VMwareVMOps, '_attach_cdrom_to_vm') self.conn._vmops._create_config_drive( mox.IgnoreArg(), self.test_instance, mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn('[ds1] fake.iso') self.conn._vmops._attach_cdrom_to_vm(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()) self.mox.ReplayAll() # if spawn does not call the _create_config_drive or # _attach_cdrom_to_vm call with the correct set of parameters # then mox's VerifyAll will throw a Expected methods never called # Exception self._spawn_vm()
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 testUpdate(self): """An update gets a master map and updates each entry.""" map_entry1 = automount.AutomountMapEntry() map_entry2 = automount.AutomountMapEntry() map_entry1.key = '/home' map_entry2.key = '/auto' map_entry1.location = 'ou=auto.home,ou=automounts' map_entry2.location = 'ou=auto.auto,ou=automounts' master_map = automount.AutomountMap([map_entry1, map_entry2]) source_mock = self.mox.CreateMock(source.Source) # return the master map source_mock.GetAutomountMasterMap().AndReturn(master_map) # the auto.home cache cache_home = self.mox.CreateMock(caches.Cache) # GetMapLocation() is called, and set to the master map map_entry cache_home.GetMapLocation().AndReturn('/etc/auto.home') # the auto.auto cache cache_auto = self.mox.CreateMock(caches.Cache) # GetMapLocation() is called, and set to the master map map_entry cache_auto.GetMapLocation().AndReturn('/etc/auto.auto') # the auto.master cache cache_master = self.mox.CreateMock(caches.Cache) self.mox.StubOutWithMock(cache_factory, 'Create') cache_factory.Create(mox.IgnoreArg(), 'automount', automount_mountpoint='/home').AndReturn(cache_home) cache_factory.Create(mox.IgnoreArg(), 'automount', automount_mountpoint='/auto').AndReturn(cache_auto) cache_factory.Create(mox.IgnoreArg(), 'automount', automount_mountpoint=None).AndReturn(cache_master) updater = map_updater.AutomountUpdater(config.MAP_AUTOMOUNT, self.workdir, {}) self.mox.StubOutClassWithMocks(map_updater, 'MapUpdater') updater_home = map_updater.MapUpdater(config.MAP_AUTOMOUNT, self.workdir, {}, automount_mountpoint='/home') updater_home.UpdateCacheFromSource( cache_home, source_mock, True, False, 'ou=auto.home,ou=automounts').AndReturn(0) updater_auto = map_updater.MapUpdater(config.MAP_AUTOMOUNT, self.workdir, {}, automount_mountpoint='/auto') updater_auto.UpdateCacheFromSource( cache_auto, source_mock, True, False, 'ou=auto.auto,ou=automounts').AndReturn(0) updater_master = map_updater.MapUpdater(config.MAP_AUTOMOUNT, self.workdir, {}) updater_master.FullUpdateFromMap(cache_master, master_map).AndReturn(0) self.mox.ReplayAll() updater.UpdateFromSource(source_mock) self.assertEqual(map_entry1.location, '/etc/auto.home') self.assertEqual(map_entry2.location, '/etc/auto.auto')
def test_send_login(self): self.ais.sock = mox.MockAnything() self.m.StubOutWithMock(self.ais, "close") self.m.StubOutWithMock(self.ais, "_sendall") # part 1 - raises self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"invalidreply") self.ais.close() # part 2 - raises (empty callsign) self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv( mox.IgnoreArg()).AndReturn(b"# logresp verified, xx") self.ais.close() # part 3 - raises (callsign doesn't match self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv( mox.IgnoreArg()).AndReturn(b"# logresp NOMATCH verified, xx") self.ais.close() # part 4 - raises (unverified, but pass is not -1) self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv( mox.IgnoreArg()).AndReturn(b"# logresp CALL unverified, xx") self.ais.close() # part 5 - normal, receive only self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv( mox.IgnoreArg()).AndReturn(b"# logresp CALL unverified, xx") # part 6 - normal, correct pass self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv( mox.IgnoreArg()).AndReturn(b"# logresp CALL verified, xx") mox.Replay(self.ais.sock) self.m.ReplayAll() # part 1 self.ais.set_login("CALL", "-1") self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login) # part 2 self.ais.set_login("CALL", "-1") self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login) # part 3 self.ais.set_login("CALL", "-1") self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login) # part 4 self.ais.set_login("CALL", "99999") self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login) # part 5 self.ais.set_login("CALL", "-1") self.ais._send_login() # part 6 self.ais.set_login("CALL", "99999") self.ais._send_login() mox.Verify(self.ais.sock) self.m.VerifyAll()
def _test_list_external_nets(self, resources, cmd, detail=False, tags=(), fields_1=(), fields_2=()): self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") self.mox.StubOutWithMock(network.ListNetwork, "extend_list") network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg()) cmd.get_client().MultipleTimes().AndReturn(self.client) reses = { resources: [ { 'id': 'myid1', }, { 'id': 'myid2', }, ], } resstr = self.client.serialize(reses) # url method body query = "" args = detail and [ '-D', ] or [] if fields_1: for field in fields_1: args.append('--fields') args.append(field) if tags: args.append('--') args.append("--tag") for tag in tags: args.append(tag) if (not tags) and fields_2: args.append('--') if fields_2: args.append("--fields") for field in fields_2: args.append(field) for field in itertools.chain(fields_1, fields_2): if query: query += "&fields=" + field else: query = "fields=" + field if query: query += '&router%3Aexternal=True' else: query += 'router%3Aexternal=True' for tag in tags: if query: query += "&tag=" + tag else: query = "tag=" + tag if detail: query = query and query + '&verbose=True' or 'verbose=True' path = getattr(self.client, resources + "_path") self.client.httpclient.request( test_cli20.MyUrlComparator(test_cli20.end_url(path, query), self.client), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() cmd_parser = cmd.get_parser("list_" + resources) shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs() _str = self.fake_stdout.make_string() self.assertIn('myid1', _str)
def test_unshelve(self): instance = self._create_fake_instance_obj() instance.task_state = task_states.UNSHELVING instance.save() image = {'id': 'fake_id'} node = test_compute.NODENAME limits = {} filter_properties = {'limits': limits} host = 'fake-mini' cur_time = timeutils.utcnow() # Adding shelved_* keys in system metadata to verify # whether those are deleted after unshelve call. 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 instance.system_metadata = sys_meta 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(self.compute.network_api, 'setup_instance_network_on_host') 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(context, instance, self.rt, _fake_resources()) tracking = { 'last_state': instance.task_state, 'spawned': False, } def check_save(expected_task_state=None): if tracking['last_state'] == task_states.UNSHELVING: if tracking['spawned']: self.assertIsNone(instance.task_state) else: self.assertEqual(task_states.SPAWNING, instance.task_state) tracking['spawned'] = True tracking['last_state'] == instance.task_state elif tracking['last_state'] == task_states.SPAWNING: self.assertEqual(vm_states.ACTIVE, instance.vm_state) tracking['last_state'] == instance.task_state else: self.fail('Unexpected save!') 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') self.compute._prep_block_device( self.context, instance, mox.IgnoreArg(), do_check_attach=False).AndReturn('fake_bdm') self.compute.network_api.setup_instance_network_on_host( self.context, instance, 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) 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), \ mock.patch.object(instance, 'save') as mock_save: mock_save.side_effect = check_save self.compute.unshelve_instance(self.context, instance, image=image, filter_properties=filter_properties, node=node) self.assertNotIn('shelved_at', instance.system_metadata) self.assertNotIn('shelved_image_id', instance.system_metadata) self.assertNotIn('shelved_host', instance.system_metadata) self.assertEqual(image['id'], self.deleted_image_id) self.assertEqual(instance.host, self.compute.host) self.assertEqual(123, instance.power_state) self.assertEqual(vm_states.ACTIVE, instance.vm_state) self.assertIsNone(instance.task_state) self.assertIsNone(instance.key_data) self.assertEqual(self.compute.host, instance.host) self.assertFalse(instance.auto_disk_config)
def create_configuration(): configuration = mox.MockObject(conf.Configuration) configuration.append_config_values(mox.IgnoreArg()) return configuration
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_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 six.iteritems(extra_body): 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: with test_cli20.capture_std_streams(): 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()
'content': 'punkins!', 'published': '2015-11-15T09:58:35-0800', 'id': 'https://kylewm.com/2015/11/like-of-alber', 'objectType': 'activity', 'verb': 'like', } # uploads send oauth params along with the post data; it's useful to # be able to check that they exist, but ignore their values. IGNORED_OAUTH_PARAMS = [ ('oauth_nonce', mox.IgnoreArg()), ('oauth_timestamp', mox.IgnoreArg()), ('oauth_version', mox.IgnoreArg()), ('oauth_signature_method', mox.IgnoreArg()), ('oauth_consumer_key', mox.IgnoreArg()), ('oauth_token', mox.IgnoreArg()), ('oauth_signature', mox.IgnoreArg()), ] class FlickrTest(testutil.TestCase): def setUp(self): super(FlickrTest, self).setUp() appengine_config.FLICKR_APP_KEY = 'fake' appengine_config.FLICKR_APP_SECRET = 'fake' self.flickr = flickr.Flickr('key', 'secret')
def test_in_blocklist(self): self.mox.StubOutWithMock(FakeSource, 'is_blocked') FakeSource.is_blocked(mox.IgnoreArg()).AndReturn(True) self.mox.ReplayAll() self.check_response('/comment/fake/%s/000/111', expected_status=410)
def test_get_request_token(self): self.api._oauth.request(mox.IgnoreArg()).AndReturn(self._200_request()) self.mox.ReplayAll() token = self.api.get_request_token() self.assertEqual(self.golden_token, token) self.mox.VerifyAll()
def testUpdateNoMaster(self): """An update skips updating the master map, and approprate sub maps.""" source_entry1 = automount.AutomountMapEntry() source_entry2 = automount.AutomountMapEntry() source_entry1.key = '/home' source_entry2.key = '/auto' source_entry1.location = 'ou=auto.home,ou=automounts' source_entry2.location = 'ou=auto.auto,ou=automounts' source_master = automount.AutomountMap([source_entry1, source_entry2]) local_entry1 = automount.AutomountMapEntry() local_entry2 = automount.AutomountMapEntry() local_entry1.key = '/home' local_entry2.key = '/auto' local_entry1.location = '/etc/auto.home' local_entry2.location = '/etc/auto.null' local_master = automount.AutomountMap([local_entry1, local_entry2]) source_mock = self.mox.CreateMock(source.Source) # return the source master map source_mock.GetAutomountMasterMap().AndReturn(source_master) # the auto.home cache cache_home = self.mox.CreateMock(caches.Cache) # GetMapLocation() is called, and set to the master map map_entry cache_home.GetMapLocation().AndReturn('/etc/auto.home') # the auto.auto cache cache_auto = self.mox.CreateMock(caches.Cache) # GetMapLocation() is called, and set to the master map map_entry cache_auto.GetMapLocation().AndReturn('/etc/auto.auto') # the auto.master cache, which should not be written to cache_master = self.mox.CreateMock(caches.Cache) cache_master.GetMap().AndReturn(local_master) self.mox.StubOutWithMock(cache_factory, 'Create') cache_factory.Create(mox.IgnoreArg(), mox.IgnoreArg(), automount_mountpoint=None).AndReturn(cache_master) cache_factory.Create(mox.IgnoreArg(), mox.IgnoreArg(), automount_mountpoint='/home').AndReturn(cache_home) cache_factory.Create(mox.IgnoreArg(), mox.IgnoreArg(), automount_mountpoint='/auto').AndReturn(cache_auto) skip = map_updater.AutomountUpdater.OPT_LOCAL_MASTER updater = map_updater.AutomountUpdater(config.MAP_AUTOMOUNT, self.workdir, {skip: 'yes'}) self.mox.StubOutClassWithMocks(map_updater, 'MapUpdater') updater_home = map_updater.MapUpdater(config.MAP_AUTOMOUNT, self.workdir, {'local_automount_master': 'yes'}, automount_mountpoint='/home') updater_home.UpdateCacheFromSource( cache_home, source_mock, True, False, 'ou=auto.home,ou=automounts').AndReturn(0) self.mox.ReplayAll() updater.UpdateFromSource(source_mock)
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 _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 _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 _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 _service_start_mocks(self): self.mox.StubOutWithMock(objects.Service, 'create') self.mox.StubOutWithMock(objects.Service, 'get_by_host_and_binary') objects.Service.get_by_host_and_binary(mox.IgnoreArg(), self.host, self.binary) objects.Service.create()
def test_connect(self): self.ais.sock = mox.MockAnything() self.m.StubOutWithMock(self.ais, "_open_socket") self.m.StubOutWithMock(self.ais, "close") # part 1 - socket creation errors self.ais._open_socket().AndRaise(socket.timeout("timed out")) self.ais.close() self.ais._open_socket().AndRaise(socket.error('any')) self.ais.close() # part 2 - invalid banner from server self.ais._open_socket() self.ais.sock.getpeername().AndReturn((1, 2)) self.ais.sock.setblocking(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.setsockopt(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"junk") self.ais.close() # part 3 - everything going well self.ais._open_socket() self.ais.sock.getpeername().AndReturn((1, 2)) self.ais.sock.setblocking(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.setsockopt(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# server banner") mox.Replay(self.ais.sock) self.m.ReplayAll() # part 1 self.assertRaises(aprslib.exceptions.ConnectionError, self.ais._connect) self.assertFalse(self.ais._connected) self.assertRaises(aprslib.exceptions.ConnectionError, self.ais._connect) self.assertFalse(self.ais._connected) # part 2 self.assertRaises(aprslib.exceptions.ConnectionError, self.ais._connect) self.assertFalse(self.ais._connected) # part 3 self.ais._connect() self.assertTrue(self.ais._connected) mox.Verify(self.ais.sock) self.m.VerifyAll()
def test_unshelve_volume_backed(self): instance = self._create_fake_instance_obj() node = test_compute.NODENAME limits = {} filter_properties = {'limits': limits} 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(self.compute.network_api, 'setup_instance_network_on_host') tracking = {'last_state': instance.task_state} def check_save(expected_task_state=None): if tracking['last_state'] == task_states.UNSHELVING: self.assertEqual(task_states.SPAWNING, instance.task_state) tracking['last_state'] = instance.task_state elif tracking['last_state'] == task_states.SPAWNING: self.assertEqual(123, instance.power_state) self.assertEqual(vm_states.ACTIVE, instance.vm_state) self.assertIsNone(instance.task_state) self.assertIsNone(instance.key_data) self.assertFalse(instance.auto_disk_config) self.assertIsNone(instance.task_state) tracking['last_state'] = instance.task_state else: self.fail('Unexpected save!') self.compute._notify_about_instance_usage(self.context, instance, 'unshelve.start') self.compute._prep_block_device( self.context, instance, mox.IgnoreArg(), do_check_attach=False).AndReturn('fake_bdm') self.compute.network_api.setup_instance_network_on_host( self.context, instance, self.compute.host) self.rt.instance_claim(self.context, instance, limits).AndReturn( claims.Claim(self.context, 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) self.compute._notify_about_instance_usage(self.context, instance, 'unshelve.end') self.mox.ReplayAll() with mock.patch.object(instance, 'save') as mock_save: mock_save.side_effect = check_save self.compute.unshelve_instance(self.context, instance, image=None, filter_properties=filter_properties, node=node)
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)] 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.stub_out('nova.db.block_device_mapping_get_all_by_instance', fake_block_device_mapping_get_all_by_instance) system_metadata = dict(image_kernel_id=_fake_id('b'), image_ramdisk_id=_fake_id('c'), image_root_device_name='/dev/vda', image_block_device_mapping=str(bdm), image_container_format='ami') instance = fakes.fake_instance_get(image_ref=str(uuid.uuid4()), vm_state=vm_states.ACTIVE, root_device_name='/dev/vda', system_metadata=system_metadata) self.stub_out('nova.db.instance_get_by_uuid', instance) self.mox.StubOutWithMock(self.controller.compute_api.compute_rpcapi, 'quiesce_instance') self.controller.compute_api.compute_rpcapi.quiesce_instance( mox.IgnoreArg(), mox.IgnoreArg()).AndRaise( exception.InstanceQuiesceNotSupported(instance_id='fake', reason='test')) 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() response = self.controller._action_create_image(self.req, FAKE_UUID, body=body) location = response.headers['Location'] image_id = location.replace(self.image_url or 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.assertTrue(properties['bdm_v2']) 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']) self.assertEqual('/dev/vda', bdms[0]['device_name']) for fld in ('connection_info', 'id', 'instance_uuid'): self.assertNotIn(fld, bdms[0]) for k in extra_properties.keys(): self.assertEqual(properties[k], extra_properties[k])