def test_customize_ks_template(self): """Test that [customizations.kernel] works correctly""" blueprint_data = """name = "test-kernel" description = "test recipe" version = "0.0.1" [customizations.kernel] append="nosmt=force" """ recipe = recipe_from_toml(blueprint_data) # Test against a kickstart without bootloader result = customize_ks_template("firewall --enabled\n", recipe) self.assertTrue(self._checkBootloader(result, "nosmt=force")) # Test against all of the available templates share_dir = "./share/" errors = [] for compose_type in compose_types(share_dir): # Read the kickstart template for this type ks_template_path = joinpaths(share_dir, "composer", compose_type) + ".ks" ks_template = open(ks_template_path, "r").read() result = customize_ks_template(ks_template, recipe) if not self._checkBootloader(result, "nosmt=force"): errors.append( ("compose_type %s failed" % compose_type, result)) # Print the bad results for e, r in errors: print("%s:\n%s\n\n" % (e, r)) self.assertEqual(errors, [])
def _blueprint_to_ks(blueprint_data): recipe_obj = recipes.recipe_from_toml(blueprint_data) ks = KickstartParser(makeVersion()) # write out the customization data, and parse the resulting kickstart with tempfile.NamedTemporaryFile(prefix="lorax.test.customizations", mode="w") as f: f.write(customize_ks_template("", recipe_obj)) add_customizations(f, recipe_obj) f.flush() ks.readKickstart(f.name) return ks
def test_template_defaults(self): """Test that customize_ks_template includes defaults correctly""" blueprint_data = """name = "test-kernel" description = "test recipe" version = "0.0.1" [[packages]] name = "lorax" version = "*" """ recipe = recipe_from_toml(blueprint_data) # Make sure that a kickstart with no bootloader and no timezone has them added result = customize_ks_template("firewall --enabled\n", recipe) print(result) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("bootloader")]), 1) self.assertTrue(self._checkBootloader(result, "none", line_limit=2)) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("timezone")]), 1) self.assertTrue(self._checkTimezone(result, {"timezone": "UTC", "ntpservers": []}, line_limit=2)) self.assertTrue("services" not in result) # Make sure that a kickstart with a bootloader, and no timezone has timezone added to the top result = customize_ks_template("firewall --enabled\nbootloader --location=mbr\n", recipe) print(result) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("bootloader")]), 1) self.assertTrue(self._checkBootloader(result, "mbr")) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("timezone")]), 1) self.assertTrue(self._checkTimezone(result, {"timezone": "UTC", "ntpservers": []}, line_limit=1)) self.assertTrue("services" not in result) # Make sure that a kickstart with a bootloader and timezone has neither added result = customize_ks_template("firewall --enabled\nbootloader --location=mbr\ntimezone US/Samoa\n", recipe) print(result) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("bootloader")]), 1) self.assertTrue(self._checkBootloader(result, "mbr")) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("timezone")]), 1) self.assertTrue(self._checkTimezone(result, {"timezone": "US/Samoa", "ntpservers": []})) self.assertTrue("services" not in result)
def test_customize_ks_template(self): """Test that customize_ks_template works correctly""" blueprint_data = """name = "test-kernel" description = "test recipe" version = "0.0.1" [customizations.kernel] append="nosmt=force" [customizations.timezone] timezone = "US/Samoa" ntpservers = ["0.north-america.pool.ntp.org", "1.north-america.pool.ntp.org"] [customizations.locale] keyboard = "de (dvorak)" languages = ["en_CA.utf8", "en_HK.utf8"] [customizations.firewall] ports = ["22:tcp", "80:tcp", "imap:tcp", "53:tcp", "53:udp"] [customizations.firewall.services] enabled = ["ftp", "ntp", "dhcp"] disabled = ["telnet"] [customizations.services] enabled = ["sshd", "cockpit.socket", "httpd"] disabled = ["postfix", "telnetd"] """ tz_dict = {"timezone": "US/Samoa", "ntpservers": ["0.north-america.pool.ntp.org", "1.north-america.pool.ntp.org"]} recipe = recipe_from_toml(blueprint_data) # Test against a kickstart without bootloader result = customize_ks_template("firewall --enabled\n", recipe) self.assertTrue(self._checkBootloader(result, "nosmt=force", line_limit=2)) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("bootloader")]), 1) self.assertTrue(self._checkTimezone(result, tz_dict, line_limit=2)) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("timezone")]), 1) self.assertTrue(self._checkLang(result, ["en_CA.utf8", "en_HK.utf8"], line_limit=4)) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("lang")]), 1) self.assertTrue(self._checkKeyboard(result, "de (dvorak)", line_limit=4)) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("keyboard")]), 1) self.assertTrue(self._checkFirewall(result, {"ports": ["22:tcp", "80:tcp", "imap:tcp", "53:tcp", "53:udp"], "enabled": ["ftp", "ntp", "dhcp"], "disabled": ["telnet"]}, line_limit=6)) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("firewall")]), 1) self.assertTrue(self._checkServices(result, {"enabled": ["cockpit.socket", "httpd", "sshd"], "disabled": ["postfix", "telnetd"]}, line_limit=8)) self.assertEqual(sum([1 for l in result.splitlines() if l.startswith("services")]), 1) # Test against a kickstart with a bootloader line result = customize_ks_template("firewall --enabled\nbootloader --location=mbr\n", recipe) self.assertTrue(self._checkBootloader(result, "nosmt=force")) self.assertTrue(self._checkTimezone(result, tz_dict, line_limit=2)) # Test against all of the available templates share_dir = "./share/" errors = [] for compose_type, _enabled in compose_types(share_dir): # Read the kickstart template for this type ks_template_path = joinpaths(share_dir, "composer", compose_type) + ".ks" ks_template = open(ks_template_path, "r").read() result = customize_ks_template(ks_template, recipe) if not self._checkBootloader(result, "nosmt=force"): errors.append(("bootloader for compose_type %s failed" % compose_type, result)) if sum([1 for l in result.splitlines() if l.startswith("bootloader")]) != 1: errors.append(("bootloader for compose_type %s failed: More than 1 entry" % compose_type, result)) # google images should retain their timezone settings if compose_type == "google": if self._checkTimezone(result, tz_dict): errors.append(("timezone for compose_type %s failed" % compose_type, result)) elif not self._checkTimezone(result, tz_dict, line_limit=2): # None of the templates have a timezone to modify, it should be placed at the top errors.append(("timezone for compose_type %s failed" % compose_type, result)) if sum([1 for l in result.splitlines() if l.startswith("timezone")]) != 1: errors.append(("timezone for compose_type %s failed: More than 1 entry" % compose_type, result)) if not self._checkLang(result, ["en_CA.utf8", "en_HK.utf8"]): errors.append(("lang for compose_type %s failed" % compose_type, result)) if sum([1 for l in result.splitlines() if l.startswith("lang")]) != 1: errors.append(("lang for compose_type %s failed: More than 1 entry" % compose_type, result)) if not self._checkKeyboard(result, "de (dvorak)"): errors.append(("keyboard for compose_type %s failed" % compose_type, result)) if sum([1 for l in result.splitlines() if l.startswith("keyboard")]) != 1: errors.append(("keyboard for compose_type %s failed: More than 1 entry" % compose_type, result)) # google and openstack templates requires the firewall to be disabled if compose_type == "google" or compose_type == "openstack": if not self._checkFirewall(result, {'ports': [], 'enabled': [], 'disabled': []}): errors.append(("firewall for compose_type %s failed" % compose_type, result)) else: if not self._checkFirewall(result, {"ports": ["22:tcp", "80:tcp", "imap:tcp", "53:tcp", "53:udp"], "enabled": ["ftp", "ntp", "dhcp"], "disabled": ["telnet"]}): errors.append(("firewall for compose_type %s failed" % compose_type, result)) if sum([1 for l in result.splitlines() if l.startswith("firewall")]) != 1: errors.append(("firewall for compose_type %s failed: More than 1 entry" % compose_type, result)) if not self._checkServices(result, {"enabled": ["cockpit.socket", "httpd", "sshd"], "disabled": ["postfix", "telnetd"]}): errors.append(("services for compose_type %s failed" % compose_type, result)) if sum([1 for l in result.splitlines() if l.startswith("services")]) != 1: errors.append(("services for compose_type %s failed: More than 1 entry" % compose_type, result)) # Print the bad results for e, r in errors: print("%s:\n%s\n\n" % (e, r)) self.assertEqual(errors, [])