def _CreateMockTestRun(self, config, state=None, create_time=None): """Creates a test run. Args: config: a test run config state: a test run state. create_time: a test run create time. Returns: a ndb_models.TestRun object. """ test = ndb_models.Test(name='test', command='command') test.put() test_run_config = ndb_models.TestRunConfig(test_key=test.key, cluster='cluster', run_target='run_target') test_resources = [ ndb_models.TestResourceObj(name='foo', url='http://foo_origin_url'), ndb_models.TestResourceObj(name='bar', url='https://bar_origin_url'), ] test_run = ndb_models.TestRun(labels=['label'], test=test, test_run_config=test_run_config, test_resources=test_resources, state=state or ndb_models.TestRunState.PENDING, create_time=create_time) test_run.key = ndb.Key(ndb_models.TestRun, str(uuid.uuid4())) test_run.put() return test_run
def _CreateMockTestRun( self, test_name='test', command='command', retry_command_line=None, runner_sharding_args=None, run_target='run_target', shard_count=1, sharding_mode=ndb_models.ShardingMode.RUNNER, edited_command=None, module_config_pattern=None, module_execution_args=None, extra_test_resources=None): test = ndb_models.Test( name=test_name, command=command, retry_command_line=retry_command_line, runner_sharding_args=runner_sharding_args, module_config_pattern=module_config_pattern, module_execution_args=module_execution_args) test.put() test_run_config = ndb_models.TestRunConfig( test_key=test.key, cluster='cluster', run_target=run_target, shard_count=shard_count, sharding_mode=sharding_mode, command=edited_command) test_resources = [ ndb_models.TestResourceObj(name='foo', url='http://foo_origin_url'), ndb_models.TestResourceObj(name='bar', url='https://bar_origin_url'), ] + (extra_test_resources or []) test_run = ndb_models.TestRun( id=str(uuid.uuid4()), test=test, labels=['label'], test_run_config=test_run_config, test_resources=test_resources, state=ndb_models.TestRunState.PENDING) test_run.put() return test_run
def testNew_withRunTarget(self, mock_run_test): test = self._createMockTest() request = { 'labels': ['label'], 'test_run_config': { 'test_id': str(test.key.id()), 'cluster': 'cluster', 'device_specs': ['foo', 'bar'], 'run_count': 10, 'shard_count': 100, 'max_retry_on_test_failures': 1000, 'test_resource_objs': [ { 'name': 'bar', 'url': 'bar_url' }, { 'name': 'zzz', 'url': 'zzz_url' }, ], }, } test_run = ndb_models.TestRun( test=test, labels=['label'], test_run_config=ndb_models.TestRunConfig( test_key=test.key, cluster='cluster', device_specs=['foo', 'bar'], run_count=10, shard_count=100, max_retry_on_test_failures=1000)) test_run.put() mock_run_test.return_value = test_run res = self.app.post_json('/_ah/api/mtt/v1/test_runs', request) mock_run_test.assert_called_with( labels=['label'], test_run_config=ndb_models.TestRunConfig( test_key=test.key, cluster='cluster', device_specs=['foo', 'bar'], run_count=10, shard_count=100, max_retry_on_test_failures=1000, test_resource_objs=[ ndb_models.TestResourceObj(name='bar', url='bar_url'), ndb_models.TestResourceObj(name='zzz', url='zzz_url'), ]), rerun_context=None, rerun_configs=[] ) self.assertEqual('200 OK', res.status) test_run_msg = protojson.decode_message(messages.TestRun, res.body) self.assertEqual( messages.Convert(test_run, messages.TestRun), test_run_msg)
def testAfterTestRunHandler(self, mock_get_request, mock_get_test_context, mock_add_task): # configure mock TFC request request_id = self.mock_test_run.request_id mock_request = api_messages.RequestMessage(id=request_id) mock_request.commands.append(api_messages.CommandMessage(id='bar')) mock_get_request.return_value = mock_request # configure mock TFC test context mock_test_context = api_messages.TestContext( command_line='command_line', env_vars=[api_messages.KeyValuePair(key='key', value='value')], test_resources=[ api_messages.TestResource( url='url', name='name', path='path', decompress=True, decompress_dir='dir', params=api_messages.TestResourceParameters( decompress_files=['file'])), api_messages.TestResource(url='url2', name='name2', path='path2') ]) mock_get_test_context.return_value = mock_test_context expected_test_context = ndb_models.TestContextObj( command_line='command_line', env_vars=[ndb_models.NameValuePair(name='key', value='value')], test_resources=[ ndb_models.TestResourceObj( url='url', name='name', decompress=True, decompress_dir='dir', params=ndb_models.TestResourceParameters( decompress_files=['file'])), ndb_models.TestResourceObj(url='url2', name='name2') ]) tfc_event_handler._AfterTestRunHandler(self.mock_test_run) # test run updated, run hooks invoked, output uploaded, and metrics tracked mock_get_request.assert_called_with(request_id) mock_get_test_context.assert_called_with(request_id, 'bar') self.assertEqual(expected_test_context, self.mock_test_run.next_test_context) mock_add_task.assert_has_calls([ mock.call(test_run_hook.ExecuteHooks, self.mock_test_run.key.id(), ndb_models.TestRunPhase.AFTER_RUN, _transactional=True), mock.call(tfc_event_handler._TrackTestRun, self.mock_test_run.key.id(), _transactional=True), ])
def testCreateTestRun(self, mock_find_resources): test = ndb_models.Test( name='test', command='command', test_resource_defs=[ ndb_models.TestResourceDef( name='foo', default_download_url='default_download_url'), ndb_models.TestResourceDef( name='bar', default_download_url='default_download_url', decompress=True, decompress_dir='dir'), ]) test.put() overwritten_obj = ndb_models.TestResourceObj( name='foo', url='origin_url', cache_url='cache_url') test_run_config = ndb_models.TestRunConfig( test_key=test.key, cluster='cluster', device_specs=['device_serial:serial'], test_resource_objs=[overwritten_obj], ) mock_find_resources.return_value = [overwritten_obj] test_run = test_kicker.CreateTestRun(['label'], test_run_config) test_run = ndb_models.TestRun.get_by_id(test_run.key.id()) self.assertModelEqual(test, test_run.test) self.assertEqual(['label'], test_run.labels) self.assertModelEqual(test_run_config, test_run.test_run_config) self.assertModelSetEqual([ ndb_models.TestResourceObj( name='bar', url='default_download_url', decompress=True, decompress_dir='dir', mount_zip=False, params=ndb_models.TestResourceParameters()), ndb_models.TestResourceObj( name='foo', url='origin_url', cache_url='cache_url', decompress=False, decompress_dir='', mount_zip=False, params=ndb_models.TestResourceParameters()), ], test_run.test_resources) self.assertEqual(ndb_models.TestRunState.PENDING, test_run.state) tasks = self.mock_task_scheduler.GetTasks( queue_names=[test_kicker.TEST_KICKER_QUEUE]) self.assertLen(tasks, 1) task = tasks[0] data = json.loads(task.payload) self.assertEqual(test_run.key.id(), data['test_run_id']) self.assertIsNone(test_run.sequence_id)
def testKickTestPlan(self, create_test_run): """Tests that a test plan can be kicked off.""" test = ndb_models.Test(id='test_id') test_device_action = ndb_models.DeviceAction(id='test_device_action') test_run_action = ndb_models.TestRunAction(id='test_run_action') # Create a test plan with multiple resources and actions test_plan = ndb_models.TestPlan( name='test_plan', labels=['label'], cron_exp='0 0 * * *', test_run_configs=[ ndb_models.TestRunConfig( test_key=test.key, cluster='cluster', run_target='run_target', before_device_action_keys=[test_device_action.key], test_run_action_refs=[ ndb_models.TestRunActionRef(action_key=test_run_action.key), ], test_resource_objs=[ ndb_models.TestResourceObj(name='res_1', url='url_1')]), ]) test_plan.put() # Test run will be created successfully test_run = ndb_models.TestRun(id='test_run_id') create_test_run.return_value = test_run executed = test_plan_kicker.KickTestPlan(test_plan.key.id()) self.assertTrue(executed) # Test run is created with the right test and test plan components create_test_run.assert_called_with( labels=['label'], test_plan_key=test_plan.key, test_run_config=ndb_models.TestRunConfig( test_key=test.key, cluster='cluster', run_target='run_target', before_device_action_keys=[test_device_action.key], test_run_action_refs=[ ndb_models.TestRunActionRef(action_key=test_run_action.key), ], test_resource_objs=[ ndb_models.TestResourceObj(name='res_1', url='url_1') ]), ) # Test run key is stored in the test plan status status = ndb_models.TestPlanStatus.query(ancestor=test_plan.key).get() self.assertEqual(status.last_run_keys, [test_run.key])
def testGetMetadata_remoteAncestry(self, mock_open_file): # create hierarchy of test_runs test = self._createMockTest() child, parent = self._createMockTestRuns(test=test, count=2) with tempfile.NamedTemporaryFile(suffix='.zip') as tmp_stream: # write parent metadata to a temporary zip file with zipfile.ZipFile(tmp_stream, 'w') as context_file: res = self.app.get('/_ah/api/mtt/v1/test_runs/%s/metadata' % parent.key.id()) context_file.writestr('mtt.json', res.body) tmp_stream.seek(0) mock_open_file.return_value = tmp_stream # mark child as a remote return child.prev_test_context = ndb_models.TestContextObj(test_resources=[ ndb_models.TestResourceObj(name='mtt.json', url='url') ]) child.put() res = self.app.get('/_ah/api/mtt/v1/test_runs/%s/metadata' % child.key.id()) # metadata is sent back w/ ordered ancestry information metadata = protojson.decode_message(messages.TestRunMetadataList, res.body) expected = messages.TestRunMetadataList( test_runs=[ messages.TestRunMetadata( test_run=messages.Convert(child, messages.TestRun)), messages.TestRunMetadata( test_run=messages.Convert(parent, messages.TestRun)) ], server_version=env.VERSION) self.assertEqual(expected, metadata)
def testConvertToTestResourceMap_unionDecompressFiles(self): test_resource_defs = [ ndb_models.TestResourceDef( name='foo', decompress=True, decompress_dir='dir', params=ndb_models.TestResourceParameters( decompress_files=['a', 'b'])), ndb_models.TestResourceDef( name='foo', decompress=True, decompress_dir='dir', params=ndb_models.TestResourceParameters( decompress_files=['', 'b', 'c'])), ndb_models.TestResourceDef(name='bar', decompress=True), ndb_models.TestResourceDef( name='bar', decompress=True, params=ndb_models.TestResourceParameters( decompress_files=['bar'])), ] objs = test_kicker._ConvertToTestResourceMap(test_resource_defs) self.assertDictEqual( objs, { 'foo': ndb_models.TestResourceObj( name='foo', decompress=True, decompress_dir='dir', mount_zip=False, params=ndb_models.TestResourceParameters( decompress_files=['a', 'b', 'c'])), 'bar': ndb_models.TestResourceObj( name='bar', decompress=True, decompress_dir='', mount_zip=False, params=ndb_models.TestResourceParameters( decompress_files=[])), }) # Assert that the input objects are unchanged. self.assertEqual(test_resource_defs[0].params.decompress_files, ['a', 'b']) self.assertEqual(test_resource_defs[1].params.decompress_files, ['', 'b', 'c']) self.assertIsNone(test_resource_defs[2].params) self.assertEqual(test_resource_defs[3].params.decompress_files, ['bar'])
def testGetTestRunStateInfo_notDownloading(self): obj = ndb_models.TestRun( state=ndb_models.TestRunState.PENDING, # resource already has cache URL (already downloaded) test_resources=[ ndb_models.TestResourceObj(name='resource', url='url', cache_url='cache') ]) self.assertIsNone(messages._GetTestRunStateInfo(obj))
def _TestResourceObjMessageConverter(msg): return ndb_models.TestResourceObj( name=msg.name, url=msg.url, cache_url=msg.cache_url, test_resource_type=msg.test_resource_type, decompress=msg.decompress, decompress_dir=msg.decompress_dir, mount_zip=msg.mount_zip, params=Convert(msg.params, ndb_models.TestResourceParameters))
def testGetTestRunStateInfo_downloading(self): obj = ndb_models.TestRun(state=ndb_models.TestRunState.PENDING, test_resources=[ ndb_models.TestResourceObj( name='resource', url='url') ]) # resource tracker found with 50% progress tracker = ndb_models.TestResourceTracker(id='url', download_progress=0.5) tracker.put() expected = 'Downloading resource [50%]' self.assertEqual(expected, messages._GetTestRunStateInfo(obj))
def testExecuteHook_withContextVariables(self, mock_init, mock_log, mock_execute): """Tests that a hook can be constructed and executed.""" test = ndb_models.Test(name='test', command='command') test.put() test_run = ndb_models.TestRun( test=test, test_run_config=ndb_models.TestRunConfig(test_key=test.key), test_resources=[ ndb_models.TestResourceObj( name='device_image', url='mtt:///android_ci/branch/target/build_id/image.zip', test_resource_type=ndb_models.TestResourceType.DEVICE_IMAGE ) ]) test_run.put() mock_init.return_value = None hook_context = mock.MagicMock() hook_context.test_run = test_run credentials = authorized_user.Credentials(None) action = ndb_models.TestRunAction( name='Test', hook_class_name='simple', options=[ ndb_models.NameValuePair(name='ham', value='eggs'), ndb_models.NameValuePair(name='test_run_id', value='${MTT_TEST_RUN_ID}'), ndb_models.NameValuePair(name='device_image_url', value='${MTT_DEVICE_IMAGE_URL}'), ndb_models.NameValuePair(name='device_image_branch', value='${MTT_DEVICE_IMAGE_BRANCH}'), ndb_models.NameValuePair(name='device_image_target', value='${MTT_DEVICE_IMAGE_TARGET}'), ndb_models.NameValuePair(name='device_image_build_id', value='${MTT_DEVICE_IMAGE_BUILD_ID}'), ], credentials=credentials, ) test_run_hook._ExecuteHook(action, hook_context) mock_init.assert_called_with( _credentials=credentials, ham='eggs', test_run_id=str(test_run.key.id()), device_image_url= 'mtt:///android_ci/branch/target/build_id/image.zip', device_image_branch='branch', device_image_target='target', device_image_build_id='build_id') mock_log.assert_called() mock_execute.assert_called_with(hook_context)
def testKickTestRun_withPrevTestContext( self, mock_download_resources, mock_new_request): test_run = self._CreateMockTestRun( command='command', retry_command_line='retry_command_line', runner_sharding_args='--shard-count ${TF_SHARD_COUNT}', shard_count=6) test_run.prev_test_context = ndb_models.TestContextObj( command_line='prev_command_line', env_vars=[], test_resources=[ndb_models.TestResourceObj(name='bar', url='zzz')]) test_run.put() test_run_id = test_run.key.id() test_run = ndb_models.TestRun.get_by_id(test_run_id) mock_download_resources.return_value = { r.url: 'cache_url' for r in test_run.test_resources } mock_request = api_messages.RequestMessage(id='request_id') mock_new_request.return_value = mock_request test_kicker.KickTestRun(test_run_id) mock_download_resources.assert_called_once_with( [r.url for r in test_run.test_resources], test_run=test_run) mock_new_request.assert_called() msg = mock_new_request.call_args[0][0] self._CheckNewRequestMessage( msg=msg, test_run=test_run, output_url='file:///data/app_default_bucket/test_runs/{}/output'.format( test_run_id), test_resource_urls={ 'foo': 'cache_url', 'bar': 'cache_url', 'mtt.json': 'http://localhost:8000/_ah/api/mtt/v1/test_runs/{}/metadata' .format(test_run_id) }, command_lines=['command --shard-count 6 --invocation-data mtt=1'], retry_command_line=( 'retry_command_line --shard-count 6 --invocation-data mtt=1'), shard_count=1) # prev_test_context's command_line should be replaced. self.assertEqual( 'retry_command_line --shard-count 6 --invocation-data mtt=1', msg.prev_test_context.command_line) test_run = ndb_models.TestRun.get_by_id(test_run_id) self.assertEqual(mock_request.id, test_run.request_id) self.assertEqual(ndb_models.TestRunState.QUEUED, test_run.state)
def testGetRerunInfo_remote(self): test = ndb_models.Test(context_file_dir='context/') # determine rerun info using parent ID prev_run_key, prev_test_context = test_kicker._GetRerunInfo( test, messages.RerunContext(context_filename='file', context_file_url='file_url')) # no test run key and test context contains provided file expected_context = ndb_models.TestContextObj(test_resources=[ ndb_models.TestResourceObj( name='context/file', url='file_url')]) self.assertIsNone(prev_run_key) self.assertEqual(expected_context, prev_test_context)
def testGetRerunInfo_local(self): # create local test run to rerun prev_test_run = self._CreateMockTestRun() prev_test_run.next_test_context = ndb_models.TestContextObj( test_resources=[ndb_models.TestResourceObj(name='bar', url='zzz')]) prev_test_run.put() # determine rerun info using parent ID prev_run_key, prev_test_context = test_kicker._GetRerunInfo( None, messages.RerunContext(test_run_id=prev_test_run.key.id())) # test run key found and next_test_context used self.assertEqual(prev_test_run.key, prev_run_key) self.assertEqual(prev_test_run.next_test_context, prev_test_context)
def testCleanTestResourceCache_recentlyUsed(self, mock_handle_factory): """Tests that recently used files are ignored when cleaning cache.""" test_run = ndb_models.TestRun(test_resources=[ ndb_models.TestResourceObj(name='resource', url='resource_url') ]) test_run.put() # Cached resource is old but recently referenced mock_handle_factory.return_value.ListFiles.return_value = [ file_util.FileInfo(url=download_util.GetCacheUrl('resource_url'), is_file=True, timestamp=datetime.datetime.utcnow() - datetime.timedelta(days=1)) ] # Recently used file is not deleted (test_run > min_access_time) min_access_time = datetime.datetime.utcnow() - datetime.timedelta( hours=1) download_util.CleanTestResourceCache(min_access_time=min_access_time) mock_handle_factory.return_value.Delete.assert_not_called()
def testKickTestRun_onPremiseMode(self, mock_download_resources, mock_new_request): test_run = self._CreateMockTestRun() # test_run = ndb_models.TestRun.get_by_id(test_run_id) test_run.prev_test_context = ndb_models.TestContextObj( command_line='prev_command_line', env_vars=[], test_resources=[ ndb_models.TestResourceObj(name='bar', url='file:///root/path') ]) test_run.put() test_run_id = test_run.key.id() test_run = ndb_models.TestRun.get_by_id(test_run_id) mock_download_resources.return_value = { r.url: 'file:///data/cache_url' for r in test_run.test_resources } mock_request = api_messages.RequestMessage(id='request_id') mock_new_request.return_value = mock_request test_kicker.KickTestRun(test_run_id) mock_download_resources.assert_called_once_with( [r.url for r in test_run.test_resources], test_run=test_run) mock_new_request.assert_called() msg = mock_new_request.call_args[0][0] self._CheckNewRequestMessage( msg=msg, test_run=test_run, output_url='http://test.hostname.com:8006/file/app_default_bucket/test_runs/{}/output' .format(test_run_id), test_resource_urls={ 'foo': 'http://test.hostname.com:8006/file/cache_url', 'bar': 'http://test.hostname.com:8006/file/cache_url', 'mtt.json': 'http://test.hostname.com:8000/_ah/api/mtt/v1/test_runs/{}/metadata' .format(test_run_id) }, command_lines=['command --invocation-data mtt=1']) self.assertEqual([ api_messages.TestResource( name='bar', url='http://test.hostname.com:8006/file/root/path') ], msg.prev_test_context.test_resources)
def FindTestResources(test_resource_objs): """Parses test resource obj urls (may include wildcards). Args: test_resource_objs: a list of TestResourceObjs Returns: parsed_objs: a list of TestResourceObj with urls parsed Raises: FileNotFoundError: if no file matching the test resource obj url is found TestResourceError: if a test resource obj url is missing """ test_resource_map = {} for obj in test_resource_objs: build_locator = BuildLocator.ParseUrl(obj.url) if build_locator: build_item = FindFile(build_locator.build_channel_id, build_locator.directory, build_locator.filename) if not build_item: raise errors.FileNotFoundError('Cannot find file from %s' % obj.url) # Build a encoded url url = BuildUrl(build_locator.build_channel_id, build_item) else: url = obj.url test_resource_map[obj.name] = ndb_models.TestResourceObj( name=obj.name, url=url, test_resource_type=obj.test_resource_type, decompress=obj.decompress, decompress_dir=obj.decompress_dir, params=ndb_models.TestResourceParameters.Clone(obj.params)) parsed_objs = sorted(test_resource_map.values(), key=lambda x: x.name) for r in parsed_objs: logging.info('\t%s: %s', r.name, r.cache_url) if not r.url: raise errors.TestResourceError('No URL for test resource %s' % r.name) return parsed_objs
def _GetRerunInfo(test, rerun_context): """Determine parent run key and test context for reruns. Args: test: ndb_models.Test that will be executed. rerun_context: rerun parameters containing parent ID or context filename. Returns: previous test run key and previous test context to use during rerun """ if rerun_context and rerun_context.test_run_id: # local rerun - fetch key and test context from DB logging.info('Rerunning local test run %s', rerun_context.test_run_id) prev_test_run_key = ndb.Key(ndb_models.TestRun, rerun_context.test_run_id) # disable cache to ensure we get the updated test context prev_test_run = prev_test_run_key.get(use_cache=False, use_memcache=False) if not prev_test_run: raise ValueError( 'Previous test run %s not found' % prev_test_run_key.id()) return prev_test_run_key, prev_test_run.next_test_context if (rerun_context and rerun_context.context_filename and rerun_context.context_file_url): # remote rerun - construct test context from locally uploaded file logging.info('Rerunning test run from %s', rerun_context.context_filename) resource_name = rerun_context.context_filename if test.context_file_dir: resource_name = os.path.join(test.context_file_dir, resource_name) remote_test_context = ndb_models.TestContextObj(test_resources=[ ndb_models.TestResourceObj( name=resource_name, url=rerun_context.context_file_url) ]) return None, remote_test_context # no rerun information return None, None
def testPrepareTestResources( self, mock_open_file, mock_get_suite_info, mock_download_resources): test_run = self._CreateMockTestRun( command='command') test_run.test_resources = [ ndb_models.TestResourceObj( name='foo', url='http://foo_origin_url', test_resource_type=ndb_models.TestResourceType.TEST_PACKAGE) ] test_run.put() test_run_id = test_run.key.id() test_run = ndb_models.TestRun.get_by_id(test_run_id) mock_download_resources.return_value = { r.url: 'cache_url' for r in test_run.test_resources } mock_open_file.return_value = mock.MagicMock() test_suite_info = file_util.TestSuiteInfo( 'build_number', 'architecture', 'name', 'fullname', 'version') mock_get_suite_info.return_value = test_suite_info test_kicker._PrepareTestResources(test_run_id) updated_test_run = ndb_models.TestRun.get_by_id(test_run.key.id()) # Resource downloaded and cached mock_download_resources.assert_called_with( ['http://foo_origin_url'], test_run=test_run) self.assertEqual(updated_test_run.test_resources[0].cache_url, 'cache_url') mock_open_file.assert_called_with('cache_url') # Test package info was updated test_package_info = updated_test_run.test_package_info self.assertEqual(test_package_info.build_number, 'build_number') self.assertEqual(test_package_info.target_architecture, 'architecture') self.assertEqual(test_package_info.name, 'name') self.assertEqual(test_package_info.fullname, 'fullname') self.assertEqual(test_package_info.version, 'version')
def _CreateMockTestResourceObj(self, name='resource_name', url='test.resource/url'): return ndb_models.TestResourceObj(name=name, url=url)
def _TestResourcePipeToObj(pipe): """Converts a TestResourcePipe to a TestResourceObj.""" return ndb_models.TestResourceObj(name=pipe.name, url=pipe.url, test_resource_type=pipe.test_resource_type)
def testKickTestRun_moduleSharding( self, mock_download_resources, mock_open_file, mock_get_test_module_infos, mock_new_request): test_run = self._CreateMockTestRun( command='command', run_target='run_target', shard_count=6, sharding_mode=ndb_models.ShardingMode.MODULE, module_config_pattern='path/to/.*\\.config', module_execution_args='-m ${MODULE_NAME}', extra_test_resources=[ ndb_models.TestResourceObj( name='test_package', url='test_package_url', test_resource_type=ndb_models.TestResourceType.TEST_PACKAGE) ]) test_run_id = test_run.key.id() test_run = ndb_models.TestRun.get_by_id(test_run_id) mock_download_resources.return_value = { r.url: '%s_cache_url' % r.name for r in test_run.test_resources } mock_get_test_module_infos.return_value = [ file_util.TestModuleInfo(name='CtsFooTestCases'), file_util.TestModuleInfo(name='CtsDeqpTestCases'), file_util.TestModuleInfo(name='CtsBarTestCases'), ] mock_request = api_messages.RequestMessage(id='request_id') mock_new_request.return_value = mock_request test_kicker.KickTestRun(test_run_id) mock_download_resources.assert_called_once_with( [r.url for r in test_run.test_resources], test_run=test_run) mock_open_file.assert_called_with('test_package_cache_url') mock_get_test_module_infos.assert_called_once_with( mock_open_file.return_value, 'path/to/.*\\.config') mock_new_request.assert_called() msg = mock_new_request.call_args[0][0] self._CheckNewRequestMessage( msg=msg, test_run=test_run, output_url='file:///data/app_default_bucket/test_runs/{}/output'.format( test_run_id), test_resource_urls={ 'foo': 'foo_cache_url', 'bar': 'bar_cache_url', 'test_package': 'test_package_cache_url', 'mtt.json': 'http://localhost:8000/_ah/api/mtt/v1/test_runs/{}/metadata' .format(test_run_id) }, command_lines=[ 'command -m CtsDeqpTestCases --invocation-data mtt=1', 'command -m CtsBarTestCases --invocation-data mtt=1', 'command -m CtsFooTestCases --invocation-data mtt=1', ], run_target='run_target', shard_count=1, max_concurrent_tasks=6) test_run = ndb_models.TestRun.get_by_id(test_run_id) self.assertEqual(mock_request.id, test_run.request_id) self.assertEqual(ndb_models.TestRunState.QUEUED, test_run.state)
def testCreateTestRun_withNodeConfig(self, mock_find_resources): test = ndb_models.Test( name='test', command='command', test_resource_defs=[ ndb_models.TestResourceDef( name='abc', default_download_url='default_download_url'), ndb_models.TestResourceDef( name='def', default_download_url='default_download_url'), ndb_models.TestResourceDef( name='xyz', default_download_url='default_download_url'), ]) test.put() node_config = ndb_models.GetNodeConfig() node_config.env_vars.append( ndb_models.NameValuePair(name='foo', value='bar')) node_config.test_resource_default_download_urls = [ ndb_models.NameValuePair(name='def', value='default_download_url2'), ndb_models.NameValuePair(name='xyz', value='default_download_url2'), ] node_config.put() overwritten_obj = ndb_models.TestResourceObj( name='xyz', url='origin_url', cache_url='cache_url') test_run_config = ndb_models.TestRunConfig( test_key=test.key, cluster='cluster', device_specs=['device_serial:serial'], test_resource_objs=[overwritten_obj]) mock_find_resources.return_value = [overwritten_obj] test_run = test_kicker.CreateTestRun( ['label'], test_run_config) test_run = ndb_models.TestRun.get_by_id(test_run.key.id()) self.assertEqual(test.command, test_run.test.command) self.assertModelListEqual(node_config.env_vars, test_run.test.env_vars) self.assertEqual(['label'], test_run.labels) self.assertModelEqual(test_run_config, test_run.test_run_config) self.assertModelSetEqual([ ndb_models.TestResourceObj( name='abc', url='default_download_url', decompress=False, decompress_dir='', mount_zip=False, params=ndb_models.TestResourceParameters()), ndb_models.TestResourceObj( name='def', url='default_download_url2', decompress=False, decompress_dir='', mount_zip=False, params=ndb_models.TestResourceParameters()), ndb_models.TestResourceObj( name='xyz', url='origin_url', cache_url='cache_url', decompress=False, decompress_dir='', mount_zip=False, params=ndb_models.TestResourceParameters()), ], test_run.test_resources) self.assertEqual(ndb_models.TestRunState.PENDING, test_run.state) tasks = self.mock_task_scheduler.GetTasks( queue_names=[test_kicker.TEST_KICKER_QUEUE]) self.assertEqual(1, len(tasks)) task = tasks[0] data = json.loads(task.payload) self.assertEqual(test_run.key.id(), data['test_run_id'])