def test_platform_tag(self): shutil.copyfile(self.content, self.test_content) rp = ReportParser(self.test_content) rp.modify_platform_tag("12") found = 0 for platform in rp.get_nodes(rp.target_tree, "platform"): if "cpe:/o:redhat:enterprise_linux:12" in platform.get('idref'): found = 1 self.assertTrue(found)
def scan_system(self): """The function is used for scanning system with all steps.""" self._set_devel_mode() if not self.is_module_set_valid(): return ReturnValues.SCENARIO ret_val = self.prepare_scan_system() if ret_val != 0: return ret_val # Update source XML file in temporary directory self.openscap_helper.update_variables(self.conf.assessment_results_dir, self.conf.result_prefix, self.conf.xml_result_name, self.conf.html_result_name, self.all_xccdf_xml_copy_path) try: self.report_parser = ReportParser(self.all_xccdf_xml_copy_path) except IOError: log_message("Error: Unable to open {0}.".format( self.all_xccdf_xml_copy_path)) return ReturnValues.SCENARIO if self.conf.mode: lines = [ i.rstrip() for i in FileHelper.get_file_content(os.path.join( self.module_set_copy_path, self.conf.mode), 'rb', method=True) ] self.report_parser.select_rules(lines) if self.conf.select_rules: lines = [i.strip() for i in self.conf.select_rules.split(',')] unknown_rules = self.report_parser.check_rules(lines) if unknown_rules: log_message(settings.unknown_rules % '\n'.join(unknown_rules)) self.report_parser.select_rules(lines) self.run_scan_process() main_report = self.scanning_progress.get_output_data() self.prepare_xml_for_html() self.generate_html_or_text() self.update_xml_after_html_generated() self.copy_postupgrade_files() self.copy_preupgrade_scripts(self.module_set_copy_path) ConfigFilesHelper.copy_modified_config_files( settings.assessment_results_dir) # It prints out result in table format ScanningHelper.format_rules_to_table(main_report, "main contents") self.tar_ball_name = TarballHelper.tarball_result_dir( self.conf.tarball_name, self.conf.verbose) log_message("The tarball with results is stored in '%s' ." % self.tar_ball_name) log_message("The latest assessment is stored in the '%s' directory." % self.conf.assessment_results_dir) # pack all configuration files to tarball return 0
def test_result_dirs_tmp_preupgrade(self): shutil.copyfile(self.content, self.test_content) rp = ReportParser(self.test_content) result_path = "/abc/def" rp.modify_result_path(result_path, "FOOBAR6_7", 'migrate') found_tmp = 0 for values in rp.get_nodes(rp.target_tree, "Value", prefix='./'): if values.get("id").endswith("_tmp_preupgrade"): for value in rp.get_nodes(values, "value"): if value.text == result_path: found_tmp = 1 self.assertEquals(found_tmp, 1)
def test_result_dirs_current_dir(self): shutil.copyfile(self.content, self.test_content) rp = ReportParser(self.test_content) result_path = "/abc/efg" scenario = 'FOOBAR6_7' rp.modify_result_path(result_path, scenario, 'migrate') found_current = 0 for values in rp.get_nodes(rp.target_tree, "Value", ".//"): if values.get("id").endswith("_preupg_state_current_directory"): for value in rp.get_nodes(values, "value"): result_dir = result_path+"/"+scenario+"/dummy_preupg" if value.text == result_dir: found_current = 1 self.assertEquals(found_current, 1)
def test_upgrade(self): """Basic test for whole program""" content = "tests/generated_results/all-xccdf-upgrade.xml" args = ["--contents", content, "--mode", "upgrade"] a = setup_preupg_environment(args, content, self.temp_dir, mode='upgrade') self.assertEqual(a.run_scan(), 0) rp = ReportParser(os.path.join(self.temp_dir, "result.xml")) rp.modify_result_path(self.temp_dir, "FOOBAR6_7", 'upgrade') for values in rp.get_nodes(rp.target_tree, "Value", ".//"): if values.get("id").endswith("_state_migrate"): for value in rp.get_nodes(values, "value"): self.assertEqual(int(value.text), 0) if values.get("id").endswith("_state_upgrade"): for value in rp.get_nodes(values, "value"): self.assertEqual(int(value.text), 1)
def scan_system(self): """The function is used for scanning system with all steps.""" self._set_devel_mode() if int(self.prepare_scan_system()) != 0: return ReturnValues.SCENARIO if int(self.generate_report()) != 0: return ReturnValues.SCENARIO # Update source XML file in temporary directory self.content = os.path.join(self.assessment_dir, settings.content_file) self.openscap_helper.update_variables(self.conf.assessment_results_dir, self.conf.result_prefix, self.conf.xml_result_name, self.conf.html_result_name, self.content) try: self.report_parser = ReportParser(self.content) except IOError: log_message("The module {0} does not exist.".format(self.content)) return ReturnValues.SCENARIO if not self.conf.contents: version = SystemIdentification.get_assessment_version( self.conf.scan) if version is None: log_message("Your scan is in a wrong format %s." % version, level=logging.ERROR) log_message( "It should be like 'RHEL6_7' for upgrade from RHEL 6->7.", level=logging.ERROR) return ReturnValues.SCENARIO self.report_parser.modify_platform_tag(version[0]) if self.conf.mode: try: lines = [ i.rstrip() for i in FileHelper.get_file_content(os.path.join( self.assessment_dir, self.conf.mode), 'rb', method=True) ] except IOError: return self.report_parser.select_rules(lines) if self.conf.select_rules: lines = [i.strip() for i in self.conf.select_rules.split(',')] unknown_rules = self.report_parser.check_rules(lines) if unknown_rules: log_message(settings.unknown_rules % '\n'.join(unknown_rules)) self.report_parser.select_rules(lines) self.run_scan_process() main_report = self.scanning_progress.get_output_data() # This function prepare XML and generate HTML self.prepare_xml_for_html() third_party_dir_name = self.get_third_party_dir(self.assessment_dir) if os.path.exists(third_party_dir_name): self.run_third_party_modules(third_party_dir_name) self.copy_preupgrade_scripts(self.assessment_dir) ConfigFilesHelper.copy_modified_config_files( settings.assessment_results_dir) # It prints out result in table format ScanningHelper.format_rules_to_table(main_report, "main contents") for target, report in six.iteritems(self.report_data): ScanningHelper.format_rules_to_table(report, "3rdparty content " + target) self.tar_ball_name = TarballHelper.tarball_result_dir( self.conf.tarball_name, self.conf.assessment_results_dir, self.conf.verbose) log_message("The tarball with results is stored in '%s' ." % self.tar_ball_name) log_message("The latest assessment is stored in the '%s' directory." % self.conf.assessment_results_dir) # pack all configuration files to tarball return 0