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

Fail if vm exists in hyperv already #21603

Merged
merged 1 commit into from
Feb 11, 2024
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
2 changes: 1 addition & 1 deletion pkg/machine/e2e/config_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (i *initMachine) withRootful(r bool) *initMachine {
return i
}

func (i *initMachine) withUserModeNetworking(r bool) *initMachine {
func (i *initMachine) withUserModeNetworking(r bool) *initMachine { //nolint:unused
i.userModeNetworking = r
return i
}
18 changes: 0 additions & 18 deletions pkg/machine/e2e/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,6 @@ var _ = Describe("podman machine init", func() {
Expect(output).To(Equal("/run/podman/podman.sock"))
})

It("init with user mode networking", func() {
if testProvider.VMType() != define.WSLVirt {
Skip("test is only supported by WSL")
}
i := new(initMachine)
name := randomString()
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withUserModeNetworking(true)).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(0))

inspect := new(inspectMachine)
inspect = inspect.withFormat("{{.UserModeNetworking}}")
inspectSession, err := mb.setName(name).setCmd(inspect).run()
Expect(err).ToNot(HaveOccurred())
Expect(inspectSession).To(Exit(0))
Expect(inspectSession.outputToString()).To(Equal("true"))
})

It("init should cleanup on failure", func() {
i := new(initMachine)
name := randomString()
Expand Down
78 changes: 78 additions & 0 deletions pkg/machine/e2e/init_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package e2e_test

import (
"fmt"
"path/filepath"

"github.com/Microsoft/go-winio/vhd"
"github.com/containers/libhvee/pkg/hypervctl"
"github.com/containers/podman/v5/pkg/machine/define"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)

var _ = Describe("podman machine init - windows only", func() {
var (
mb *machineTestBuilder
testDir string
)

BeforeEach(func() {
testDir, mb = setup()
})
AfterEach(func() {
teardown(originalHomeDir, testDir, mb)
})

It("init with user mode networking", func() {
if testProvider.VMType() != define.WSLVirt {
Skip("test is only supported by WSL")
}
i := new(initMachine)
name := randomString()
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withUserModeNetworking(true)).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(0))

inspect := new(inspectMachine)
inspect = inspect.withFormat("{{.UserModeNetworking}}")
inspectSession, err := mb.setName(name).setCmd(inspect).run()
Expect(err).ToNot(HaveOccurred())
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()
vhdxPath := filepath.Join(testDir, fmt.Sprintf("%s.vhdx", name))
if err := vhd.CreateVhdx(vhdxPath, 1, 1); err != nil {
Fail(fmt.Sprintf("failed to create dummy vhdx %q: %q", vhdxPath, err))
}
vmm := hypervctl.NewVirtualMachineManager()
hwConfig := hypervctl.HardwareConfig{
CPUs: 1,
DiskPath: vhdxPath,
DiskSize: 0,
Memory: 0,
Network: false,
}

if err := vmm.NewVirtualMachine(name, &hwConfig); err != nil {
Fail(fmt.Sprintf("failed to create vm %q: %q", name, err))
}
vm, err := vmm.GetMachine(name)
if err != nil {
Fail(fmt.Sprintf("failed to get vm %q: %q", name, err))
}
defer func() {
if err := vm.Remove(""); err != nil {
fmt.Printf("failed to clean up vm %q from hypervisor: %q /n", name, err)
}
}()
i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
Expect(session).To(Exit(125))
Expect(session.errorToString()).To(ContainSubstring("already exists on hypervisor"))
})
})
2 changes: 1 addition & 1 deletion pkg/machine/hyperv/stubber.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (h HyperVStubber) GetHyperVisorVMs() ([]string, error) {
return nil, err
}
for _, vm := range vms {
vmNames = append(vmNames, vm.Name)
vmNames = append(vmNames, vm.ElementName) // Note: elementname is human-readable name
}
return vmNames, nil
}
Expand Down
Loading