Beispiel #1
0
    def toml_to_recipe_fail_test(self):
        """Test trying to convert a non-TOML string to a Recipe"""
        with self.assertRaises(TomlError):
            recipes.recipe_from_toml("This is not a TOML string\n")

        with self.assertRaises(recipes.RecipeError):
            recipes.recipe_from_toml('name = "a failed toml string"\n')
Beispiel #2
0
    def test_get_timezone_settings(self):
        """Test get_timezone_settings function"""
        blueprint_data = """name = "test-kernel"
description = "test recipe"
version = "0.0.1"
"""
        blueprint2_data = blueprint_data + """
[customizations.timezone]
timezone = "US/Samoa"
"""
        blueprint3_data = blueprint_data + """
[customizations.timezone]
ntpservers = ["0.north-america.pool.ntp.org", "1.north-america.pool.ntp.org"]
"""
        blueprint4_data = blueprint_data + """
[customizations.timezone]
timezone = "US/Samoa"
ntpservers = ["0.north-america.pool.ntp.org", "1.north-america.pool.ntp.org"]
"""
        recipe = recipe_from_toml(blueprint_data)
        self.assertEqual(get_timezone_settings(recipe), {})

        recipe = recipe_from_toml(blueprint2_data)
        self.assertEqual(get_timezone_settings(recipe), {"timezone": "US/Samoa"})

        recipe = recipe_from_toml(blueprint3_data)
        self.assertEqual(get_timezone_settings(recipe),
                         {"ntpservers": ["0.north-america.pool.ntp.org", "1.north-america.pool.ntp.org"]})

        recipe = recipe_from_toml(blueprint4_data)
        self.assertEqual(get_timezone_settings(recipe),
                         {"timezone": "US/Samoa",
                          "ntpservers": ["0.north-america.pool.ntp.org", "1.north-america.pool.ntp.org"]})
Beispiel #3
0
    def test_get_default_services(self):
        """Test get_default_services function"""
        blueprint_data = """name = "test-services"
description = "test recipe"
version = "0.0.1"

[customizations.services]
        """
        enable_services = """
enabled = ["sshd", "cockpit.socket", "httpd"]
        """
        disable_services = """
disabled = ["postfix", "telnetd"]
        """
        blueprint2_data = blueprint_data + enable_services
        blueprint3_data = blueprint_data + disable_services
        blueprint4_data = blueprint_data + enable_services + disable_services

        with self.assertRaises(RecipeError):
            recipe = recipe_from_toml(blueprint_data)

        recipe = recipe_from_toml(blueprint2_data)
        self.assertEqual(get_default_services(recipe), "services")

        recipe = recipe_from_toml(blueprint3_data)
        self.assertEqual(get_default_services(recipe), "services")

        recipe = recipe_from_toml(blueprint4_data)
        self.assertEqual(get_default_services(recipe), "services")
Beispiel #4
0
 def recipe_to_toml_test(self):
     """Test converting a Recipe object to a TOML string"""
     # In order to avoid problems from matching strings we convert to TOML and
     # then back so compare the Recipes.
     for (toml_str, _recipe_dict) in self.input_toml:
         # This is tested in toml_to_recipe
         recipe_1 = recipes.recipe_from_toml(toml_str)
         # Convert the Recipe to TOML and then back to a Recipe
         toml_2 = recipe_1.toml()
         recipe_2 = recipes.recipe_from_toml(toml_2)
         self.assertEqual(recipe_1, recipe_2)
Beispiel #5
0
    def test_get_kernel_append(self):
        """Test get_kernel_append function"""
        blueprint_data = """name = "test-kernel"
description = "test recipe"
version = "0.0.1"
"""
        blueprint2_data = blueprint_data + """
[customizations.kernel]
append="nosmt=force"
"""
        recipe = recipe_from_toml(blueprint_data)
        self.assertEqual(get_kernel_append(recipe), "")

        recipe = recipe_from_toml(blueprint2_data)
        self.assertEqual(get_kernel_append(recipe), "nosmt=force")
Beispiel #6
0
    def test_get_firewall_settings(self):
        """Test get_firewall_settings function"""
        blueprint_data = """name = "test-firewall"
description = "test recipe"
version = "0.0.1"
        """
        firewall_ports = """
[customizations.firewall]
ports = ["22:tcp", "80:tcp", "imap:tcp", "53:tcp", "53:udp"]
"""
        firewall_services = """
[customizations.firewall.services]
enabled = ["ftp", "ntp", "dhcp"]
disabled = ["telnet"]
"""
        blueprint2_data = blueprint_data + firewall_ports
        blueprint3_data = blueprint_data + firewall_services
        blueprint4_data = blueprint_data + firewall_ports + firewall_services

        recipe = recipe_from_toml(blueprint_data)
        self.assertEqual(get_firewall_settings(recipe), {
            'ports': [],
            'enabled': [],
            'disabled': []
        })

        recipe = recipe_from_toml(blueprint2_data)
        self.assertEqual(
            get_firewall_settings(recipe), {
                "ports": ["22:tcp", "80:tcp", "imap:tcp", "53:tcp", "53:udp"],
                "enabled": [],
                "disabled": []
            })

        recipe = recipe_from_toml(blueprint3_data)
        self.assertEqual(get_firewall_settings(recipe), {
            "ports": [],
            "enabled": ["ftp", "ntp", "dhcp"],
            "disabled": ["telnet"]
        })

        recipe = recipe_from_toml(blueprint4_data)
        self.assertEqual(
            get_firewall_settings(recipe), {
                "ports": ["22:tcp", "80:tcp", "imap:tcp", "53:tcp", "53:udp"],
                "enabled": ["ftp", "ntp", "dhcp"],
                "disabled": ["telnet"]
            })
