Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quadlet -dryrun arg #16926

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions cmd/quadlet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
verboseFlag bool // True if -v passed
noKmsgFlag bool
isUser bool // True if run as quadlet-user-generator executable
dryRun bool // True if -dryrun is used
)

var (
Expand Down Expand Up @@ -291,27 +292,32 @@ func warnIfAmbigiousName(container *parser.UnitFile) {
}

func main() {
exitCode := 0
prgname := path.Base(os.Args[0])
isUser = strings.Contains(prgname, "user")

flag.Parse()

if verboseFlag {
if verboseFlag || dryRun {
enableDebug()
}

if noKmsgFlag {
if noKmsgFlag || dryRun {
noKmsg = true
}

if flag.NArg() < 1 {
if !dryRun && flag.NArg() < 1 {
Logf("Missing output directory argument")
os.Exit(1)
}

outputPath := flag.Arg(0)
var outputPath string

Debugf("Starting quadlet-generator, output to: %s", outputPath)
if !dryRun {
outputPath = flag.Arg(0)

Debugf("Starting quadlet-generator, output to: %s", outputPath)
}

sourcePaths := getUnitDirs(isUser)

Expand All @@ -320,12 +326,19 @@ func main() {
loadUnitsFromDir(d, units)
}

err := os.MkdirAll(outputPath, os.ModePerm)
if err != nil {
Logf("Can't create dir %s: %s", outputPath, err)
if len(units) == 0 {
Debugf("No files to parse from %s", sourcePaths)
os.Exit(1)
}

if !dryRun {
err := os.MkdirAll(outputPath, os.ModePerm)
if err != nil {
Logf("Can't create dir %s: %s", outputPath, err)
os.Exit(1)
}
}

for name, unit := range units {
var service *parser.UnitFile
var err error
Expand All @@ -350,16 +363,29 @@ func main() {
} else {
service.Path = path.Join(outputPath, service.Filename)

if err := generateServiceFile(service); err != nil {
Logf("Error writing '%s'o: %s", service.Path, err)
if dryRun {
data, err := service.ToString()
if err != nil {
Debugf("Error parsing %s\n---\n", service.Path)
exitCode = 1
} else {
fmt.Printf("---%s---\n%s\n", service.Path, data)
}
} else {
if err := generateServiceFile(service); err != nil {
Logf("Error writing '%s'o: %s", service.Path, err)
}
enableServiceFile(outputPath, service)
}
enableServiceFile(outputPath, service)
}
}

os.Exit(exitCode)
}

func init() {
flag.BoolVar(&verboseFlag, "v", false, "Print debug information")
flag.BoolVar(&noKmsgFlag, "no-kmsg-log", false, "Don't log to kmsg")
flag.BoolVar(&isUser, "user", false, "Run as systemd user")
flag.BoolVar(&dryRun, "dryrun", false, "run in dryrun mode printing debug information")
}
67 changes: 67 additions & 0 deletions test/e2e/quadlet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,73 @@ var _ = Describe("quadlet system generator", func() {

})

Describe("Running quadlet dryrun tests", func() {
It("Should exit with an error because of no files are found to parse", func() {
fileName := "basic.kube"
testcase := loadQuadletTestcase(filepath.Join("quadlet", fileName))

// Write the tested file to the quadlet dir
err = os.WriteFile(filepath.Join(quadletDir, fileName), testcase.data, 0644)
Expect(err).ToNot(HaveOccurred())

session := podmanTest.Quadlet([]string{"-dryrun"}, "/something")
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(1))

current := session.ErrorToStringArray()
expected := "No files to parse from [/something]"

Expect(strings.Contains(current[0], expected)).To(BeTrue())
})

It("Should parse a kube file and print it to stdout", func() {
fileName := "basic.kube"
testcase := loadQuadletTestcase(filepath.Join("quadlet", fileName))

// Write the tested file to the quadlet dir
err = os.WriteFile(filepath.Join(quadletDir, fileName), testcase.data, 0644)
Expect(err).ToNot(HaveOccurred())

session := podmanTest.Quadlet([]string{"-dryrun"}, quadletDir)
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

current := session.OutputToStringArray()
expected := []string{
"---basic.service---",
"## assert-podman-args \"kube\"",
"## assert-podman-args \"play\"",
"## assert-podman-final-args-regex /tmp/podman_test.*/quadlet/deployment.yml",
"## assert-podman-args \"--replace\"",
"## assert-podman-args \"--service-container=true\"",
"## assert-podman-stop-args \"kube\"",
"## assert-podman-stop-args \"down\"",
"## assert-podman-stop-final-args-regex /tmp/podman_test.*/quadlet/deployment.yml",
"## assert-key-is \"Unit\" \"RequiresMountsFor\" \"%t/containers\"",
"## assert-key-is \"Service\" \"KillMode\" \"mixed\"",
"## assert-key-is \"Service\" \"Type\" \"notify\"",
"## assert-key-is \"Service\" \"NotifyAccess\" \"all\"",
"## assert-key-is \"Service\" \"Environment\" \"PODMAN_SYSTEMD_UNIT=%n\"",
"## assert-key-is \"Service\" \"SyslogIdentifier\" \"%N\"",
"[X-Kube]",
"Yaml=deployment.yml",
"[Unit]",
fmt.Sprintf("SourcePath=%s/basic.kube", quadletDir),
"RequiresMountsFor=%t/containers",
"[Service]",
"KillMode=mixed",
"Environment=PODMAN_SYSTEMD_UNIT=%n",
"Type=notify",
"NotifyAccess=all",
"SyslogIdentifier=%N",
fmt.Sprintf("ExecStart=/usr/local/bin/podman kube play --replace --service-container=true %s/deployment.yml", quadletDir),
fmt.Sprintf("ExecStop=/usr/local/bin/podman kube down %s/deployment.yml", quadletDir),
}

Expect(expected).To(Equal(current))
})
})

DescribeTable("Running quadlet test case",
func(fileName string) {
testcase := loadQuadletTestcase(filepath.Join("quadlet", fileName))
Expand Down