diff --git a/src/pylorax/api/compose.py b/src/pylorax/api/compose.py index 5ed78c58..171651c9 100644 --- a/src/pylorax/api/compose.py +++ b/src/pylorax/api/compose.py @@ -182,15 +182,20 @@ def add_customizations(f, recipe): continue f.write('sshkey --user %s "%s"\n' % (sshkey["user"], sshkey["key"])) + # Creating a user also creates a group. Make a list of the names for later + user_groups = [] if "user" in customizations: # only name is required, everything else is optional for user in customizations["user"]: write_ks_user(f, user) + user_groups.append(user["name"]) if "group" in customizations: for group in customizations["group"]: - write_ks_group(f, group) - + if group["name"] not in user_groups: + write_ks_group(f, group) + else: + log.warning("Skipping group %s, already created by user", group["name"]) def start_build(cfg, dnflock, gitlock, branch, recipe_name, compose_type, test_mode=0): """ Start the build diff --git a/tests/pylorax/blueprints/custom-base.toml b/tests/pylorax/blueprints/custom-base.toml index e9a6c627..e8d5dc1d 100644 --- a/tests/pylorax/blueprints/custom-base.toml +++ b/tests/pylorax/blueprints/custom-base.toml @@ -26,7 +26,7 @@ description = "Widget admin account" password = "$6$CHO2$3rN8eviE2t50lmVyBYihTgVRHcaecmeCk31LeOUleVK/R/aeWVHVZDi26zAH.o0ywBKH9Tc0/wm7sW/q39uyd1" home = "/srv/widget/" shell = "/usr/bin/bash" -groups = ["widget", "users"] +groups = ["widget", "users", "students"] uid = 1200 [[customizations.user]] @@ -36,6 +36,10 @@ password = "simple plain password" [[customizations.user]] name = "bart" key = "SSH KEY FOR BART" +groups = ["students"] [[customizations.group]] name = "widget" + +[[customizations.group]] +name = "students" diff --git a/tests/pylorax/test_compose.py b/tests/pylorax/test_compose.py index 99b235ab..9b22de15 100644 --- a/tests/pylorax/test_compose.py +++ b/tests/pylorax/test_compose.py @@ -85,6 +85,10 @@ GROUP_GID = GROUP + """ gid = 1011 """ +USER_GROUP = USER + """[[customizations.group]] +name = "tester" +""" + KS_USER_ALL = '''sshkey --user tester "A SSH KEY FOR TESTER" user --name tester --homedir /opt/users/tester/ --iscrypted --password "$6$CHO2$3rN8eviE2t50lmVyBYihTgVRHcaecmeCk31LeOUleVK/R/aeWVHVZDi26zAH.o0ywBKH9Tc0/wm7sW/q39uyd1" --shell /usr/bin/zsh --uid 1013 --gid 4242 --gecos "a test user account" ''' @@ -96,6 +100,12 @@ class CustomizationsTestCase(unittest.TestCase): add_customizations(f, r) self.assertTrue(result in f.getvalue(), f.getvalue()) + 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()) + def test_set_hostname(self): """Test setting the hostname""" self.assertCustomization(HOSTNAME, "network --hostname=testhostname") @@ -144,6 +154,13 @@ class CustomizationsTestCase(unittest.TestCase): """Test creating user with group membership""" self.assertCustomization(USER + USER_GROUPS, "--groups wheel,users") + def test_user_same_group(self): + """Test creating a group with the same name as a user""" + + # Creating a group with the same name should skip the group creation + self.assertCustomization(USER_GROUP, "user --name tester") + self.assertNotCustomization(USER_GROUP, "group --name tester") + def test_create_user_all(self): """Test creating user with all settings""" r = recipe_from_toml(USER_ALL)