Fix use of duplicate machine names

A condition was changed in the refgactor of init where duplicate names would be allowed but no machine was created.  Duplicate names are not permitted and should return an error.

Signed-off-by: Brent Baude <bbaude@redhat.com>
This commit is contained in:
Brent Baude
2025-10-30 07:50:08 -05:00
parent 0b17d3ff55
commit 553c4a16d5
2 changed files with 9 additions and 3 deletions

View File

@@ -208,7 +208,10 @@ func initMachine(cmd *cobra.Command, args []string) error {
// Check if machine already exists
var errNotExists *define.ErrVMDoesNotExist
_, _, err := shim.VMExists(initOpts.Name)
// errors.As checks for nil so safe to use.
// if nil, means we found a vm and need to reject it by name
if err == nil {
return &define.ErrVMAlreadyExists{Name: initOpts.Name}
}
if !errors.As(err, &errNotExists) {
return err
}

View File

@@ -117,6 +117,11 @@ var _ = Describe("podman machine init", func() {
Expect(testMachine.Resources.CPUs).To(Equal(uint64(cpus)))
Expect(testMachine.Resources.Memory).To(BeEquivalentTo(uint64(2048)))
}
// creating a new VM with the same name must fail
repeatSession, err := mb.setCmd(i.withImage(mb.imagePath)).run()
Expect(err).ToNot(HaveOccurred())
Expect(repeatSession).To(Exit(125))
Expect(repeatSession.errorToString()).To(ContainSubstring(fmt.Sprintf("Error: machine %q already exists", mb.names[0])))
})
It("run playbook", func() {
@@ -655,7 +660,6 @@ var _ = Describe("podman machine init", func() {
session, err := mb.setName(machineName).setCmd(i.withImage(mb.imagePath).withProvider(providerOverride)).run()
Expect(err).ToNot(HaveOccurred())
Expect(session.errorToString()).To(ContainSubstring(fmt.Sprintf("unsupported provider %q", providerOverride)))
})
It("machine init --provider", func() {
@@ -699,7 +703,6 @@ var _ = Describe("podman machine init", func() {
Expect(p).To(Equal(l.VMType))
}
})
})
var p4Config = []byte(`{