diff --git a/test/e2e/capacitytest/capacity_test.go b/test/e2e/capacitytest/capacity_test.go index 43123971..43317ac4 100644 --- a/test/e2e/capacitytest/capacity_test.go +++ b/test/e2e/capacitytest/capacity_test.go @@ -1,14 +1,13 @@ package capacitytest import ( - "io" - "os" "strings" "testing" "github.com/equinix/metal-cli/internal/capacity" root "github.com/equinix/metal-cli/internal/cli" outputPkg "github.com/equinix/metal-cli/internal/outputs" + "github.com/equinix/metal-cli/test/helper" "github.com/spf13/cobra" ) @@ -38,19 +37,9 @@ func TestCli_Capacity(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "get"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "n3.xlarge.x86") && !strings.Contains(string(out[:]), "m3.large.x86") && !strings.Contains(string(out[:]), "s3.xlarge.x86") && @@ -71,19 +60,9 @@ func TestCli_Capacity(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "get", "-m", "-P", "c3.small.x86"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "c3.small.x86") && !strings.Contains(string(out[:]), "mt") && !strings.Contains(string(out[:]), "sv") && @@ -104,19 +83,9 @@ func TestCli_Capacity(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "get", "-m", "-P", "m3.large.x86"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "m3.large.x86") && !strings.Contains(string(out[:]), "mt") && !strings.Contains(string(out[:]), "sv") && @@ -136,19 +105,9 @@ func TestCli_Capacity(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "check", "-m", "ny,da", "-P", "c3.medium.x86", "-q", "10"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "c3.medium.x86") && !strings.Contains(string(out[:]), "ny") && !strings.Contains(string(out[:]), "da") { @@ -166,19 +125,9 @@ func TestCli_Capacity(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "check", "-m", "da", "-P", "c3.medium.x86,m3.large.x86", "-q", "10"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "c3.medium.x86") && !strings.Contains(string(out[:]), "m3.large.x86") && !strings.Contains(string(out[:]), "ny") && @@ -197,19 +146,9 @@ func TestCli_Capacity(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "check", "-m", "ny,da", "-P", "c3.medium.x86,m3.large.x86", "-q", "10"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "c3.medium.x86") && !strings.Contains(string(out[:]), "m3.large.x86") && !strings.Contains(string(out[:]), "ny") && diff --git a/test/e2e/devices/devicecreateflagstest/device_create_flags_test.go b/test/e2e/devices/devicecreateflagstest/device_create_flags_test.go index 8857d407..d13a48d4 100644 --- a/test/e2e/devices/devicecreateflagstest/device_create_flags_test.go +++ b/test/e2e/devices/devicecreateflagstest/device_create_flags_test.go @@ -1,8 +1,6 @@ package devicecreateflagstest import ( - "io" - "os" "regexp" "strings" "testing" @@ -15,7 +13,7 @@ import ( ) func TestCli_Devices_Create_Flags(t *testing.T) { - var projectId, deviceId string + var deviceId string var err error subCommand := "device" consumerToken := "" @@ -42,59 +40,35 @@ func TestCli_Devices_Create_Flags(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-device-create-flags" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) - t.Cleanup(func() { - if err := helper.CleanTestProject(t, projectId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) + project := helper.CreateTestProject(t, projectName) + + root.SetArgs([]string{subCommand, "create", "-p", project.GetId(), "-P", "m3.small.x86", "-m", "da", "-H", "metal-cli-create-flags-dev", "--operating-system", "custom_ipxe", "--always-pxe=true", "--ipxe-script-url", "https://boot.netboot.xyz/"}) + + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), "metal-cli-create-flags-dev") && + !strings.Contains(string(out[:]), "Ubuntu 20.04 LTS") && + !strings.Contains(string(out[:]), "queued") { + t.Error("expected output should include metal-cli-create-flags-dev, Ubuntu 20.04 LTS, and queued strings in the out string ") } + name := "metal-cli-create-flags-dev" + idNamePattern := `(?m)^\| ([a-zA-Z0-9-]+) +\| *` + name + ` *\|` - if len(projectId) != 0 { + // Find the match of the ID and NAME pattern in the table string + match := regexp.MustCompile(idNamePattern).FindStringSubmatch(string(out[:])) - root.SetArgs([]string{subCommand, "create", "-p", projectId, "-P", "m3.small.x86", "-m", "da", "-H", "metal-cli-create-flags-dev", "--operating-system", "custom_ipxe", "--always-pxe=true", "--ipxe-script-url", "https://boot.netboot.xyz/"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w + // Extract the ID from the match + if len(match) > 1 { + deviceId = strings.TrimSpace(match[1]) + _, err = helper.IsDeviceStateActive(t, deviceId) t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout + helper.CleanTestDevice(t, deviceId) }) - - if err := root.Execute(); err != nil { + if err != nil { t.Fatal(err) } - - out, _ := io.ReadAll(r) - - if !strings.Contains(string(out[:]), "metal-cli-create-flags-dev") && - !strings.Contains(string(out[:]), "Ubuntu 20.04 LTS") && - !strings.Contains(string(out[:]), "queued") { - t.Error("expected output should include metal-cli-create-flags-dev, Ubuntu 20.04 LTS, and queued strings in the out string ") - } - name := "metal-cli-create-flags-dev" - idNamePattern := `(?m)^\| ([a-zA-Z0-9-]+) +\| *` + name + ` *\|` - - // Find the match of the ID and NAME pattern in the table string - match := regexp.MustCompile(idNamePattern).FindStringSubmatch(string(out[:])) - - // Extract the ID from the match - if len(match) > 1 { - deviceId = strings.TrimSpace(match[1]) - _, err = helper.IsDeviceStateActive(t, deviceId) - t.Cleanup(func() { - if err := helper.CleanTestDevice(t, deviceId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } - } + } else { + t.Errorf("No match found for %v in %v", idNamePattern, string(out[:])) } }, }, diff --git a/test/e2e/devices/devicecreatetest/device_create_test.go b/test/e2e/devices/devicecreatetest/device_create_test.go index 4641af6c..cdd5b435 100644 --- a/test/e2e/devices/devicecreatetest/device_create_test.go +++ b/test/e2e/devices/devicecreatetest/device_create_test.go @@ -1,8 +1,6 @@ package devicecreatetest import ( - "io" - "os" "regexp" "strings" "testing" @@ -15,7 +13,7 @@ import ( ) func TestCli_Devices_Create(t *testing.T) { - var projectId, deviceId string + var deviceId string var err error subCommand := "device" consumerToken := "" @@ -44,60 +42,36 @@ func TestCli_Devices_Create(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-device-create" + randomId - projectId, err = helper.CreateTestProject(t, projectName) - t.Cleanup(func() { - if err := helper.CleanTestProject(t, projectId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } + project := helper.CreateTestProject(t, projectName) - if len(projectId) != 0 { + deviceName := "metal-cli-create-dev" + randomId + root.SetArgs([]string{subCommand, "create", "-p", project.GetId(), "-P", "m3.small.x86", "-m", "da", "-O", "ubuntu_20_04", "-H", deviceName}) - deviceName := "metal-cli-create-dev" + randomId - root.SetArgs([]string{subCommand, "create", "-p", projectId, "-P", "m3.small.x86", "-m", "da", "-O", "ubuntu_20_04", "-H", deviceName}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) - - if !strings.Contains(string(out[:]), deviceName) && - !strings.Contains(string(out[:]), "Ubuntu 20.04 LTS") && - !strings.Contains(string(out[:]), "queued") { - t.Errorf("expected output should include %s, Ubuntu 20.04 LTS, and queued strings in the out string ", deviceName) - } + if !strings.Contains(string(out[:]), deviceName) && + !strings.Contains(string(out[:]), "Ubuntu 20.04 LTS") && + !strings.Contains(string(out[:]), "queued") { + t.Errorf("expected output should include %s, Ubuntu 20.04 LTS, and queued strings in the out string ", deviceName) + } - idNamePattern := `(?m)^\| ([a-zA-Z0-9-]+) +\| *` + deviceName + ` *\|` + idNamePattern := `(?m)^\| ([a-zA-Z0-9-]+) +\| *` + deviceName + ` *\|` - // Find the match of the ID and NAME pattern in the table string - match := regexp.MustCompile(idNamePattern).FindStringSubmatch(string(out[:])) + // Find the match of the ID and NAME pattern in the table string + match := regexp.MustCompile(idNamePattern).FindStringSubmatch(string(out[:])) - // Extract the ID from the match - if len(match) > 1 { - deviceId = strings.TrimSpace(match[1]) - _, err = helper.IsDeviceStateActive(t, deviceId) - t.Cleanup(func() { - if err := helper.CleanTestDevice(t, deviceId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } + // Extract the ID from the match + if len(match) > 1 { + deviceId = strings.TrimSpace(match[1]) + _, err = helper.IsDeviceStateActive(t, deviceId) + t.Cleanup(func() { + helper.CleanTestDevice(t, deviceId) + }) + if err != nil { + t.Fatal(err) } + } else { + t.Errorf("No match found for %v in %v", idNamePattern, string(out[:])) } }, }, diff --git a/test/e2e/devices/devicegettest/device_get_test.go b/test/e2e/devices/devicegettest/device_get_test.go index 403398b8..3b5c978c 100644 --- a/test/e2e/devices/devicegettest/device_get_test.go +++ b/test/e2e/devices/devicegettest/device_get_test.go @@ -1,8 +1,6 @@ package devicestest import ( - "io" - "os" "strings" "testing" @@ -14,8 +12,6 @@ import ( ) func TestCli_Devices_Get(t *testing.T) { - var projectId, deviceId string - var err error subCommand := "device" consumerToken := "" apiURL := "" @@ -41,45 +37,15 @@ func TestCli_Devices_Get(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-device-get" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) - t.Cleanup(func() { - if err := helper.CleanTestProject(t, projectId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } - - deviceId, err = helper.CreateTestDevice(t, projectId, "metal-cli-get-dev") - t.Cleanup(func() { - if err := helper.CleanTestDevice(t, deviceId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } + project := helper.CreateTestProject(t, projectName) + device := helper.CreateTestDevice(t, project.GetId(), "metal-cli-get-dev") - root.SetArgs([]string{subCommand, "get", "-i", deviceId}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } + root.SetArgs([]string{subCommand, "get", "-i", device.GetId()}) - out, _ := io.ReadAll(r) + out := helper.ExecuteAndCaptureOutput(t, root) - if !strings.Contains(string(out[:]), deviceId) { - t.Fatal("expected output should include " + deviceId) + if !strings.Contains(string(out[:]), device.GetId()) { + t.Fatal("expected output should include " + device.GetId()) } }, }, diff --git a/test/e2e/devices/devicereinstalltest/device_reinstall_test.go b/test/e2e/devices/devicereinstalltest/device_reinstall_test.go index 25161bce..12a0737c 100644 --- a/test/e2e/devices/devicereinstalltest/device_reinstall_test.go +++ b/test/e2e/devices/devicereinstalltest/device_reinstall_test.go @@ -1,7 +1,6 @@ package devicereinstalltest import ( - "strings" "testing" root "github.com/equinix/metal-cli/internal/cli" @@ -12,7 +11,6 @@ import ( ) func TestCli_Devices_Update(t *testing.T) { - var projectId, deviceId string var err error var status bool subCommand := "device" @@ -40,49 +38,29 @@ func TestCli_Devices_Update(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-device-reinstall" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) - t.Cleanup(func() { - if err := helper.CleanTestProject(t, projectId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } - - deviceId, err = helper.CreateTestDevice(t, projectId, "metal-cli-reinstall-dev") - t.Cleanup(func() { - if err := helper.CleanTestDevice(t, deviceId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } - - status, err = helper.IsDeviceStateActive(t, deviceId) + project := helper.CreateTestProject(t, projectName) + device := helper.CreateTestDevice(t, project.GetId(), "metal-cli-reinstall-dev") + status, err = helper.IsDeviceStateActive(t, device.GetId()) if err != nil { - status, err = helper.IsDeviceStateActive(t, deviceId) + status, err = helper.IsDeviceStateActive(t, device.GetId()) if err != nil { t.Fatal(err) } } - if len(projectId) != 0 && len(deviceId) != 0 && status { - root.SetArgs([]string{subCommand, "reinstall", "--id", deviceId, "-O", "ubuntu_22_04", "--preserve-data"}) + if status { + root.SetArgs([]string{subCommand, "reinstall", "--id", device.GetId(), "-O", "ubuntu_22_04", "--preserve-data"}) err = root.Execute() if err != nil { t.Fatal(err) } - status, err = helper.IsDeviceStateActive(t, deviceId) + status, err = helper.IsDeviceStateActive(t, device.GetId()) if err != nil { t.Fatal(err) } if !status { - t.Fatalf("Device not yet active, %s", deviceId) + t.Fatalf("Device not yet active, %s", device.GetId()) } } }, diff --git a/test/e2e/devices/devicestarttest/device_start_test.go b/test/e2e/devices/devicestarttest/device_start_test.go index 2f67223d..8c1fbb24 100644 --- a/test/e2e/devices/devicestarttest/device_start_test.go +++ b/test/e2e/devices/devicestarttest/device_start_test.go @@ -1,8 +1,6 @@ package devicestarttest import ( - "io" - "os" "strings" "testing" @@ -14,9 +12,6 @@ import ( ) func TestCli_Devices_Update(t *testing.T) { - var projectId, deviceId string - var err error - var status bool subCommand := "device" consumerToken := "" apiURL := "" @@ -42,70 +37,41 @@ func TestCli_Devices_Update(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-device-start" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) - t.Cleanup(func() { - if err := helper.CleanTestProject(t, projectId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } + project := helper.CreateTestProject(t, projectName) + device := helper.CreateTestDevice(t, project.GetId(), "metal-cli-start-dev") + status, err := helper.IsDeviceStateActive(t, device.GetId()) - deviceId, err = helper.CreateTestDevice(t, projectId, "metal-cli-start-dev") - t.Cleanup(func() { - if err := helper.CleanTestDevice(t, deviceId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) if err != nil { - t.Fatal(err) - } - - status, err = helper.IsDeviceStateActive(t, deviceId) - if err != nil { - _, err := helper.IsDeviceStateActive(t, deviceId) + _, err := helper.IsDeviceStateActive(t, device.GetId()) if err != nil { t.Error(err) } else { - err = helper.StopTestDevice(t, deviceId) + err = helper.StopTestDevice(t, device.GetId()) if err != nil { t.Error(err) } - status, err = helper.IsDeviceStateActive(t, deviceId) + status, err = helper.IsDeviceStateActive(t, device.GetId()) if err == nil { t.Error(err) } } } - if len(projectId) != 0 && len(deviceId) != 0 && !status { - root.SetArgs([]string{subCommand, "start", "--id", deviceId}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) + if !status { + root.SetArgs([]string{subCommand, "start", "--id", device.GetId()}) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) - if !strings.Contains(string(out[:]), "Device "+deviceId+" successfully started.") { - t.Fatal("expected output should include" + "Device " + deviceId + " successfully started." + "in the out string ") + if !strings.Contains(string(out[:]), "Device "+device.GetId()+" successfully started.") { + t.Fatal("expected output should include" + "Device " + device.GetId() + " successfully started." + "in the out string ") } - status, err = helper.IsDeviceStateActive(t, deviceId) + status, err = helper.IsDeviceStateActive(t, device.GetId()) if err != nil { t.Fatal(err) } if !status { - t.Fatalf("Device not yet active, %s", deviceId) + t.Fatalf("Device not yet active, %s", device.GetId()) } } }, diff --git a/test/e2e/devices/devicestoptest/device_stop_test.go b/test/e2e/devices/devicestoptest/device_stop_test.go index de52d074..3e68ab10 100644 --- a/test/e2e/devices/devicestoptest/device_stop_test.go +++ b/test/e2e/devices/devicestoptest/device_stop_test.go @@ -1,8 +1,6 @@ package devicestoptest import ( - "io" - "os" "strings" "testing" @@ -14,8 +12,6 @@ import ( ) func TestCli_Devices_Update(t *testing.T) { - var projectId, deviceId string - var err error subCommand := "device" consumerToken := "" apiURL := "" @@ -41,49 +37,20 @@ func TestCli_Devices_Update(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-device-stop" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) - t.Cleanup(func() { - if err := helper.CleanTestProject(t, projectId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } - - deviceId, err = helper.CreateTestDevice(t, projectId, "metal-cli-stop-dev") - t.Cleanup(func() { - if err := helper.CleanTestDevice(t, deviceId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } + project := helper.CreateTestProject(t, projectName) + device := helper.CreateTestDevice(t, project.GetId(), "metal-cli-stop-dev") - status, err := helper.IsDeviceStateActive(t, deviceId) + status, err := helper.IsDeviceStateActive(t, device.GetId()) if err != nil { t.Fatal(err) } - if len(projectId) != 0 && len(deviceId) != 0 && status { - root.SetArgs([]string{subCommand, "stop", "--id", deviceId}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) + if status { + root.SetArgs([]string{subCommand, "stop", "--id", device.GetId()}) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) - if !strings.Contains(string(out[:]), "Device "+deviceId+" successfully stopped.") { - t.Fatal("expected output should include" + "Device " + deviceId + " successfully stopped." + "in the out string ") + if !strings.Contains(string(out[:]), "Device "+device.GetId()+" successfully stopped.") { + t.Fatal("expected output should include" + "Device " + device.GetId() + " successfully stopped." + "in the out string ") } } }, diff --git a/test/e2e/devices/deviceupdatetest/device_update_test.go b/test/e2e/devices/deviceupdatetest/device_update_test.go index a5f9b8c0..2471f806 100644 --- a/test/e2e/devices/deviceupdatetest/device_update_test.go +++ b/test/e2e/devices/deviceupdatetest/device_update_test.go @@ -1,8 +1,6 @@ package deviceupdatetest import ( - "io" - "os" "strings" "testing" @@ -14,8 +12,6 @@ import ( ) func TestCli_Devices_Update(t *testing.T) { - var projectId, deviceId string - var err error subCommand := "device" consumerToken := "" apiURL := "" @@ -41,47 +37,18 @@ func TestCli_Devices_Update(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-device-update" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) - t.Cleanup(func() { - if err := helper.CleanTestProject(t, projectId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } - - deviceId, err = helper.CreateTestDevice(t, projectId, "metal-cli-update-dev") - t.Cleanup(func() { - if err := helper.CleanTestDevice(t, deviceId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Fatal(err) - } + project := helper.CreateTestProject(t, projectName) + device := helper.CreateTestDevice(t, project.GetId(), "metal-cli-update-dev") - status, err := helper.IsDeviceStateActive(t, deviceId) + status, err := helper.IsDeviceStateActive(t, device.GetId()) if err != nil { t.Fatal(err) } - if len(projectId) != 0 && len(deviceId) != 0 && status == true { - root.SetArgs([]string{subCommand, "update", "-i", deviceId, "-H", "metal-cli-update-dev-test", "-d", "This device used for testing"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) + if status == true { + root.SetArgs([]string{subCommand, "update", "-i", device.GetId(), "-H", "metal-cli-update-dev-test", "-d", "This device used for testing"}) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "metal-cli-update-dev-test") { t.Error("expected output should include metal-cli-update-dev-test in the out string ") } diff --git a/test/e2e/events/deviceeventstest/device_event_test.go b/test/e2e/events/deviceeventstest/device_event_test.go index fa910da5..9c471384 100644 --- a/test/e2e/events/deviceeventstest/device_event_test.go +++ b/test/e2e/events/deviceeventstest/device_event_test.go @@ -1,8 +1,6 @@ package eventsprojtest import ( - "io" - "os" "strings" "testing" @@ -14,9 +12,6 @@ import ( ) func TestCli_Events_Get(t *testing.T) { - var projectId, deviceId string - var err error - var status bool subCommand := "event" consumerToken := "" apiURL := "" @@ -42,48 +37,24 @@ func TestCli_Events_Get(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-device-events" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) - if err != nil { - t.Error(err) - } - deviceId, err = helper.CreateTestDevice(t, projectId, "metal-cli-events-dev") - if err != nil { - t.Error(err) - } - status, err = helper.IsDeviceStateActive(t, deviceId) - if err != nil { - status, err = helper.IsDeviceStateActive(t, deviceId) + project := helper.CreateTestProject(t, projectName) + device := helper.CreateTestDevice(t, project.GetId(), "metal-cli-events-dev") + status, err := helper.IsDeviceStateActive(t, device.GetId()) + if err != nil || !status { + status, err = helper.IsDeviceStateActive(t, device.GetId()) if err != nil || !status { - t.Error(err) + t.Fatal(err) } } - root.SetArgs([]string{subCommand, "get", "-d", deviceId}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) + root.SetArgs([]string{subCommand, "get", "-d", device.GetId()}) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "Queued for provisioning") && !strings.Contains(string(out[:]), "Connected to magic install system") && !strings.Contains(string(out[:]), "Provision complete! Your device is ready to go.") { t.Error("expected output should include Queued for provisioning in output string") } - err = helper.CleanTestDevice(t, deviceId) - if err != nil { - t.Error(err) - } - err = helper.CleanTestProject(t, projectId) - if err != nil { - t.Error(err) - } }, }, } diff --git a/test/e2e/events/projecteventstest/project_events_test.go b/test/e2e/events/projecteventstest/project_events_test.go index 6db71251..904aec2a 100644 --- a/test/e2e/events/projecteventstest/project_events_test.go +++ b/test/e2e/events/projecteventstest/project_events_test.go @@ -1,8 +1,6 @@ package eventsprojtest import ( - "io" - "os" "strings" "testing" @@ -14,8 +12,6 @@ import ( ) func TestCli_Events_Get(t *testing.T) { - var projectId string - var err error subCommand := "event" consumerToken := "" apiURL := "" @@ -42,31 +38,15 @@ func TestCli_Events_Get(t *testing.T) { root := c.Root() projectName := "metal-cli-projects-events" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) - if err != nil { - t.Error(err) - } - root.SetArgs([]string{subCommand, "get", "-p", projectId}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) + project := helper.CreateTestProject(t, projectName) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + root.SetArgs([]string{subCommand, "get", "-p", project.GetId()}) + + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "metal-cli-events-pro") { t.Error("expected output should include metal-cli-events-pro in output string") } - err = helper.CleanTestProject(t, projectId) - if err != nil { - t.Error(err) - } }, }, } diff --git a/test/e2e/gateways/create_test.go b/test/e2e/gateways/create_test.go index af353f2d..3de64b46 100644 --- a/test/e2e/gateways/create_test.go +++ b/test/e2e/gateways/create_test.go @@ -3,8 +3,6 @@ package gateways import ( "context" "errors" - "io" - "os" "strconv" "strings" "testing" @@ -18,35 +16,14 @@ import ( ) func TestGateways_Create(t *testing.T) { - var projectId, deviceId string subCommand := "gateways" consumerToken := "" apiURL := "" Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-gateways-create") - t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if device == nil { - return - } - - vlan, err := helper.CreateTestVLAN(t, projectId) - t.Cleanup(func() { - if err := helper.CleanTestVlan(t, vlan.GetId()); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Error(err) - return - } + project := helper.CreateTestProject(t, "metal-cli-gateways-create") + vlan := helper.CreateTestVLAN(t, project.GetId()) tests := []struct { name string @@ -61,25 +38,13 @@ func TestGateways_Create(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - root.SetArgs([]string{subCommand, "create", "-p", projectId, "-v", vlan.GetId(), "-s", "8"}) - - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } + root.SetArgs([]string{subCommand, "create", "-p", project.GetId(), "-v", vlan.GetId(), "-s", "8"}) - out, _ := io.ReadAll(r) + out := helper.ExecuteAndCaptureOutput(t, root) apiClient := helper.TestClient() gateways, _, err := apiClient.MetalGatewaysApi. - FindMetalGatewaysByProject(context.Background(), projectId). + FindMetalGatewaysByProject(context.Background(), project.GetId()). Include([]string{"ip_reservation"}). Execute() if err != nil { @@ -90,7 +55,7 @@ func TestGateways_Create(t *testing.T) { return } - assertGatewaysCmdOutput(t, string(out[:]), gateways.MetalGateways[0].MetalGateway.GetId(), device.Metro.GetCode(), strconv.Itoa(int(vlan.GetVxlan()))) + assertGatewaysCmdOutput(t, string(out[:]), gateways.MetalGateways[0].MetalGateway.GetId(), vlan.GetMetroCode(), strconv.Itoa(int(vlan.GetVxlan()))) }, }, } @@ -110,7 +75,7 @@ func assertGatewaysCmdOutput(t *testing.T, out, gatewayId, metro, vxlan string) } if !strings.Contains(out, metro) { - t.Errorf("cmd output should contain metro same as device: [%s] \n output:\n%s", metro, out) + t.Errorf("cmd output should contain metro same as vlan: [%s] \n output:\n%s", metro, out) } if !strings.Contains(out, vxlan) { diff --git a/test/e2e/gateways/delete_test.go b/test/e2e/gateways/delete_test.go index 8f4a6458..1b407a88 100644 --- a/test/e2e/gateways/delete_test.go +++ b/test/e2e/gateways/delete_test.go @@ -3,8 +3,6 @@ package gateways import ( "context" "fmt" - "io" - "os" "strings" "testing" @@ -17,53 +15,18 @@ import ( ) func TestGateways_Delete(t *testing.T) { - var projectId, deviceId string subCommand := "gateways" consumerToken := "" apiURL := "" Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - defer func() { - if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil { - t.Error(err) - } - }() - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-gateway-delete") - t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if device == nil { - return - } + project := helper.CreateTestProject(t, "metal-cli-gateway-delete") - vlan, err := helper.CreateTestVLAN(t, projectId) - t.Cleanup(func() { - if err := helper.CleanTestVlan(t, vlan.GetId()); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error("Error while cleaning up vLan", err) - } - }) - if err != nil { - t.Error(err) - return - } + vlan := helper.CreateTestVLAN(t, project.GetId()) subnetSize := int32(8) - metalGateway, err := helper.CreateTestGateway(t, projectId, vlan.GetId(), &subnetSize) - t.Cleanup(func() { - if err := helper.CleanTestGateway(t, metalGateway.GetId()); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Error(err) - return - } + metalGateway := helper.CreateTestGateway(t, project.GetId(), vlan.GetId(), &subnetSize) tests := []struct { name string @@ -80,19 +43,7 @@ func TestGateways_Delete(t *testing.T) { root.SetArgs([]string{subCommand, "delete", "-f", "-i", metalGateway.GetId()}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) + out := helper.ExecuteAndCaptureOutput(t, root) apiClient := helper.TestClient() gateways, _, err := apiClient.MetalGatewaysApi. diff --git a/test/e2e/gateways/retrieve_test.go b/test/e2e/gateways/retrieve_test.go index 3bcafea6..07db5e95 100644 --- a/test/e2e/gateways/retrieve_test.go +++ b/test/e2e/gateways/retrieve_test.go @@ -1,10 +1,7 @@ package gateways import ( - "io" - "os" "strconv" - "strings" "testing" root "github.com/equinix/metal-cli/internal/cli" @@ -16,46 +13,18 @@ import ( ) func TestGateways_Retrieve(t *testing.T) { - var projectId, deviceId string subCommand := "gateways" consumerToken := "" apiURL := "" Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-gateway-get") - t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil { - t.Error(err) - } - }) - if device == nil { - return - } + project := helper.CreateTestProject(t, "metal-cli-gateway-get") - vlan, err := helper.CreateTestVLAN(t, projectId) - t.Cleanup(func() { - if err := helper.CleanTestVlan(t, vlan.GetId()); err != nil { - t.Error(err) - } - }) - if err != nil { - t.Error(err) - return - } + vlan := helper.CreateTestVLAN(t, project.GetId()) subnetSize := int32(8) - metalGateway, err := helper.CreateTestGateway(t, projectId, vlan.GetId(), &subnetSize) - t.Cleanup(func() { - if err := helper.CleanTestGateway(t, metalGateway.GetId()); err != nil && - !strings.Contains(err.Error(), "Not Found") { - t.Error(err) - } - }) - if err != nil { - t.Error(err) - return - } + metalGateway := helper.CreateTestGateway(t, project.GetId(), vlan.GetId(), &subnetSize) tests := []struct { name string @@ -71,23 +40,11 @@ func TestGateways_Retrieve(t *testing.T) { root := c.Root() // get using projectId - root.SetArgs([]string{subCommand, "get", "-p", projectId}) - - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } + root.SetArgs([]string{subCommand, "get", "-p", project.GetId()}) - out, _ := io.ReadAll(r) + out := helper.ExecuteAndCaptureOutput(t, root) - assertGatewaysCmdOutput(t, string(out[:]), metalGateway.GetId(), device.Metro.GetCode(), strconv.Itoa(int(vlan.GetVxlan()))) + assertGatewaysCmdOutput(t, string(out[:]), metalGateway.GetId(), vlan.GetMetroCode(), strconv.Itoa(int(vlan.GetVxlan()))) }, }, } diff --git a/test/e2e/ipstest/ips_get_test.go b/test/e2e/ipstest/ips_get_test.go index acf186cc..0f1ef87b 100644 --- a/test/e2e/ipstest/ips_get_test.go +++ b/test/e2e/ipstest/ips_get_test.go @@ -1,8 +1,6 @@ package ipstest import ( - "io" - "os" "strings" "testing" @@ -14,7 +12,7 @@ import ( ) func TestCli_Ips_Get(t *testing.T) { - var projectId, ipsId string + var ipsId string var err error subCommand := "ip" consumerToken := "" @@ -44,26 +42,13 @@ func TestCli_Ips_Get(t *testing.T) { } root := c.Root() projectName := "metal-cli-ips-get" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) - if err != nil { - t.Error(err) - } - ipsId, err = helper.CreateTestIps(t, projectId, 1, "public_ipv4") - if len(projectId) != 0 && len(ipsId) != 0 { - root.SetArgs([]string{subCommand, "get", "-p", projectId}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) + project := helper.CreateTestProject(t, projectName) + ipsId, err = helper.CreateTestIps(t, project.GetId(), 1, "public_ipv4") + if len(ipsId) != 0 { + root.SetArgs([]string{subCommand, "get", "-p", project.GetId()}) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), ipsId) && !strings.Contains(string(out[:]), "da") { t.Error("expected output should include " + ipsId + " da strings in the out string") @@ -73,10 +58,6 @@ func TestCli_Ips_Get(t *testing.T) { if err != nil { t.Error(err) } - err = helper.CleanTestProject(t, projectId) - if err != nil { - t.Error(err) - } } }, }, diff --git a/test/e2e/ipstest/ips_request_test.go b/test/e2e/ipstest/ips_request_test.go index a7bb6ee9..aec6a565 100644 --- a/test/e2e/ipstest/ips_request_test.go +++ b/test/e2e/ipstest/ips_request_test.go @@ -1,11 +1,8 @@ package ipstest import ( - "io" - "os" "strings" "testing" - "time" root "github.com/equinix/metal-cli/internal/cli" "github.com/equinix/metal-cli/internal/ips" @@ -15,8 +12,6 @@ import ( ) func TestCli_Vlan_Create(t *testing.T) { - var projectId string - var err error subCommand := "ip" consumerToken := "" apiURL := "" @@ -45,35 +40,16 @@ func TestCli_Vlan_Create(t *testing.T) { } root := c.Root() projectName := "metal-cli-ips-get" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) - if err != nil { - t.Error(err) - } - time.Sleep(10 * time.Second) - if len(projectId) != 0 { - root.SetArgs([]string{subCommand, "request", "-p", projectId, "-t", "public_ipv4", "-m", "da", "-q", "4"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) + project := helper.CreateTestProject(t, projectName) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + root.SetArgs([]string{subCommand, "request", "-p", project.GetId(), "-t", "public_ipv4", "-m", "da", "-q", "4"}) - out, _ := io.ReadAll(r) - if !strings.Contains(string(out[:]), "ID") && - !strings.Contains(string(out[:]), "PUBLIC") && - !strings.Contains(string(out[:]), "true") { - t.Error("expected output should include ID, PUBLIC and true strings in the out string") - } - } - err = helper.CleanTestProject(t, projectId) - if err != nil { - t.Error(err) + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), "ID") && + !strings.Contains(string(out[:]), "PUBLIC") && + !strings.Contains(string(out[:]), "true") { + t.Error("expected output should include ID, PUBLIC and true strings in the out string") } }, }, diff --git a/test/e2e/metrotest/metro_test.go b/test/e2e/metrotest/metro_test.go index 1688c22f..7d34d9d5 100644 --- a/test/e2e/metrotest/metro_test.go +++ b/test/e2e/metrotest/metro_test.go @@ -1,14 +1,13 @@ package metrotest import ( - "io" - "os" "strings" "testing" root "github.com/equinix/metal-cli/internal/cli" "github.com/equinix/metal-cli/internal/metros" outputPkg "github.com/equinix/metal-cli/internal/outputs" + "github.com/equinix/metal-cli/test/helper" "github.com/spf13/cobra" ) @@ -38,19 +37,9 @@ func TestCli_Metros(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "get"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "Seattle") && !strings.Contains(string(out[:]), "Tokyo") && !strings.Contains(string(out[:]), "Sydney") && diff --git a/test/e2e/ostest/os_test.go b/test/e2e/ostest/os_test.go index add4a628..509156fd 100644 --- a/test/e2e/ostest/os_test.go +++ b/test/e2e/ostest/os_test.go @@ -1,14 +1,13 @@ package ostest import ( - "io" - "os" "strings" "testing" root "github.com/equinix/metal-cli/internal/cli" metalos "github.com/equinix/metal-cli/internal/os" outputPkg "github.com/equinix/metal-cli/internal/outputs" + "github.com/equinix/metal-cli/test/helper" "github.com/spf13/cobra" ) @@ -38,19 +37,9 @@ func TestCli_OperatingSystem(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "get"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "RedHat Enterprise Linux 7") && !strings.Contains(string(out[:]), "RancherOS") && !strings.Contains(string(out[:]), "VMware ESXi 8.0") && diff --git a/test/e2e/plantest/plan_test.go b/test/e2e/plantest/plan_test.go index 7954fbf6..28cf3aae 100644 --- a/test/e2e/plantest/plan_test.go +++ b/test/e2e/plantest/plan_test.go @@ -1,7 +1,6 @@ package plantest import ( - "io" "os" "strings" "testing" @@ -9,6 +8,7 @@ import ( root "github.com/equinix/metal-cli/internal/cli" outputPkg "github.com/equinix/metal-cli/internal/outputs" "github.com/equinix/metal-cli/internal/plans" + "github.com/equinix/metal-cli/test/helper" "github.com/spf13/cobra" ) @@ -41,19 +41,9 @@ func TestCli_Plans(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "get"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "m3.small.x86") && !strings.Contains(string(out[:]), "m3.large.x86") && !strings.Contains(string(out[:]), "c3.medium.x86") && @@ -74,19 +64,9 @@ func TestCli_Plans(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "get", "--token", os.Getenv("METAL_AUTH_TOKEN"), "--filter", "slug=m3.small.x86"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "m3.small.x86") { t.Error("expected output should include m3.small.x86 by SLUG") } @@ -102,20 +82,9 @@ func TestCli_Plans(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "get", "--token", os.Getenv("METAL_AUTH_TOKEN"), "--filter", "type=standard"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "m3.small.x86") && !strings.Contains(string(out[:]), "m3.large.x86") && !strings.Contains(string(out[:]), "c3.medium.x86") && diff --git a/test/e2e/ports/convert_test.go b/test/e2e/ports/convert_test.go index 4a628028..ca01dd99 100644 --- a/test/e2e/ports/convert_test.go +++ b/test/e2e/ports/convert_test.go @@ -1,8 +1,6 @@ package ports import ( - "io" - "os" "strconv" "strings" "testing" @@ -17,22 +15,13 @@ import ( ) func TestPorts_Convert(t *testing.T) { - var projectId, deviceId string subCommand := "port" consumerToken := "" apiURL := "" Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-port-convert") - t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil { - t.Error(err) - } - }) - if device == nil { - return - } + _, device := helper.SetupProjectAndDevice(t, "metal-cli-port-convert") port := &device.GetNetworkPorts()[2] if port == nil { @@ -57,19 +46,7 @@ func TestPorts_Convert(t *testing.T) { root.SetArgs([]string{subCommand, "convert", "-i", port.GetId(), "--layer2", "--bonded=false", "--force"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) + out := helper.ExecuteAndCaptureOutput(t, root) assertPortCmdOutput(t, port, string(out[:]), "layer2-individual", false) }, @@ -83,19 +60,7 @@ func TestPorts_Convert(t *testing.T) { root.SetArgs([]string{subCommand, "convert", "-i", port.GetId(), "--layer2", "--bonded", "--force"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) + out := helper.ExecuteAndCaptureOutput(t, root) assertPortCmdOutput(t, port, string(out[:]), "layer2-bonded", true) }, @@ -109,19 +74,7 @@ func TestPorts_Convert(t *testing.T) { root.SetArgs([]string{subCommand, "convert", "-i", port.GetId(), "-2=false", "--force"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) + out := helper.ExecuteAndCaptureOutput(t, root) assertPortCmdOutput(t, port, string(out[:]), "layer3", true) }, diff --git a/test/e2e/ports/retrieve_test.go b/test/e2e/ports/retrieve_test.go index 920c6e0e..f815770b 100644 --- a/test/e2e/ports/retrieve_test.go +++ b/test/e2e/ports/retrieve_test.go @@ -1,8 +1,6 @@ package ports import ( - "io" - "os" "strings" "testing" @@ -15,22 +13,13 @@ import ( ) func TestPorts_Retrieve(t *testing.T) { - var projectId, deviceId string subCommand := "port" consumerToken := "" apiURL := "" Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-port-get") - t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil { - t.Error(err) - } - }) - if device == nil { - return - } + _, device := helper.SetupProjectAndDevice(t, "metal-cli-port-get") port := &device.GetNetworkPorts()[2] if port == nil { @@ -52,19 +41,7 @@ func TestPorts_Retrieve(t *testing.T) { root := c.Root() root.SetArgs([]string{subCommand, "get", "-i", port.GetId()}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) + out := helper.ExecuteAndCaptureOutput(t, root) if !strings.Contains(string(out[:]), port.Data.GetMac()) { t.Errorf("cmd output should contain MAC address of the port: %s", port.Data.GetMac()) diff --git a/test/e2e/ports/vlans_test.go b/test/e2e/ports/vlans_test.go index d26971dd..b94bd1a6 100644 --- a/test/e2e/ports/vlans_test.go +++ b/test/e2e/ports/vlans_test.go @@ -2,8 +2,6 @@ package ports import ( "context" - "io" - "os" "strconv" "testing" @@ -17,22 +15,13 @@ import ( ) func TestPorts_VLANs(t *testing.T) { - var projectId, deviceId string subCommand := "port" consumerToken := "" apiURL := "" Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-port-vlan") - t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil { - t.Error(err) - } - }) - if device == nil { - return - } + project, device := helper.SetupProjectAndDevice(t, "metal-cli-port-vlan") port := &device.GetNetworkPorts()[2] if port == nil { @@ -45,19 +34,12 @@ func TestPorts_VLANs(t *testing.T) { return } - vlan, err := helper.CreateTestVLAN(t, projectId) + vlan := helper.CreateTestVLAN(t, project.GetId()) t.Cleanup(func() { if err := helper.UnAssignPortVlan(t, port.GetId(), vlan.GetId()); err != nil { t.Error(err) } - if err := helper.CleanTestVlan(t, vlan.GetId()); err != nil { - t.Error(err) - } }) - if err != nil { - t.Error(err) - return - } tests := []struct { name string @@ -76,19 +58,7 @@ func TestPorts_VLANs(t *testing.T) { // should be layer2-bonded root.SetArgs([]string{subCommand, "vlan", "-i", port.GetId(), "-a", vxLanStr}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) + out := helper.ExecuteAndCaptureOutput(t, root) // wait for port to have vlans attached if err := helper.WaitForAttachVlanToPort(t, port.GetId(), true); err != nil { diff --git a/test/e2e/projecttest/project_test.go b/test/e2e/projecttest/project_test.go index f93b7feb..59c4bf7d 100644 --- a/test/e2e/projecttest/project_test.go +++ b/test/e2e/projecttest/project_test.go @@ -2,8 +2,6 @@ package projecttest import ( "fmt" - "io" - "os" "strings" "testing" @@ -14,40 +12,6 @@ import ( "github.com/spf13/cobra" ) -// setupTestOrganization initializes a test Orge and returns its ID along with a cleanup function. -func setupTestOrganization(t *testing.T, projectName string) (string, func()) { - orgId, err := helper.CreateTestOrganization(projectName) - if err != nil { - t.Fatal(err) - } - - teardown := func() { - err := helper.CleanTestOrganization(orgId) - if err != nil { - t.Error(err) - } - } - - return orgId, teardown -} - -// setupTestProject initializes a test project and returns its ID along with a cleanup function. -func setupTestProject(t *testing.T, projectName string) (string, func()) { - projectId, err := helper.CreateTestProject(t, projectName) - if err != nil { - t.Fatal(err) - } - - teardown := func() { - err := helper.CleanTestProject(t, projectId) - if err != nil { - t.Error(err) - } - } - - return projectId, teardown -} - func TestCli_Project_Tests(t *testing.T) { subCommand := "project" consumerToken := "" @@ -77,27 +41,14 @@ func TestCli_Project_Tests(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command, rootCmd *cobra.Command, projectID string) { root := c.Root() projName := "metal-cli-" + randName + "-project-create-test" - orgId, cleanupOrg := setupTestOrganization(t, projName) - defer cleanupOrg() - - if orgId != "" { - root.SetArgs([]string{subCommand, "create", "-O", orgId, "-n", projName}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) - if !strings.Contains(string(out[:]), projName) { - t.Error("expected output should include " + projName + ", in the out string ") - } + org := helper.CreateTestOrganization(t, projName) + + root.SetArgs([]string{subCommand, "create", "-O", org.GetId(), "-n", projName}) + + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), projName) { + t.Error("expected output should include " + projName + ", in the out string ") } }, }, @@ -114,29 +65,16 @@ func TestCli_Project_Tests(t *testing.T) { projName := "metal-cli-" + randName + "-project-update-test" - projectId, cleanProject := setupTestProject(t, projName) - defer cleanProject() + project := helper.CreateTestProject(t, projName) updateProjName := projName + "-123" - if projectId != "" { - root.SetArgs([]string{subCommand, "update", "-i", projectId, "-n", updateProjName}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) - if !strings.Contains(string(out[:]), updateProjName) && - !strings.Contains(string(out[:]), projectId) { - t.Error("expected output should include " + updateProjName + projectId + ", in the out string ") - } + root.SetArgs([]string{subCommand, "update", "-i", project.GetId(), "-n", updateProjName}) + + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), updateProjName) && + !strings.Contains(string(out[:]), project.GetId()) { + t.Error("expected output should include " + updateProjName + project.GetId() + ", in the out string ") } }, }, @@ -152,28 +90,15 @@ func TestCli_Project_Tests(t *testing.T) { root := c.Root() projName := "metal-cli-" + randName + "-project-get-test" - projectId, cleanProject := setupTestProject(t, projName) - defer cleanProject() - - if projectId != "" { - root.SetArgs([]string{subCommand, "get"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) - if !strings.Contains(string(out[:]), projName) && - !strings.Contains(string(out[:]), projectId) { - t.Error("expected output should include " + projName + projectId + ", in the out string ") - } + project := helper.CreateTestProject(t, projName) + + root.SetArgs([]string{subCommand, "get"}) + + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), projName) && + !strings.Contains(string(out[:]), project.GetId()) { + t.Error("expected output should include " + projName + project.GetId() + ", in the out string ") } }, }, @@ -189,28 +114,15 @@ func TestCli_Project_Tests(t *testing.T) { root := c.Root() projName := "metal-cli-" + randName + "-project-get-id-test" - projectId, cleanProject := setupTestProject(t, projName) - defer cleanProject() - - if projectId != "" { - root.SetArgs([]string{subCommand, "get", "-i", projectId}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) - if !strings.Contains(string(out[:]), projName) && - !strings.Contains(string(out[:]), projectId) { - t.Error("expected output should include " + projName + projectId + ", in the out string ") - } + project := helper.CreateTestProject(t, projName) + + root.SetArgs([]string{subCommand, "get", "-i", project.GetId()}) + + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), projName) && + !strings.Contains(string(out[:]), project.GetId()) { + t.Error("expected output should include " + projName + project.GetId() + ", in the out string ") } }, }, @@ -227,27 +139,15 @@ func TestCli_Project_Tests(t *testing.T) { projName := "metal-cli-" + randName + "-project-delete-test" - projectId, _ := setupTestProject(t, projName) - - if projectId != "" { - root.SetArgs([]string{subCommand, "delete", "-i", projectId, "-f"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) - expectedOut := fmt.Sprintf("Project %s successfully deleted.", projectId) - if !strings.Contains(string(out[:]), expectedOut) { - t.Error(fmt.Errorf("expected output: '%s' but got '%s'", expectedOut, string(out))) - } + project := helper.CreateTestProject(t, projName) + + root.SetArgs([]string{subCommand, "delete", "-i", project.GetId(), "-f"}) + + out := helper.ExecuteAndCaptureOutput(t, root) + + expectedOut := fmt.Sprintf("Project %s successfully deleted.", project.GetId()) + if !strings.Contains(string(out[:]), expectedOut) { + t.Error(fmt.Errorf("expected output: '%s' but got '%s'", expectedOut, string(out))) } }, }, @@ -264,31 +164,18 @@ func TestCli_Project_Tests(t *testing.T) { projName := "metal-cli-" + randName + "-project-bgpenable-test" - projectId, cleanProject := setupTestProject(t, projName) - defer cleanProject() + project := helper.CreateTestProject(t, projName) asn := "65000" dtype := "local" - if projectId != "" { - root.SetArgs([]string{subCommand, "bgp-enable", "--project-id", projectId, "--deployment-type", dtype, "--asn", asn}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) - if !strings.Contains(string(out[:]), projectId) && - !strings.Contains(string(out[:]), asn) && - !strings.Contains(string(out[:]), dtype) { - t.Error("expected output should include " + projectId + "," + asn + " and " + dtype + ", in the out string ") - } + root.SetArgs([]string{subCommand, "bgp-enable", "--project-id", project.GetId(), "--deployment-type", dtype, "--asn", asn}) + + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), project.GetId()) && + !strings.Contains(string(out[:]), asn) && + !strings.Contains(string(out[:]), dtype) { + t.Error("expected output should include " + project.GetId() + "," + asn + " and " + dtype + ", in the out string ") } }, }, @@ -305,10 +192,9 @@ func TestCli_Project_Tests(t *testing.T) { projName := "metal-cli-" + randName + "-project-bgpconfig-test" - projectId, cleanProject := setupTestProject(t, projName) - defer cleanProject() + project := helper.CreateTestProject(t, projName) - err := helper.CreateTestBgpEnableTest(projectId) + err := helper.CreateTestBgpEnableTest(project.GetId()) if err != nil { t.Error(err) } @@ -317,27 +203,15 @@ func TestCli_Project_Tests(t *testing.T) { dtype := "local" status := "enabled" - if projectId != "" { - root.SetArgs([]string{subCommand, "bgp-config", "--project-id", projectId}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - - if err := root.Execute(); err != nil { - t.Fatal(err) - } - - out, _ := io.ReadAll(r) - if !strings.Contains(string(out[:]), projectId) && - !strings.Contains(string(out[:]), asn) && - !strings.Contains(string(out[:]), status) && - !strings.Contains(string(out[:]), dtype) { - t.Error("expected output should include " + projectId + "," + asn + "," + dtype + " and " + status + ", in the out string ") - } + root.SetArgs([]string{subCommand, "bgp-config", "--project-id", project.GetId()}) + + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), project.GetId()) && + !strings.Contains(string(out[:]), asn) && + !strings.Contains(string(out[:]), status) && + !strings.Contains(string(out[:]), dtype) { + t.Error("expected output should include " + project.GetId() + "," + asn + "," + dtype + " and " + status + ", in the out string ") } }, }, diff --git a/test/e2e/sshtest/ssh_test.go b/test/e2e/sshtest/ssh_test.go index 22ca3eef..70e7eef1 100644 --- a/test/e2e/sshtest/ssh_test.go +++ b/test/e2e/sshtest/ssh_test.go @@ -1,14 +1,13 @@ package sshtest import ( - "io" - "os" "strings" "testing" root "github.com/equinix/metal-cli/internal/cli" outputPkg "github.com/equinix/metal-cli/internal/outputs" "github.com/equinix/metal-cli/internal/ssh" + "github.com/equinix/metal-cli/test/helper" "github.com/spf13/cobra" ) @@ -38,19 +37,9 @@ func TestCli_SshKey(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "get"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "ID") && !strings.Contains(string(out[:]), "LABEL") && !strings.Contains(string(out[:]), "CREATED") { diff --git a/test/e2e/usertest/user_test.go b/test/e2e/usertest/user_test.go index c80141a5..0194d3e0 100644 --- a/test/e2e/usertest/user_test.go +++ b/test/e2e/usertest/user_test.go @@ -1,14 +1,13 @@ package usertest import ( - "io" - "os" "strings" "testing" root "github.com/equinix/metal-cli/internal/cli" outputPkg "github.com/equinix/metal-cli/internal/outputs" "github.com/equinix/metal-cli/internal/users" + "github.com/equinix/metal-cli/test/helper" "github.com/spf13/cobra" ) @@ -38,19 +37,9 @@ func TestCli_Users(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() root.SetArgs([]string{subCommand, "get"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "ID") && !strings.Contains(string(out[:]), "FULL NAME") && !strings.Contains(string(out[:]), "EMAIL") && diff --git a/test/e2e/vlan/vlan_creat_test.go b/test/e2e/vlan/vlan_creat_test.go index eca53d18..502fa28b 100644 --- a/test/e2e/vlan/vlan_creat_test.go +++ b/test/e2e/vlan/vlan_creat_test.go @@ -1,8 +1,6 @@ package vlan import ( - "io" - "os" "strings" "testing" @@ -14,7 +12,6 @@ import ( ) func TestCli_Vlan_Create(t *testing.T) { - var projectId string var err error subCommand := "vlan" consumerToken := "" @@ -41,36 +38,18 @@ func TestCli_Vlan_Create(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-vlan-create-pro" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) + project := helper.CreateTestProject(t, projectName) if err != nil { t.Error(err) } - if len(projectId) != 0 { - root.SetArgs([]string{subCommand, "create", "-p", projectId, "-m", "da", "--vxlan", "2023", "-d", "metal-cli-vlan-test"}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) + root.SetArgs([]string{subCommand, "create", "-p", project.GetId(), "-m", "da", "--vxlan", "2023", "-d", "metal-cli-vlan-test"}) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) - - if !strings.Contains(string(out[:]), "metal-cli-vlan-test") && - !strings.Contains(string(out[:]), "da") && - !strings.Contains(string(out[:]), "2023") { - t.Error("expected output should include metal-cli-vlan-test, da and 2023 strings in the out string") - } - - err = helper.CleanTestProject(t, projectId) - if err != nil { - t.Error(err) - } + if !strings.Contains(string(out[:]), "metal-cli-vlan-test") && + !strings.Contains(string(out[:]), "da") && + !strings.Contains(string(out[:]), "2023") { + t.Error("expected output should include metal-cli-vlan-test, da and 2023 strings in the out string") } }, }, diff --git a/test/e2e/vlan/vlan_delete_test.go b/test/e2e/vlan/vlan_delete_test.go index 5aa5dd68..bf442509 100644 --- a/test/e2e/vlan/vlan_delete_test.go +++ b/test/e2e/vlan/vlan_delete_test.go @@ -1,8 +1,6 @@ package vlan import ( - "io" - "os" "strings" "testing" @@ -14,7 +12,7 @@ import ( ) func TestCli_Vlan_Clean(t *testing.T) { - var projectId, vlanId string + var vlanId string var err error subCommand := "vlan" consumerToken := "" @@ -41,33 +39,19 @@ func TestCli_Vlan_Clean(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-vlan-get-pro" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) + project := helper.CreateTestProject(t, projectName) if err != nil { t.Error(err) } - vlanId, err = helper.CreateTestVlanWithVxLan(t, projectId, 2023, "metal-cli-vlan-get-test") - if len(projectId) != 0 && len(vlanId) != 0 { + vlanId, err = helper.CreateTestVlanWithVxLan(t, project.GetId(), 2023, "metal-cli-vlan-get-test") + if len(vlanId) != 0 { root.SetArgs([]string{subCommand, "delete", "-f", "-i", vlanId}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "Virtual Network "+vlanId+" successfully deleted.") { t.Error("expected output should include Virtual Network " + vlanId + "successfully deleted." + "in the out string") } - err = helper.CleanTestProject(t, projectId) - if err != nil { - t.Error(err) - } } }, }, diff --git a/test/e2e/vlan/vlan_get_test.go b/test/e2e/vlan/vlan_get_test.go index 296e447b..c27053d7 100644 --- a/test/e2e/vlan/vlan_get_test.go +++ b/test/e2e/vlan/vlan_get_test.go @@ -1,8 +1,6 @@ package vlan import ( - "io" - "os" "strings" "testing" @@ -14,7 +12,7 @@ import ( ) func TestCli_Vlan_Get(t *testing.T) { - var projectId, vlanId string + var vlanId string var err error subCommand := "vlan" consumerToken := "" @@ -41,40 +39,23 @@ func TestCli_Vlan_Get(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() projectName := "metal-cli-vlan-delete-pro" + helper.GenerateRandomString(5) - projectId, err = helper.CreateTestProject(t, projectName) + project := helper.CreateTestProject(t, projectName) if err != nil { t.Error(err) } - vlanId, err = helper.CreateTestVlanWithVxLan(t, projectId, 2023, "metal-cli-vlan-delete-test") - if len(projectId) != 0 && len(vlanId) != 0 { - root.SetArgs([]string{subCommand, "get", "-p", projectId}) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - t.Cleanup(func() { - w.Close() - os.Stdout = rescueStdout - }) + vlanId, err = helper.CreateTestVlanWithVxLan(t, project.GetId(), 2023, "metal-cli-vlan-delete-test") + if len(vlanId) != 0 { + root.SetArgs([]string{subCommand, "get", "-p", project.GetId()}) - if err := root.Execute(); err != nil { - t.Fatal(err) - } + out := helper.ExecuteAndCaptureOutput(t, root) - out, _ := io.ReadAll(r) if !strings.Contains(string(out[:]), "metal-cli-vlan-get-test") && !strings.Contains(string(out[:]), "da") && !strings.Contains(string(out[:]), "2023") { t.Error("expected output should include metal-cli-vlan-get-test, da and 2023 strings in the out string") } - err = helper.CleanTestVlan(t, vlanId) - if err != nil { - t.Error(err) - } - err = helper.CleanTestProject(t, projectId) - if err != nil { - t.Error(err) - } + helper.CleanTestVlan(t, vlanId) } }, }, diff --git a/test/helper/helper.go b/test/helper/helper.go index d5363190..109e2eea 100644 --- a/test/helper/helper.go +++ b/test/helper/helper.go @@ -3,67 +3,81 @@ package helper import ( "context" "fmt" + "io" + "net/http" "os" "testing" "time" "golang.org/x/exp/rand" - openapiclient "github.com/equinix/equinix-sdk-go/services/metalv1" + "github.com/equinix/equinix-sdk-go/services/metalv1" "github.com/pkg/errors" + "github.com/spf13/cobra" ) -func TestClient() *openapiclient.APIClient { - configuration := openapiclient.NewConfiguration() +func TestClient() *metalv1.APIClient { + configuration := metalv1.NewConfiguration() configuration.AddDefaultHeader("X-Auth-Token", os.Getenv("METAL_AUTH_TOKEN")) // For debug purpose //configuration.Debug = true - apiClient := openapiclient.NewAPIClient(configuration) + apiClient := metalv1.NewAPIClient(configuration) return apiClient } -func CreateTestProject(t *testing.T, name string) (string, error) { +func CreateTestProject(t *testing.T, name string) *metalv1.Project { t.Helper() TestApiClient := TestClient() - projectCreateFromRootInput := *openapiclient.NewProjectCreateFromRootInput(name) // ProjectCreateFromRootInput | Project to create + projectCreateFromRootInput := *metalv1.NewProjectCreateFromRootInput(name) // ProjectCreateFromRootInput | Project to create - projectResp, _, err := TestApiClient.ProjectsApi.CreateProject(context.Background()).ProjectCreateFromRootInput(projectCreateFromRootInput).Execute() + project, _, err := TestApiClient.ProjectsApi.CreateProject(context.Background()).ProjectCreateFromRootInput(projectCreateFromRootInput).Execute() if err != nil { - return "", fmt.Errorf("Error when calling `ProjectsApi.CreateProject`: %v\n", err) + t.Fatalf("Error when calling `ProjectsApi.CreateProject`: %v\n", err) + return nil } - return projectResp.GetId(), nil + + t.Cleanup(func() { + CleanTestProject(t, project.GetId()) + }) + + return project } -func CreateTestDevice(t *testing.T, projectId, name string) (string, error) { +func CreateTestDevice(t *testing.T, projectId, name string) *metalv1.Device { t.Helper() TestApiClient := TestClient() hostname := name - metroDeviceRequest := openapiclient.CreateDeviceRequest{ - DeviceCreateInMetroInput: &openapiclient.DeviceCreateInMetroInput{ + metroDeviceRequest := metalv1.CreateDeviceRequest{ + DeviceCreateInMetroInput: &metalv1.DeviceCreateInMetroInput{ Metro: "sv", Plan: "m3.small.x86", OperatingSystem: "ubuntu_20_04", Hostname: &hostname, }, } - deviceResp, _, err := TestApiClient.DevicesApi. + device, _, err := TestApiClient.DevicesApi. CreateDevice(context.Background(), projectId). CreateDeviceRequest(metroDeviceRequest). Execute() if err != nil { - return "", fmt.Errorf("Error when calling `DevicesApi.CreateDevice`: %v\n", err) + t.Fatalf("Error when calling `DevicesApi.CreateDevice`: %v\n", err) } - return deviceResp.GetId(), nil + + t.Cleanup(func() { + CleanTestDevice(t, device.GetId()) + }) + + return device } -func CreateTestVLAN(t *testing.T, projectId string) (*openapiclient.VirtualNetwork, error) { +func CreateTestVLAN(t *testing.T, projectId string) *metalv1.VirtualNetwork { TestApiClient := TestClient() t.Helper() metro := "sv" - vlanCreateInput := openapiclient.VirtualNetworkCreateInput{ + vlanCreateInput := metalv1.VirtualNetworkCreateInput{ Metro: &metro, } vlan, _, err := TestApiClient.VLANsApi. @@ -71,17 +85,22 @@ func CreateTestVLAN(t *testing.T, projectId string) (*openapiclient.VirtualNetwo VirtualNetworkCreateInput(vlanCreateInput). Execute() if err != nil { - return nil, fmt.Errorf("Error when calling `VLANsApi.CreateVirtualNetwork`: %v\n", err) + t.Fatalf("Error when calling `VLANsApi.CreateVirtualNetwork`: %v\n", err) } - return vlan, nil + + t.Cleanup(func() { + CleanTestVlan(t, vlan.GetId()) + }) + + return vlan } -func CreateTestGateway(t *testing.T, projectId, vlanId string, privateIPv4SubnetSize *int32) (*openapiclient.MetalGateway, error) { +func CreateTestGateway(t *testing.T, projectId, vlanId string, privateIPv4SubnetSize *int32) *metalv1.MetalGateway { TestApiClient := TestClient() t.Helper() - gatewayCreateInput := openapiclient.CreateMetalGatewayRequest{ - MetalGatewayCreateInput: &openapiclient.MetalGatewayCreateInput{ + gatewayCreateInput := metalv1.CreateMetalGatewayRequest{ + MetalGatewayCreateInput: &metalv1.MetalGatewayCreateInput{ VirtualNetworkId: vlanId, PrivateIpv4SubnetSize: privateIPv4SubnetSize, }, @@ -92,12 +111,13 @@ func CreateTestGateway(t *testing.T, projectId, vlanId string, privateIPv4Subnet CreateMetalGatewayRequest(gatewayCreateInput). Execute() if err != nil { - return nil, fmt.Errorf("Error when calling `MetalGatewaysApi.CreateMetalGateway`: %v\n", err) + t.Fatalf("Error when calling `MetalGatewaysApi.CreateMetalGateway`: %v\n", err) } - return gateway.MetalGateway, nil + + return gateway.MetalGateway } -func GetDeviceById(t *testing.T, deviceId string) (*openapiclient.Device, error) { +func GetDeviceById(t *testing.T, deviceId string) (*metalv1.Device, error) { TestApiClient := TestClient() t.Helper() @@ -114,7 +134,7 @@ func GetDeviceById(t *testing.T, deviceId string) (*openapiclient.Device, error) return device, nil } -func GetPortById(t *testing.T, portId string) (*openapiclient.Port, error) { +func GetPortById(t *testing.T, portId string) (*metalv1.Port, error) { t.Helper() TestApiClient := TestClient() includes := []string{"virtual_network"} @@ -186,7 +206,7 @@ func WaitForAttachVlanToPort(t *testing.T, portId string, attach bool) error { func StopTestDevice(t *testing.T, deviceId string) error { t.Helper() - deviceActionInput := *openapiclient.NewDeviceActionInput("power_off") + deviceActionInput := *metalv1.NewDeviceActionInput("power_off") TestApiClient := TestClient() _, err := TestApiClient.DevicesApi.PerformAction(context.Background(), deviceId).DeviceActionInput(deviceActionInput).Execute() @@ -196,30 +216,34 @@ func StopTestDevice(t *testing.T, deviceId string) error { return nil } -func CleanTestDevice(t *testing.T, deviceId string) error { +func CleanTestDevice(t *testing.T, deviceId string) { t.Helper() + _, err := IsDeviceStateActive(t, deviceId) + if err != nil { + t.Fatal(err) + } + TestApiClient := TestClient() - _, err := TestApiClient.DevicesApi. + _, err = TestApiClient.DevicesApi. DeleteDevice(context.Background(), deviceId). ForceDelete(true). Execute() + if err != nil { - return fmt.Errorf("Error when calling `DevicesApi.DeleteDevice``: %v\n", err) + t.Fatalf("Error when calling `DevicesApi.DeleteDevice`` for %v: %v\n", deviceId, err) } - return nil } -func CleanTestProject(t *testing.T, projectId string) error { +func CleanTestProject(t *testing.T, projectId string) { t.Helper() TestApiClient := TestClient() _, err := TestApiClient.ProjectsApi. DeleteProject(context.Background(), projectId). Execute() if err != nil { - return fmt.Errorf("Error when calling `ProjectsApi.DeleteProject``: %v\n", err) + t.Fatalf("Error when calling `ProjectsApi.DeleteProject`` for %v: %v\n", projectId, err) } - return nil } func CreateTestIps(t *testing.T, projectId string, quantity int, ipType string) (string, error) { @@ -229,7 +253,7 @@ func CreateTestIps(t *testing.T, projectId string, quantity int, ipType string) var tags []string var facility string - req := &openapiclient.IPReservationRequestInput{ + req := &metalv1.IPReservationRequestInput{ Metro: &metro, Tags: tags, Quantity: int32(quantity), @@ -237,7 +261,7 @@ func CreateTestIps(t *testing.T, projectId string, quantity int, ipType string) Facility: &facility, } - requestIPReservationRequest := &openapiclient.RequestIPReservationRequest{ + requestIPReservationRequest := &metalv1.RequestIPReservationRequest{ IPReservationRequestInput: req, } @@ -253,7 +277,7 @@ func CleanTestIps(t *testing.T, ipsId string) error { TestApiClient := TestClient() _, err := TestApiClient.IPAddressesApi.DeleteIPAddress(context.Background(), ipsId).Execute() if err != nil { - return fmt.Errorf("Error when calling `IPAddressesApi.DeleteIPAddress``: %v\n", err) + return fmt.Errorf("Error when calling `IPAddressesApi.DeleteIPAddress`` for %v: %v\n", ipsId, err) } return nil } @@ -261,7 +285,7 @@ func CleanTestIps(t *testing.T, ipsId string) error { func CreateTestVlanWithVxLan(t *testing.T, projectId string, Id int, desc string) (string, error) { t.Helper() TestApiClient := TestClient() - virtualNetworkCreateInput := *openapiclient.NewVirtualNetworkCreateInput() + virtualNetworkCreateInput := *metalv1.NewVirtualNetworkCreateInput() virtualNetworkCreateInput.SetDescription(desc) virtualNetworkCreateInput.SetMetro("da") virtualNetworkCreateInput.SetVxlan(int32(Id)) @@ -273,15 +297,13 @@ func CreateTestVlanWithVxLan(t *testing.T, projectId string, Id int, desc string return vlanresp.GetId(), nil } -func CleanTestVlan(t *testing.T, vlanId string) error { +func CleanTestVlan(t *testing.T, vlanId string) { t.Helper() TestApiClient := TestClient() - _, _, err := TestApiClient.VLANsApi.DeleteVirtualNetwork(context.Background(), vlanId).Execute() - if err != nil { - return fmt.Errorf("Error when calling `VLANsApi.DeleteVirtualNetwork``: %v\n", err) + _, resp, err := TestApiClient.VLANsApi.DeleteVirtualNetwork(context.Background(), vlanId).Execute() + if err != nil && resp.StatusCode != http.StatusNotFound { + t.Fatalf("Error when calling `VLANsApi.DeleteVirtualNetwork`` for %v: %v\n", vlanId, err) } - - return nil } func UnAssignPortVlan(t *testing.T, portId, vlanId string) error { @@ -289,67 +311,40 @@ func UnAssignPortVlan(t *testing.T, portId, vlanId string) error { testClient := TestClient() _, _, err := testClient.PortsApi. UnassignPort(context.Background(), portId). - PortAssignInput(openapiclient.PortAssignInput{Vnid: &vlanId}). + PortAssignInput(metalv1.PortAssignInput{Vnid: &vlanId}). Execute() return err } -func CleanupProjectAndDevice(t *testing.T, deviceId, projectId string) error { - t.Helper() - resp, err := IsDeviceStateActive(t, deviceId) - if err == nil && resp { - err = CleanTestDevice(t, deviceId) - if err != nil { - return err - } - err = CleanTestProject(t, projectId) - if err != nil { - return err - } - } - - return nil -} - //nolint:staticcheck -func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string, projPrefix string) *openapiclient.Device { +func SetupProjectAndDevice(t *testing.T, projPrefix string) (*metalv1.Project, *metalv1.Device) { t.Helper() projectName := projPrefix + GenerateRandomString(5) - projId, err := CreateTestProject(t, projectName) - if err != nil { - t.Fatal(err) - return nil - } - *projectId = projId + project := CreateTestProject(t, projectName) - devId, err := CreateTestDevice(t, *projectId, "metal-cli-test-device") - if err != nil { - t.Fatal(err) - return nil - } - *deviceId = devId + device := CreateTestDevice(t, project.GetId(), "metal-cli-test-device") - active, err := IsDeviceStateActive(t, *deviceId) + active, err := IsDeviceStateActive(t, device.GetId()) if err != nil { t.Fatal(err) - return nil + return nil, nil } if !active { - t.Fatalf("Timeout while waiting for device: %s to be active", *deviceId) - return nil + t.Fatalf("Timeout while waiting for device: %s to be active", device.GetId()) + return nil, nil } - device, err := GetDeviceById(t, *deviceId) + device, err = GetDeviceById(t, device.GetId()) if err != nil { t.Fatal(err) - return nil + return nil, nil } if len(device.NetworkPorts) < 3 { t.Fatalf("All 3 ports doesnot exist for the created device: %s", device.GetId()) - return nil + return nil, nil } - return device + return project, device } func CleanTestGateway(t *testing.T, gatewayId string) error { @@ -361,20 +356,20 @@ func CleanTestGateway(t *testing.T, gatewayId string) error { Include([]string{"ip_reservation"}). Execute() if err != nil { - return fmt.Errorf("Error when calling `MetalGatewaysApi.DeleteMetalGateway``: %v\n", err) + return fmt.Errorf("Error when calling `MetalGatewaysApi.DeleteMetalGateway`` for %v: %v\n", gatewayId, err) } return nil } -func CreateTestOrganization(name string) (string, error) { +func CreateTestOrganization(t *testing.T, name string) *metalv1.Organization { TestApiClient := TestClient() - organizationInput := openapiclient.NewOrganizationInput() + organizationInput := metalv1.NewOrganizationInput() organizationInput.Name = &name organizationInput.Description = &name - addr := openapiclient.NewAddressWithDefaults() + addr := metalv1.NewAddressWithDefaults() addr.SetAddress("Boston") addr.SetCity("Boston") addr.SetCountry("US") @@ -387,27 +382,29 @@ func CreateTestOrganization(name string) (string, error) { resp, _, err := TestApiClient.OrganizationsApi.CreateOrganization(context.Background()).OrganizationInput(*organizationInput).Include(defaultIncludes).Execute() if err != nil { - return "", fmt.Errorf("Error when calling `OrganizationsApi.CreateOrganization``: %v\n", err) + t.Fatalf("Error when calling `OrganizationsApi.CreateOrganization``: %v\n", err) } - return resp.GetId(), nil + t.Cleanup(func() { + CleanTestOrganization(t, resp.GetId()) + }) + + return resp } -func CleanTestOrganization(orgId string) error { +func CleanTestOrganization(t *testing.T, orgId string) { TestApiClient := TestClient() _, err := TestApiClient.OrganizationsApi.DeleteOrganization(context.Background(), orgId).Execute() if err != nil { - return fmt.Errorf("Error when calling `OrganizationsApi.DeleteOrganization``: %v\n", err) + t.Fatalf("Error when calling `OrganizationsApi.DeleteOrganization`` for %v: %v\n", orgId, err) } - - return nil } func CreateTestBgpEnableTest(projId string) error { TestApiClient := TestClient() - bgpConfigRequestInput := *openapiclient.NewBgpConfigRequestInput(int32(65000), openapiclient.BgpConfigRequestInputDeploymentType("local")) + bgpConfigRequestInput := *metalv1.NewBgpConfigRequestInput(int32(65000), metalv1.BgpConfigRequestInputDeploymentType("local")) _, err := TestApiClient.BGPApi.RequestBgpConfig(context.Background(), projId).BgpConfigRequestInput(bgpConfigRequestInput).Execute() if err != nil { @@ -427,3 +424,29 @@ func GenerateRandomString(length int) string { } return string(result) } + +func ExecuteAndCaptureOutput(t *testing.T, root *cobra.Command) []byte { + rescueStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + err := root.Execute() + + ioErr := w.Close() + if ioErr != nil { + t.Logf("error while capturing command output: %v", ioErr) + } + + os.Stdout = rescueStdout + + if err != nil { + t.Fatal(err) + } + + out, ioErr := io.ReadAll(r) + if ioErr != nil { + t.Logf("error while reading command output: %v", ioErr) + } + + return out +}