From 144e420bb9c62989c31d54a4f93635b0af40ada6 Mon Sep 17 00:00:00 2001 From: Brent Baude Date: Tue, 13 Feb 2024 10:56:47 -0600 Subject: [PATCH] Add testcase for WSL dist conflicts Signed-off-by: Brent Baude --- pkg/machine/e2e/config_test.go | 18 +++++++++++ pkg/machine/e2e/init_windows_test.go | 48 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/pkg/machine/e2e/config_test.go b/pkg/machine/e2e/config_test.go index 2dd56bc217..6c7466b829 100644 --- a/pkg/machine/e2e/config_test.go +++ b/pkg/machine/e2e/config_test.go @@ -235,3 +235,21 @@ func isVmtype(vmType define.VMType) bool { func isWSL() bool { return isVmtype(define.WSLVirt) } + +func runSystemCommand(binary string, cmdArgs []string, timeout time.Duration, wait bool) (*machineSession, error) { + if len(os.Getenv("DEBUG")) > 0 { + cmdArgs = append([]string{"--log-level=debug"}, cmdArgs...) + } + GinkgoWriter.Println(binary + " " + strings.Join(cmdArgs, " ")) + c := exec.Command(binary, cmdArgs...) + session, err := Start(c, GinkgoWriter, GinkgoWriter) + if err != nil { + Fail(fmt.Sprintf("Unable to start session: %q", err)) + return nil, err + } + ms := machineSession{session} + if wait { + ms.waitWithTimeout(timeout) + } + return &ms, nil +} diff --git a/pkg/machine/e2e/init_windows_test.go b/pkg/machine/e2e/init_windows_test.go index 196fa1af7d..4cfb36fe64 100644 --- a/pkg/machine/e2e/init_windows_test.go +++ b/pkg/machine/e2e/init_windows_test.go @@ -2,6 +2,7 @@ package e2e_test import ( "fmt" + "os" "path/filepath" "github.com/Microsoft/go-winio/vhd" @@ -42,6 +43,7 @@ var _ = Describe("podman machine init - windows only", func() { Expect(inspectSession).To(Exit(0)) Expect(inspectSession.outputToString()).To(Equal("true")) }) + It("init should not should not overwrite existing HyperV vms", func() { skipIfNotVmtype(define.HyperVVirt, "HyperV test only") name := randomString() @@ -75,4 +77,50 @@ var _ = Describe("podman machine init - windows only", func() { Expect(session).To(Exit(125)) Expect(session.errorToString()).To(ContainSubstring("already exists on hypervisor")) }) + + It("init should not should not overwrite existing HyperV vms", func() { + skipIfNotVmtype(define.WSLVirt, "WSL test only") + + var ( + wsl string = "wsl" + ) + + name := randomString() + distName := fmt.Sprintf("podman-%s", name) + exportedPath := filepath.Join(testDir, "bogus.tar") + distrDir := filepath.Join(testDir, "testDistro") + err := os.Mkdir(distrDir, 0755) + Expect(err).ToNot(HaveOccurred()) + + // create a bogus machine + i := new(initMachine) + session, err := mb.setName("foobarexport").setCmd(i.withImagePath(mb.imagePath)).run() + Expect(err).ToNot(HaveOccurred()) + Expect(session).To(Exit(0)) + + // export the bogus machine so we have input for making + // a vm outside the context of podman-machine and also + // so we dont have to download a distribution from microsoft + // servers + exportSession, err := runSystemCommand(wsl, []string{"--export", "podman-foobarexport", exportedPath}, defaultTimeout, true) + Expect(err).ToNot(HaveOccurred()) + Expect(exportSession).To(Exit(0)) + + // importing the machine and creating a vm + importSession, err := runSystemCommand(wsl, []string{"--import", distName, distrDir, exportedPath}, defaultTimeout, true) + Expect(err).ToNot(HaveOccurred()) + Expect(importSession).To(Exit(0)) + + defer func() { + _, err := runSystemCommand(wsl, []string{"--unregister", distName}, defaultTimeout, true) + if err != nil { + fmt.Println("unable to remove bogus wsl instance") + } + }() + + // Trying to make a vm with the same name as an existing name should result in a 125 + checkSession, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run() + Expect(err).ToNot(HaveOccurred()) + Expect(checkSession).To(Exit(125)) + }) })