def test_validate_environment(self): config_type = 'handlerEnvironment' # file has no content ext_env_handler = ExtEnvHandler(self.json_file_handler) install_command_handler = InstallCommandHandler(self.logger, ext_env_handler) self.assertRaises(Exception, install_command_handler.validate_environment) # Validating datatype for fields in HandlerEnvironment handler_environment = [] handler_environment_dict = {} handler_environment.append(handler_environment_dict) install_command_handler = InstallCommandHandler(self.logger, handler_environment) self.verify_key(handler_environment[0], 'version', 1.0, 'abc', True, Exception, install_command_handler.validate_environment) self.verify_key(handler_environment[0], 'version', 1.0, '', True, Exception, install_command_handler.validate_environment) self.verify_key(handler_environment[0], 'handlerEnvironment', {}, 'abc', True, Exception, install_command_handler.validate_environment) self.verify_key(handler_environment[0][config_type], 'logFolder', 'test', 1.0, True, Exception, install_command_handler.validate_environment) self.verify_key(handler_environment[0][config_type], 'configFolder', 'test', 1.0, True, Exception, install_command_handler.validate_environment) self.verify_key(handler_environment[0][config_type], 'statusFolder', 'test', 1.0, True, Exception, install_command_handler.validate_environment) # Validating HandlerEnvironment.json file # reseting mock to original func def self.json_file_handler.get_json_file_content = self.get_json_file_content_backup ext_env_handler = ExtEnvHandler(self.json_file_handler, handler_env_file_path=os.path.join(os.path.pardir, "tests", "helpers")) install_command_handler = InstallCommandHandler(self.logger, ext_env_handler) install_command_handler.validate_environment()
def test_execute_action_handler(self): sys.platform = 'linux' # reseting mock to original func def self.json_file_handler.get_json_file_content = self.get_json_file_content_backup ext_env_handler = ExtEnvHandler(self.json_file_handler, handler_env_file_path=os.path.join(os.path.pardir, "tests", "helpers")) install_command_handler = InstallCommandHandler(self.logger, ext_env_handler) self.assertEqual(install_command_handler.execute_handler_action(), Constants.ExitCode.Okay)
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 test_file_read_success(self): ext_env_handler = ExtEnvHandler(self.json_file_handler, handler_env_file_path=os.path.join( os.path.pardir, "tests", "helpers")) self.assertTrue(ext_env_handler.log_folder is not None) self.assertEqual(ext_env_handler.log_folder, "mockLog") self.assertTrue(ext_env_handler.status_folder is not None)
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_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
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")
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
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_read_event_folder_preview(self): ext_env_settings = [{ Constants.EnvSettingsFields.version: "1.0", Constants.EnvSettingsFields.settings_parent_key: { Constants.EnvSettingsFields.log_folder: "testLog", Constants.EnvSettingsFields.config_folder: "testConfig", Constants.EnvSettingsFields.status_folder: "testStatus", Constants.EnvSettingsFields.events_folder_preview: "testEventsPreview" } }] test_dir = tempfile.mkdtemp() file_name = Constants.HANDLER_ENVIRONMENT_FILE self.runtime.create_temp_file(test_dir, file_name, content=json.dumps(ext_env_settings)) ext_env_handler = ExtEnvHandler(self.json_file_handler, handler_env_file_path=test_dir) self.assertTrue(ext_env_handler.log_folder is not None) self.assertEqual(ext_env_handler.events_folder, "testEventsPreview") shutil.rmtree(test_dir)
def test_validate_os_type_not_linux(self): ext_env_handler = ExtEnvHandler(self.json_file_handler) install_command_handler = InstallCommandHandler(self.logger, ext_env_handler) sys.platform = 'win32' self.assertRaises(Exception, install_command_handler.validate_os_type)
def test_validate_os_type_is_linux(self): ext_env_handler = ExtEnvHandler(self.json_file_handler) install_command_handler = InstallCommandHandler(self.logger, ext_env_handler) sys.platform = 'linux' self.assertTrue(install_command_handler.validate_os_type())