diff --git a/test/e2e/capacitytest/capacity_test.go b/test/e2e/capacitytest/capacity_test.go index 3b389445..43123971 100644 --- a/test/e2e/capacitytest/capacity_test.go +++ b/test/e2e/capacitytest/capacity_test.go @@ -41,12 +41,16 @@ func TestCli_Capacity(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), "n3.xlarge.x86") && !strings.Contains(string(out[:]), "m3.large.x86") && !strings.Contains(string(out[:]), "s3.xlarge.x86") && @@ -70,12 +74,16 @@ func TestCli_Capacity(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), "c3.small.x86") && !strings.Contains(string(out[:]), "mt") && !strings.Contains(string(out[:]), "sv") && @@ -99,12 +107,16 @@ func TestCli_Capacity(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), "m3.large.x86") && !strings.Contains(string(out[:]), "mt") && !strings.Contains(string(out[:]), "sv") && @@ -127,12 +139,16 @@ func TestCli_Capacity(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), "c3.medium.x86") && !strings.Contains(string(out[:]), "ny") && !strings.Contains(string(out[:]), "da") { @@ -153,12 +169,16 @@ func TestCli_Capacity(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), "c3.medium.x86") && !strings.Contains(string(out[:]), "m3.large.x86") && !strings.Contains(string(out[:]), "ny") && @@ -180,12 +200,16 @@ func TestCli_Capacity(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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 de98d5fa..8857d407 100644 --- a/test/e2e/devices/devicecreateflagstest/device_create_flags_test.go +++ b/test/e2e/devices/devicecreateflagstest/device_create_flags_test.go @@ -17,7 +17,6 @@ import ( func TestCli_Devices_Create_Flags(t *testing.T) { var projectId, deviceId string var err error - var resp bool subCommand := "device" consumerToken := "" apiURL := "" @@ -42,22 +41,35 @@ func TestCli_Devices_Create_Flags(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-create-flags-pro") + 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.Error(err) + t.Fatal(err) } + if len(projectId) != 0 { 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 + t.Cleanup(func() { + w.Close() + os.Stdout = rescueStdout + }) + if err := root.Execute(); err != nil { - t.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout + if !strings.Contains(string(out[:]), "metal-cli-create-flags-dev") && !strings.Contains(string(out[:]), "Ubuntu 20.04 LTS") && !strings.Contains(string(out[:]), "queued") { @@ -72,18 +84,15 @@ func TestCli_Devices_Create_Flags(t *testing.T) { // Extract the ID from the match if len(match) > 1 { deviceId = strings.TrimSpace(match[1]) - resp, err = helper.IsDeviceStateActive(deviceId) - if err == nil && resp == true { - err = helper.CleanTestDevice(deviceId) - if err != nil { - t.Error(err) - } - err = helper.CleanTestProject(projectId) - if err != nil { + _, 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) } - } else { - t.Error(err) + }) + if err != nil { + t.Fatal(err) } } } diff --git a/test/e2e/devices/devicecreatetest/device_create_test.go b/test/e2e/devices/devicecreatetest/device_create_test.go index efc8fce7..4641af6c 100644 --- a/test/e2e/devices/devicecreatetest/device_create_test.go +++ b/test/e2e/devices/devicecreatetest/device_create_test.go @@ -17,12 +17,13 @@ import ( func TestCli_Devices_Create(t *testing.T) { var projectId, deviceId string var err error - var resp bool subCommand := "device" consumerToken := "" apiURL := "" Version := "metal" rootClient := root.NewClient(consumerToken, apiURL, Version) + randomId := helper.GenerateRandomString(5) + type fields struct { MainCmd *cobra.Command Outputer outputPkg.Outputer @@ -42,29 +43,43 @@ func TestCli_Devices_Create(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-create-pro") + 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.Error(err) + t.Fatal(err) } + if len(projectId) != 0 { - root.SetArgs([]string{subCommand, "create", "-p", projectId, "-P", "m3.small.x86", "-m", "da", "-O", "ubuntu_20_04", "-H", "metal-cli-create-dev"}) + 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout - if !strings.Contains(string(out[:]), "metal-cli-create-dev") && + + if !strings.Contains(string(out[:]), deviceName) && !strings.Contains(string(out[:]), "Ubuntu 20.04 LTS") && !strings.Contains(string(out[:]), "queued") { - t.Error("expected output should include metal-cli-create-dev, Ubuntu 20.04 LTS, and queued strings in the out string ") + t.Errorf("expected output should include %s, Ubuntu 20.04 LTS, and queued strings in the out string ", deviceName) } - name := "metal-cli-create-dev" - idNamePattern := `(?m)^\| ([a-zA-Z0-9-]+) +\| *` + name + ` *\|` + + 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[:])) @@ -72,19 +87,15 @@ func TestCli_Devices_Create(t *testing.T) { // Extract the ID from the match if len(match) > 1 { deviceId = strings.TrimSpace(match[1]) - resp, err = helper.IsDeviceStateActive(deviceId) - if err != nil || resp { - if !resp { - resp, err = helper.IsDeviceStateActive(deviceId) - } - err = helper.CleanTestDevice(deviceId) - if err != nil { - t.Error(err) - } - err = helper.CleanTestProject(projectId) - if err != nil { + _, 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) } } } diff --git a/test/e2e/devices/devicegettest/device_get_test.go b/test/e2e/devices/devicegettest/device_get_test.go index a8383b53..403398b8 100644 --- a/test/e2e/devices/devicegettest/device_get_test.go +++ b/test/e2e/devices/devicegettest/device_get_test.go @@ -16,7 +16,6 @@ import ( func TestCli_Devices_Get(t *testing.T) { var projectId, deviceId string var err error - var resp bool subCommand := "device" consumerToken := "" apiURL := "" @@ -41,41 +40,46 @@ func TestCli_Devices_Get(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-get-pro") + 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.Error(err) + t.Fatal(err) } - deviceId, err = helper.CreateTestDevice(projectId, "metal-cli-get-dev") + + 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.Error(err) + t.Fatal(err) } + 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout + if !strings.Contains(string(out[:]), deviceId) { - t.Error("expected output should include " + deviceId) - } - if len(projectId) != 0 && len(deviceId) != 0 { - resp, err = helper.IsDeviceStateActive(deviceId) - if err == nil && resp == true { - err = helper.CleanTestDevice(deviceId) - if err != nil { - t.Error(err) - } - err = helper.CleanTestProject(projectId) - if err != nil { - t.Error(err) - } - } else { - t.Error(err) - } + t.Fatal("expected output should include " + deviceId) } }, }, diff --git a/test/e2e/devices/devicereinstalltest/device_reinstall_test.go b/test/e2e/devices/devicereinstalltest/device_reinstall_test.go index f40bd47d..25161bce 100644 --- a/test/e2e/devices/devicereinstalltest/device_reinstall_test.go +++ b/test/e2e/devices/devicereinstalltest/device_reinstall_test.go @@ -1,6 +1,7 @@ package devicereinstalltest import ( + "strings" "testing" root "github.com/equinix/metal-cli/internal/cli" @@ -38,19 +39,34 @@ func TestCli_Devices_Update(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-reinstall-pro") + 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.Error(err) + t.Fatal(err) } - deviceId, err = helper.CreateTestDevice(projectId, "metal-cli-reinstall-dev") + + 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.Error(err) + t.Fatal(err) } - status, err = helper.IsDeviceStateActive(deviceId) + + status, err = helper.IsDeviceStateActive(t, deviceId) if err != nil { - status, err = helper.IsDeviceStateActive(deviceId) + status, err = helper.IsDeviceStateActive(t, deviceId) if err != nil { - t.Error(err) + t.Fatal(err) } } @@ -58,26 +74,15 @@ func TestCli_Devices_Update(t *testing.T) { root.SetArgs([]string{subCommand, "reinstall", "--id", deviceId, "-O", "ubuntu_22_04", "--preserve-data"}) err = root.Execute() if err != nil { - t.Error(err) - } else { - status, err = helper.IsDeviceStateActive(deviceId) - // The below case will excute in both Device Active and Non-active states. - if err != nil || status { - if !status { - _, err = helper.IsDeviceStateActive(deviceId) - if err != nil { - t.Error(err) - } - } - err = helper.CleanTestDevice(deviceId) - if err != nil { - t.Error(err) - } - err = helper.CleanTestProject(projectId) - if err != nil { - t.Error(err) - } - } + t.Fatal(err) + } + + status, err = helper.IsDeviceStateActive(t, deviceId) + if err != nil { + t.Fatal(err) + } + if !status { + t.Fatalf("Device not yet active, %s", deviceId) } } }, diff --git a/test/e2e/devices/devicestarttest/device_start_test.go b/test/e2e/devices/devicestarttest/device_start_test.go index 23136d10..2f67223d 100644 --- a/test/e2e/devices/devicestarttest/device_start_test.go +++ b/test/e2e/devices/devicestarttest/device_start_test.go @@ -1,7 +1,6 @@ package devicestarttest import ( - "fmt" "io" "os" "strings" @@ -42,25 +41,40 @@ func TestCli_Devices_Update(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-start-pro") + 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.Error(err) + t.Fatal(err) } - deviceId, err = helper.CreateTestDevice(projectId, "metal-cli-start-dev") + + 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.Error(err) + t.Fatal(err) } - status, err = helper.IsDeviceStateActive(deviceId) + + status, err = helper.IsDeviceStateActive(t, deviceId) if err != nil { - _, err := helper.IsDeviceStateActive(deviceId) + _, err := helper.IsDeviceStateActive(t, deviceId) if err != nil { t.Error(err) } else { - err = helper.StopTestDevice(deviceId) + err = helper.StopTestDevice(t, deviceId) if err != nil { t.Error(err) } - status, err = helper.IsDeviceStateActive(deviceId) + status, err = helper.IsDeviceStateActive(t, deviceId) if err == nil { t.Error(err) } @@ -72,35 +86,26 @@ func TestCli_Devices_Update(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), "Device "+deviceId+" successfully started.") { - t.Error("expected output should include" + "Device " + deviceId + " successfully started." + "in the out string ") - } else { - status, _ = helper.IsDeviceStateActive(deviceId) - if err != nil || status { - if !status { - _, err = helper.IsDeviceStateActive(deviceId) - if err != nil { - t.Error(err) - } - } - fmt.Print("Device is Active") - err = helper.CleanTestDevice(deviceId) - if err != nil { - t.Error(err) - } - fmt.Print("Cleaned Test Device") - err = helper.CleanTestProject(projectId) - if err != nil { - t.Error(err) - } - fmt.Print("Cleaned Test Project") - } + t.Fatal("expected output should include" + "Device " + deviceId + " successfully started." + "in the out string ") + } + + status, err = helper.IsDeviceStateActive(t, deviceId) + if err != nil { + t.Fatal(err) + } + if !status { + t.Fatalf("Device not yet active, %s", deviceId) } } }, diff --git a/test/e2e/devices/devicestoptest/device_stop_test.go b/test/e2e/devices/devicestoptest/device_stop_test.go index d755d476..de52d074 100644 --- a/test/e2e/devices/devicestoptest/device_stop_test.go +++ b/test/e2e/devices/devicestoptest/device_stop_test.go @@ -40,40 +40,50 @@ func TestCli_Devices_Update(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-stop-pro") + 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.Error(err) + t.Fatal(err) } - deviceId, err = helper.CreateTestDevice(projectId, "metal-cli-stop-dev") + + 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.Error(err) + t.Fatal(err) } - status, err := helper.IsDeviceStateActive(deviceId) + + status, err := helper.IsDeviceStateActive(t, deviceId) if err != nil { - t.Error(err) + 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 err := root.Execute(); err != nil { - t.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), "Device "+deviceId+" successfully stopped.") { - t.Error("expected output should include" + "Device " + deviceId + " successfully stopped." + "in the out string ") - } else { - err = helper.CleanTestDevice(deviceId) - if err != nil { - t.Error(err) - } - err = helper.CleanTestProject(projectId) - if err != nil { - t.Error(err) - } + t.Fatal("expected output should include" + "Device " + deviceId + " 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 bb6d64e0..a5f9b8c0 100644 --- a/test/e2e/devices/deviceupdatetest/device_update_test.go +++ b/test/e2e/devices/deviceupdatetest/device_update_test.go @@ -40,41 +40,51 @@ func TestCli_Devices_Update(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-update-pro") + 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.Error(err) + t.Fatal(err) } - deviceId, err = helper.CreateTestDevice(projectId, "metal-cli-update-dev") + + 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.Error(err) + t.Fatal(err) } - status, err := helper.IsDeviceStateActive(deviceId) + + status, err := helper.IsDeviceStateActive(t, deviceId) if err != nil { - t.Error(err) + 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 err := root.Execute(); err != nil { - t.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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 ") } - - err = helper.CleanTestDevice(deviceId) - if err != nil { - t.Error(err) - } - err = helper.CleanTestProject(projectId) - if err != nil { - t.Error(err) - } } }, }, diff --git a/test/e2e/events/deviceeventstest/device_event_test.go b/test/e2e/events/deviceeventstest/device_event_test.go index ac09459d..fa910da5 100644 --- a/test/e2e/events/deviceeventstest/device_event_test.go +++ b/test/e2e/events/deviceeventstest/device_event_test.go @@ -41,18 +41,18 @@ func TestCli_Events_Get(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - - projectId, err = helper.CreateTestProject("metal-cli-events-pro") + projectName := "metal-cli-device-events" + helper.GenerateRandomString(5) + projectId, err = helper.CreateTestProject(t, projectName) if err != nil { t.Error(err) } - deviceId, err = helper.CreateTestDevice(projectId, "metal-cli-events-dev") + deviceId, err = helper.CreateTestDevice(t, projectId, "metal-cli-events-dev") if err != nil { t.Error(err) } - status, err = helper.IsDeviceStateActive(deviceId) + status, err = helper.IsDeviceStateActive(t, deviceId) if err != nil { - status, err = helper.IsDeviceStateActive(deviceId) + status, err = helper.IsDeviceStateActive(t, deviceId) if err != nil || !status { t.Error(err) } @@ -61,22 +61,26 @@ func TestCli_Events_Get(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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(deviceId) + err = helper.CleanTestDevice(t, deviceId) if err != nil { t.Error(err) } - err = helper.CleanTestProject(projectId) + 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 499dc32f..6db71251 100644 --- a/test/e2e/events/projecteventstest/project_events_test.go +++ b/test/e2e/events/projecteventstest/project_events_test.go @@ -41,7 +41,8 @@ func TestCli_Events_Get(t *testing.T) { cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-events-pro") + projectName := "metal-cli-projects-events" + helper.GenerateRandomString(5) + projectId, err = helper.CreateTestProject(t, projectName) if err != nil { t.Error(err) } @@ -49,16 +50,20 @@ func TestCli_Events_Get(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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(projectId) + 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 f135ba7a..af353f2d 100644 --- a/test/e2e/gateways/create_test.go +++ b/test/e2e/gateways/create_test.go @@ -25,9 +25,9 @@ func TestGateways_Create(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-gateways-create") t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil && + if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil && !strings.Contains(err.Error(), "Not Found") { t.Error(err) } @@ -36,9 +36,9 @@ func TestGateways_Create(t *testing.T) { return } - vlan, err := helper.CreateTestVLAN(projectId) + vlan, err := helper.CreateTestVLAN(t, projectId) t.Cleanup(func() { - if err := helper.CleanTestVlan(vlan.GetId()); err != nil && + if err := helper.CleanTestVlan(t, vlan.GetId()); err != nil && !strings.Contains(err.Error(), "Not Found") { t.Error(err) } @@ -66,12 +66,16 @@ func TestGateways_Create(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout apiClient := helper.TestClient() gateways, _, err := apiClient.MetalGatewaysApi. @@ -79,8 +83,7 @@ func TestGateways_Create(t *testing.T) { Include([]string{"ip_reservation"}). Execute() if err != nil { - t.Error(err) - return + t.Fatal(err) } if len(gateways.MetalGateways) != 1 { t.Error(errors.New("Gateway Not Found. Failed to create gateway")) diff --git a/test/e2e/gateways/delete_test.go b/test/e2e/gateways/delete_test.go index 84e7eb9b..8f4a6458 100644 --- a/test/e2e/gateways/delete_test.go +++ b/test/e2e/gateways/delete_test.go @@ -25,13 +25,13 @@ func TestGateways_Delete(t *testing.T) { rootClient := root.NewClient(consumerToken, apiURL, Version) defer func() { - if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil { t.Error(err) } }() - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-gateway-delete") t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil && + if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil && !strings.Contains(err.Error(), "Not Found") { t.Error(err) } @@ -40,9 +40,9 @@ func TestGateways_Delete(t *testing.T) { return } - vlan, err := helper.CreateTestVLAN(projectId) + vlan, err := helper.CreateTestVLAN(t, projectId) t.Cleanup(func() { - if err := helper.CleanTestVlan(vlan.GetId()); err != nil && + if err := helper.CleanTestVlan(t, vlan.GetId()); err != nil && !strings.Contains(err.Error(), "Not Found") { t.Error("Error while cleaning up vLan", err) } @@ -53,9 +53,9 @@ func TestGateways_Delete(t *testing.T) { } subnetSize := int32(8) - metalGateway, err := helper.CreateTestGateway(projectId, vlan.GetId(), &subnetSize) + metalGateway, err := helper.CreateTestGateway(t, projectId, vlan.GetId(), &subnetSize) t.Cleanup(func() { - if err := helper.CleanTestGateway(metalGateway.GetId()); err != nil && + if err := helper.CleanTestGateway(t, metalGateway.GetId()); err != nil && !strings.Contains(err.Error(), "Not Found") { t.Error(err) } @@ -83,12 +83,16 @@ func TestGateways_Delete(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout apiClient := helper.TestClient() gateways, _, err := apiClient.MetalGatewaysApi. diff --git a/test/e2e/gateways/retrieve_test.go b/test/e2e/gateways/retrieve_test.go index c633edd4..3bcafea6 100644 --- a/test/e2e/gateways/retrieve_test.go +++ b/test/e2e/gateways/retrieve_test.go @@ -23,9 +23,9 @@ func TestGateways_Retrieve(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-gateway-get") t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil { t.Error(err) } }) @@ -33,9 +33,9 @@ func TestGateways_Retrieve(t *testing.T) { return } - vlan, err := helper.CreateTestVLAN(projectId) + vlan, err := helper.CreateTestVLAN(t, projectId) t.Cleanup(func() { - if err := helper.CleanTestVlan(vlan.GetId()); err != nil { + if err := helper.CleanTestVlan(t, vlan.GetId()); err != nil { t.Error(err) } }) @@ -45,9 +45,9 @@ func TestGateways_Retrieve(t *testing.T) { } subnetSize := int32(8) - metalGateway, err := helper.CreateTestGateway(projectId, vlan.GetId(), &subnetSize) + metalGateway, err := helper.CreateTestGateway(t, projectId, vlan.GetId(), &subnetSize) t.Cleanup(func() { - if err := helper.CleanTestGateway(metalGateway.GetId()); err != nil && + if err := helper.CleanTestGateway(t, metalGateway.GetId()); err != nil && !strings.Contains(err.Error(), "Not Found") { t.Error(err) } @@ -76,12 +76,16 @@ func TestGateways_Retrieve(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout assertGatewaysCmdOutput(t, string(out[:]), metalGateway.GetId(), device.Metro.GetCode(), strconv.Itoa(int(vlan.GetVxlan()))) }, diff --git a/test/e2e/ipstest/ips_get_test.go b/test/e2e/ipstest/ips_get_test.go index 2e72b21d..acf186cc 100644 --- a/test/e2e/ipstest/ips_get_test.go +++ b/test/e2e/ipstest/ips_get_test.go @@ -43,32 +43,37 @@ func TestCli_Ips_Get(t *testing.T) { t.Skip("Skipping this test because someCondition is true") } root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-ips-get-pro") + projectName := "metal-cli-ips-get" + helper.GenerateRandomString(5) + projectId, err = helper.CreateTestProject(t, projectName) if err != nil { t.Error(err) } - ipsId, err = helper.CreateTestIps(projectId, 1, "public_ipv4") + 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 + }) + if err := root.Execute(); err != nil { - t.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), ipsId) && !strings.Contains(string(out[:]), "da") { t.Error("expected output should include " + ipsId + " da strings in the out string") } - err = helper.CleanTestIps(ipsId) + err = helper.CleanTestIps(t, ipsId) if err != nil { t.Error(err) } - err = helper.CleanTestProject(projectId) + 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 fcb01148..a7bb6ee9 100644 --- a/test/e2e/ipstest/ips_request_test.go +++ b/test/e2e/ipstest/ips_request_test.go @@ -44,7 +44,8 @@ func TestCli_Vlan_Create(t *testing.T) { t.Skip("Skipping temporarily for now") } root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-ips-create-pro") + projectName := "metal-cli-ips-get" + helper.GenerateRandomString(5) + projectId, err = helper.CreateTestProject(t, projectName) if err != nil { t.Error(err) } @@ -54,19 +55,23 @@ func TestCli_Vlan_Create(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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(projectId) + err = helper.CleanTestProject(t, projectId) if err != nil { t.Error(err) } diff --git a/test/e2e/metrotest/metro_test.go b/test/e2e/metrotest/metro_test.go index 4042e27b..1688c22f 100644 --- a/test/e2e/metrotest/metro_test.go +++ b/test/e2e/metrotest/metro_test.go @@ -41,12 +41,16 @@ func TestCli_Metros(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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 61ffcee8..add4a628 100644 --- a/test/e2e/ostest/os_test.go +++ b/test/e2e/ostest/os_test.go @@ -41,12 +41,16 @@ func TestCli_OperatingSystem(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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 f7a53210..7954fbf6 100644 --- a/test/e2e/plantest/plan_test.go +++ b/test/e2e/plantest/plan_test.go @@ -44,12 +44,16 @@ func TestCli_Plans(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), "m3.small.x86") && !strings.Contains(string(out[:]), "m3.large.x86") && !strings.Contains(string(out[:]), "c3.medium.x86") && @@ -73,12 +77,16 @@ func TestCli_Plans(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), "m3.small.x86") { t.Error("expected output should include m3.small.x86 by SLUG") } @@ -97,12 +105,17 @@ func TestCli_Plans(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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 bc974e7e..4a628028 100644 --- a/test/e2e/ports/convert_test.go +++ b/test/e2e/ports/convert_test.go @@ -24,9 +24,9 @@ func TestPorts_Convert(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-port-convert") t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil { t.Error(err) } }) @@ -60,12 +60,16 @@ func TestPorts_Convert(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout assertPortCmdOutput(t, port, string(out[:]), "layer2-individual", false) }, @@ -82,12 +86,16 @@ func TestPorts_Convert(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout assertPortCmdOutput(t, port, string(out[:]), "layer2-bonded", true) }, @@ -104,12 +112,16 @@ func TestPorts_Convert(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout assertPortCmdOutput(t, port, string(out[:]), "layer3", true) }, diff --git a/test/e2e/ports/retrieve_test.go b/test/e2e/ports/retrieve_test.go index 50a4a76c..920c6e0e 100644 --- a/test/e2e/ports/retrieve_test.go +++ b/test/e2e/ports/retrieve_test.go @@ -22,9 +22,9 @@ func TestPorts_Retrieve(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-port-get") t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil { t.Error(err) } }) @@ -55,12 +55,16 @@ func TestPorts_Retrieve(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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 c14bd629..d26971dd 100644 --- a/test/e2e/ports/vlans_test.go +++ b/test/e2e/ports/vlans_test.go @@ -24,9 +24,9 @@ func TestPorts_VLANs(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId, "metal-cli-port-vlan") t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + if err := helper.CleanupProjectAndDevice(t, deviceId, projectId); err != nil { t.Error(err) } }) @@ -45,12 +45,12 @@ func TestPorts_VLANs(t *testing.T) { return } - vlan, err := helper.CreateTestVLAN(projectId) + vlan, err := helper.CreateTestVLAN(t, projectId) t.Cleanup(func() { - if err := helper.UnAssignPortVlan(port.GetId(), vlan.GetId()); err != nil { + if err := helper.UnAssignPortVlan(t, port.GetId(), vlan.GetId()); err != nil { t.Error(err) } - if err := helper.CleanTestVlan(vlan.GetId()); err != nil { + if err := helper.CleanTestVlan(t, vlan.GetId()); err != nil { t.Error(err) } }) @@ -79,17 +79,20 @@ func TestPorts_VLANs(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout // wait for port to have vlans attached - if err := helper.WaitForAttachVlanToPort(port.GetId(), true); err != nil { - t.Error(err) - return + if err := helper.WaitForAttachVlanToPort(t, port.GetId(), true); err != nil { + t.Fatal(err) } assertPortCmdOutput(t, port, string(out[:]), "layer2-bonded", true) diff --git a/test/e2e/projecttest/project_test.go b/test/e2e/projecttest/project_test.go index a202033e..f93b7feb 100644 --- a/test/e2e/projecttest/project_test.go +++ b/test/e2e/projecttest/project_test.go @@ -33,13 +33,13 @@ func setupTestOrganization(t *testing.T, projectName string) (string, func()) { // 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(projectName) + projectId, err := helper.CreateTestProject(t, projectName) if err != nil { t.Fatal(err) } teardown := func() { - err := helper.CleanTestProject(projectId) + err := helper.CleanTestProject(t, projectId) if err != nil { t.Error(err) } @@ -54,7 +54,7 @@ func TestCli_Project_Tests(t *testing.T) { apiURL := "" Version := "metal" rootClient := root.NewClient(consumerToken, apiURL, Version) - randName, _ := helper.GenerateRandomString(32) + randName := helper.GenerateRandomString(32) type fields struct { MainCmd *cobra.Command @@ -85,12 +85,16 @@ func TestCli_Project_Tests(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), projName) { t.Error("expected output should include " + projName + ", in the out string ") } @@ -119,12 +123,16 @@ func TestCli_Project_Tests(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), updateProjName) && !strings.Contains(string(out[:]), projectId) { t.Error("expected output should include " + updateProjName + projectId + ", in the out string ") @@ -152,12 +160,16 @@ func TestCli_Project_Tests(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), projName) && !strings.Contains(string(out[:]), projectId) { t.Error("expected output should include " + projName + projectId + ", in the out string ") @@ -185,12 +197,16 @@ func TestCli_Project_Tests(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), projName) && !strings.Contains(string(out[:]), projectId) { t.Error("expected output should include " + projName + projectId + ", in the out string ") @@ -218,12 +234,16 @@ func TestCli_Project_Tests(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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))) @@ -254,12 +274,16 @@ func TestCli_Project_Tests(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), projectId) && !strings.Contains(string(out[:]), asn) && !strings.Contains(string(out[:]), dtype) { @@ -298,12 +322,16 @@ func TestCli_Project_Tests(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout if !strings.Contains(string(out[:]), projectId) && !strings.Contains(string(out[:]), asn) && !strings.Contains(string(out[:]), status) && diff --git a/test/e2e/sshtest/ssh_test.go b/test/e2e/sshtest/ssh_test.go index 17b882c3..22ca3eef 100644 --- a/test/e2e/sshtest/ssh_test.go +++ b/test/e2e/sshtest/ssh_test.go @@ -41,12 +41,16 @@ func TestCli_SshKey(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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 d5e6ea97..c80141a5 100644 --- a/test/e2e/usertest/user_test.go +++ b/test/e2e/usertest/user_test.go @@ -41,12 +41,16 @@ func TestCli_Users(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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 f244e044..eca53d18 100644 --- a/test/e2e/vlan/vlan_creat_test.go +++ b/test/e2e/vlan/vlan_creat_test.go @@ -40,7 +40,8 @@ func TestCli_Vlan_Create(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-vlan-create-pro") + projectName := "metal-cli-vlan-create-pro" + helper.GenerateRandomString(5) + projectId, err = helper.CreateTestProject(t, projectName) if err != nil { t.Error(err) } @@ -49,19 +50,24 @@ func TestCli_Vlan_Create(t *testing.T) { 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout + 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(projectId) + err = helper.CleanTestProject(t, projectId) if err != nil { t.Error(err) } diff --git a/test/e2e/vlan/vlan_delete_test.go b/test/e2e/vlan/vlan_delete_test.go index af85c5e0..5aa5dd68 100644 --- a/test/e2e/vlan/vlan_delete_test.go +++ b/test/e2e/vlan/vlan_delete_test.go @@ -40,26 +40,31 @@ func TestCli_Vlan_Clean(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-vlan-get-pro") + projectName := "metal-cli-vlan-get-pro" + helper.GenerateRandomString(5) + projectId, err = helper.CreateTestProject(t, projectName) if err != nil { t.Error(err) } - vlanId, err = helper.CreateTestVlanWithVxLan(projectId, 2023, "metal-cli-vlan-get-test") + vlanId, err = helper.CreateTestVlanWithVxLan(t, projectId, 2023, "metal-cli-vlan-get-test") if len(projectId) != 0 && 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.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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(projectId) + 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 3d959d36..296e447b 100644 --- a/test/e2e/vlan/vlan_get_test.go +++ b/test/e2e/vlan/vlan_get_test.go @@ -40,33 +40,38 @@ func TestCli_Vlan_Get(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-vlan-delete-pro") + projectName := "metal-cli-vlan-delete-pro" + helper.GenerateRandomString(5) + projectId, err = helper.CreateTestProject(t, projectName) if err != nil { t.Error(err) } - vlanId, err = helper.CreateTestVlanWithVxLan(projectId, 2023, "metal-cli-vlan-delete-test") + 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 + }) + if err := root.Execute(); err != nil { - t.Error(err) + t.Fatal(err) } - w.Close() + out, _ := io.ReadAll(r) - os.Stdout = rescueStdout 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(vlanId) + err = helper.CleanTestVlan(t, vlanId) if err != nil { t.Error(err) } - err = helper.CleanTestProject(projectId) + err = helper.CleanTestProject(t, projectId) if err != nil { t.Error(err) } diff --git a/test/helper/helper.go b/test/helper/helper.go index 4f910406..d5363190 100644 --- a/test/helper/helper.go +++ b/test/helper/helper.go @@ -2,13 +2,13 @@ package helper import ( "context" - "crypto/rand" - "encoding/base64" "fmt" "os" "testing" "time" + "golang.org/x/exp/rand" + openapiclient "github.com/equinix/equinix-sdk-go/services/metalv1" "github.com/pkg/errors" ) @@ -22,21 +22,21 @@ func TestClient() *openapiclient.APIClient { return apiClient } -func CreateTestProject(name string) (string, error) { +func CreateTestProject(t *testing.T, name string) (string, error) { + t.Helper() TestApiClient := TestClient() projectCreateFromRootInput := *openapiclient.NewProjectCreateFromRootInput(name) // ProjectCreateFromRootInput | Project to create - projectResp, r, err := TestApiClient.ProjectsApi.CreateProject(context.Background()).ProjectCreateFromRootInput(projectCreateFromRootInput).Execute() + projectResp, _, err := TestApiClient.ProjectsApi.CreateProject(context.Background()).ProjectCreateFromRootInput(projectCreateFromRootInput).Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `ProjectsApi.CreateProject`: %v\n", err) - fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r) - return "", err + return "", fmt.Errorf("Error when calling `ProjectsApi.CreateProject`: %v\n", err) } return projectResp.GetId(), nil } -func CreateTestDevice(projectId, name string) (string, error) { +func CreateTestDevice(t *testing.T, projectId, name string) (string, error) { + t.Helper() TestApiClient := TestClient() hostname := name @@ -53,14 +53,14 @@ func CreateTestDevice(projectId, name string) (string, error) { CreateDeviceRequest(metroDeviceRequest). Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.CreateDevice`: %v\n", err) - return "", err + return "", fmt.Errorf("Error when calling `DevicesApi.CreateDevice`: %v\n", err) } return deviceResp.GetId(), nil } -func CreateTestVLAN(projectId string) (*openapiclient.VirtualNetwork, error) { +func CreateTestVLAN(t *testing.T, projectId string) (*openapiclient.VirtualNetwork, error) { TestApiClient := TestClient() + t.Helper() metro := "sv" vlanCreateInput := openapiclient.VirtualNetworkCreateInput{ @@ -71,14 +71,14 @@ func CreateTestVLAN(projectId string) (*openapiclient.VirtualNetwork, error) { VirtualNetworkCreateInput(vlanCreateInput). Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `VLANsApi.CreateVirtualNetwork`: %v\n", err) - return nil, err + return nil, fmt.Errorf("Error when calling `VLANsApi.CreateVirtualNetwork`: %v\n", err) } return vlan, nil } -func CreateTestGateway(projectId, vlanId string, privateIPv4SubnetSize *int32) (*openapiclient.MetalGateway, error) { +func CreateTestGateway(t *testing.T, projectId, vlanId string, privateIPv4SubnetSize *int32) (*openapiclient.MetalGateway, error) { TestApiClient := TestClient() + t.Helper() gatewayCreateInput := openapiclient.CreateMetalGatewayRequest{ MetalGatewayCreateInput: &openapiclient.MetalGatewayCreateInput{ @@ -92,14 +92,15 @@ func CreateTestGateway(projectId, vlanId string, privateIPv4SubnetSize *int32) ( CreateMetalGatewayRequest(gatewayCreateInput). Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `MetalGatewaysApi.CreateMetalGateway`: %v\n", err) - return nil, err + return nil, fmt.Errorf("Error when calling `MetalGatewaysApi.CreateMetalGateway`: %v\n", err) } return gateway.MetalGateway, nil } -func GetDeviceById(deviceId string) (*openapiclient.Device, error) { +func GetDeviceById(t *testing.T, deviceId string) (*openapiclient.Device, error) { TestApiClient := TestClient() + t.Helper() + includes := []string{"network_ports"} device, _, err := TestApiClient.DevicesApi. @@ -107,14 +108,14 @@ func GetDeviceById(deviceId string) (*openapiclient.Device, error) { Include(includes). Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.FindDeviceById`: %v\n", err) - return nil, err + return nil, fmt.Errorf("Error when calling `DevicesApi.FindDeviceById`: %v\n", err) } return device, nil } -func GetPortById(portId string) (*openapiclient.Port, error) { +func GetPortById(t *testing.T, portId string) (*openapiclient.Port, error) { + t.Helper() TestApiClient := TestClient() includes := []string{"virtual_network"} @@ -123,19 +124,19 @@ func GetPortById(portId string) (*openapiclient.Port, error) { Include(includes). Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `PortsApi.FindPortById`: %v\n", err) - return nil, err + return nil, fmt.Errorf("Error when calling `PortsApi.FindPortById`: %v\n", err) } return port, nil } -func IsDeviceStateActive(deviceId string) (bool, error) { +func IsDeviceStateActive(t *testing.T, deviceId string) (bool, error) { + t.Helper() predefinedTime := 500 * time.Second // Adjust this as needed retryInterval := 10 * time.Second // Adjust this as needed startTime := time.Now() for time.Since(startTime) < predefinedTime { - device, err := GetDeviceById(deviceId) + device, err := GetDeviceById(t, deviceId) if err != nil { return false, err } @@ -149,7 +150,8 @@ func IsDeviceStateActive(deviceId string) (bool, error) { return false, fmt.Errorf("timed out waiting for device %v to become active", deviceId) } -func WaitForAttachVlanToPort(portId string, attach bool) error { +func WaitForAttachVlanToPort(t *testing.T, portId string, attach bool) error { + t.Helper() ticker := time.NewTicker(5 * time.Second) defer ticker.Stop() @@ -162,7 +164,7 @@ func WaitForAttachVlanToPort(portId string, attach bool) error { case <-ctx.Done(): return fmt.Errorf("Timeout exceeded for vlan assignment with port ID: %s", portId) case <-ticker.C: - port, err := GetPortById(portId) + port, err := GetPortById(t, portId) if err != nil { return errors.Wrapf(err, "Error while fetching the port using ID: %s", portId) } @@ -181,48 +183,47 @@ func WaitForAttachVlanToPort(portId string, attach bool) error { } } -func StopTestDevice(deviceId string) error { +func StopTestDevice(t *testing.T, deviceId string) error { + t.Helper() deviceActionInput := *openapiclient.NewDeviceActionInput("power_off") TestApiClient := TestClient() _, err := TestApiClient.DevicesApi.PerformAction(context.Background(), deviceId).DeviceActionInput(deviceActionInput).Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.PerformAction``: %v\n", err) - return err + return fmt.Errorf("Error when calling `DevicesApi.PerformAction``: %v\n", err) } return nil } -func CleanTestDevice(deviceId string) error { - forceDelete := true // bool | Force the deletion of the device, by detaching any storage volume still active. (optional) +func CleanTestDevice(t *testing.T, deviceId string) error { + t.Helper() TestApiClient := TestClient() _, err := TestApiClient.DevicesApi. DeleteDevice(context.Background(), deviceId). - ForceDelete(forceDelete). + ForceDelete(true). Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.DeleteDevice``: %v\n", err) - return err + return fmt.Errorf("Error when calling `DevicesApi.DeleteDevice``: %v\n", err) } return nil } -func CleanTestProject(projectId string) error { +func CleanTestProject(t *testing.T, projectId string) error { + t.Helper() TestApiClient := TestClient() - r, err := TestApiClient.ProjectsApi. + _, err := TestApiClient.ProjectsApi. DeleteProject(context.Background(), projectId). Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `ProjectsApi.DeleteProject``: %v\n", err) - fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r) - return err + return fmt.Errorf("Error when calling `ProjectsApi.DeleteProject``: %v\n", err) } return nil } -func CreateTestIps(projectId string, quantity int, ipType string) (string, error) { +func CreateTestIps(t *testing.T, projectId string, quantity int, ipType string) (string, error) { + t.Helper() TestApiClient := TestClient() metro := "da" var tags []string @@ -242,23 +243,23 @@ func CreateTestIps(projectId string, quantity int, ipType string) (string, error ipsresp, _, err := TestApiClient.IPAddressesApi.RequestIPReservation(context.Background(), projectId).RequestIPReservationRequest(*requestIPReservationRequest).Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `IPAddressesApi.FindIPReservations``: %v\n", err) - return "", err + return "", fmt.Errorf("Error when calling `IPAddressesApi.FindIPReservations``: %v\n", err) } return ipsresp.IPReservation.GetId(), nil } -func CleanTestIps(ipsId string) error { +func CleanTestIps(t *testing.T, ipsId string) error { + t.Helper() TestApiClient := TestClient() _, err := TestApiClient.IPAddressesApi.DeleteIPAddress(context.Background(), ipsId).Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `IPAddressesApi.DeleteIPAddress``: %v\n", err) - return err + return fmt.Errorf("Error when calling `IPAddressesApi.DeleteIPAddress``: %v\n", err) } return nil } -func CreateTestVlanWithVxLan(projectId string, Id int, desc string) (string, error) { +func CreateTestVlanWithVxLan(t *testing.T, projectId string, Id int, desc string) (string, error) { + t.Helper() TestApiClient := TestClient() virtualNetworkCreateInput := *openapiclient.NewVirtualNetworkCreateInput() virtualNetworkCreateInput.SetDescription(desc) @@ -267,24 +268,24 @@ func CreateTestVlanWithVxLan(projectId string, Id int, desc string) (string, err vlanresp, _, err := TestApiClient.VLANsApi.CreateVirtualNetwork(context.Background(), projectId).VirtualNetworkCreateInput(virtualNetworkCreateInput).Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `VLANsApi.CreateVirtualNetwork``: %v\n", err) - return "", err + return "", fmt.Errorf("Error when calling `VLANsApi.CreateVirtualNetwork``: %v\n", err) } return vlanresp.GetId(), nil } -func CleanTestVlan(vlanId string) error { +func CleanTestVlan(t *testing.T, vlanId string) error { + t.Helper() TestApiClient := TestClient() _, _, err := TestApiClient.VLANsApi.DeleteVirtualNetwork(context.Background(), vlanId).Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `VLANsApi.DeleteVirtualNetwork``: %v\n", err) - return err + return fmt.Errorf("Error when calling `VLANsApi.DeleteVirtualNetwork``: %v\n", err) } return nil } -func UnAssignPortVlan(portId, vlanId string) error { +func UnAssignPortVlan(t *testing.T, portId, vlanId string) error { + t.Helper() testClient := TestClient() _, _, err := testClient.PortsApi. UnassignPort(context.Background(), portId). @@ -293,14 +294,15 @@ func UnAssignPortVlan(portId, vlanId string) error { return err } -func CleanupProjectAndDevice(deviceId, projectId string) error { - resp, err := IsDeviceStateActive(deviceId) +func CleanupProjectAndDevice(t *testing.T, deviceId, projectId string) error { + t.Helper() + resp, err := IsDeviceStateActive(t, deviceId) if err == nil && resp { - err = CleanTestDevice(deviceId) + err = CleanTestDevice(t, deviceId) if err != nil { return err } - err = CleanTestProject(projectId) + err = CleanTestProject(t, projectId) if err != nil { return err } @@ -310,23 +312,24 @@ func CleanupProjectAndDevice(deviceId, projectId string) error { } //nolint:staticcheck -func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) *openapiclient.Device { +func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string, projPrefix string) *openapiclient.Device { t.Helper() - projId, err := CreateTestProject("metal-cli-test-project") + projectName := projPrefix + GenerateRandomString(5) + projId, err := CreateTestProject(t, projectName) if err != nil { t.Fatal(err) return nil } *projectId = projId - devId, err := CreateTestDevice(*projectId, "metal-cli-test-device") + devId, err := CreateTestDevice(t, *projectId, "metal-cli-test-device") if err != nil { t.Fatal(err) return nil } *deviceId = devId - active, err := IsDeviceStateActive(*deviceId) + active, err := IsDeviceStateActive(t, *deviceId) if err != nil { t.Fatal(err) return nil @@ -336,7 +339,7 @@ func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) *openapicl return nil } - device, err := GetDeviceById(*deviceId) + device, err := GetDeviceById(t, *deviceId) if err != nil { t.Fatal(err) return nil @@ -349,15 +352,16 @@ func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) *openapicl return device } -func CleanTestGateway(gatewayId string) error { +func CleanTestGateway(t *testing.T, gatewayId string) error { + t.Helper() + TestApiClient := TestClient() _, _, err := TestApiClient.MetalGatewaysApi. DeleteMetalGateway(context.Background(), gatewayId). Include([]string{"ip_reservation"}). Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `MetalGatewaysApi.DeleteMetalGateway``: %v\n", err) - return err + return fmt.Errorf("Error when calling `MetalGatewaysApi.DeleteMetalGateway``: %v\n", err) } return nil @@ -383,8 +387,7 @@ func CreateTestOrganization(name string) (string, error) { resp, _, err := TestApiClient.OrganizationsApi.CreateOrganization(context.Background()).OrganizationInput(*organizationInput).Include(defaultIncludes).Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `OrganizationsApi.CreateOrganization``: %v\n", err) - return "", err + return "", fmt.Errorf("Error when calling `OrganizationsApi.CreateOrganization``: %v\n", err) } return resp.GetId(), nil @@ -395,8 +398,7 @@ func CleanTestOrganization(orgId string) error { _, err := TestApiClient.OrganizationsApi.DeleteOrganization(context.Background(), orgId).Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `OrganizationsApi.DeleteOrganization``: %v\n", err) - return err + return fmt.Errorf("Error when calling `OrganizationsApi.DeleteOrganization``: %v\n", err) } return nil @@ -409,31 +411,19 @@ func CreateTestBgpEnableTest(projId string) error { _, err := TestApiClient.BGPApi.RequestBgpConfig(context.Background(), projId).BgpConfigRequestInput(bgpConfigRequestInput).Execute() if err != nil { - fmt.Fprintf(os.Stderr, "Error when calling `BGPApi.RequestBgpConfig``: %v\n", err) - return err + return fmt.Errorf("Error when calling `BGPApi.RequestBgpConfig``: %v\n", err) } return nil } //nolint:staticcheck -func GenerateRandomString(length int) (string, error) { - // Calculate the number of bytes needed for the given string length - numBytes := (length * 3) / 4 - - // Create a byte slice to store the random bytes - randomBytes := make([]byte, numBytes) +func GenerateRandomString(length int) string { + charSet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + random := rand.New(rand.NewSource(uint64(time.Now().UnixNano()))) - // Read random bytes from the crypto/rand package - _, err := rand.Read(randomBytes) - if err != nil { - return "", err + result := make([]byte, length) + for i := range result { + result[i] = charSet[random.Intn(len(charSet))] } - - // Encode the random bytes to base64 to get a string - randomString := base64.URLEncoding.EncodeToString(randomBytes) - - // Trim any padding characters - randomString = randomString[:length] - - return randomString, nil + return string(result) }