def test_parse_time_raises_exception(self, parse_date_mock): input = "some time" parsed_result = None parse_date_mock.return_value = parsed_result with self.assertRaises(UserException) as ctx: LogsCommandContext._parse_time(input, "some prop") self.assertEqual(str(ctx.exception), "Unable to parse the time provided by 'some prop'")
def test_parse_time_raises_exception(self, parse_date_mock): input = "some time" parsed_result = None parse_date_mock.return_value = parsed_result with self.assertRaises(UserException) as ctx: LogsCommandContext._parse_time(input, "some prop") self.assertEquals(str(ctx.exception), "Unable to parse the time provided by 'some prop'")
def test_setup_output_file(self, open_mock): open_mock.return_value = "handle" result = LogsCommandContext._setup_output_file(self.output_file) self.assertEquals(result, "handle") open_mock.assert_called_with(self.output_file, "wb")
def test_must_get_from_cfn(self): expected_params = { "StackName": self.stack_name, "LogicalResourceId": self.logical_id } mock_response = { "StackResourceDetail": { "PhysicalResourceId": self.physical_id, "LogicalResourceId": self.logical_id, "ResourceType": "AWS::Lambda::Function", "ResourceStatus": "UPDATE_COMPLETE", "LastUpdatedTimestamp": "2017-07-28T23:34:13.435Z" } } self.cfn_client_stubber.add_response("describe_stack_resource", mock_response, expected_params) with self.cfn_client_stubber: result = LogsCommandContext._get_resource_id_from_stack(self.real_client, self.stack_name, self.logical_id) self.assertEquals(result, self.physical_id)
def test_must_get_from_cfn(self): expected_params = { "StackName": self.stack_name, "LogicalResourceId": self.logical_id } mock_response = { "StackResourceDetail": { "PhysicalResourceId": self.physical_id, "LogicalResourceId": self.logical_id, "ResourceType": "AWS::Lambda::Function", "ResourceStatus": "UPDATE_COMPLETE", "LastUpdatedTimestamp": "2017-07-28T23:34:13.435Z" } } self.cfn_client_stubber.add_response("describe_stack_resource", mock_response, expected_params) with self.cfn_client_stubber: result = LogsCommandContext._get_resource_id_from_stack( self.real_client, self.stack_name, self.logical_id) self.assertEquals(result, self.physical_id)
def test_setup_output_file(self, open_mock): open_mock.return_value = "handle" result = LogsCommandContext._setup_output_file(self.output_file) self.assertEquals(result, "handle") open_mock.assert_called_with(self.output_file, "wb")
def test_must_handle_resource_not_found(self): errmsg = "Something went wrong" errcode = "SomeException" self.cfn_client_stubber.add_client_error("describe_stack_resource", service_error_code=errcode, service_message=errmsg) expected_error_msg = "An error occurred ({}) when calling the DescribeStackResource operation: {}".format( errcode, errmsg) with self.cfn_client_stubber: with self.assertRaises(UserException) as context: LogsCommandContext._get_resource_id_from_stack( self.real_client, self.stack_name, self.logical_id) self.assertEquals(expected_error_msg, str(context.exception))
def test_must_handle_resource_not_found(self): errmsg = "Something went wrong" errcode = "SomeException" self.cfn_client_stubber.add_client_error("describe_stack_resource", service_error_code=errcode, service_message=errmsg) expected_error_msg = "An error occurred ({}) when calling the DescribeStackResource operation: {}".format( errcode, errmsg) with self.cfn_client_stubber: with self.assertRaises(UserException) as context: LogsCommandContext._get_resource_id_from_stack(self.real_client, self.stack_name, self.logical_id) self.assertEquals(expected_error_msg, str(context.exception))
def test_parse_time(self, to_utc_mock, parse_date_mock): input = "some time" parsed_result = "parsed" expected = "bar" parse_date_mock.return_value = parsed_result to_utc_mock.return_value = expected actual = LogsCommandContext._parse_time(input, "some prop") self.assertEquals(actual, expected) parse_date_mock.assert_called_with(input) to_utc_mock.assert_called_with(parsed_result)
def test_parse_time(self, to_utc_mock, parse_date_mock): input = "some time" parsed_result = "parsed" expected = "bar" parse_date_mock.return_value = parsed_result to_utc_mock.return_value = expected actual = LogsCommandContext._parse_time(input, "some prop") self.assertEquals(actual, expected) parse_date_mock.assert_called_with(input) to_utc_mock.assert_called_with(parsed_result)
def test_colored_property_without_output_file(self, ColoredMock): ColoredMock.return_value = Mock() # No output file. It means we are printing to Terminal. Hence set the color ctx = LogsCommandContext(self.function_name, stack_name=self.stack_name, filter_pattern=self.filter_pattern, start_time=self.start_time, end_time=self.end_time, output_file=None) self.assertEqual(ctx.colored, ColoredMock.return_value) ColoredMock.assert_called_with(colorize=True) # Must enable colors
def test_context_manager_no_output_file(self, setup_output_file_mock): setup_output_file_mock.return_value = None with LogsCommandContext(self.function_name, stack_name=self.stack_name, filter_pattern=self.filter_pattern, start_time=self.start_time, end_time=self.end_time, output_file=None) as context: self.assertEquals(context._output_file_handle, None) # Context should be reset setup_output_file_mock.assert_called_with(None)
def setUp(self): self.function_name = "name" self.stack_name = "stack name" self.filter_pattern = "filter" self.start_time = "start" self.end_time = "end" self.output_file = "somefile" self.context = LogsCommandContext(self.function_name, stack_name=self.stack_name, filter_pattern=self.filter_pattern, start_time=self.start_time, end_time=self.end_time, output_file=self.output_file)
def test_context_manager_with_output_file(self, setup_output_file_mock): handle = Mock() setup_output_file_mock.return_value = handle with LogsCommandContext(self.function_name, stack_name=self.stack_name, filter_pattern=self.filter_pattern, start_time=self.start_time, end_time=self.end_time, output_file=self.output_file) as context: self.assertEquals(context._output_file_handle, handle) # Context should be reset self.assertIsNone(self.context._output_file_handle) handle.close.assert_called_with() setup_output_file_mock.assert_called_with(self.output_file)
def test_log_group_name_property_without_stack_name(self, get_resource_id_mock, LogGroupProviderMock): group = "groupname" LogGroupProviderMock.for_lambda_function.return_value = group ctx = LogsCommandContext( self.function_name, stack_name=None, # No Stack Name filter_pattern=self.filter_pattern, start_time=self.start_time, end_time=self.end_time, output_file=self.output_file, ) self.assertEqual(ctx.log_group_name, group) LogGroupProviderMock.for_lambda_function.assert_called_with(self.function_name) get_resource_id_mock.assert_not_called()
def test_parse_time_empty_time(self): result = LogsCommandContext._parse_time(None, "some prop") self.assertIsNone(result)
def test_setup_output_file_without_file(self): self.assertIsNone(LogsCommandContext._setup_output_file(None))
def test_setup_output_file_without_file(self): self.assertIsNone(LogsCommandContext._setup_output_file(None))
def test_parse_time_empty_time(self): result = LogsCommandContext._parse_time(None, "some prop") self.assertIsNone(result)