Esempio n. 1
0
    def test_read_file(self):
        file_name = "test"
        dir_path = tempfile.mkdtemp()
        operation = "Assessment"

        ext_output_status_handler = ExtOutputStatusHandler(self.logger, self.utility, self.json_file_handler, dir_path)
        ext_output_status_handler.write_status_file(operation, file_name, self.status.Transitioning.lower())
        status_json = ext_output_status_handler.read_file(file_name)
        parent_key = self.status_file_fields.status
        self.assertEqual(status_json[0][parent_key][self.status_file_fields.status_name], "Azure Patch Management")
        self.assertEqual(status_json[0][parent_key][self.status_file_fields.status_operation], operation)
        self.assertEqual(status_json[0][parent_key][self.status_file_fields.status_status], self.status.Transitioning.lower())
        shutil.rmtree(dir_path)
Esempio n. 2
0
    def test_create_status_file(self):
        file_name = "test"
        dir_path = tempfile.mkdtemp()
        operation = "Assessment"
        ext_status_handler = ExtOutputStatusHandler(self.logger, self.utility, self.json_file_handler, dir_path)
        ext_status_handler.write_status_file(operation, file_name, self.status.Transitioning.lower())

        with open(dir_path + "\\" + file_name + ext_status_handler.file_ext) as status_file:
            content = json.load(status_file)
            parent_key = self.status_file_fields.status
            self.assertTrue(content is not None)
            self.assertEqual(content[0][parent_key][self.status_file_fields.status_name], "Azure Patch Management")
            self.assertEqual(content[0][parent_key][self.status_file_fields.status_operation], operation)
            self.assertEqual(content[0][parent_key][self.status_file_fields.status_status], self.status.Transitioning.lower())
        shutil.rmtree(dir_path)
 def setUp(self):
     VirtualTerminal().print_lowlight(
         "\n----------------- setup test runner -----------------")
     self.runtime = RuntimeComposer()
     runtime_context_handler = RuntimeContextHandler(self.runtime.logger)
     ext_env_handler = ExtEnvHandler(self.runtime.json_file_handler,
                                     handler_env_file_path=os.path.join(
                                         os.path.pardir, "tests",
                                         "helpers"))
     ext_config_settings_handler = ExtConfigSettingsHandler(
         self.runtime.logger, self.runtime.json_file_handler,
         ext_env_handler.config_folder)
     core_state_handler = CoreStateHandler(ext_env_handler.config_folder,
                                           self.runtime.json_file_handler)
     ext_state_handler = ExtStateHandler(ext_env_handler.config_folder,
                                         self.runtime.utility,
                                         self.runtime.json_file_handler)
     ext_output_status_handler = ExtOutputStatusHandler(
         self.runtime.logger, self.runtime.utility,
         self.runtime.json_file_handler, ext_env_handler.status_folder)
     process_handler = ProcessHandler(self.runtime.logger,
                                      ext_output_status_handler)
     self.action_handler = ActionHandler(
         self.runtime.logger, self.runtime.utility, runtime_context_handler,
         self.runtime.json_file_handler, ext_env_handler,
         ext_config_settings_handler, core_state_handler, ext_state_handler,
         ext_output_status_handler, process_handler,
         "2020-09-02T13:40:54.8862542Z")
