diff --git a/samcli/commands/_utils/options.py b/samcli/commands/_utils/options.py index b04e0492ee..627851792c 100644 --- a/samcli/commands/_utils/options.py +++ b/samcli/commands/_utils/options.py @@ -21,6 +21,7 @@ ) from samcli.commands._utils.custom_options.option_nargs import OptionNargs from samcli.commands._utils.template import get_template_artifacts_format +from samcli.lib.observability.util import OutputOption from samcli.lib.utils.packagetype import ZIP, IMAGE _TEMPLATE_OPTION_DEFAULT_VALUE = "template.[yaml|yml|json]" @@ -369,12 +370,14 @@ def common_observability_click_options(): "become available. [Beta Feature] If in beta --tail without a --name will pull from all possible resources", ), click.option( - "--unformatted", - "-u", - is_flag=True, - help="[Beta Feature] " - "Print events without any text formatting in JSON. This option might be useful if you are reading " - "output into another tool.", + "--output", + help=""" + [Beta Feature] + The formatting style of the command output. Following options are available:\n + TEXT: Prints information as regular text with some formatting (default option)\n + JSON: Prints each line as JSON without formatting + """, + type=click.Choice(OutputOption.__members__, case_sensitive=False), ), ] diff --git a/samcli/commands/logs/command.py b/samcli/commands/logs/command.py index 90ddf40a5e..cb8a8d0a09 100644 --- a/samcli/commands/logs/command.py +++ b/samcli/commands/logs/command.py @@ -86,7 +86,7 @@ @print_cmdline_args @force_experimental_option("include_traces", config_entry=ExperimentalFlag.Accelerate) # pylint: disable=E1120 @force_experimental_option("cw_log_group", config_entry=ExperimentalFlag.Accelerate) # pylint: disable=E1120 -@force_experimental_option("unformatted", config_entry=ExperimentalFlag.Accelerate) # pylint: disable=E1120 +@force_experimental_option("output", config_entry=ExperimentalFlag.Accelerate) # pylint: disable=E1120 def cli( ctx, name, @@ -96,7 +96,7 @@ def cli( include_traces, start_time, end_time, - unformatted, + output, cw_log_group, config_file, config_env, @@ -115,7 +115,7 @@ def cli( start_time, end_time, cw_log_group, - unformatted, + output, ctx.region, ctx.profile, ) # pragma: no cover @@ -130,7 +130,7 @@ def do_cli( start_time, end_time, cw_log_groups, - unformatted, + output, region, profile, ): @@ -142,6 +142,7 @@ def do_cli( from samcli.commands.logs.logs_context import parse_time, ResourcePhysicalIdResolver from samcli.commands.logs.puller_factory import generate_puller + from samcli.lib.observability.util import OutputOption from samcli.lib.utils.boto_utils import get_boto_client_provider_with_config, get_boto_resource_provider_with_config if not names or len(names) > 1: @@ -167,7 +168,7 @@ def do_cli( resource_logical_id_resolver.get_resource_information(fetch_all_when_no_resource_name_given), filter_pattern, cw_log_groups, - unformatted, + OutputOption(output) if output else OutputOption.text, include_tracing, ) diff --git a/samcli/commands/logs/puller_factory.py b/samcli/commands/logs/puller_factory.py index 1926ce5562..3cd5246d1e 100644 --- a/samcli/commands/logs/puller_factory.py +++ b/samcli/commands/logs/puller_factory.py @@ -24,6 +24,7 @@ ObservabilityEventConsumer, ObservabilityCombinedPuller, ) +from samcli.lib.observability.util import OutputOption from samcli.lib.utils.boto_utils import BotoProviderType from samcli.lib.utils.cloudformation import CloudFormationResourceSummary from samcli.lib.utils.colors import Colored @@ -43,7 +44,7 @@ def generate_puller( resource_information_list: List[CloudFormationResourceSummary], filter_pattern: Optional[str] = None, additional_cw_log_groups: Optional[List[str]] = None, - unformatted: bool = False, + output: OutputOption = OutputOption.text, include_tracing: bool = False, ) -> ObservabilityPuller: """ @@ -62,9 +63,9 @@ def generate_puller( additional_cw_log_groups : Optional[str] Optional list of additional CloudWatch log groups which will be used to fetch log events from. - unformatted : bool - By default, logs and traces are printed with a format for terminal. If this option is provided, the events - will be printed unformatted in JSON. + output : OutputOption + Decides how the output will be presented in the console. It is been used to select correct consumer type + between (default) text consumer or json consumer include_tracing: bool A flag to include the xray traces log or not @@ -87,7 +88,7 @@ def generate_puller( LOG.debug("Can't find CloudWatch LogGroup name for resource (%s)", resource_information.logical_resource_id) continue - consumer = generate_consumer(filter_pattern, unformatted, resource_information.logical_resource_id) + consumer = generate_consumer(filter_pattern, output, resource_information.logical_resource_id) pullers.append( CWLogPuller( boto_client_provider("logs"), @@ -99,7 +100,7 @@ def generate_puller( # populate puller instances for the additional CloudWatch log groups for cw_log_group in additional_cw_log_groups: - consumer = generate_consumer(filter_pattern, unformatted) + consumer = generate_consumer(filter_pattern, output) pullers.append( CWLogPuller( boto_client_provider("logs"), @@ -110,7 +111,7 @@ def generate_puller( # if tracing flag is set, add the xray traces puller to fetch debug traces if include_tracing: - trace_puller = generate_trace_puller(boto_client_provider("xray"), unformatted) + trace_puller = generate_trace_puller(boto_client_provider("xray"), output) pullers.append(trace_puller) # if no puller have been collected, raise an exception since there is nothing to pull @@ -122,22 +123,22 @@ def generate_puller( def generate_consumer( - filter_pattern: Optional[str] = None, unformatted: bool = False, resource_name: Optional[str] = None + filter_pattern: Optional[str] = None, output: OutputOption = OutputOption.text, resource_name: Optional[str] = None ): """ Generates consumer instance with the given variables. - If unformatted is True, then it will return consumer with formatters for just JSON. - If not, it will return console consumer + If output is JSON, then it will return consumer with formatters for just JSON. + Otherwise, it will return regular text console consumer """ - if unformatted: - return generate_unformatted_consumer() + if output == OutputOption.json: + return generate_json_consumer() - return generate_console_consumer(filter_pattern) + return generate_text_consumer(filter_pattern) -def generate_unformatted_consumer() -> ObservabilityEventConsumer: +def generate_json_consumer() -> ObservabilityEventConsumer: """ - Creates event consumer, which prints CW Log Events unformatted as JSON into terminal + Creates event consumer, which prints CW Log Events as JSON into terminal Returns ------- @@ -151,7 +152,7 @@ def generate_unformatted_consumer() -> ObservabilityEventConsumer: ) -def generate_console_consumer(filter_pattern: Optional[str]) -> ObservabilityEventConsumer: +def generate_text_consumer(filter_pattern: Optional[str]) -> ObservabilityEventConsumer: """ Creates a console event consumer, which is used to display events in the user's console diff --git a/samcli/commands/traces/command.py b/samcli/commands/traces/command.py index b2fab56f5b..f014d2320b 100644 --- a/samcli/commands/traces/command.py +++ b/samcli/commands/traces/command.py @@ -8,6 +8,7 @@ from samcli.cli.cli_config_file import configuration_option, TomlProvider from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options, print_cmdline_args from samcli.commands._utils.options import common_observability_options +from samcli.lib.observability.util import OutputOption from samcli.lib.telemetry.metric import track_command from samcli.lib.utils.version_checker import check_newer_version from samcli.commands._utils.experimental import ExperimentalFlag, force_experimental @@ -41,17 +42,17 @@ def cli( start_time, end_time, tail, - unformatted, + output, config_file, config_env, ): """ `sam traces` command entry point """ - do_cli(trace_id, start_time, end_time, tail, unformatted, ctx.region) + do_cli(trace_id, start_time, end_time, tail, output, ctx.region) -def do_cli(trace_ids, start_time, end_time, tailing, unformatted, region): +def do_cli(trace_ids, start_time, end_time, tailing, output, region): """ Implementation of the ``cli`` method """ @@ -68,7 +69,7 @@ def do_cli(trace_ids, start_time, end_time, tailing, unformatted, region): xray_client = boto3.client("xray", config=boto_config) # generate puller depending on the parameters - puller = generate_trace_puller(xray_client, unformatted) + puller = generate_trace_puller(xray_client, OutputOption(output) if output else OutputOption.text) if trace_ids: puller.load_events(trace_ids) diff --git a/samcli/commands/traces/traces_puller_factory.py b/samcli/commands/traces/traces_puller_factory.py index 7c3f5a4860..34089917c6 100644 --- a/samcli/commands/traces/traces_puller_factory.py +++ b/samcli/commands/traces/traces_puller_factory.py @@ -10,6 +10,7 @@ ObservabilityEventConsumerDecorator, ObservabilityCombinedPuller, ) +from samcli.lib.observability.util import OutputOption from samcli.lib.observability.xray_traces.xray_event_mappers import ( XRayTraceConsoleMapper, XRayServiceGraphConsoleMapper, @@ -22,7 +23,7 @@ def generate_trace_puller( xray_client: Any, - unformatted: bool = False, + output: OutputOption = OutputOption.text, ) -> ObservabilityPuller: """ Generates puller instance with correct consumer and/or mapper configuration @@ -31,22 +32,22 @@ def generate_trace_puller( ---------- xray_client : Any boto3 xray client to be used in XRayTracePuller instance - unformatted : bool - By default, logs and traces are printed with a format for terminal. If this option is provided, the events - will be printed unformatted in JSON. + output : OutputOption + Decides how the output will be presented in the console. It is been used to select correct consumer type + between (default) text consumer or json consumer Returns ------- Puller instance with desired configuration """ pullers: List[ObservabilityPuller] = [] - pullers.append(XRayTracePuller(xray_client, generate_xray_event_consumer(unformatted))) - pullers.append(XRayServiceGraphPuller(xray_client, generate_xray_service_graph_consumer(unformatted))) + pullers.append(XRayTracePuller(xray_client, generate_xray_event_consumer(output))) + pullers.append(XRayServiceGraphPuller(xray_client, generate_xray_service_graph_consumer(output))) return ObservabilityCombinedPuller(pullers) -def generate_unformatted_xray_event_consumer() -> ObservabilityEventConsumer: +def generate_json_xray_event_consumer() -> ObservabilityEventConsumer: """ Generates unformatted consumer, which will print XRay events unformatted JSON into terminal @@ -68,18 +69,18 @@ def generate_xray_event_console_consumer() -> ObservabilityEventConsumer: return ObservabilityEventConsumerDecorator([XRayTraceConsoleMapper()], XRayTraceConsoleConsumer()) -def generate_xray_event_consumer(unformatted: bool = False) -> ObservabilityEventConsumer: +def generate_xray_event_consumer(output: OutputOption = OutputOption.text) -> ObservabilityEventConsumer: """ Generates consumer instance with the given variables. - If unformatted is True, then it will return consumer with formatters for just JSON. - If not, it will return console consumer + If output is JSON, then it will return consumer with formatters for just JSON. + Otherwise, it will return regular text console consumer """ - if unformatted: - return generate_unformatted_xray_event_consumer() + if output == OutputOption.json: + return generate_json_xray_event_consumer() return generate_xray_event_console_consumer() -def generate_unformatted_xray_service_graph_consumer() -> ObservabilityEventConsumer: +def generate_json_xray_service_graph_consumer() -> ObservabilityEventConsumer: """ Generates unformatted consumer, which will print XRay events unformatted JSON into terminal @@ -101,12 +102,12 @@ def generate_xray_service_graph_console_consumer() -> ObservabilityEventConsumer return ObservabilityEventConsumerDecorator([XRayServiceGraphConsoleMapper()], XRayTraceConsoleConsumer()) -def generate_xray_service_graph_consumer(unformatted: bool = False) -> ObservabilityEventConsumer: +def generate_xray_service_graph_consumer(output: OutputOption = OutputOption.text) -> ObservabilityEventConsumer: """ Generates consumer instance with the given variables. - If unformatted is True, then it will return consumer with formatters for just JSON. - If not, it will return console consumer + If output is JSON, then it will return consumer with formatters for just JSON. + Otherwise, it will return regular text console consumer """ - if unformatted: - return generate_unformatted_xray_service_graph_consumer() + if output == OutputOption.json: + return generate_json_xray_service_graph_consumer() return generate_xray_service_graph_console_consumer() diff --git a/samcli/lib/observability/util.py b/samcli/lib/observability/util.py new file mode 100644 index 0000000000..d3996d6353 --- /dev/null +++ b/samcli/lib/observability/util.py @@ -0,0 +1,13 @@ +""" +Utility classes and methods for observability commands and functionality +""" +from enum import Enum + + +class OutputOption(Enum): # pragma: no cover + """ + Used to configure how output will be presented with observability commands + """ + + text = "text" # default + json = "json" diff --git a/tests/unit/commands/logs/test_command.py b/tests/unit/commands/logs/test_command.py index 976ef99044..ac65365674 100644 --- a/tests/unit/commands/logs/test_command.py +++ b/tests/unit/commands/logs/test_command.py @@ -1,9 +1,11 @@ +import itertools from unittest import TestCase from unittest.mock import Mock, patch, call, ANY from parameterized import parameterized from samcli.commands.logs.command import do_cli +from samcli.lib.observability.util import OutputOption @patch("samcli.commands._utils.experimental.is_experimental_enabled") @@ -16,33 +18,13 @@ def setUp(self): self.filter_pattern = "filter" self.start_time = "start" self.end_time = "end" - self.output_dir = "output_dir" self.region = "region" self.profile = "profile" @parameterized.expand( - [ - ( - True, - False, - [], - ), - ( - False, - False, - [], - ), - ( - True, - False, - ["cw_log_group"], - ), - ( - False, - False, - ["cw_log_group", "cw_log_group2"], - ), - ] + itertools.product( + [True, False], [True, False], [[], ["cw_log_group"], ["cw_log_group", "cw_log_group2"]], ["text", "json"] + ) ) @patch("samcli.commands.logs.puller_factory.generate_puller") @patch("samcli.commands.logs.logs_context.ResourcePhysicalIdResolver") @@ -54,6 +36,7 @@ def test_logs_command( tailing, include_tracing, cw_log_group, + output, patched_boto_resource_provider, patched_boto_client_provider, patched_parse_time, @@ -89,7 +72,7 @@ def test_logs_command( self.start_time, self.end_time, cw_log_group, - self.output_dir, + output, self.region, self.profile, ) @@ -116,8 +99,8 @@ def test_logs_command( mocked_resource_information, self.filter_pattern, cw_log_group, - self.output_dir, - False, + OutputOption(output), + include_tracing, ) if tailing: diff --git a/tests/unit/commands/logs/test_puller_factory.py b/tests/unit/commands/logs/test_puller_factory.py index bf4f6dd143..1bbc304265 100644 --- a/tests/unit/commands/logs/test_puller_factory.py +++ b/tests/unit/commands/logs/test_puller_factory.py @@ -1,13 +1,15 @@ +import itertools from unittest import TestCase from unittest.mock import Mock, patch, call, ANY from parameterized import parameterized +from samcli.lib.observability.util import OutputOption from samcli.lib.utils.resources import AWS_LAMBDA_FUNCTION from samcli.commands.logs.puller_factory import ( generate_puller, - generate_unformatted_consumer, - generate_console_consumer, + generate_json_consumer, + generate_text_consumer, NoPullerGeneratedException, generate_consumer, ) @@ -15,17 +17,14 @@ class TestPullerFactory(TestCase): @parameterized.expand( - [ - (None, None, False), - ("filter_pattern", None, False), - ("filter_pattern", ["cw_log_groups"], False), - ("filter_pattern", ["cw_log_groups"], True), - (None, ["cw_log_groups"], True), - (None, None, True), - ] + itertools.product( + [None, "filter_pattern"], + [None, ["cw_log_groups"]], + ["text", "json"], + ) ) - @patch("samcli.commands.logs.puller_factory.generate_console_consumer") - @patch("samcli.commands.logs.puller_factory.generate_unformatted_consumer") + @patch("samcli.commands.logs.puller_factory.generate_text_consumer") + @patch("samcli.commands.logs.puller_factory.generate_json_consumer") @patch("samcli.commands.logs.puller_factory.CWLogPuller") @patch("samcli.commands.logs.puller_factory.generate_trace_puller") @patch("samcli.commands.logs.puller_factory.ObservabilityCombinedPuller") @@ -33,12 +32,12 @@ def test_generate_puller( self, param_filter_pattern, param_cw_log_groups, - param_unformatted, + param_output, patched_combined_puller, patched_xray_puller, patched_cw_log_puller, - patched_unformatted_consumer, - patched_console_consumer, + patched_json_consumer, + patched_text_consumer, ): mock_logs_client = Mock() mock_xray_client = Mock() @@ -56,10 +55,10 @@ def test_generate_puller( mocked_consumers = mocked_resource_consumers + mocked_cw_specific_consumers # depending on the output_dir param patch file consumer or console consumer - if param_unformatted: - patched_unformatted_consumer.side_effect = mocked_consumers + if param_output == "json": + patched_json_consumer.side_effect = mocked_consumers else: - patched_console_consumer.side_effect = mocked_consumers + patched_text_consumer.side_effect = mocked_consumers mocked_xray_puller = Mock() patched_xray_puller.return_value = mocked_xray_puller @@ -76,13 +75,13 @@ def test_generate_puller( mock_resource_info_list, param_filter_pattern, param_cw_log_groups, - param_unformatted, + OutputOption(param_output), True, ) self.assertEqual(puller, mocked_combined_puller) - patched_xray_puller.assert_called_once_with(mock_xray_client, param_unformatted) + patched_xray_puller.assert_called_once_with(mock_xray_client, OutputOption(param_output)) patched_cw_log_puller.assert_has_calls( [call(mock_logs_client, consumer, ANY, ANY) for consumer in mocked_resource_consumers] @@ -95,10 +94,10 @@ def test_generate_puller( patched_combined_puller.assert_called_with(mocked_pullers) # depending on the output_dir param assert calls for file consumer or console consumer - if param_unformatted: - patched_unformatted_consumer.assert_has_calls([call() for _ in mocked_consumers]) + if param_output == "json": + patched_json_consumer.assert_has_calls([call() for _ in mocked_consumers]) else: - patched_console_consumer.assert_has_calls([call(param_filter_pattern) for _ in mocked_consumers]) + patched_text_consumer.assert_has_calls([call(param_filter_pattern) for _ in mocked_consumers]) def test_puller_with_invalid_resource_type(self): mock_logs_client = Mock() @@ -108,18 +107,18 @@ def test_puller_with_invalid_resource_type(self): with self.assertRaises(NoPullerGeneratedException): generate_puller(mock_logs_client, [mock_resource_information]) - @patch("samcli.commands.logs.puller_factory.generate_console_consumer") + @patch("samcli.commands.logs.puller_factory.generate_text_consumer") @patch("samcli.commands.logs.puller_factory.CWLogPuller") @patch("samcli.commands.logs.puller_factory.ObservabilityCombinedPuller") def test_generate_puller_with_console_with_additional_cw_logs_groups( - self, patched_combined_puller, patched_cw_log_puller, patched_console_consumer + self, patched_combined_puller, patched_cw_log_puller, patched_text_consumer ): mock_logs_client = Mock() mock_logs_client_generator = lambda client: mock_logs_client mock_cw_log_groups = [Mock(), Mock(), Mock()] mocked_consumers = [Mock() for _ in mock_cw_log_groups] - patched_console_consumer.side_effect = mocked_consumers + patched_text_consumer.side_effect = mocked_consumers mocked_pullers = [Mock() for _ in mock_cw_log_groups] patched_cw_log_puller.side_effect = mocked_pullers @@ -135,32 +134,32 @@ def test_generate_puller_with_console_with_additional_cw_logs_groups( patched_combined_puller.assert_called_with(mocked_pullers) - patched_console_consumer.assert_has_calls([call(None) for _ in mock_cw_log_groups]) + patched_text_consumer.assert_has_calls([call(None) for _ in mock_cw_log_groups]) @parameterized.expand( [ - (False,), - (True,), + (OutputOption.json,), + (OutputOption.text,), ] ) - @patch("samcli.commands.logs.puller_factory.generate_unformatted_consumer") - @patch("samcli.commands.logs.puller_factory.generate_console_consumer") - def test_generate_consumer(self, param_unformatted, patched_console_consumer, patched_unformatted_consumer): + @patch("samcli.commands.logs.puller_factory.generate_json_consumer") + @patch("samcli.commands.logs.puller_factory.generate_text_consumer") + def test_generate_consumer(self, param_output, patched_text_consumer, patched_json_consumer): given_filter_pattern = Mock() given_resource_name = Mock() given_console_consumer = Mock() - patched_console_consumer.return_value = given_console_consumer + patched_text_consumer.return_value = given_console_consumer given_file_consumer = Mock() - patched_unformatted_consumer.return_value = given_file_consumer + patched_json_consumer.return_value = given_file_consumer - actual_consumer = generate_consumer(given_filter_pattern, param_unformatted, given_resource_name) + actual_consumer = generate_consumer(given_filter_pattern, param_output, given_resource_name) - if param_unformatted: - patched_unformatted_consumer.assert_called_with() + if param_output == OutputOption.json: + patched_json_consumer.assert_called_with() self.assertEqual(actual_consumer, given_file_consumer) else: - patched_console_consumer.assert_called_with(given_filter_pattern) + patched_text_consumer.assert_called_with(given_filter_pattern) self.assertEqual(actual_consumer, given_console_consumer) @patch("samcli.commands.logs.puller_factory.ObservabilityEventConsumerDecorator") @@ -181,7 +180,7 @@ def test_generate_unformatted_consumer( expected_json_formatter = Mock() patched_json_formatter.return_value = expected_json_formatter - consumer = generate_unformatted_consumer() + consumer = generate_json_consumer() self.assertEqual(expected_consumer, consumer) @@ -234,7 +233,7 @@ def test_generate_console_consumer( expected_consumer = Mock() patched_decorated_consumer.return_value = expected_consumer - consumer = generate_console_consumer(mock_filter_pattern) + consumer = generate_text_consumer(mock_filter_pattern) self.assertEqual(expected_consumer, consumer) diff --git a/tests/unit/commands/samconfig/test_samconfig.py b/tests/unit/commands/samconfig/test_samconfig.py index 92cbd8b27a..5f6f09a701 100644 --- a/tests/unit/commands/samconfig/test_samconfig.py +++ b/tests/unit/commands/samconfig/test_samconfig.py @@ -751,7 +751,7 @@ def test_logs(self, do_cli_mock, experimental_mock): "end_time": "endtime", "region": "myregion", } - experimental_mock.return_value = False + experimental_mock.return_value = True with samconfig_parameters(["logs"], self.scratch_dir, **config_values) as config_path: from samcli.commands.logs.command import cli @@ -775,7 +775,7 @@ def test_logs(self, do_cli_mock, experimental_mock): "starttime", "endtime", (), - False, + None, "myregion", None, ) @@ -817,7 +817,7 @@ def test_logs_tail(self, do_cli_mock, experimental_mock): "starttime", "endtime", ("cw_log_group",), - False, + None, "myregion", None, ) diff --git a/tests/unit/commands/traces/test_command.py b/tests/unit/commands/traces/test_command.py index 69d457eaa3..59bc6185b4 100644 --- a/tests/unit/commands/traces/test_command.py +++ b/tests/unit/commands/traces/test_command.py @@ -4,6 +4,7 @@ from parameterized import parameterized from samcli.commands.traces.command import do_cli +from samcli.lib.observability.util import OutputOption class TestTracesCommand(TestCase): @@ -12,12 +13,12 @@ def setUp(self): @parameterized.expand( [ - (None, None, None, False, None), - (["trace_id1", "trace_id2"], None, None, False, None), - (None, "start_time", None, False, None), - (None, "start_time", "end_time", False, None), - (None, None, None, True, None), - (None, None, None, True, "output_dir"), + (None, None, None, False, "text"), + (["trace_id1", "trace_id2"], None, None, False, "text"), + (None, "start_time", None, False, "text"), + (None, "start_time", "end_time", False, "text"), + (None, None, None, True, "text"), + (None, None, None, True, "json"), ] ) @patch("samcli.commands.logs.logs_context.parse_time") @@ -30,7 +31,7 @@ def test_traces_command( start_time, end_time, tail, - output_dir, + output, patched_generate_puller, patched_boto3, patched_get_boto_config_with_user_agent, @@ -49,7 +50,7 @@ def test_traces_command( given_puller = Mock() patched_generate_puller.return_value = given_puller - do_cli(trace_ids, start_time, end_time, tail, output_dir, self.region) + do_cli(trace_ids, start_time, end_time, tail, output, self.region) patched_parse_time.assert_has_calls( [ @@ -59,7 +60,7 @@ def test_traces_command( ) patched_get_boto_config_with_user_agent.assert_called_with(region_name=self.region) patched_boto3.assert_called_with("xray", config=given_boto_config) - patched_generate_puller.assert_called_with(given_xray_client, output_dir) + patched_generate_puller.assert_called_with(given_xray_client, OutputOption(output)) if trace_ids: given_puller.load_events.assert_called_with(trace_ids) diff --git a/tests/unit/commands/traces/test_traces_puller_factory.py b/tests/unit/commands/traces/test_traces_puller_factory.py index 10c0ad6c26..d840747bb3 100644 --- a/tests/unit/commands/traces/test_traces_puller_factory.py +++ b/tests/unit/commands/traces/test_traces_puller_factory.py @@ -5,30 +5,31 @@ from samcli.commands.traces.traces_puller_factory import ( generate_trace_puller, - generate_unformatted_xray_event_consumer, + generate_json_xray_event_consumer, generate_xray_event_console_consumer, ) +from samcli.lib.observability.util import OutputOption class TestGenerateTracePuller(TestCase): @parameterized.expand( [ - (False,), - (True,), + ("text",), + ("json",), ] ) @patch("samcli.commands.traces.traces_puller_factory.generate_xray_event_console_consumer") - @patch("samcli.commands.traces.traces_puller_factory.generate_unformatted_xray_event_consumer") + @patch("samcli.commands.traces.traces_puller_factory.generate_json_xray_event_consumer") @patch("samcli.commands.traces.traces_puller_factory.XRayTracePuller") @patch("samcli.commands.traces.traces_puller_factory.XRayServiceGraphPuller") @patch("samcli.commands.traces.traces_puller_factory.ObservabilityCombinedPuller") def test_generate_trace_puller( self, - unformatted, + output, patched_combine_puller, patched_xray_service_graph_puller, patched_xray_trace_puller, - patched_generate_unformatted_consumer, + patched_generate_json_consumer, patched_generate_console_consumer, ): given_xray_client = Mock() @@ -43,13 +44,13 @@ def test_generate_trace_puller( patched_generate_console_consumer.return_value = given_console_consumer given_file_consumer = Mock() - patched_generate_unformatted_consumer.return_value = given_file_consumer + patched_generate_json_consumer.return_value = given_file_consumer - actual_puller = generate_trace_puller(given_xray_client, unformatted) + actual_puller = generate_trace_puller(given_xray_client, OutputOption(output)) self.assertEqual(given_combine_puller, actual_puller) - if unformatted: - patched_generate_unformatted_consumer.assert_called_with() + if output == "json": + patched_generate_json_consumer.assert_called_with() patched_xray_trace_puller.assert_called_with(given_xray_client, given_file_consumer) else: patched_generate_console_consumer.assert_called_once() @@ -62,7 +63,7 @@ def test_generate_file_consumer(self, patched_consumer, patched_trace_json_mappe given_consumer = Mock() patched_consumer_decorator.return_value = given_consumer - actual_consumer = generate_unformatted_xray_event_consumer() + actual_consumer = generate_json_xray_event_consumer() self.assertEqual(given_consumer, actual_consumer) patched_trace_json_mapper.assert_called_once()