def test_strip_credentials_from_log(self): def verify_no_credentials(kwargs): return ('REDACTED' in kwargs['body']) and ( self.client.password not in kwargs['body']) def verify_credentials(body): return 'REDACTED' not in body and self.client.password in body self.mox.StubOutWithMock(self.client, "request") self.mox.StubOutWithMock(utils, "http_log_req") res200 = get_response(200) utils.http_log_req(mox.IgnoreArg(), mox.IgnoreArg(), mox.Func( verify_no_credentials)) self.client.request( mox.IsA(str), mox.IsA(str), body=mox.Func(verify_credentials), headers=mox.IgnoreArg() ).AndReturn((res200, json.dumps(KS_TOKEN_RESULT))) utils.http_log_req(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()) self.client.request( mox.IsA(str), mox.IsA(str), headers=mox.IsA(dict) ).AndReturn((res200, '')) self.mox.ReplayAll() self.client.do_request('/resource', 'GET')
def _setup_get_cached_image_mocks(self, cow=True, vhd_format=constants.DISK_FORMAT_VHD): m = vhdutils.VHDUtils.get_vhd_format(mox.Func(self._check_img_path)) m.AndReturn(vhd_format) def check_img_path_with_ext(image_path): return image_path == self._fetched_image + '.' + vhd_format.lower() fake.PathUtils.rename(mox.Func(self._check_img_path), mox.Func(check_img_path_with_ext)) if cow and vhd_format == constants.DISK_FORMAT_VHD: m = vhdutils.VHDUtils.get_vhd_info( mox.Func(check_img_path_with_ext)) m.AndReturn({'MaxInternalSize': 1024}) fake.PathUtils.copyfile(mox.IsA(str), mox.IsA(str)) m = vhdutils.VHDUtils.get_internal_vhd_size_by_file_size( mox.IsA(six.text_type), mox.IsA(object)) m.AndReturn(1025) vhdutils.VHDUtils.resize_vhd(mox.IsA(str), mox.IsA(object), is_file_max_size=False)
def expect_request(self, method, path, params, params_func=None, response=None, raises=None): if params_func == None: params_func = lambda p: self.compare_params( p, self.EXPECTED_PREAUTH_PARAMS) self.expected_calls.request( method, path, mox.Func(params_func), { 'User-Agent': self.EXPECTED_USER_AGENT, 'Host': self.HOST, 'Content-type': 'application/x-www-form-urlencoded', 'Authorization': mox.Func((lambda s: s.startswith('Basic ') and not s. startswith('Basic b\''))), 'Date': mox.Func((lambda s: bool(email.utils.parsedate_tz(s)))) }, ) meth = self.expected_calls.getresponse() if raises is not None: meth.AndRaise(raises) else: meth.AndReturn(response) self.expected_calls.close()
def test_rpc_topic_uses_message_type(self): self.flags(rpc_driver_queue_base='cells.intercell42', group='cells') msg_runner = fakes.get_message_runner('api-cell') cell_state = fakes.get_cell_state('api-cell', 'child-cell2') message = messaging._BroadcastMessage(msg_runner, self.ctxt, 'fake', {}, 'down', fanout=True) message.message_type = 'fake-message-type' expected_server_params = {'hostname': 'rpc_host2', 'password': '******', 'port': 3092, 'username': '******', 'virtual_host': 'rpc_vhost2'} expected_url = ('rabbit://%(username)s:%(password)s@' '%(hostname)s:%(port)d/%(virtual_host)s' % expected_server_params) def check_transport_url(cell_state): return cell_state.db_info['transport_url'] == expected_url rpcapi = self.driver.intercell_rpcapi rpcclient = self.mox.CreateMockAnything() self.mox.StubOutWithMock(rpcapi, '_get_client') rpcapi._get_client( mox.Func(check_transport_url), 'cells.intercell42.fake-message-type').AndReturn(rpcclient) rpcclient.prepare(fanout=True).AndReturn(rpcclient) rpcclient.cast(mox.IgnoreArg(), 'process_message', message=message.to_json()) self.mox.ReplayAll() self.driver.send_message_to_cell(cell_state, message)
def _setup_destroy_mocks(self, destroy_disks=True): m = vmutils.VMUtils.vm_exists(mox.Func(self._check_instance_name)) m.AndReturn(True) func = mox.Func(self._check_instance_name) vmutils.VMUtils.set_vm_state(func, constants.HYPERV_VM_STATE_DISABLED) self._setup_delete_vm_log_mocks() vmutils.VMUtils.destroy_vm(func) if destroy_disks: m = fake.PathUtils.get_instance_dir(mox.IsA(str), create_dir=False, remove_dir=True) m.AndReturn(self._test_instance_dir)
def _mock_attach_config_drive(self, instance, config_drive_format): instance['config_drive'] = True self._mox.StubOutWithMock(fake.PathUtils, 'lookup_configdrive_path') m = fake.PathUtils.lookup_configdrive_path( mox.Func(self._check_instance_name)) if config_drive_format in constants.DISK_FORMAT_MAP: m.AndReturn(self._test_instance_dir + '/configdrive.' + config_drive_format) else: m.AndReturn(None) m = vmutils.VMUtils.attach_ide_drive( mox.Func(self._check_instance_name), mox.IsA(str), mox.IsA(int), mox.IsA(int), mox.IsA(str)) m.WithSideEffects(self._add_disk).InAnyOrder()
def test_send_message_to_cell_cast(self): msg_runner = fakes.get_message_runner('api-cell') cell_state = fakes.get_cell_state('api-cell', 'child-cell2') message = messaging._TargetedMessage(msg_runner, self.ctxt, 'fake', {}, 'down', cell_state, fanout=False) expected_server_params = {'hostname': 'rpc_host2', 'password': '******', 'port': 3092, 'username': '******', 'virtual_host': 'rpc_vhost2'} expected_url = ('rabbit://%(username)s:%(password)s@' '%(hostname)s:%(port)d/%(virtual_host)s' % expected_server_params) def check_transport_url(cell_state): return cell_state.db_info['transport_url'] == expected_url rpcapi = self.driver.intercell_rpcapi rpcclient = self.mox.CreateMockAnything() self.mox.StubOutWithMock(rpcapi, '_get_client') rpcapi._get_client( mox.Func(check_transport_url), 'cells.intercell.targeted').AndReturn(rpcclient) rpcclient.cast(mox.IgnoreArg(), 'process_message', message=message.to_json()) self.mox.ReplayAll() self.driver.send_message_to_cell(cell_state, message)
def _test_finish_revert_migration(self, power_on, ephemeral_storage=False, config_drive=False, config_drive_format='iso'): self._instance = self._get_instance() self._instance.memory_mb = 100 self._instance.vcpus = 2 self._instance['system_metadata'] = {} network_info = fake_network.fake_get_instance_nw_info(self.stubs) fake_revert_path = ('C:\\FakeInstancesPath\\%s\\_revert' % self._instance.name) m = fake.PathUtils.get_instance_dir(mox.IsA(str), create_dir=False, remove_dir=True) m.AndReturn(self._test_instance_dir) m = pathutils.PathUtils.get_instance_migr_revert_dir( self._instance.name) m.AndReturn(fake_revert_path) fake.PathUtils.rename(fake_revert_path, mox.IsA(str)) m = fake.PathUtils.get_instance_dir(mox.IsA(str)) m.AndReturn(self._test_instance_dir) m = fake.PathUtils.get_instance_dir(mox.IsA(str)) if ephemeral_storage: m.AndReturn(self._test_instance_dir) else: m.AndReturn(None) m = imagecache.ImageCache.get_image_details(mox.IsA(object), mox.IsA(object)) m.AndReturn({'properties': {constants.IMAGE_PROP_VM_GEN: constants.IMAGE_PROP_VM_GEN_1}}) self._set_vm_name(self._instance.name) self._setup_create_instance_mocks(None, False, ephemeral_storage=ephemeral_storage) if power_on: vmutils.VMUtils.set_vm_state(mox.Func(self._check_instance_name), constants.HYPERV_VM_STATE_ENABLED) self._setup_log_vm_output_mocks() if config_drive: self._mock_attach_config_drive(self._instance, config_drive_format) self._mox.ReplayAll() self._conn.finish_revert_migration(self._context, self._instance, network_info, None, power_on) self._mox.VerifyAll() if config_drive: self._verify_attach_config_drive(config_drive_format)
def expect_get_posts(self): post = data.BlogPost() post.id = util.Struct(text='tag:blogger.com,1999:blog-111.post-222') feed = data.BlogFeed() feed.entry = [post] def check_path(query): return query.custom_parameters['path'] == '/path/to/post' self.blogger_client.get_posts('111', query=mox.Func(check_path)).AndReturn(feed)
def expect_task(self, queue, eta_seconds=None, **kwargs): if not self.stubbed_create_task: self.mox.StubOutWithMock(tasks_client, 'create_task') self.stubbed_create_task = True def check_queue(path): if not path.endswith('/' + queue): # print("expect_task: %s doesn't end with /%s!" % (path, queue)) return False return True def check_params(params): req = params['app_engine_http_request'] if not check_queue(req['relative_uri']): return False # convert model objects and keys to url-safe key strings for comparison for name, val in kwargs.items(): if isinstance(val, ndb.Model): kwargs[name] = val.key.urlsafe().decode() elif isinstance(val, ndb.Key): kwargs[name] = val.urlsafe().decode() got = set(urllib.parse.parse_qsl(req['body'].decode())) expected = set(kwargs.items()) if got != expected: # print('expect_task: expected %s, got %s' % (expected, got)) return False if eta_seconds is not None: got = params['schedule_time'].seconds - util.to_utc_timestamp( util.now_fn()) delta = eta_seconds * .2 + 10 if not (got + delta >= eta_seconds >= got - delta): # print('expect_task: expected schedule_time %r, got %r' % (eta_seconds, got)) return False return True return tasks_client.create_task( mox.Func(check_queue), mox.Func(check_params)).InAnyOrder().AndReturn( Task(name='my task'))
def testIncrementalUpdate(self): """An incremental update reads a partial map and merges it.""" # Unlike in a full update, we create a cache map and a source map, and # let it merge them. If it goes to write the merged map, we're good. # Also check that timestamps were updated, as in testFullUpdate above. def compare_function(map_object): return len(map_object) == 2 original_modify_stamp = 1 new_modify_stamp = 2 updater = map_updater.MapUpdater(config.MAP_PASSWORD, self.workdir, {}, can_do_incremental=True) updater.WriteModifyTimestamp(original_modify_stamp) cache_map_entry = passwd.PasswdMapEntry({ 'name': 'bar', 'uid': 20, 'gid': 20 }) cache_map = passwd.PasswdMap([cache_map_entry]) cache_map.SetModifyTimestamp(original_modify_stamp) cache_mock = self.mox.CreateMock(caches.Cache) cache_mock.GetMap().AndReturn(cache_map) cache_mock.WriteMap(map_data=mox.Func(compare_function)).AndReturn(0) source_map_entry = passwd.PasswdMapEntry({ 'name': 'foo', 'uid': 10, 'gid': 10 }) source_map = passwd.PasswdMap([source_map_entry]) source_map.SetModifyTimestamp(new_modify_stamp) source_mock = self.mox.CreateMock(source.Source) source_mock.GetMap(config.MAP_PASSWORD, location=None, since=original_modify_stamp).AndReturn(source_map) self.mox.ReplayAll() self.assertEqual( 0, updater.UpdateCacheFromSource(cache_mock, source_mock, incremental=True, force_write=False, location=None)) self.assertEqual(updater.GetModifyTimestamp(), new_modify_stamp) self.assertNotEqual(updater.GetUpdateTimestamp(), None)
def test_create_new_webmention(self): """We should subscribe to webmention sources in Superfeedr.""" self.expect_webmention_requests_get('http://primary/', 'no webmention endpoint') self.mox.StubOutWithMock(superfeedr, 'subscribe') def check_source(source): assert isinstance(source, FakeSource) assert source.is_saved return True superfeedr.subscribe(mox.Func(check_source), self.handler) self.mox.ReplayAll() FakeSource.create_new(self.handler, features=['webmention'], domains=['primary/'], domain_urls=['http://primary/'])
def _setup_create_instance_mocks(self, setup_vif_mocks_func=None, boot_from_volume=False, block_device_info=None, admin_permissions=True, ephemeral_storage=False): vmutils.VMUtils.create_vm(mox.IsA(str), mox.IsA(int), mox.IsA(int), mox.IsA(bool), CONF.hyperv.dynamic_memory_ratio, mox.IsA(int), mox.IsA(str), mox.IsA(list)) if not boot_from_volume: m = vmutils.VMUtils.attach_ide_drive(mox.Func(self._check_vm_name), mox.IsA(str), mox.IsA(int), mox.IsA(int), mox.IsA(str)) m.WithSideEffects(self._add_disk).InAnyOrder() if ephemeral_storage: m = vmutils.VMUtils.attach_ide_drive(mox.Func(self._check_vm_name), mox.IsA(str), mox.IsA(int), mox.IsA(int), mox.IsA(str)) m.WithSideEffects(self._add_disk).InAnyOrder() func = mox.Func(self._check_vm_name) m = vmutils.VMUtils.create_scsi_controller(func) m.InAnyOrder() if boot_from_volume: mapping = driver.block_device_info_get_mapping(block_device_info) data = mapping[0]['connection_info']['data'] target_lun = data['target_lun'] target_iqn = data['target_iqn'] target_portal = data['target_portal'] self._mock_attach_volume(mox.Func(self._check_vm_name), target_iqn, target_lun, target_portal, True) vmutils.VMUtils.create_nic(mox.Func(self._check_vm_name), mox.IsA(str), mox.IsA(six.text_type)).InAnyOrder() if setup_vif_mocks_func: setup_vif_mocks_func() if CONF.hyperv.enable_instance_metrics_collection: vmutils.VMUtils.enable_vm_metrics_collection( mox.Func(self._check_vm_name)) vmutils.VMUtils.get_vm_serial_port_connection( mox.IsA(str), update_connection=mox.IsA(str))
def expect_task(self, queue, eta_seconds=None, **kwargs): self.stub_create_task() def check_task(task): if not task.parent.endswith('/' + queue): # These can help for debugging, but can also be misleading, since many # tests insert multiple tasks, so check_task() runs on all of them (due # to InAnyOrder() below) until it finds one that matches. # print("expect_task: %s doesn't end with /%s!" % (task.parent, queue)) return False req = task.task.app_engine_http_request if not req.relative_uri.endswith('/' + queue): # print("expect_task: relative_uri %s doesn't end with /%s!" % ( # req.relative_uri, queue)) return False # convert model objects and keys to url-safe key strings for comparison for name, val in kwargs.items(): if isinstance(val, ndb.Model): kwargs[name] = val.key.urlsafe().decode() elif isinstance(val, ndb.Key): kwargs[name] = val.urlsafe().decode() got = set(urllib.parse.parse_qsl(req.body.decode())) expected = set(kwargs.items()) if got != expected: # print('expect_task: expected %s, got %s' % (expected, got)) return False if eta_seconds is not None: got = (util.to_utc_timestamp(task.task.schedule_time) - util.to_utc_timestamp(util.now_fn())) delta = eta_seconds * .2 + 10 if not (got + delta >= eta_seconds >= got - delta): # print('expect_task: expected schedule_time %r, got %r' % (eta_seconds, got)) return False return True return tasks_client.create_task( mox.Func(check_task)).InAnyOrder().AndReturn(Task(name='my task'))
def test_http_json(self): data = {"test": "json_request"} fake = utils.FakeResponse({}, b"OK") def test_json(passed_data): """ This function tests whether the data being passed to request's method is a valid json or not. This function will be called by pymox :params passed_data: The data being passed to requests.Session.request. """ if not isinstance(passed_data, six.string_types): return False try: passed_data = json.loads(passed_data) return data == passed_data except (TypeError, ValueError): return False requests.Session.request(mox.IgnoreArg(), mox.IgnoreArg(), stream=mox.IgnoreArg(), data=mox.Func(test_json), headers=mox.IgnoreArg()).AndReturn(fake) self.mock.ReplayAll() headers = {"test": u'chunked_request'} resp, body = self.client.post('/v1/images/', headers=headers, data=data) self.assertEqual(fake, resp)
def _test_finish_migration(self, power_on, ephemeral_storage=False, config_drive=False, config_drive_format='iso'): self._instance = self._get_instance() self._instance.memory_mb = 100 self._instance.vcpus = 2 self._instance['system_metadata'] = {} self._instance['old_flavor'] = objects.Flavor(root_gb=5) network_info = fake_network.fake_get_instance_nw_info(self.stubs) m = fake.PathUtils.get_instance_dir(mox.IsA(str)) m.AndReturn(self._test_instance_dir) self._mox.StubOutWithMock(fake.PathUtils, 'exists') m = fake.PathUtils.exists(mox.IsA(six.text_type)) m.AndReturn(True) fake_parent_vhd_path = (os.path.join( 'FakeParentPath', '%s.vhd' % self._instance["image_ref"])) m = vhdutils.VHDUtils.get_vhd_info(mox.IsA(str)) m.AndReturn({'ParentPath': fake_parent_vhd_path, 'MaxInternalSize': 1}) m = vhdutils.VHDUtils.get_internal_vhd_size_by_file_size( mox.IsA(six.text_type), mox.IsA(object)) m.AndReturn(1025) vhdutils.VHDUtils.reconnect_parent_vhd(mox.IsA(str), mox.IsA(six.text_type)) m = vhdutils.VHDUtils.get_vhd_info(mox.IsA(six.text_type)) m.AndReturn({'MaxInternalSize': 1024}) m = fake.PathUtils.exists(mox.IsA(six.text_type)) m.AndReturn(True) m = fake.PathUtils.get_instance_dir(mox.IsA(str)) if ephemeral_storage: return m.AndReturn(self._test_instance_dir) else: m.AndReturn(None) self._set_vm_name(self._instance.name) self._setup_create_instance_mocks(None, False, ephemeral_storage=ephemeral_storage) if power_on: vmutils.VMUtils.set_vm_state(mox.Func(self._check_instance_name), constants.HYPERV_VM_STATE_ENABLED) self._setup_log_vm_output_mocks() if config_drive: self._mock_attach_config_drive(self._instance, config_drive_format) self._mox.ReplayAll() image_meta = { 'properties': { constants.IMAGE_PROP_VM_GEN: constants.IMAGE_PROP_VM_GEN_1 } } self._conn.finish_migration(self._context, None, self._instance, "", network_info, image_meta, False, None, power_on) self._mox.VerifyAll() if config_drive: self._verify_attach_config_drive(config_drive_format)
def _setup_test_migrate_disk_and_power_off_mocks(self, same_host=False, copy_exception=False, size_exception=False): self._instance = self._get_instance() network_info = fake_network.fake_get_instance_nw_info(self.stubs) self._instance['root_gb'] = 10 fake_local_ip = '10.0.0.1' if same_host: fake_dest_ip = fake_local_ip else: fake_dest_ip = '10.0.0.2' if size_exception: flavor = 'm1.tiny' else: flavor = 'm1.small' flavor = db.flavor_get_by_name(self._context, flavor) if not size_exception: fake_root_vhd_path = 'C:\\FakePath\\root.vhd' fake_revert_path = os.path.join(self._test_instance_dir, '_revert') func = mox.Func(self._check_instance_name) vmutils.VMUtils.set_vm_state(func, constants.HYPERV_VM_STATE_DISABLED) self._setup_delete_vm_log_mocks() m = vmutils.VMUtils.get_vm_storage_paths(func) m.AndReturn(([fake_root_vhd_path], [])) m = hostutils.HostUtils.get_local_ips() m.AndReturn([fake_local_ip]) m = fake.PathUtils.get_instance_dir(mox.IsA(str)) m.AndReturn(self._test_instance_dir) m = pathutils.PathUtils.get_instance_migr_revert_dir( self._instance.name, remove_dir=True, create_dir=True) m.AndReturn(fake_revert_path) if same_host: fake.PathUtils.makedirs(mox.IsA(str)) m = fake.PathUtils.copy(fake_root_vhd_path, mox.IsA(str)) if copy_exception: m.AndRaise(shutil.Error('Simulated copy error')) m = fake.PathUtils.get_instance_dir(mox.IsA(str), mox.IsA(str), remove_dir=True) m.AndReturn(self._test_instance_dir) else: fake.PathUtils.move_folder_files(mox.IsA(str), mox.IsA(str)) destroy_disks = True if same_host: fake.PathUtils.move_folder_files(mox.IsA(str), mox.IsA(str)) destroy_disks = False self._setup_destroy_mocks(False) if destroy_disks: m = fake.PathUtils.get_instance_dir(mox.IsA(str), mox.IsA(str), remove_dir=True) m.AndReturn(self._test_instance_dir) return (self._instance, fake_dest_ip, network_info, flavor)
def _setup_spawn_instance_mocks(self, cow, setup_vif_mocks_func=None, with_exception=False, block_device_info=None, boot_from_volume=False, config_drive=False, use_cdrom=False, admin_permissions=True, vhd_format=constants.DISK_FORMAT_VHD, ephemeral_storage=False): m = vmutils.VMUtils.vm_exists(mox.IsA(str)) m.WithSideEffects(self._set_vm_name).AndReturn(False) m = fake.PathUtils.get_instance_dir(mox.IsA(str), create_dir=False, remove_dir=True) m.AndReturn(self._test_instance_dir) if block_device_info: m = basevolumeutils.BaseVolumeUtils.volume_in_mapping( 'fake_root_device_name', block_device_info) m.AndReturn(boot_from_volume) if not boot_from_volume: m = fake.PathUtils.get_instance_dir(mox.Func(self._check_vm_name)) m.AndReturn(self._test_instance_dir) self._setup_get_cached_image_mocks(cow, vhd_format) m = vhdutils.VHDUtils.get_vhd_info(mox.IsA(str)) m.AndReturn({'MaxInternalSize': 1024, 'FileSize': 1024, 'Type': 2}) if cow: vhdutils.VHDUtils.create_differencing_vhd( mox.IsA(str), mox.IsA(str)) m = vhdutils.VHDUtils.get_vhd_format(mox.IsA(str)) m.AndReturn(vhd_format) else: fake.PathUtils.copyfile(mox.IsA(str), mox.IsA(str)) if not (cow and vhd_format == constants.DISK_FORMAT_VHD): m = vhdutils.VHDUtils.get_internal_vhd_size_by_file_size( mox.IsA(six.text_type), mox.IsA(object)) m.AndReturn(1025) vhdutils.VHDUtils.resize_vhd(mox.IsA(str), mox.IsA(object), is_file_max_size=False) self._setup_check_admin_permissions_mocks( admin_permissions=admin_permissions) if ephemeral_storage: m = fake.PathUtils.get_instance_dir(mox.Func(self._check_vm_name)) m.AndReturn(self._test_instance_dir) vhdutils.VHDUtils.create_dynamic_vhd(mox.IsA(str), mox.IsA(int), mox.IsA(str)) self._setup_create_instance_mocks(setup_vif_mocks_func, boot_from_volume, block_device_info, ephemeral_storage=ephemeral_storage) if config_drive and not with_exception: self._setup_spawn_config_drive_mocks(use_cdrom) # TODO(alexpilotti) Based on where the exception is thrown # some of the above mock calls need to be skipped if with_exception: self._setup_destroy_mocks() else: vmutils.VMUtils.set_vm_state(mox.Func(self._check_vm_name), constants.HYPERV_VM_STATE_ENABLED) self._setup_log_vm_output_mocks()