Skip to content

Commit

Permalink
chore: refactor tests to fix timeouts and improve cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreatma committed Dec 20, 2023
1 parent a25763c commit dc7d337
Show file tree
Hide file tree
Showing 28 changed files with 359 additions and 1,162 deletions.
75 changes: 7 additions & 68 deletions test/e2e/capacitytest/capacity_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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") &&
Expand All @@ -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") &&
Expand All @@ -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") &&
Expand All @@ -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") {
Expand All @@ -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") &&
Expand All @@ -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") &&
Expand Down
75 changes: 26 additions & 49 deletions test/e2e/devices/devicecreateflagstest/device_create_flags_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package devicecreateflagstest

import (
"io"
"os"
"regexp"
"strings"
"testing"
Expand All @@ -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 := ""
Expand All @@ -42,59 +40,38 @@ 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)

if len(projectId) != 0 {
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/"})

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.Fatal(err)
}
out := helper.ExecuteAndCaptureOutput(t, root)

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 + ` *\|`
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[:]))
// 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() {
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[:]))
}
},
},
Expand Down
75 changes: 26 additions & 49 deletions test/e2e/devices/devicecreatetest/device_create_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package devicecreatetest

import (
"io"
"os"
"regexp"
"strings"
"testing"
Expand All @@ -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 := ""
Expand Down Expand Up @@ -44,60 +42,39 @@ 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() {
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[:]))
}
},
},
Expand Down
Loading

0 comments on commit dc7d337

Please sign in to comment.