def testExecuteHooks(self, mock_get_latest_attempt, mock_execute_hook): """Tests that relevant actions can be found and executed.""" before_action = ndb_models.TestRunAction( name='Before', hook_class_name='simple', phases=[ndb_models.TestRunPhase.BEFORE_RUN]) after_action = ndb_models.TestRunAction( name='After', hook_class_name='simple', phases=[ndb_models.TestRunPhase.AFTER_RUN]) # Create test run with two actions and an attempt test_run = ndb_models.TestRun( test_run_actions=[before_action, after_action]) test_run.put() attempt = mock.MagicMock() mock_get_latest_attempt.return_value = attempt # Execute after run hooks and verify test_run_hook.ExecuteHooks(test_run.key.id(), ndb_models.TestRunPhase.AFTER_RUN, attempt_id='attempt_id') mock_get_latest_attempt.assert_called_once_with(test_run, 'attempt_id') hook_context = plugins.TestRunHookContext( test_run=test_run, latest_attempt=attempt, phase=ndb_models.TestRunPhase.AFTER_RUN) mock_execute_hook.assert_called_once_with(after_action, hook_context)
def testGetAuthorizationState_authorized(self): """Tests detecting hooks requiring authorization and with credentials.""" action = ndb_models.TestRunAction( name='Test', hook_class_name='oauth2', credentials=authorized_user.Credentials(None), ) self.assertEqual(ndb_models.AuthorizationState.AUTHORIZED, test_run_hook.GetAuthorizationState(action))
def _TestRunActionMessageConverter(msg): return ndb_models.TestRunAction( key=ConvertToKeyOrNone(ndb_models.TestRunAction, msg.id), name=msg.name, description=msg.description, hook_class_name=msg.hook_class_name, phases=msg.phases, options=ConvertNameValuePairs(msg.options, ndb_models.NameValuePair), tradefed_result_reporters=ConvertList( msg.tradefed_result_reporters, ndb_models.TradefedConfigObject))
def _CreateMockTestRunAction(self, action_id='action.id', namespace='', name='Test Run Action 1'): if namespace: action_id = config_encoder._AddNamespaceToId(namespace, action_id) action = ndb_models.TestRunAction(name=name, hook_class_name='hook class') action.key = ndb.Key(ndb_models.TestRunAction, action_id) action.put() return action
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_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 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 testExecuteHook(self, mock_init, mock_log, mock_execute): """Tests that a hook can be constructed and executed.""" mock_init.return_value = None hook_context = mock.MagicMock() credentials = authorized_user.Credentials(None) action = ndb_models.TestRunAction( name='Test', hook_class_name='simple', options=[ndb_models.NameValuePair(name='ham', value='eggs')], credentials=credentials, ) test_run_hook._ExecuteHook(action, hook_context) mock_init.assert_called_with(_credentials=credentials, ham='eggs') mock_log.assert_called() mock_execute.assert_called_with(hook_context)
def testGetAuthorizationState_notApplicable(self): """Tests detecting hooks not requiring authorization.""" action = ndb_models.TestRunAction(name='Test', hook_class_name='simple') self.assertEqual(ndb_models.AuthorizationState.NOT_APPLICABLE, test_run_hook.GetAuthorizationState(action))
def testGetOAuth2Config(self): """Tests that a hook's OAuth2 config can be retrieved.""" action = ndb_models.TestRunAction(name='Test', hook_class_name='oauth2') self.assertEqual(OAuth2Hook.oauth2_config, test_run_hook.GetOAuth2Config(action))
def testGetOAuth2Config_noConfig(self): """Tests that no OAuth2 config is returned if class doesn't have any.""" action = ndb_models.TestRunAction(name='Test', hook_class_name='simple') self.assertIsNone(test_run_hook.GetOAuth2Config(action))
def testGetOAuth2Config_notFound(self): """Tests that no OAuth2 config is returned if hook class not found.""" action = ndb_models.TestRunAction(name='Test', hook_class_name='unknown') self.assertIsNone(test_run_hook.GetOAuth2Config(action))
def testKickTestRun_withTestRunActions( self, mock_download_resources, mock_execute_hooks, mock_new_request): # Create test run action with two TF result reporters test_run_action = ndb_models.TestRunAction( name='Foo', hook_class_name='foo', tradefed_result_reporters=[ ndb_models.TradefedConfigObject( class_name='com.android.foo', option_values=[ndb_models.NameMultiValuePair( name='test-run-id', values=['${MTT_TEST_RUN_ID}'])] ), ndb_models.TradefedConfigObject(class_name='com.android.bar'), ]) test_run_action.put() # Create test run with test run action test_run = self._CreateMockTestRun(command='command') test_run.test_run_actions = [test_run_action] test_run.put() test_run_id = test_run.key.id() test_run = ndb_models.TestRun.get_by_id(test_run_id) # Mock responses 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) # Resources downloaded, hooks executed, and new TFC request created mock_download_resources.assert_called_once_with( [r.url for r in test_run.test_resources], test_run=test_run) mock_execute_hooks.assert_called_with( test_run_id, ndb_models.TestRunPhase.BEFORE_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 --invocation-data mtt=1']) # TFC request has two TF result reporters with right class names and options tradefed_config_objects = msg.test_environment.tradefed_config_objects self.assertEqual( [tradefed_config_objects.pop(0)], test_kicker.DEFAULT_TF_CONFIG_OBJECTS) self.assertLen(tradefed_config_objects, 2) self.assertEqual(api_messages.TradefedConfigObjectType.RESULT_REPORTER, tradefed_config_objects[1].type) self.assertEqual('com.android.foo', tradefed_config_objects[0].class_name) self.assertEqual('test-run-id', tradefed_config_objects[0].option_values[0].key) self.assertEqual([str(test_run_id)], tradefed_config_objects[0].option_values[0].values) self.assertEqual(api_messages.TradefedConfigObjectType.RESULT_REPORTER, tradefed_config_objects[1].type) self.assertEqual('com.android.bar', tradefed_config_objects[1].class_name) self.assertEmpty(tradefed_config_objects[1].option_values) # Test run now queued 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 _CreateTestRunAction(self, **kwargs): """Convenience method to create a test run action.""" action = ndb_models.TestRunAction(**kwargs) action.put() return action