From 8e650d1d074e830886c7875d138d68d2f8a3a78d Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Tue, 11 Jun 2019 16:33:58 -0700 Subject: [PATCH] Update for differences from py3 in the backported code In py3 dicts are not predictable, make sure the keys are sorted so that the tests are useful. Use StringIO.StringIO instead of io.StringIO which requires unicode text. kickstart timezone.ntpservers is a set() so adjust the test for it. Related: rhbz#1718473 --- src/composer/cli/blueprints.py | 2 +- src/pylorax/api/compose.py | 6 +++--- tests/pylorax/test_compose.py | 14 ++++++++------ tests/pylorax/test_recipes.py | 4 ++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/composer/cli/blueprints.py b/src/composer/cli/blueprints.py index dc374872..f34ae7a3 100644 --- a/src/composer/cli/blueprints.py +++ b/src/composer/cli/blueprints.py @@ -202,7 +202,7 @@ def pretty_dict(d): key="str", key="str1,str2", ... """ result = [] - for k in d: + for k in sorted(d.keys()): if type(d[k]) == type(""): result.append('%s="%s"' % (k, d[k])) elif type(d[k]) == type([]) and type(d[k][0]) == type(""): diff --git a/src/pylorax/api/compose.py b/src/pylorax/api/compose.py index 7ca5f9ba..35f3282f 100644 --- a/src/pylorax/api/compose.py +++ b/src/pylorax/api/compose.py @@ -36,7 +36,7 @@ log = logging.getLogger("lorax-composer") import os from glob import glob -from io import StringIO +from StringIO import StringIO from math import ceil import pytoml as toml import shutil @@ -448,7 +448,7 @@ def customize_ks_template(ks_template, recipe): output = StringIO() for line in ks_template.splitlines(): - for cmd in commands: + for cmd in sorted(commands.keys()): (new_command, value, default, replace) = commands[cmd] if line.startswith(cmd): found[cmd] = True @@ -466,7 +466,7 @@ def customize_ks_template(ks_template, recipe): # Write out defaults for the ones not found # These must go FIRST because the template still needs to have the packages added defaults = StringIO() - for cmd in commands: + for cmd in sorted(commands.keys()): if cmd in found: continue (new_command, value, default, _) = commands[cmd] diff --git a/tests/pylorax/test_compose.py b/tests/pylorax/test_compose.py index 34b53b30..e493b957 100644 --- a/tests/pylorax/test_compose.py +++ b/tests/pylorax/test_compose.py @@ -636,7 +636,7 @@ version = "*" 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(self._checkTimezone(result, {"timezone": "UTC", "ntpservers": []}, line_limit=5)) self.assertTrue("services" not in result) # Make sure that a kickstart with a bootloader, and no timezone has timezone added to the top @@ -645,7 +645,7 @@ version = "*" 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(self._checkTimezone(result, {"timezone": "UTC", "ntpservers": []}, line_limit=5)) self.assertTrue("services" not in result) # Make sure that a kickstart with a bootloader and timezone has neither added @@ -690,13 +690,14 @@ disabled = ["postfix", "telnetd"] # Test against a kickstart without bootloader result = customize_ks_template("firewall --enabled\n", recipe) + print(result) 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.assertTrue(self._checkTimezone(result, tz_dict, line_limit=5)) 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.assertTrue(self._checkKeyboard(result, "de (dvorak)", line_limit=3)) 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"], @@ -704,13 +705,14 @@ disabled = ["postfix", "telnetd"] 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)) + line_limit=6)) 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) + print(result) self.assertTrue(self._checkBootloader(result, "nosmt=force")) - self.assertTrue(self._checkTimezone(result, tz_dict, line_limit=2)) + self.assertTrue(self._checkTimezone(result, tz_dict, line_limit=5)) # Test against all of the available templates share_dir = "./share/" diff --git a/tests/pylorax/test_recipes.py b/tests/pylorax/test_recipes.py index b0a33048..d484330e 100644 --- a/tests/pylorax/test_recipes.py +++ b/tests/pylorax/test_recipes.py @@ -804,7 +804,7 @@ ntpservers = ["1.north-america.pool.ntp.org"] """ ks = self._blueprint_to_ks(blueprint_data) self.assertEqual(ks.handler.timezone.timezone, "US/Samoa") - self.assertEqual(ks.handler.timezone.ntpservers, ["1.north-america.pool.ntp.org"]) + self.assertEqual(ks.handler.timezone.ntpservers, set(["1.north-america.pool.ntp.org"])) def test_locale_languages(self): blueprint_data = """name = "test-locale" @@ -1078,4 +1078,4 @@ ntpservers = ["0.north-america.pool.ntp.org", "1.north-america.pool.ntp.org"] self.assertEqual(studentsGroup.name, "students") self.assertEqual(ks.handler.timezone.timezone, "US/Samoa") - self.assertEqual(ks.handler.timezone.ntpservers, ["0.north-america.pool.ntp.org", "1.north-america.pool.ntp.org"]) + self.assertEqual(ks.handler.timezone.ntpservers, set(["0.north-america.pool.ntp.org", "1.north-america.pool.ntp.org"]))