コード例 #1
0
 def test_get_env_settings(self):
     handler_env_file_path = os.path.join(os.path.pardir, "tests",
                                          "helpers")
     ext_env_handler = ExtEnvHandler(
         self.json_file_handler,
         handler_env_file_path=handler_env_file_path)
     process_handler = ProcessHandler(self.logger, self.env_layer,
                                      self.ext_output_status_handler)
     env_settings = process_handler.get_env_settings(ext_env_handler)
     self.assertTrue(env_settings is not None)
     self.assertEqual(
         env_settings.get(Constants.EnvSettingsFields.log_folder),
         "mockLog")
コード例 #2
0
 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")
コード例 #3
0
    def test_start_daemon(self):
        # setting mocks
        get_python_cmd_backup = ProcessHandler.get_python_cmd
        ProcessHandler.get_python_cmd = self.mock_get_python_cmd
        subprocess_popen_backup = subprocess.Popen

        # Initializing config env
        ext_config_settings_handler = ExtConfigSettingsHandler(
            self.logger, self.json_file_handler,
            os.path.join(os.path.pardir, "tests", "helpers"))
        seq_no = "1234"
        config_settings = ext_config_settings_handler.read_file(seq_no)
        handler_env_file_path = os.path.join(os.path.pardir, "tests",
                                             "helpers")
        ext_env_handler = ExtEnvHandler(
            self.json_file_handler,
            handler_env_file_path=handler_env_file_path)

        # process was not launched
        subprocess.Popen = self.mock_subprocess_popen_process_not_launched
        process_handler = ProcessHandler(self.logger, self.env_layer,
                                         self.ext_output_status_handler)
        process = process_handler.start_daemon(seq_no, config_settings,
                                               ext_env_handler)
        self.assertTrue(process is None)

        # process launched with no issues
        subprocess.Popen = self.mock_subprocess_popen_process_launched_with_no_issues
        process_handler = ProcessHandler(self.logger, self.env_layer,
                                         self.ext_output_status_handler)
        process = process_handler.start_daemon(seq_no, config_settings,
                                               ext_env_handler)
        self.assertTrue(process is not None)

        # process launched but is not running soon after
        subprocess.Popen = self.mock_subprocess_popen_process_not_running_after_launch
        process_handler = ProcessHandler(self.logger, self.env_layer,
                                         self.ext_output_status_handler)

        # todo mock auto assess run command the way core has, where individual commands and expected output for each
        # mock run command output for Auto Assess shell file permission change
        run_command_output_backup = process_handler.env_layer.run_command_output
        process_handler.env_layer.run_command_output = self.mock_run_command_to_set_auto_assess_shell_file_permission

        process = process_handler.start_daemon(seq_no, config_settings,
                                               ext_env_handler)
        self.assertTrue(process is None)

        # resetting mocks
        ProcessHandler.get_python_cmd = get_python_cmd_backup
        subprocess.Popen = subprocess_popen_backup
        process_handler.env_layer.run_command_output = run_command_output_backup
コード例 #4
0
 def test_get_public_config_settings(self):
     ext_config_settings_handler = ExtConfigSettingsHandler(
         self.logger, self.json_file_handler,
         os.path.join(os.path.pardir, "tests", "helpers"))
     seq_no = "1234"
     config_settings = ext_config_settings_handler.read_file(seq_no)
     process_handler = ProcessHandler(self.logger,
                                      self.ext_output_status_handler)
     public_config_settings = process_handler.get_public_config_settings(
         config_settings)
     self.assertTrue(public_config_settings is not None)
     self.assertEqual(
         public_config_settings.get(
             Constants.ConfigPublicSettingsFields.operation),
         "Installation")
     self.assertEqual(
         public_config_settings.get(
             Constants.ConfigPublicSettingsFields.maintenance_run_id),
         "2019-07-20T12:12:14Z")
コード例 #5
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()
コード例 #6
0
    def test_get_python_cmd(self):
        # setting mocks
        run_command_output_backup = EnvLayer.run_command_output
        process_handler = ProcessHandler(self.logger, self.env_layer,
                                         self.ext_output_status_handler)

        # testing for 'python' command
        EnvLayer.run_command_output = self.mock_run_command_output_for_python
        self.assertEqual(process_handler.get_python_cmd(), "python")

        # testing for 'python3' command
        EnvLayer.run_command_output = self.mock_run_command_output_for_python3
        self.assertEqual(process_handler.get_python_cmd(), "python3")

        # testing when python is not found on machine
        EnvLayer.run_command_output = self.mock_run_command_output_for_python_not_found
        self.assertEqual(process_handler.get_python_cmd(),
                         Constants.PYTHON_NOT_FOUND)

        # resetting mocks
        EnvLayer.run_command_output = run_command_output_backup
コード例 #7
0
    def test_start_daemon(self):
        # setting mocks
        get_python_cmd_backup = ProcessHandler.get_python_cmd
        ProcessHandler.get_python_cmd = self.mock_get_python_cmd
        subprocess_popen_backup = subprocess.Popen

        # Initializing config env
        ext_config_settings_handler = ExtConfigSettingsHandler(
            self.logger, self.json_file_handler,
            os.path.join(os.path.pardir, "tests", "helpers"))
        seq_no = "1234"
        config_settings = ext_config_settings_handler.read_file(seq_no)
        handler_env_file_path = os.path.join(os.path.pardir, "tests",
                                             "helpers")
        ext_env_handler = ExtEnvHandler(
            self.json_file_handler,
            handler_env_file_path=handler_env_file_path)

        # process was not launched
        subprocess.Popen = self.mock_subprocess_popen_process_not_launched
        process_handler = ProcessHandler(self.logger,
                                         self.ext_output_status_handler)
        process = process_handler.start_daemon(seq_no, config_settings,
                                               ext_env_handler)
        self.assertTrue(process is None)

        # process launched with no issues
        subprocess.Popen = self.mock_subprocess_popen_process_launched_with_no_issues
        process_handler = ProcessHandler(self.logger,
                                         self.ext_output_status_handler)
        process = process_handler.start_daemon(seq_no, config_settings,
                                               ext_env_handler)
        self.assertTrue(process is not None)

        # process launched but is not running soon after
        subprocess.Popen = self.mock_subprocess_popen_process_not_running_after_launch
        process_handler = ProcessHandler(self.logger,
                                         self.ext_output_status_handler)
        process = process_handler.start_daemon(seq_no, config_settings,
                                               ext_env_handler)
        self.assertTrue(process is None)

        # resetting mocks
        ProcessHandler.get_python_cmd = get_python_cmd_backup
        subprocess.Popen = subprocess_popen_backup
コード例 #8
0
    def test_kill_process(self):
        # setting mocks
        is_process_running_backup = ProcessHandler.is_process_running
        ProcessHandler.is_process_running = self.mock_is_process_running_to_return_true
        os_kill_backup = os.kill
        os.kill = self.mock_os_kill_to_raise_exception

        # error in terminating process
        pid = 123
        process_handler = ProcessHandler(self.logger, self.env_layer,
                                         self.ext_output_status_handler)
        self.assertRaises(OSError, process_handler.kill_process, pid)

        # reseting mocks
        ProcessHandler.is_process_running = is_process_running_backup
        os.kill = os_kill_backup
コード例 #9
0
    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