diff --git a/cmd/ignite/run/create.go b/cmd/ignite/run/create.go index 6cadd1545..0f85ab8bf 100644 --- a/cmd/ignite/run/create.go +++ b/cmd/ignite/run/create.go @@ -77,6 +77,11 @@ func (cf *CreateFlags) NewCreateOptions(args []string, fs *flag.FlagSet) (*Creat baseVM.Spec.Image.OCI = ociRef } + // Generate a VM name and UID if not set yet. + if err := metadata.SetNameAndUID(baseVM, providers.Client); err != nil { + return nil, err + } + // Apply the VM config on the base VM, if a VM config is given. if len(cf.ConfigFile) != 0 { if err := applyVMConfigFile(baseVM, cf.ConfigFile); err != nil { diff --git a/e2e/run_test.go b/e2e/run_test.go index 3b4ef6793..4cb7d32b7 100644 --- a/e2e/run_test.go +++ b/e2e/run_test.go @@ -1,8 +1,10 @@ package e2e import ( + "fmt" "os" "path" + "strings" "testing" "gotest.tools/assert" @@ -114,3 +116,34 @@ func TestCurlWithContainerdAndCNI(t *testing.T) { "cni", ) } + +func TestRunWithoutName(t *testing.T) { + assert.Assert(t, e2eHome != "", "IGNITE_E2E_HOME should be set") + + igniteCmd := util.NewCommand(t, igniteBin) + + labelKey := "testName" + labelVal := t.Name() + + // Create VM with label to be able to identify the created VM without name. + igniteCmd.New(). + With("run"). + With(util.DefaultVMImage). + With("--label", fmt.Sprintf("%s=%s", labelKey, labelVal)). + Run() + + // List the VM with label and get the VM name. + psCmd := igniteCmd.New(). + With("ps"). + With("--filter={{.ObjectMeta.Labels}}=~" + labelKey + ":" + labelVal). + With("--template={{.ObjectMeta.Name}}") + psOut, psErr := psCmd.Cmd.CombinedOutput() + assert.Check(t, psErr, fmt.Sprintf("ps: \n%q\n%s", psCmd.Cmd, psOut)) + vmName := strings.TrimSpace(string(psOut)) + + // Delete the VM. + igniteCmd.New(). + With("rm", "-f"). + With(vmName). + Run() +}