def testAssertion(self, assertion, actual, matches): if matches: with assertions.FailureCollector([]) as f: f.AddAll(assertion.Check(updates.Context.Empty(), actual)) else: with self.assertRaises(assertions.Error): with assertions.FailureCollector([]) as f: f.AddAll(assertion.Check(updates.Context.Empty(), actual))
def testBasic(self): # No errors is OK. with assertions.FailureCollector([]): pass # Error is an error with self.assertRaises(assertions.Error): with assertions.FailureCollector([]) as f: f.Add(assertions.Failure.ForScalar( updates.Context.Empty(), 'expected', 'actual'))
def testUpdateMode(self): context = updates.Context({}, 'field', updates.Mode.RESULT) # Fail if not in update mode. with self.assertRaises(assertions.Error): with assertions.FailureCollector([]) as f: f.Add(assertions.Failure.ForScalar(context, 'expected', 'actual')) # Updates without failures with assertions.FailureCollector([updates.Mode.RESULT]) as f: f.Add(assertions.Failure.ForScalar(context, 'expected', 'actual'))
def Execute(self, scenario_context): original_data = scenario_context.resource_ref_resolver.Resolve( self._original_data) original_data.setdefault('expect_exit', {}) exit_event = events.ExitEvent.FromData(original_data) stdout_event = events.StdoutEvent.FromData(original_data) stderr_event = events.StderrEvent.FromData(original_data) stdout = io.StringIO() stderr = io.StringIO() return_code = execution_utils.Exec(self._args, no_exit=True, in_str=self._stdin, out_func=stdout.write, err_func=stderr.write) action_location = updates.Context(self._original_data, None, None).Location() try: with assertions.FailureCollector( scenario_context.update_modes, spec_name=scenario_context.spec_name, action_location=action_location, execution_mode=scenario_context.execution_mode.name ) as failures: failures.AddAll(exit_event.HandleReturnCode(return_code)) failures.AddAll(stdout_event.Handle(stdout.getvalue())) failures.AddAll(stderr_event.Handle(stderr.getvalue())) finally: if scenario_context.update_modes: scenario_context.RewriteScenario()
def Execute(self, scenario_context): if scenario_context.execution_mode == session.ExecutionMode.LOCAL: if self.validation_only: # If this command is only used to validate the side effects of a # previous command, don't bother running it in LOCAL mode since # everything is fake anyway. return ignore_api_calls = False else: # In remote mode, ignore api_call validation if this is a validation only # command or if api_call validation is explicitly disabled. ignore_api_calls = (self.validation_only or not self.validate_remote_api_calls) update_modes = scenario_context.update_modes stream_mocker = scenario_context.stream_mocker action_location = updates.Context(self._original_data, None, None).Location() session_obj = None try: with assertions.FailureCollector( update_modes, spec_name=scenario_context.spec_name, action_location=action_location, execution_mode=scenario_context.execution_mode.name ) as failures: with session.Session(self._LoadEvents( scenario_context.resource_ref_resolver), failures, stream_mocker, scenario_context.execution_mode, ignore_api_calls, scenario_context.resource_ref_resolver, action_location=action_location, debug=scenario_context.debug) as s: session_obj = s scenario_context.command_executor( scenario_context.resource_ref_resolver.Resolve( self.command)) finally: if session_obj: if update_modes: # Update spec file event_data = session_obj.GetEventSequence() if event_data is not None: self._SetEventData(scenario_context, event_data) # If this is a cleanup step, remove the resource ref so it can no longer # be used and we know it doesn't require further cleanup. if self.cleanup_for: scenario_context.resource_ref_resolver.RemoveGeneratedResourceId( self.cleanup_for) remaining_stdin = sys.stdin.read() if remaining_stdin: raise Error( 'Not all stdin was consumed: [{}]'.format(remaining_stdin))
def testCheckJsonContent(self, assertion, matches): actual = { 'a': {'b': 'c', 'd': 'e', 'f': 1, 'g': True}, 'h': { 'i': {'j': 'k', 'l': 'm'}, 'o': {'p': 'q', 'r': 'r'}, }, 'list': [ {'s': 't', 'u': 'v'}, {'w': 'x', 'y': 'z'}, ], 'n_l': {'l': [ {'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6} ]} } if matches: with assertions.FailureCollector([]) as f: f.AddAll(assertion.Check(updates.Context.Empty(), actual)) else: with self.assertRaises(assertions.Error): with assertions.FailureCollector([]) as f: f.AddAll(assertion.Check(updates.Context.Empty(), actual))