Skip to content

Commit

Permalink
fix: Add cleanups in device,vlan and ports e2e tests to stabilise the…
Browse files Browse the repository at this point in the history
… CI pipeline (#405)

fixes #343
fixes #387
  • Loading branch information
displague authored Dec 18, 2023
2 parents 45b7930 + 149fcf8 commit 4b0a89e
Show file tree
Hide file tree
Showing 28 changed files with 601 additions and 411 deletions.
60 changes: 42 additions & 18 deletions test/e2e/capacitytest/capacity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") &&
Expand All @@ -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") &&
Expand All @@ -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") &&
Expand All @@ -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") {
Expand All @@ -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") &&
Expand All @@ -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") &&
Expand Down
41 changes: 25 additions & 16 deletions test/e2e/devices/devicecreateflagstest/device_create_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand All @@ -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") {
Expand All @@ -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)
}
}
}
Expand Down
55 changes: 33 additions & 22 deletions test/e2e/devices/devicecreatetest/device_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,49 +43,59 @@ 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[:]))

// 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)
}
}
}
Expand Down
52 changes: 28 additions & 24 deletions test/e2e/devices/devicegettest/device_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand All @@ -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)
}
},
},
Expand Down
Loading

0 comments on commit 4b0a89e

Please sign in to comment.