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 _CreateMockTest(self, test_id='test.id', namespace='', name='Test 1'): if namespace: test_id = config_encoder._AddNamespaceToId(namespace, test_id) test = ndb_models.Test(name=name, command='command') test.key = ndb.Key(ndb_models.Test, test_id) test.put() return test
def testKickTestPlan_withError(self, create_test_run, set_test_run_state): """Tests that errors are handled when kicking off a test plan.""" test = ndb_models.Test(id='test_id') test_run_config = ndb_models.TestRunConfig( test_key=test.key, cluster='cluster', run_target='run_target') test_plan = ndb_models.TestPlan( name='test_plan', test_run_configs=[test_run_config, test_run_config]) test_plan.put() # First test run created successfully, but second fails even with retries test_run = ndb_models.TestRun(id='test_run_id') create_test_run.side_effect = ( [test_run] + test_plan_kicker.MAX_RETRY_COUNT * [RuntimeError('test_run_error')]) # Retries a few times before canceling test run and raising exception with self.assertRaises(RuntimeError): test_plan_kicker.KickTestPlan(test_plan.key.id()) self.assertEqual(create_test_run.call_count, 1 + test_plan_kicker.MAX_RETRY_COUNT) set_test_run_state.assert_called_once_with('test_run_id', ndb_models.TestRunState.CANCELED) # Stores the canceled test run key and the error message status = ndb_models.TestPlanStatus.query(ancestor=test_plan.key).get() self.assertEqual(status.last_run_keys, [test_run.key]) self.assertEqual(status.last_run_error, 'test_run_error')
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 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 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 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 testConvert_Test(self): obj = ndb_models.Test( name='name', test_resource_defs=[ ndb_models.TestResourceDef(name='foo', default_download_url='bar') ], command='command', env_vars=[ ndb_models.NameValuePair(name='name', value='value'), ], output_file_patterns=['pattern_1', 'pattern_2'], setup_scripts=['script_1', 'script_2', 'script_3']) msg = messages.Convert(obj, messages.Test) self.assertIsInstance(msg, messages.Test) self.assertSameTest(obj, msg)
def Get(self, request): """Fetches a test suite. Parameters: test_id: Test ID, or zero for an empty test suite """ if request.test_id == '0': # For ID 0, return an empty test object to use as a template in UI. test = ndb_models.Test(name='') else: test = mtt_messages.ConvertToKey(ndb_models.Test, request.test_id).get() if not test: raise endpoints.NotFoundException('no test found for ID %s' % request.test_id) return mtt_messages.Convert(test, mtt_messages.Test)
def setUp(self): super(TfcEventHandlerTest, self).setUp() self.mock_test = ndb_models.Test(name='test', command='command', result_file='result_file') self.mock_test.put() self.mock_test_plan = ndb_models.TestPlan(name='plan') self.mock_test_plan.put() self.mock_test_run = ndb_models.TestRun( test_plan_key=self.mock_test_plan.key, test_run_config=ndb_models.TestRunConfig( test_key=self.mock_test.key, run_target='run_target', command='mock test run command', retry_command='mock test run retry command'), test=self.mock_test, request_id='request_id', state=ndb_models.TestRunState.UNKNOWN) self.mock_test_run.put()
def setUp(self): super(TestPlanApiTest, self).setUp(test_plan_api.TestPlanApi) self.mock_test = ndb_models.Test(name='mock_test', command='command') self.mock_test.put() self.mock_test_id = str(self.mock_test.key.id()) self.mock_build_channel = ndb_models.BuildChannelConfig( id=str(uuid.uuid4()), name='mock_build_channel', provider_name='mock_build_provider') self.mock_build_channel.put() self.mock_build_channel_id = self.mock_build_channel.key.id() self.mock_device_action = ndb_models.DeviceAction( name='mock_device_action') self.mock_device_action.put() self.mock_device_action_id = str(self.mock_device_action.key.id()) build_item = base.BuildItem(name='zz', path='/foo/bar/zz', is_file=True) self.mock_test_resource_url = build.BuildUrl( self.mock_build_channel_id, build_item)
def testKickTestPlan_withRetry(self, create_test_run): """Tests that a test plan kick operations can be retried.""" test = ndb_models.Test(id='test_id') test_run_config = ndb_models.TestRunConfig( test_key=test.key, cluster='cluster', run_target='run_target') test_plan = ndb_models.TestPlan( name='test_plan', test_run_configs=[test_run_config]) test_plan.put() # First test run creation fails, but second attempt succeeds test_run = ndb_models.TestRun(id='test_run_id') create_test_run.side_effect = [RuntimeError('test_run_error'), test_run] executed = test_plan_kicker.KickTestPlan(test_plan.key.id()) self.assertTrue(executed) # Create test run operation retried until successful self.assertEqual(create_test_run.call_count, 2) status = ndb_models.TestPlanStatus.query(ancestor=test_plan.key).get() # Test run key stored and error message is empty self.assertEqual(status.last_run_keys, [test_run.key]) self.assertIsNone(status.last_run_error)
def _CreateMockTest(self): test = ndb_models.Test( name='Foo', test_resource_defs=[ ndb_models.TestResourceDef( name='test_resource_name', default_download_url='test_resource_url'), ], command='command', env_vars=[ ndb_models.NameValuePair(name='env_var_name', value='env_var_value') ], output_file_patterns=['output_file_pattern'], setup_scripts=['setup_script'], jvm_options=['jvm_option'], java_properties=[ ndb_models.NameValuePair(name='java_property_name', value='java_property_value') ]) test.put() return test
def testCreateTestRun_withTestRunActions(self): """Tests that test run can be created with test run actions.""" test = ndb_models.Test(name='test', command='command') test.put() # Create a placeholder action with two default options action = ndb_models.TestRunAction( name='Foo', hook_class_name='foo', options=[ ndb_models.NameValuePair(name='key1', value='default'), ndb_models.NameValuePair(name='key2', value='default'), ]) action.put() # Create action ref with overridden and added options test_run_config = ndb_models.TestRunConfig( test_key=test.key, test_run_action_refs=[ ndb_models.TestRunActionRef( action_key=action.key, options=[ ndb_models.NameValuePair(name='key2', value='updated'), ndb_models.NameValuePair(name='key3', value='added'), ], ) ]) # Test run created with the right test run action test_run = test_kicker.CreateTestRun([], test_run_config) self.assertModelEqual(test_run_config, test_run.test_run_config) self.assertModelListEqual(test_run.test_run_actions, [ ndb_models.TestRunAction( name='Foo', hook_class_name='foo', options=[ ndb_models.NameValuePair(name='key1', value='default'), ndb_models.NameValuePair(name='key2', value='updated'), ndb_models.NameValuePair(name='key3', value='added'), ]) ])
def _TestMessageConverter(msg): return ndb_models.Test( key=ConvertToKeyOrNone(ndb_models.Test, msg.id), name=msg.name, description=msg.description, test_resource_defs=ConvertList( msg.test_resource_defs, ndb_models.TestResourceDef), command=msg.command, env_vars=ConvertNameValuePairs(msg.env_vars, ndb_models.NameValuePair), setup_scripts=msg.setup_scripts, output_file_patterns=msg.output_file_patterns, result_file=msg.result_file, jvm_options=msg.jvm_options, java_properties=ConvertNameValuePairs( msg.java_properties, ndb_models.NameValuePair), context_file_dir=msg.context_file_dir, context_file_pattern=msg.context_file_pattern, retry_command_line=msg.retry_command_line, runner_sharding_args=msg.runner_sharding_args, default_test_run_parameters=Convert( msg.default_test_run_parameters, ndb_models.TestRunParameter), module_config_pattern=msg.module_config_pattern, module_execution_args=msg.module_execution_args)
def _createMockTest(self, name='test', command='command'): """Create a mock ndb_models.Test object.""" test = ndb_models.Test(name=name, command=command) test.put() return test
def _CreateMockTest(self, test_id='test.id', name='test name'): """Creates a mock ndb_models.Test object.""" test = ndb_models.Test(name=name, command='command') test.key = mtt_messages.ConvertToKey(ndb_models.Test, test_id) test.put() return test
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'])