Esempio n. 4
0
def main(argv):
    stdout_file_mirror = None
    file_logger = None
    env_layer = EnvLayer()
    logger = Logger()
    telemetry_writer = TelemetryWriter(logger, env_layer)
    logger.telemetry_writer = telemetry_writer  # Need to set telemetry_writer within logger to enable sending all logs to telemetry
    try:
        # initializing action handler
        # args will have values install, uninstall, etc, as given in MsftLinuxPatchExtShim.sh in the operation var
        cmd_exec_start_time = datetime.datetime.utcnow()
        utility = Utility(logger)
        runtime_context_handler = RuntimeContextHandler(logger)
        json_file_handler = JsonFileHandler(logger)
        ext_env_handler = ExtEnvHandler(json_file_handler)
        env_health_manager = EnvHealthManager(env_layer)
        if ext_env_handler.handler_environment_json is not None and ext_env_handler.config_folder is not None:
            config_folder = ext_env_handler.config_folder
            if config_folder is None or not os.path.exists(config_folder):
                logger.log_error("Config folder not found at [{0}].".format(
                    repr(config_folder)))
                exit(Constants.ExitCode.MissingConfig)

            ext_config_settings_handler = ExtConfigSettingsHandler(
                logger, json_file_handler, config_folder)
            core_state_handler = CoreStateHandler(config_folder,
                                                  json_file_handler)
            ext_state_handler = ExtStateHandler(config_folder, utility,
                                                json_file_handler)
            ext_output_status_handler = ExtOutputStatusHandler(
                logger, utility, json_file_handler,
                ext_env_handler.status_folder)
            process_handler = ProcessHandler(logger, env_layer,
                                             ext_output_status_handler)
            action_handler = ActionHandler(
                logger, env_layer, telemetry_writer, utility,
                runtime_context_handler, json_file_handler, env_health_manager,
                ext_env_handler, ext_config_settings_handler,
                core_state_handler, ext_state_handler,
                ext_output_status_handler, process_handler,
                cmd_exec_start_time)
            action_handler.determine_operation(argv[1])
        else:
            error_cause = "No configuration provided in HandlerEnvironment" if ext_env_handler.handler_environment_json is None else "Path to config folder not specified in HandlerEnvironment"
            error_msg = "Error processing file. [File={0}] [Error={1}]".format(
                Constants.HANDLER_ENVIRONMENT_FILE, error_cause)
            raise Exception(error_msg)
    except Exception as error:
        logger.log_error(repr(error))
        return Constants.ExitCode.HandlerFailed
    finally:
        if stdout_file_mirror is not None:
            stdout_file_mirror.stop()
        if file_logger is not None:
            file_logger.close()
Esempio n. 5
0
 def setUp(self):
     VirtualTerminal().print_lowlight(
         "\n----------------- setup test runner -----------------")
     runtime = RuntimeComposer()
     self.logger = runtime.logger
     self.utility = runtime.utility
     self.json_file_handler = runtime.json_file_handler
     seq_no = 1234
     dir_path = os.path.join(os.path.pardir, "tests", "helpers")
     self.ext_output_status_handler = ExtOutputStatusHandler(
         self.logger, self.utility, self.json_file_handler, dir_path)
     self.process = subprocess.Popen(["echo", "Hello World!"],
                                     shell=True,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE)
Esempio n. 6
0
    def test_update_file(self):
        file_name = "test"
        dir_path = tempfile.mkdtemp()
        operation = "Assessment"

        ext_status_handler = ExtOutputStatusHandler(self.logger, self.utility, self.json_file_handler, dir_path)
        ext_status_handler.write_status_file(operation, file_name, self.status.Success.lower())
        stat_file_name = os.stat(os.path.join(dir_path, file_name + ".status"))
        prev_modified_time = stat_file_name.st_mtime

        ext_status_handler.update_file("test1", dir_path)
        stat_file_name = os.stat(os.path.join(dir_path, file_name + ".status"))
        modified_time = stat_file_name.st_mtime
        self.assertEqual(prev_modified_time, modified_time)

        ext_status_handler.update_file(file_name, dir_path)
        stat_file_name = os.stat(os.path.join(dir_path, file_name + ".status"))
        modified_time = stat_file_name.st_mtime
        self.assertNotEqual(prev_modified_time, modified_time)
        updated_status_json = ext_status_handler.read_file(file_name)
        self.assertEqual(updated_status_json[0][self.status_file_fields.status][self.status_file_fields.status_status], self.status.Transitioning.lower())
        shutil.rmtree(dir_path)
    def setUp(self):
        VirtualTerminal().print_lowlight(
            "\n----------------- setup test runner -----------------")
        self.temp_dir = tempfile.mkdtemp()

        self.runtime = RuntimeComposer()
        runtime_context_handler = RuntimeContextHandler(self.runtime.logger)
        self.ext_env_handler = ExtEnvHandler(
            self.runtime.json_file_handler,
            handler_env_file_path=os.path.join(os.path.pardir, "tests",
                                               "helpers"))
        self.setup_files_and_folders(self.temp_dir)

        self.ext_config_settings_handler = ExtConfigSettingsHandler(
            self.runtime.logger, self.runtime.json_file_handler,
            self.ext_env_handler.config_folder)
        core_state_handler = CoreStateHandler(
            self.ext_env_handler.config_folder, self.runtime.json_file_handler)
        ext_state_handler = ExtStateHandler(self.ext_env_handler.config_folder,
                                            self.runtime.utility,
                                            self.runtime.json_file_handler)
        ext_output_status_handler = ExtOutputStatusHandler(
            self.runtime.logger, self.runtime.utility,
            self.runtime.json_file_handler, self.ext_env_handler.status_folder)
        process_handler = ProcessHandler(self.runtime.logger,
                                         self.runtime.env_layer,
                                         ext_output_status_handler)
        self.action_handler = ActionHandler(
            self.runtime.logger, self.runtime.env_layer,
            self.runtime.telemetry_writer, self.runtime.utility,
            runtime_context_handler, self.runtime.json_file_handler,
            self.runtime.env_health_manager, self.ext_env_handler,
            self.ext_config_settings_handler, core_state_handler,
            ext_state_handler, ext_output_status_handler, process_handler,
            datetime.datetime.utcnow())

        self.backup_get_seq_no_from_env_var = self.ext_config_settings_handler.get_seq_no_from_env_var
        self.ext_config_settings_handler.get_seq_no_from_env_var = self.mock_get_seq_no_from_env_var

        self.backup_mock_os_path_realpath = os.path.realpath
        os.path.realpath = self.mock_os_path_realpath
