def test_create_file(self): test_dir = tempfile.mkdtemp() ext_state_handler = ExtStateHandler(test_dir, self.utility, self.json_file_handler) ext_state_handler.create_file(1, "Assessment", datetime.datetime.utcnow()) self.assertTrue( os.path.exists(os.path.join(test_dir, Constants.EXT_STATE_FILE))) self.utility.delete_file(ext_state_handler.dir_path, ext_state_handler.file) shutil.rmtree(test_dir)
def test_read_file_no_content(self): ext_state_handler = ExtStateHandler( os.path.join(os.path.pardir, "tests", "helper"), self.utility, self.json_file_handler) ext_state_values = ext_state_handler.read_file() self.assertTrue(ext_state_values is not None) self.assertTrue( ext_state_values.__getattribute__( self.ext_state_fields.ext_seq_number) is None) self.assertTrue( ext_state_values.__getattribute__( self.ext_state_fields.ext_seq_achieve_enable_by) is None) self.assertTrue( ext_state_values.__getattribute__( self.ext_state_fields.ext_seq_operation) is None)
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")
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_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 ext_state_handler = ExtStateHandler('test', self.utility, self.json_file_handler) self.assertRaises(Exception, self.utility.delete_file, ext_state_handler.dir_path, ext_state_handler.file) self.assertTrue(os.path.exists(file_path)) # Remove the directory after the test shutil.rmtree(test_dir)
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