def split(self, path): """Split the kickstart given by path into elements.""" self._elements = None self._kickstart_path = path handler = makeVersion() ksparser = SplitKickstartParser(handler, valid_sections=VALID_SECTIONS_ANACONDA) try: result = ksparser.split(path) except KickstartParseError as e: raise SplitKickstartSectionParsingError(e) except KickstartError as e: raise SplitKickstartMissingIncludeError(e) log.info("split %s: %s", path, result) self._elements = result
def invalid_command_test(self): """Test invalid command or option in kickstart. Invalid command or command option in kickstart does not raise KickstartParseError because commands are not parsed in the filter. """ ks_content = """ network --device=ens3 --activate netork --device=ens5 --activate network --device=ens7 --activate network --devce=ens9 --activate """.strip() handler = makeVersion() ksparser = SplitKickstartParser(handler) result = ksparser.split_from_string(ks_content) self.assertEqual(len(result.all_elements), 4)
def split_kickstart_parser_test(self): """Test splitting and dumping of various kickstart samples.""" valid_sections = VALID_SECTIONS_ANACONDA handler = makeVersion() ksparser = SplitKickstartParser(handler, valid_sections) for kickstart_files, expected_output in self._kickstart_samples: self._split_kickstart_parser_test(ksparser, kickstart_files, expected_output)
def split_from_string_filename_test(self): """Test splitting kickstart supplied by string.""" valid_sections = VALID_SECTIONS_ANACONDA handler = makeVersion() ksparser = SplitKickstartParser(handler, valid_sections) kickstart_files, _output = self._kickstart_flat filename, content = kickstart_files[0] # Kickstart from string has "<MAIN>" as filename result = ksparser.split_from_string(content) for element in result.all_elements: self.assertEqual(element.filename, SplitKickstartParser.unknown_filename) # Or the value supplied by filename optional argument result = ksparser.split_from_string(content, filename=filename) for element in result.all_elements: self.assertEqual(element.filename, filename)
def raising_kickstarts_split_test(self): """Test of kickstarts expected to raise KickstartParseError.""" valid_sections = VALID_SECTIONS_ANACONDA handler = makeVersion() ksparser = SplitKickstartParser(handler, valid_sections) for kickstart_files in self._flat_kickstarts_raising: _filename, content = kickstart_files[0] self.assertRaises(KickstartParseError, ksparser.split_from_string, content)
def conflicting_commands_test(self): """Test conflicting commands in kickstart. Conflicting commands in kickstart do not raise KickstartParseError because commands are not parsed in the filter. """ ks_content = """ # Partitioning conflicts with autopart part /boot --fstype=xfs --onpart=vda1 part pv.100000 --size=18436 --ondisk=vda volgroup Vol00 --pesize=4096 pv.100000 logvol / --fstype=xfs --name=lv_root --vgname=Vol00 --size=15360 logvol /home --fstype=xfs --name=lv_home --vgname=Vol00 --size=1024 logvol swap --fstype=swap --name=lv_swap --vgname=Vol00 --size=2048 autopart --encrypted --passphrase=starost --type=lvm """.strip() handler = makeVersion() ksparser = SplitKickstartParser(handler) result = ksparser.split_from_string(ks_content) self.assertEqual(len(result.all_elements), 7)
def missing_include_test(self): """Test behaviour for missing kickstart include files.""" ks_content = """ network --device=ens3 %include missing_include.cfg """.strip() handler = makeVersion() ksparser = SplitKickstartParser(handler) # By default raises error self.assertRaises(KickstartError, ksparser.split_from_string, ks_content) # But can be configured not to ksparser = SplitKickstartParser(handler, missing_include_is_fatal=False) ksparser.split_from_string(ks_content)
def valid_sections_test(self): """Test setting of valid sections for the parser.""" valid_sections = VALID_SECTIONS_ANACONDA handler = makeVersion() ksparser = SplitKickstartParser(handler, valid_sections) kickstart_files, _output = self._kickstart_flat _filename, content = kickstart_files[0] returned_valid_sections = ksparser.valid_sections # valid_sections returns new list, not a reference to the internal object returned_valid_sections.append("%test") self.assertNotEqual(returned_valid_sections, ksparser.valid_sections) ksparser.valid_sections = ["%packages"] # Invalid section raises exception self.assertRaises(KickstartParseError, ksparser.split_from_string, content) # setting valid sections back to the original, the exception is gone ksparser.valid_sections = valid_sections ksparser.split_from_string(content)
def simple_split_kickstart_parser_test(self): """This test should demonstrate usage and output of the parser.""" ks_content = """ %pre echo PRE %end network --device=ens3 --activate network --device=ens4 --activate %addon pony --fly=True %end firewall --enabled %addon scorched --planet=Earth nuke %end %post --nochroot --interpreter /usr/bin/bash echo POST1 %end """.lstrip() element1 = ("pre", "%pre\necho PRE\n%end\n", 1) element2 = ("network", "network --device=ens3 --activate\n", 4) element3 = ("network", "network --device=ens4 --activate\n", 5) element4 = ("pony", "%addon pony --fly=True\n%end\n", 6) element5 = ("firewall", "firewall --enabled\n", 8) element6 = ("scorched", "%addon scorched --planet=Earth\nnuke\n%end\n", 9) element7 = ( "post", "%post --nochroot --interpreter /usr/bin/bash\necho POST1\n%end\n", 12) expected_result = [ element1, element2, element3, element4, element5, element6, element7 ] filename = "ks.test.simple.cfg" valid_sections = VALID_SECTIONS_ANACONDA handler = makeVersion() ksparser = SplitKickstartParser(handler, valid_sections) # Reading kickstart from file with open(filename, "w") as f: f.write(ks_content) result = ksparser.split(filename) os.remove(filename) for element, expected in zip(result.all_elements, expected_result): self.assertEqual(element.filename, filename) self.assertEqual((element.name, element.content, element.lineno), expected) self.assertEqual( result.get_kickstart_from_elements(result.all_elements), ks_content) # Reading kickstart from string filename = ksparser.unknown_filename result = ksparser.split_from_string(ks_content) for element, expected in zip(result.all_elements, expected_result): self.assertEqual(element.filename, filename) self.assertEqual((element.name, element.content, element.lineno), expected) self.assertEqual( result.get_kickstart_from_elements(result.all_elements), ks_content) # Reading kickstart from string supplying filename filename = "MY_FILENAME" result = ksparser.split_from_string(ks_content, filename=filename) for element, expected in zip(result.all_elements, expected_result): self.assertEqual(element.filename, filename) self.assertEqual((element.name, element.content, element.lineno), expected) # Dumping kickstart self.assertEqual( result.get_kickstart_from_elements(result.all_elements), ks_content)