def test_file_check_script_not_exists(self): """Test of missing 'check_script' file""" self.test_ini['check_script'] = "this_should_be_unexpected_file.txt" self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.assertRaises(MissingFileInContentError, lambda: list(self.xml_utils.prepare_sections()))
def test_missing_tag_solution_script(self): """Test of missing tag 'solution' - SystemExit should be raised""" self.test_ini.pop('solution', None) self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.assertRaises(MissingTagsIniFileError, lambda: list(self.xml_utils.prepare_sections()))
def test_file_solution_not_exists(self): """Test of missing 'solution' file - SystemExit should be raised""" self.test_ini['solution'] = "this_should_be_unexpected_file.txt" self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.assertRaises(MissingTagsIniFileError, lambda: list(self.xml_utils.prepare_sections()))
def test_xml_not_migrate_not_upgrade(self): test_ini = { 'content_title': 'Testing only migrate title', 'content_description': ' some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf', 'check_script': self.check_script, 'solution': self.test_solution, 'applies_to': 'test', 'requires': 'bash', 'binary_req': 'sed' } ini = {} old_settings = settings.UPGRADE_PATH migrate, upgrade = self._create_temporary_dir() ini[self.filename] = [] ini[self.filename].append(test_ini) xml_utils = XmlUtils(self.dirname, ini) xml_utils.prepare_sections() migrate_file = FileHelper.get_file_content(migrate, 'rb', method=True) tag = [ x.strip() for x in migrate_file if 'xccdf_preupg_rule_test_check_script' in x.strip() ] self.assertIsNotNone(tag) upgrade_file = FileHelper.get_file_content(upgrade, 'rb', method=True) tag = [ x.strip() for x in upgrade_file if 'xccdf_preupg_rule_test_check_script' in x.strip() ] self.assertIsNotNone(tag) self._delete_temporary_dir(migrate, upgrade) settings.UPGRADE_PATH = old_settings
def test_missing_tag_check_script(self): """Basic test for whole program""" self.test_ini.pop('check_script', None) self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.assertRaises(MissingTagsIniFileError, lambda: list(self.xml_utils.prepare_sections()))
def test_check_rpm(self): self.test_ini['requires'] = 'test_rpm' self.loaded_ini[self.filename] = [] self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dirname, self.loaded_ini) self.rule = self.xml_utils.prepare_sections() self.assertTrue(self._return_check('check_rpm_to "test_rpm" ""'))
def test_check_bin(self): self.test_ini['binary_req'] = 'cpf' self.loaded_ini[self.filename] = [] self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dirname, self.loaded_ini) self.rule = self.xml_utils.prepare_sections() self.assertTrue(self._return_check('check_rpm_to "" "cpf"'))
def test_applies_to(self): self.test_ini['applies_to'] = 'test_rpm' self.loaded_ini[self.filename] = self.test_ini self.xml_utils = XmlUtils(self.root_dir_name, self.dirname, self.loaded_ini) self.rule = self.xml_utils.prepare_sections() self.assertTrue(self._return_check('check_applies_to "test_rpm"'))
def test_check_rpm_bin(self): self.test_ini['binary_req'] = 'cpf' self.test_ini['requires'] = 'test_rpm' self.loaded_ini[self.filename] = self.test_ini self.xml_utils = XmlUtils(self.root_dir_name, self.dirname, self.loaded_ini) self.rule = self.xml_utils.prepare_sections() self.assertTrue(self._return_check('check_rpm_to "test_rpm" "cpf"'))
def test_xml_solution_type_html(self): self.loaded_ini[self.filename][0]['solution_type'] = "html" self.xml_utils = XmlUtils(self.dirname, self.loaded_ini) self.rule = self.xml_utils.prepare_sections() fix_text = [ x for x in self.rule if "<fixtext>_test_SOLUTION_MSG_HTML</fixtext>" in x ] self.assertTrue(fix_text)
def test_xml_solution_type_text(self): self.xml_utils = XmlUtils(self.root_dir_name, self.dirname, self.loaded_ini) self.rule = self.xml_utils.prepare_sections() fix_text = [ x for x in self.rule if "<fixtext>_test_SOLUTION_MSG</fixtext>" in x ] self.assertTrue(fix_text)
def test_secret_check_script(self): """Check occurrence of secret file for check script""" self.test_ini['check_script'] = '.minicheck' text = """#!/usr/bin/sh\necho 'ahojky'\n""" FileHelper.write_to_file( os.path.join(self.dir_name, self.check_script), "wb", text) self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.assertRaises(MissingFileInContentError, lambda: list(self.xml_utils.prepare_sections()))
def test_check_script_is_directory(self): """ Directory as input instead of regular file is incorrect input Tests issue #29 """ self.test_ini['check_script'] = '.' self.loaded_ini[self.filename].append(self.test_ini) self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.assertRaises(MissingTagsIniFileError, lambda: list(self.xml_utils.prepare_sections()))
def write_xml(self): """The function is used for storing a group.xml file""" self.find_all_ini() self.write_list_rules() xml_utils = XmlUtils(self.module_set_dir, self.dirname, self.loaded) self.rule = xml_utils.prepare_sections() file_name = os.path.join(self.dirname, "group.xml") try: FileHelper.write_to_file(file_name, "wb", ["%s" % item for item in self.rule]) except IOError as ior: raise IOError('Problem with writing to file %s.\nDetails: %s' % (file_name, ior.message))
def test_group_ini(self): """Basic test creation group.xml file""" self.xml_utils = XmlUtils(self.dir_name, self.loaded_ini) self.rule = self.xml_utils.prepare_sections() group_tag = [ x for x in self.rule if '<Group id="xccdf_preupg_group_test_group" selected="true">' in x ] self.assertTrue(group_tag) title_tag = [ x for x in self.rule if '<title>Testing content title</title>' in x ] self.assertTrue(title_tag)
def setUp(self): self.dirname = os.path.join("tests", "FOOBAR6_7" + settings.results_postfix, "test") if os.path.exists(self.dirname): shutil.rmtree(self.dirname) os.makedirs(self.dirname) self.filename = os.path.join(self.dirname, 'test.ini') self.rule = [] self.test_solution = "test_solution.txt" self.check_script = "check_script.sh" self.loaded_ini = {} test_ini = { 'content_title': 'Testing content title', 'content_description': ' some content description', 'author': 'test <*****@*****.**>', 'config_file': '/etc/named.conf', 'check_script': self.check_script, 'solution': self.test_solution, 'applies_to': 'test', 'requires': 'bash', 'binary_req': 'sed' } self.loaded_ini[self.filename] = [] self.loaded_ini[self.filename].append(test_ini) self.check_sh = """#!/bin/bash #END GENERATED SECTION #This is testing check script """ check_name = os.path.join(self.dirname, self.check_script) FileHelper.write_to_file(check_name, "wb", self.check_sh) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU) self.solution_text = """ A solution text for test suite" """ test_solution_name = os.path.join(self.dirname, self.test_solution) FileHelper.write_to_file(test_solution_name, "wb", self.solution_text) os.chmod(check_name, stat.S_IEXEC | stat.S_IRWXG | stat.S_IRWXU) self.xml_utils = XmlUtils(self.dirname, self.loaded_ini) self.rule = self.xml_utils.prepare_sections()