Skip to content

Commit

Permalink
quadlet: Add tests for templates
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Larsson <[email protected]>
  • Loading branch information
alexlarsson committed Jan 26, 2024
1 parent dc94a10 commit cd5982e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
11 changes: 11 additions & 0 deletions test/e2e/quadlet/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## assert-podman-final-args localhost/imagename
## assert-podman-args "--name=systemd-%P_%I"
## assert-symlink want.service.wants/[email protected] ../[email protected]
## assert-podman-args --env "FOO=bar"

[Container]
Image=localhost/imagename

[Install]
WantedBy=want.service
DefaultInstance=default
2 changes: 2 additions & 0 deletions test/e2e/quadlet/[email protected]/10-env.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Container]
Environment=FOO=bar
11 changes: 11 additions & 0 deletions test/e2e/quadlet/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## assert-podman-final-args localhost/changed-image
## assert-podman-args "--name=systemd-%P_%I"
## assert-symlink want.service.wants/[email protected] ../[email protected]
## assert-podman-args --env "FOO=bar"

[Container]
# Will be changed by /[email protected]/10-image.conf
Image=localhost/imagename

[Install]
WantedBy=want.service
2 changes: 2 additions & 0 deletions test/e2e/quadlet/[email protected]/10-image.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Container]
Image=localhost/changed-image
33 changes: 26 additions & 7 deletions test/e2e/quadlet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ type quadletTestcase struct {
checks [][]string
}

// Converts "[email protected]" to "[email protected]"
func getGenericTemplateFile(fileName string) (bool, string) {
extension := filepath.Ext(fileName)
base := strings.TrimSuffix(fileName, extension)
parts := strings.SplitN(base, "@", 2)
if len(parts) == 2 && len(parts[1]) > 0 {
return true, parts[0] + "@" + extension
}
return false, ""
}

func loadQuadletTestcase(path string) *quadletTestcase {
data, err := os.ReadFile(path)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -724,13 +735,19 @@ BOGUS=foo
Expect(err).ToNot(HaveOccurred())

// Also copy any extra snippets
dotdDir := filepath.Join("quadlet", fileName+".d")
if s, err := os.Stat(dotdDir); err == nil && s.IsDir() {
dotdDirDest := filepath.Join(quadletDir, fileName+".d")
err = os.Mkdir(dotdDirDest, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = CopyDirectory(dotdDir, dotdDirDest)
Expect(err).ToNot(HaveOccurred())
snippetdirs := []string{fileName + ".d"}
if ok, genericFileName := getGenericTemplateFile(fileName); ok {
snippetdirs = append(snippetdirs, genericFileName+".d")
}
for _, snippetdir := range snippetdirs {
dotdDir := filepath.Join("quadlet", snippetdir)
if s, err := os.Stat(dotdDir); err == nil && s.IsDir() {
dotdDirDest := filepath.Join(quadletDir, snippetdir)
err = os.Mkdir(dotdDirDest, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = CopyDirectory(dotdDir, dotdDirDest)
Expect(err).ToNot(HaveOccurred())
}
}

// Run quadlet to convert the file
Expand Down Expand Up @@ -825,6 +842,8 @@ BOGUS=foo
Entry("Container - Containers Conf Modules", "containersconfmodule.container", 0, ""),
Entry("merged.container", "merged.container", 0, ""),
Entry("merged-override.container", "merged-override.container", 0, ""),
Entry("[email protected]", "[email protected]", 0, ""),
Entry("[email protected]", "[email protected]", 0, ""),

Entry("basic.volume", "basic.volume", 0, ""),
Entry("device-copy.volume", "device-copy.volume", 0, ""),
Expand Down

0 comments on commit cd5982e

Please sign in to comment.