def test_file_empty(self):
        test_dir = tempfile.mkdtemp()
        file_name = Constants.CORE_STATE_FILE

        # test on empty file
        self.runtime.create_temp_file(test_dir, file_name, content=None)
        core_state_handler = CoreStateHandler(os.path.join(test_dir),
                                              self.json_file_handler)
        core_state_json = core_state_handler.read_file()
        self.assertTrue(core_state_json is None)
        self.assertFalse(
            hasattr(core_state_json, self.core_state_fields.number))

        # test on file with empty JSON
        self.runtime.create_temp_file(test_dir, file_name, "{}")
        core_state_json = core_state_handler.read_file()
        self.assertTrue(hasattr(core_state_json,
                                self.core_state_fields.number))
        self.assertTrue(hasattr(core_state_json,
                                self.core_state_fields.action))
        self.assertTrue(core_state_json.number is None)
        self.assertTrue(core_state_json.action is None)

        # Remove the directory after the test
        shutil.rmtree(test_dir)
 def test_success_file_read(self):
     core_state_handler = CoreStateHandler(
         os.path.join(os.path.pardir, "tests", "helpers"),
         self.json_file_handler)
     core_state_json = core_state_handler.read_file()
     self.assertTrue(core_state_json.number is not None)
     self.assertTrue(core_state_json.completed is not None)
     self.assertEqual(core_state_json.action, "Assessment")
     self.assertEqual(core_state_json.number, 1234)
 def test_file_does_not_exists(self):
     core_state_handler = CoreStateHandler(
         os.path.join(os.path.pardir, "tests", "helper"),
         self.json_file_handler)
     core_state_json = core_state_handler.read_file()
     self.assertTrue(core_state_handler.read_file() is None)
     self.assertFalse(
         hasattr(core_state_json, self.core_state_fields.number))
     self.assertFalse(
         hasattr(core_state_json, self.core_state_fields.last_heartbeat))
 def test_key_not_in_file(self):
     parent_key = self.core_state_fields.parent_key
     core_state_json = {parent_key: {"test_no": 1}}
     test_dir = tempfile.mkdtemp()
     file_name = Constants.CORE_STATE_FILE
     self.runtime.create_temp_file(test_dir, file_name, "{}")
     core_state_handler = CoreStateHandler(os.path.join(test_dir),
                                           self.json_file_handler)
     core_state_handler.read_file()
     seq_no = self.core_state_fields.number
     self.assertTrue(
         core_state_handler.json_file_handler.get_json_config_value_safely(
             core_state_json, seq_no, parent_key, False) is None)
     # Remove the directory after the test
     shutil.rmtree(test_dir)
Exemplo n.º 5
0
    def test_check_if_patch_completes_in_time(self):
        core_state_handler = CoreStateHandler(
            os.path.join(os.path.pardir, "tests", "helpers"),
            self.json_file_handler)
        # Unable to identify wait time
        time_for_prev_patch_to_complete = ""
        self.assertRaises(
            Exception,
            self.runtime_context_handler.check_if_patch_completes_in_time,
            time_for_prev_patch_to_complete, core_state_handler)

        # patch complete
        time_for_prev_patch_to_complete = datetime.datetime.utcnow(
        ) + datetime.timedelta(hours=0, minutes=3)
        self.assertTrue(
            self.runtime_context_handler.check_if_patch_completes_in_time(
                time_for_prev_patch_to_complete, "2019-07-20T12:12:14Z",
                core_state_handler))

        # patch still incomplete after wait
        time_for_prev_patch_to_complete = datetime.datetime.utcnow(
        ) + datetime.timedelta(hours=0, minutes=0, seconds=0.01)
        core_state_read_backup = CoreStateHandler.read_file
        CoreStateHandler.read_file = self.mock_read_core_state_operation_incomplete
        print(type(time_for_prev_patch_to_complete))
        self.assertFalse(
            self.runtime_context_handler.check_if_patch_completes_in_time(
                time_for_prev_patch_to_complete, "2019-07-20T12:12:14Z",
                core_state_handler))

        CoreStateHandler.read_file = core_state_read_backup
 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")
Exemplo n.º 7
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()
 def test_delete_file_success(self):
     # Create a temporary directory
     test_dir = tempfile.mkdtemp()
     file_name = Constants.CORE_STATE_FILE
     file_path = os.path.join(test_dir, file_name)
     # create a file
     self.runtime.create_temp_file(test_dir, file_name, content=None)
     # delete file
     core_state_handler = CoreStateHandler(test_dir, self.json_file_handler)
     self.utility.delete_file(core_state_handler.dir_path,
                              core_state_handler.file)
     self.assertFalse(os.path.exists(file_path))
     # Remove the directory after the test
     shutil.rmtree(test_dir)
 def test_delete_file_failure(self):
     # Create a temporary directory
     test_dir = tempfile.mkdtemp()
     file_name = Constants.EXT_STATE_FILE
     file_path = os.path.join(test_dir, file_name)
     # create a file
     self.runtime.create_temp_file(test_dir, file_name, content=None)
     # delete file
     core_state_handler = CoreStateHandler("test", self.json_file_handler)
     self.assertRaises(Exception, self.utility.delete_file,
                       core_state_handler.dir_path, core_state_handler.file)
     self.assertTrue(os.path.exists(file_path))
     # Remove the directory after the test
     shutil.rmtree(test_dir)
Exemplo n.º 10
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
 def test_file_exists(self):
     core_state_handler = CoreStateHandler(
         os.path.join(os.path.pardir, "tests", "helpers"),
         self.json_file_handler)
     self.assertTrue(core_state_handler.read_file() is not None)