From dbf40562cc9fc28ae172c199b99a6412299f0adc Mon Sep 17 00:00:00 2001 From: larohra <41490930+larohra@users.noreply.github.com> Date: Mon, 14 Mar 2022 14:31:58 -0700 Subject: [PATCH 1/2] Add telemetry for environment variables being passed to extensions --- azurelinuxagent/ga/exthandlers.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/azurelinuxagent/ga/exthandlers.py b/azurelinuxagent/ga/exthandlers.py index 635352863d..cc9d0afc32 100644 --- a/azurelinuxagent/ga/exthandlers.py +++ b/azurelinuxagent/ga/exthandlers.py @@ -1902,12 +1902,15 @@ def launch_command(self, cmd, cmd_name=None, timeout=300, extension_error_code=E if supported_features: env[ExtCommandEnvVariable.ExtensionSupportedFeatures] = json.dumps(supported_features) + ext_name = self.get_extension_full_name(extension) try: # Some extensions erroneously begin cmd with a slash; don't interpret those # as root-relative. (Issue #1170) command_full_path = os.path.join(base_dir, cmd.lstrip(os.path.sep)) - self.logger.info("Executing command: {0} with environment variables: {1}".format(command_full_path, - json.dumps(env))) + log_msg = "Executing command: {0} with environment variables: {1}".format(command_full_path, + json.dumps(env)) + self.logger.info(log_msg) + self.report_event(name=ext_name, message=log_msg, log_event=False) # Add the os environment variables before executing command env.update(os.environ) @@ -1928,7 +1931,6 @@ def launch_command(self, cmd, cmd_name=None, timeout=300, extension_error_code=E code=extension_error_code) duration = elapsed_milliseconds(begin_utc) - ext_name = self.get_extension_full_name(extension) log_msg = "Command: {0}\n{1}".format(cmd, "\n".join( [line for line in process_output.split('\n') if line != ""])) self.logger.info(log_msg) From 64af91b82c563d90432e3c186b5bccd42de52b33 Mon Sep 17 00:00:00 2001 From: larohra <41490930+larohra@users.noreply.github.com> Date: Mon, 14 Mar 2022 17:05:59 -0700 Subject: [PATCH 2/2] Add telemetry for environment variables being passed to extensions --- tests/ga/test_extension.py | 16 ++++++++-------- tests/ga/test_multi_config_extension.py | 11 ++++++++--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/tests/ga/test_extension.py b/tests/ga/test_extension.py index 5967031293..7ec539541b 100644 --- a/tests/ga/test_extension.py +++ b/tests/ga/test_extension.py @@ -2336,8 +2336,8 @@ def test_ext_sequence_no_should_be_set_from_within_extension(self, *args): exthandlers_handler.report_ext_handlers_status() for _, kwargs in mock_report_event.call_args_list: - # The output is of the format - 'testfile.sh\n[stdout]ConfigSequenceNumber=N\n[stderr]' - if test_file_name not in kwargs['message']: + # The output is of the format - 'Command: testfile.sh -{Operation} \n[stdout]ConfigSequenceNumber=N\n[stderr]' + if ("Command: " + test_file_name) not in kwargs['message']: continue self.assertIn("{0}={1}".format(ExtCommandEnvVariable.ExtensionSeqNumber, expected_seq_no), kwargs['message']) @@ -2405,13 +2405,13 @@ def test_correct_exit_code_should_be_set_on_uninstall_cmd_failure(self, *args): with patch.object(ExtHandlerInstance, 'report_event') as mock_report_event: exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() - exthandlers_handler.report_ext_handlers_status() - _, disable_kwargs = mock_report_event.call_args_list[1] # pylint: disable=unused-variable - _, update_kwargs = mock_report_event.call_args_list[2] - _, uninstall_kwargs = mock_report_event.call_args_list[3] # pylint: disable=unused-variable - _, install_kwargs = mock_report_event.call_args_list[4] - _, enable_kwargs = mock_report_event.call_args_list[5] + update_kwargs = next(kwargs for _, kwargs in mock_report_event.call_args_list if + "Command: testfile.sh -update" in kwargs['message']) + install_kwargs = next(kwargs for _, kwargs in mock_report_event.call_args_list if + "Command: testfile.sh -install" in kwargs['message']) + enable_kwargs = next(kwargs for _, kwargs in mock_report_event.call_args_list if + "Command: testfile.sh -enable" in kwargs['message']) self.assertIn("%s=%s" % (ExtCommandEnvVariable.DisableReturnCode, exit_code), update_kwargs['message']) self.assertIn("%s=%s" % (ExtCommandEnvVariable.UninstallReturnCode, exit_code), install_kwargs['message']) diff --git a/tests/ga/test_multi_config_extension.py b/tests/ga/test_multi_config_extension.py index e424afd4bf..a9a07bd67e 100644 --- a/tests/ga/test_multi_config_extension.py +++ b/tests/ga/test_multi_config_extension.py @@ -1,6 +1,7 @@ import contextlib import json import os.path +import re import subprocess import uuid @@ -887,15 +888,19 @@ def test_it_should_ignore_disable_errors_for_multi_config_extensions(self): (sc_ext, ExtensionCommandNames.ENABLE) ) + reported_events = [kwargs for _, kwargs in patch_report_event.call_args_list if + re.search("Executing command: (.+) with environment variables: ", + kwargs['message']) is None] + self.assertTrue(all( - fail_code in kwargs['message'] for args, kwargs in patch_report_event.call_args_list if + fail_code in kwargs['message'] for kwargs in reported_events if kwargs['name'] == first_ext.name), "Error not reported") self.assertTrue(all( - fail_code in kwargs['message'] for args, kwargs in patch_report_event.call_args_list if + fail_code in kwargs['message'] for kwargs in reported_events if kwargs['name'] == second_ext.name), "Error not reported") # Make sure fail code is not reported for any other extension self.assertFalse(all( - fail_code in kwargs['message'] for args, kwargs in patch_report_event.call_args_list if + fail_code in kwargs['message'] for kwargs in reported_events if kwargs['name'] == third_ext.name), "Error not reported") def test_it_should_report_transitioning_if_status_file_not_found(self):