Esempio n. 8
0
    def test_add_error_to_status(self):
        file_name = "test"
        dir_path = tempfile.mkdtemp()
        ext_output_status_handler = ExtOutputStatusHandler(self.logger, self.utility, self.json_file_handler, dir_path)
        ext_output_status_handler.set_current_operation(Constants.NOOPERATION)
        self.logger.file_logger = FileLogger(dir_path, "test.log")
        ext_output_status_handler.read_file(file_name)
        # Unexpected input
        self.assertTrue(ext_output_status_handler.add_error_to_status(None) is None)

        ext_output_status_handler.add_error_to_status("Adding test exception", Constants.PatchOperationErrorCodes.DEFAULT_ERROR)
        ext_output_status_handler.set_nooperation_substatus_json(Constants.NOOPERATION, activity_id="", start_time="", seq_no=file_name, status=self.status.Success.lower())
        updated_status_json = ext_output_status_handler.read_file(file_name)
        self.assertEqual(updated_status_json[0][self.status_file_fields.status][self.status_file_fields.status_substatus][0][self.status_file_fields.status_name], Constants.PATCH_NOOPERATION_SUMMARY)
        self.assertNotEqual(json.loads(updated_status_json[0]["status"]["substatus"][0]["formattedMessage"]["message"])["errors"], None)
        self.assertEqual(json.loads(updated_status_json[0]["status"]["substatus"][0]["formattedMessage"]["message"])["errors"]["code"], 1)
        self.assertEqual(json.loads(updated_status_json[0]["status"]["substatus"][0]["formattedMessage"]["message"])["errors"]["details"][0]["code"], Constants.PatchOperationErrorCodes.DEFAULT_ERROR)

        ext_output_status_handler.add_error_to_status("exception1", Constants.PatchOperationErrorCodes.DEFAULT_ERROR)
        ext_output_status_handler.add_error_to_status("exception2", Constants.PatchOperationErrorCodes.DEFAULT_ERROR)
        ext_output_status_handler.add_error_to_status("exception3", Constants.PatchOperationErrorCodes.DEFAULT_ERROR)
        ext_output_status_handler.add_error_to_status("exception4", Constants.PatchOperationErrorCodes.DEFAULT_ERROR)
        ext_output_status_handler.add_error_to_status("exception5", Constants.PatchOperationErrorCodes.DEFAULT_ERROR)
        ext_output_status_handler.add_error_to_status("exception6", Constants.PatchOperationErrorCodes.DEFAULT_ERROR)
        ext_output_status_handler.set_nooperation_substatus_json(Constants.NOOPERATION, activity_id="", start_time="", seq_no=file_name, status=self.status.Success.lower())
        updated_status_json = ext_output_status_handler.read_file(file_name)
        self.assertEqual(updated_status_json[0][self.status_file_fields.status][self.status_file_fields.status_substatus][0][self.status_file_fields.status_name], Constants.PATCH_NOOPERATION_SUMMARY)
        self.assertNotEqual(json.loads(updated_status_json[0]["status"]["substatus"][0]["formattedMessage"]["message"])["errors"], None)
        self.assertEqual(json.loads(updated_status_json[0]["status"]["substatus"][0]["formattedMessage"]["message"])["errors"]["code"], 1)
        self.assertEqual(len(json.loads(updated_status_json[0]["status"]["substatus"][0]["formattedMessage"]["message"])["errors"]["details"]), 5)

        self.logger.file_logger.close()
        shutil.rmtree(dir_path)