Beispiel #7
0
def workspace_read(repo, branch, recipe_name):
    """Read a Recipe from the branch's workspace

    :param repo: Open repository
    :type repo: Git.Repository
    :param branch: Branch name
    :type branch: str
    :param recipe_name: The name of the recipe
    :type recipe_name: str
    :returns: The workspace copy of the recipe, or None if it doesn't exist
    :rtype: Recipe or None
    :raises: RecipeFileError
    """
    ws_dir = workspace_dir(repo, branch)
    if not os.path.isdir(ws_dir):
        os.makedirs(ws_dir)
    filename = joinpaths(ws_dir, recipe_filename(recipe_name))
    if not os.path.exists(filename):
        return None
    try:
        f = open(filename, 'rb')
        recipe = recipe_from_toml(f.read().decode("UTF-8"))
    except IOError:
        raise RecipeFileError
    return recipe
Beispiel #8
0
    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, [])
Beispiel #9
0
    def recipe_freeze_test(self):
        """Test the recipe freeze() function"""
        # Use the repos-git.toml test, it only has http and php in it
        deps = [{
            "arch": "x86_64",
            "epoch": 0,
            "name": "httpd",
            "release": "1.el7",
            "version": "2.4.11"
        }, {
            "arch": "x86_64",
            "epoch": 0,
            "name": "php",
            "release": "1.el7",
            "version": "5.4.2"
        }]
        result = recipes.recipe_from_toml(self.input_toml["repos-git.toml"][0])
        self.assertEqual(result, self.input_toml["repos-git.toml"][1])

        # Freeze the recipe with our fake deps
        frozen = result.freeze(deps)
        self.assertTrue(frozen is not None)
        http_module = recipes.find_name("httpd", frozen["modules"])
        self.assertTrue(http_module is not None)
        self.assertEqual(http_module["version"], "2.4.11-1.el7.x86_64")

        php_module = recipes.find_name("php", frozen["modules"])
        self.assertTrue(php_module is not None)
        self.assertEqual(php_module["version"], "5.4.2-1.el7.x86_64")
Beispiel #10
0
    def setUpClass(self):
        self.repo_dir = tempfile.mkdtemp(prefix="lorax.test.repo.")
        self.repo = recipes.open_or_create_repo(self.repo_dir)

        self.results_path = "./tests/pylorax/results/"
        self.examples_path = "./tests/pylorax/blueprints/"

        recipe_path = joinpaths(self.examples_path, "http-server.toml")
        f = open(recipe_path, 'rb')
        self.example_recipe = recipes.recipe_from_toml(f.read())
Beispiel #11
0
    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:
            add_customizations(f, recipe_obj)
            f.flush()
            ks.readKickstart(f.name)

        return ks
Beispiel #12
0
    def test_get_services(self):
        """Test get_services function"""
        blueprint_data = """name = "test-services"
description = "test recipe"
version = "0.0.1"
[customizations.services]
        """
        enable_services = """
enabled = ["sshd", "cockpit.socket", "httpd"]
        """
        disable_services = """
disabled = ["postfix", "telnetd"]
        """
        blueprint2_data = blueprint_data + enable_services
        blueprint3_data = blueprint_data + disable_services
        blueprint4_data = blueprint_data + enable_services + disable_services

        recipe = recipe_from_toml(blueprint_data)
        self.assertEqual(get_services(recipe), {'enabled': [], 'disabled': []})

        recipe = recipe_from_toml(blueprint2_data)
        self.assertEqual(get_services(recipe), {
            "enabled": ["cockpit.socket", "httpd", "sshd"],
            "disabled": []
        })

        recipe = recipe_from_toml(blueprint3_data)
        self.assertEqual(get_services(recipe), {
            "enabled": [],
            "disabled": ["postfix", "telnetd"]
        })

        recipe = recipe_from_toml(blueprint4_data)
        self.assertEqual(
            get_services(recipe), {
                "enabled": ["cockpit.socket", "httpd", "sshd"],
                "disabled": ["postfix", "telnetd"]
            })
Beispiel #13
0
    def test_get_keyboard_layout(self):
        """Test get_keyboard_layout function"""
        blueprint_data = """name = "test-locale"
description = "test recipe"
version = "0.0.1"
        """
        blueprint2_data = blueprint_data + """
[customizations.locale]
keyboard = "de (dvorak)"
"""
        blueprint3_data = blueprint_data + """
[customizations.locale]
keyboard = "de (dvorak)"
languages = ["en_CA.utf8", "en_HK.utf8"]
"""
        recipe = recipe_from_toml(blueprint_data)
        self.assertEqual(get_keyboard_layout(recipe), [])

        recipe = recipe_from_toml(blueprint2_data)
        self.assertEqual(get_keyboard_layout(recipe), "de (dvorak)")

        recipe = recipe_from_toml(blueprint3_data)
        self.assertEqual(get_keyboard_layout(recipe), "de (dvorak)")
Beispiel #14
0
    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)
Beispiel #15
0
 def test_create_user_all(self):
     """Test creating user with all settings"""
     r = recipe_from_toml(USER_ALL)
     f = StringIO()
     add_customizations(f, r)
     self.assertEqual(KS_USER_ALL, f.getvalue())
Beispiel #16
0
 def assertNotCustomization(self, test, result):
     r = recipe_from_toml(test)
     f = StringIO()
     add_customizations(f, r)
     self.assertTrue(result not in f.getvalue(), f.getvalue())
Beispiel #17
0
 def toml_to_recipe_test(self):
     """Test converting the TOML string to a Recipe object"""
     for (toml_str, recipe_dict) in self.input_toml:
         result = recipes.recipe_from_toml(toml_str)
         self.assertEqual(result, recipe_dict)
Beispiel #18
0
    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, [])