def test_must_record_wrapped_user_exception(self, ContextMock): ContextMock.get_current_context.return_value = self.context_mock expected_exception = UserException("Something went wrong", wrapped_from="CustomException") expected_exception.exit_code = 1235 def real_fn(): raise expected_exception with self.assertRaises(UserException) as context: track_command(real_fn)() self.assertEqual( context.exception, expected_exception, "Must re-raise the original exception object " "without modification", ) expected_attrs = _ignore_common_attributes({ "exitReason": "CustomException", "exitCode": 1235 }) args, _ = self.telemetry_instance.emit.call_args_list[0] metric = args[0] assert metric.get_metric_name() == "commandRun" self.assertGreaterEqual(metric.get_data().items(), expected_attrs.items())
def test_must_record_any_exceptions(self, ContextMock): ContextMock.get_current_context.return_value = self.context_mock expected_exception = KeyError("IO Error test") def real_fn(): raise expected_exception with self.assertRaises(KeyError) as context: track_command(real_fn)() self.assertEqual( context.exception, expected_exception, "Must re-raise the original exception object " "without modification", ) expected_attrs = _ignore_common_attributes({ "exitReason": "KeyError", "exitCode": 255 } # Unhandled exceptions always use exit code 255 ) args, _ = self.telemetry_instance.emit.call_args_list[0] metric = args[0] assert metric.get_metric_name() == "commandRun" self.assertGreaterEqual(metric.get_data().items(), expected_attrs.items())
def test_must_emit_one_metric(self, ContextMock): ContextMock.get_current_context.return_value = self.context_mock def real_fn(): pass track_command(real_fn)() args, _ = self.telemetry_instance.emit.call_args_list[0] metric = args[0] assert metric.get_metric_name() == "commandRun" self.assertEqual(self.telemetry_instance.emit.mock_calls, [call(ANY)], "The one command metric must be sent")
def test_must_emit_command_run_metric_with_sanitized_profile_value(self, ContextMock): ContextMock.get_current_context.return_value = self.context_mock self.context_mock.profile = "myprofilename" def real_fn(): pass track_command(real_fn)() expected_attrs = _ignore_common_attributes({"awsProfileProvided": True}) args, _ = self.telemetry_instance.emit.call_args_list[0] metric = args[0] assert metric.get_metric_name() == "commandRun" self.assertGreaterEqual(metric.get_data().items(), expected_attrs.items())
def test_must_return_value_from_decorated_function(self, ContextMock): expected_value = "some return value" def real_fn(): return expected_value actual = track_command(real_fn)() self.assertEqual(actual, "some return value")
def test_must_pass_all_arguments_to_wrapped_function(self, ContextMock): def real_fn(*args, **kwargs): # simply return the arguments to be able to examine & assert return args, kwargs actual_args, actual_kwargs = track_command(real_fn)(1, 2, 3, a=1, b=2, c=3) self.assertEqual(actual_args, (1, 2, 3)) self.assertEqual(actual_kwargs, {"a": 1, "b": 2, "c": 3})
def test_must_record_function_duration(self, ContextMock): ContextMock.get_current_context.return_value = self.context_mock sleep_duration = 0.01 # 10 millisecond def real_fn(): time.sleep(sleep_duration) track_command(real_fn)() # commandRun metric should be the only call to emit. # And grab the second argument passed to this call, which are the attributes args, _ = self.telemetry_instance.emit.call_args_list[0] metric = args[0] assert metric.get_metric_name() == "commandRun" self.assertGreaterEqual( metric.get_data()["duration"], sleep_duration, "Measured duration must be in milliseconds and " "greater than equal to the sleep duration", )
def test_must_emit_command_run_metric(self, ContextMock): ContextMock.get_current_context.return_value = self.context_mock def real_fn(): pass track_command(real_fn)() expected_attrs = { "awsProfileProvided": False, "debugFlagProvided": False, "region": "myregion", "commandName": "fakesam local invoke", "duration": ANY, "exitReason": "success", "exitCode": 0, } args, _ = self.telemetry_instance.emit.call_args_list[0] metric = args[0] assert metric.get_metric_name() == "commandRun" self.assertGreaterEqual(metric.get_data().items(), expected_attrs.items())