From 64561c2c1c4c3c411df0c190d9e378f8c2fd4ffa Mon Sep 17 00:00:00 2001 From: Paul Mars Date: Tue, 19 Mar 2024 09:38:49 +0100 Subject: [PATCH] Test customizations on TestSuccessfulClassicRun --- internal/imagedefinition/README.rst | 3 +- internal/statemachine/classic_test.go | 46 +++++++++++++++++++ .../image_definitions/test_amd64.yaml | 12 +++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/internal/imagedefinition/README.rst b/internal/imagedefinition/README.rst index c94181dd..c45ea819 100644 --- a/internal/imagedefinition/README.rst +++ b/internal/imagedefinition/README.rst @@ -263,6 +263,7 @@ The following specification defines what is supported in the YAML: - - # Environment variables to set before executing the command + # Format: ENV=VALUE env: (optional) - - @@ -286,7 +287,7 @@ The following specification defines what is supported in the YAML: # The name of the group to create. name: # The GID to assign to this group. - gid: (optional) + id: (optional) # Set a custom fstab. The existing one (if any) will be truncated. fstab: (optional) - diff --git a/internal/statemachine/classic_test.go b/internal/statemachine/classic_test.go index cf45d608..a033ffdc 100644 --- a/internal/statemachine/classic_test.go +++ b/internal/statemachine/classic_test.go @@ -3168,6 +3168,52 @@ func TestSuccessfulClassicRun(t *testing.T) { t.Error("ubuntu2 user password should be expired") } + // test addGroup customization + groupFilePath := filepath.Join(mountDir, "etc", "group") + groupFile, err := os.Open(groupFilePath) + asserter.AssertErrNil(err, true) + defer groupFile.Close() + addGroupFeatureFound := false + addGroupLine := "" + + scanner = bufio.NewScanner(groupFile) + + for scanner.Scan() { + if strings.HasPrefix(scanner.Text(), "addgroupfeature") { + addGroupLine = scanner.Text() + addGroupFeatureFound = true + break + } + } + + if !addGroupFeatureFound { + t.Error("addgroupfeature user not created") + } + + gid := strings.Split(addGroupLine, ":")[2] + + if gid != "4321" { + t.Errorf("addgroupfeature did not create the group with the right gid. Got gid %s", gid) + } + + // test touch file customization + toucheFilePath := filepath.Join(mountDir, "etc", "touchfilefeature") + _, err = os.Stat(toucheFilePath) + if err != nil { + if os.IsNotExist(err) { + t.Errorf("File \"%s\" should exist, but does not", toucheFilePath) + } + } + + // test execute file customization + executeFilePath := filepath.Join(mountDir, "etc", "executefeature") + executeBytes, err := os.ReadFile(executeFilePath) + asserter.AssertErrNil(err, true) + if string(executeBytes) != "test" { + t.Errorf("Expected 'test' in %s, but got %s", executeFilePath, string(executeBytes)) + } + + // test grub conf grubCfg := filepath.Join(mountDir, "boot", "grub", "grub.cfg") _, err = os.Stat(grubCfg) if err != nil { diff --git a/internal/statemachine/testdata/image_definitions/test_amd64.yaml b/internal/statemachine/testdata/image_definitions/test_amd64.yaml index b8e92341..2b8ec327 100644 --- a/internal/statemachine/testdata/image_definitions/test_amd64.yaml +++ b/internal/statemachine/testdata/image_definitions/test_amd64.yaml @@ -30,10 +30,22 @@ customization: make-dirs: - path: /etc/foo/bar permissions: 0755 + touch-file: + - path: /etc/touchfilefeature + execute: + - path: /usr/bin/bash + args: + - "-c" + - "echo -n 'test' > $ENV_VAR_TEST" + env: + - "ENV_VAR_TEST=/etc/executefeature" add-user: - name: ubuntu2 password: ubuntu2 password-type: text + add-group: + - name: addgroupfeature + id: 4321 components: - main - universe