From ebe9ee693d0b9f02c587d363568e8e13608b06d0 Mon Sep 17 00:00:00 2001 From: Ayush Rangwala Date: Thu, 19 Oct 2023 19:46:40 +0530 Subject: [PATCH] fix goimports and vlan test Signed-off-by: Ayush Rangwala --- internal/ports/port.go | 3 ++- test/e2e/ports/convert_test.go | 42 +++---------------------------- test/e2e/ports/retrieve_test.go | 2 +- test/e2e/ports/vlans_test.go | 36 ++++++++++++++++++++------ test/e2e/vlan/vlan_delete_test.go | 2 +- test/e2e/vlan/vlan_get_test.go | 2 +- test/helper/helper.go | 37 ++++++++++++++++++++++++++- 7 files changed, 72 insertions(+), 52 deletions(-) diff --git a/internal/ports/port.go b/internal/ports/port.go index 2b246b5b..4bd8a9e2 100644 --- a/internal/ports/port.go +++ b/internal/ports/port.go @@ -21,8 +21,9 @@ package ports import ( - metal "github.com/equinix-labs/metal-go/metal/v1" "github.com/equinix/metal-cli/internal/outputs" + + metal "github.com/equinix-labs/metal-go/metal/v1" "github.com/spf13/cobra" ) diff --git a/test/e2e/ports/convert_test.go b/test/e2e/ports/convert_test.go index cbb87c2d..3f686b32 100644 --- a/test/e2e/ports/convert_test.go +++ b/test/e2e/ports/convert_test.go @@ -7,14 +7,12 @@ import ( "strings" "testing" - "github.com/equinix/metal-cli/internal/ports" - - metal "github.com/equinix-labs/metal-go/metal/v1" - root "github.com/equinix/metal-cli/internal/cli" outputPkg "github.com/equinix/metal-cli/internal/outputs" + "github.com/equinix/metal-cli/internal/ports" "github.com/equinix/metal-cli/test/helper" + metal "github.com/equinix-labs/metal-go/metal/v1" "github.com/spf13/cobra" ) @@ -26,7 +24,7 @@ func TestPorts_Convert(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - portList := setupProjectAndDevice(t, &projectId, &deviceId) + portList := helper.SetupProjectAndDevice(t, &projectId, &deviceId) port := &portList[2] defer func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { @@ -123,40 +121,6 @@ func TestPorts_Convert(t *testing.T) { } } -//nolint:staticcheck -func setupProjectAndDevice(t *testing.T, projectId, deviceId *string) []metal.Port { - projId, err := helper.CreateTestProject("metal-cli-test-ports-project") - if err != nil { - t.Error(err) - } - projectId = &projId - - devId, err := helper.CreateTestDevice(*projectId, "metal-cli-test-ports-device") - if err != nil { - t.Error(err) - } - deviceId = &devId - - active, err := helper.IsDeviceStateActive(*deviceId) - if err != nil { - t.Error(err) - } - if !active { - t.Errorf("Timeout while waiting for device: %s to be active", *deviceId) - } - - device, err := helper.GetDeviceById(*deviceId) - if err != nil { - t.Error(err) - return nil - } - if len(device.NetworkPorts) < 3 { - t.Errorf("All 3 ports doesnot exist for the created device: %s", device.GetId()) - } - - return device.GetNetworkPorts() -} - func assertPortCmdOutput(t *testing.T, port *metal.Port, out, networkType string, bonded bool) { if !strings.Contains(out, port.GetId()) { t.Errorf("cmd output should contain ID of the port: %s", port.GetId()) diff --git a/test/e2e/ports/retrieve_test.go b/test/e2e/ports/retrieve_test.go index 771e9cd5..7648dd56 100644 --- a/test/e2e/ports/retrieve_test.go +++ b/test/e2e/ports/retrieve_test.go @@ -22,7 +22,7 @@ func TestPorts_Retrieve(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - portList := setupProjectAndDevice(t, &projectId, &deviceId) + portList := helper.SetupProjectAndDevice(t, &projectId, &deviceId) port := &portList[2] defer func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { diff --git a/test/e2e/ports/vlans_test.go b/test/e2e/ports/vlans_test.go index b63fe614..d7b152b0 100644 --- a/test/e2e/ports/vlans_test.go +++ b/test/e2e/ports/vlans_test.go @@ -1,6 +1,7 @@ package ports import ( + "context" "io" "os" "strconv" @@ -11,6 +12,7 @@ import ( "github.com/equinix/metal-cli/internal/ports" "github.com/equinix/metal-cli/test/helper" + metal "github.com/equinix-labs/metal-go/metal/v1" "github.com/spf13/cobra" ) @@ -22,26 +24,35 @@ func TestPorts_VLANs(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - portList := setupProjectAndDevice(t, &projectId, &deviceId) + portList := helper.SetupProjectAndDevice(t, &projectId, &deviceId) port := &portList[2] + if port == nil { + t.Error("bond0 Port not found on device") + return + } + + if err := convertToLayer2(port.GetId()); err != nil { + t.Error(err) + return + } + vlan, err := helper.CreateTestVLAN(projectId) if err != nil { t.Error(err) + return } defer func() { if err := helper.UnAssignPortVlan(port.GetId(), vlan.GetId()); err != nil { t.Error(err) - return + } + if err := helper.CleanTestVlan(vlan.GetId()); err != nil { + t.Error(err) } if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } }() - if port == nil { - t.Error("bond0 Port not found on device") - return - } tests := []struct { name string @@ -57,7 +68,7 @@ func TestPorts_VLANs(t *testing.T) { root := c.Root() vxLanStr := strconv.Itoa(int(vlan.GetVxlan())) - // should be hybrid-bonded + // should be layer2-bonded root.SetArgs([]string{subCommand, "vlan", "-i", port.GetId(), "-a", vxLanStr}) rescueStdout := os.Stdout @@ -76,7 +87,7 @@ func TestPorts_VLANs(t *testing.T) { return } - assertPortCmdOutput(t, port, string(out[:]), "hybrid-bonded", true) + assertPortCmdOutput(t, port, string(out[:]), "layer2-bonded", true) }, }, } @@ -89,3 +100,12 @@ func TestPorts_VLANs(t *testing.T) { }) } } + +func convertToLayer2(portId string) error { + apiClient := helper.TestClient() + + _, _, err := apiClient.PortsApi.ConvertLayer2(context.Background(), portId). + PortAssignInput(*metal.NewPortAssignInput()). + Execute() + return err +} diff --git a/test/e2e/vlan/vlan_delete_test.go b/test/e2e/vlan/vlan_delete_test.go index 653274a1..af85c5e0 100644 --- a/test/e2e/vlan/vlan_delete_test.go +++ b/test/e2e/vlan/vlan_delete_test.go @@ -44,7 +44,7 @@ func TestCli_Vlan_Clean(t *testing.T) { if err != nil { t.Error(err) } - vlanId, err = helper.CreateTestVlan(projectId, 2023, "metal-cli-vlan-get-test") + vlanId, err = helper.CreateTestVlanWithVxLan(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 diff --git a/test/e2e/vlan/vlan_get_test.go b/test/e2e/vlan/vlan_get_test.go index c06c5862..3d959d36 100644 --- a/test/e2e/vlan/vlan_get_test.go +++ b/test/e2e/vlan/vlan_get_test.go @@ -44,7 +44,7 @@ func TestCli_Vlan_Get(t *testing.T) { if err != nil { t.Error(err) } - vlanId, err = helper.CreateTestVlan(projectId, 2023, "metal-cli-vlan-delete-test") + vlanId, err = helper.CreateTestVlanWithVxLan(projectId, 2023, "metal-cli-vlan-delete-test") if len(projectId) != 0 && len(vlanId) != 0 { root.SetArgs([]string{subCommand, "get", "-p", projectId}) rescueStdout := os.Stdout diff --git a/test/helper/helper.go b/test/helper/helper.go index 88081bac..b07a5e4e 100644 --- a/test/helper/helper.go +++ b/test/helper/helper.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "testing" "time" openapiclient "github.com/equinix-labs/metal-go/metal/v1" @@ -231,7 +232,7 @@ func CleanTestIps(ipsId string) error { return nil } -func CreateTestVlan(projectId string, Id int, desc string) (string, error) { +func CreateTestVlanWithVxLan(projectId string, Id int, desc string) (string, error) { TestApiClient := TestClient() virtualNetworkCreateInput := *openapiclient.NewVirtualNetworkCreateInput() virtualNetworkCreateInput.SetDescription(desc) @@ -280,3 +281,37 @@ func CleanupProjectAndDevice(deviceId, projectId string) error { return nil } + +//nolint:staticcheck +func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) []openapiclient.Port { + projId, err := CreateTestProject("metal-cli-test-ports-project") + if err != nil { + t.Error(err) + } + *projectId = projId + + devId, err := CreateTestDevice(*projectId, "metal-cli-test-ports-device") + if err != nil { + t.Error(err) + } + *deviceId = devId + + active, err := IsDeviceStateActive(*deviceId) + if err != nil { + t.Error(err) + } + if !active { + t.Errorf("Timeout while waiting for device: %s to be active", *deviceId) + } + + device, err := GetDeviceById(*deviceId) + if err != nil { + t.Error(err) + return nil + } + if len(device.NetworkPorts) < 3 { + t.Errorf("All 3 ports doesnot exist for the created device: %s", device.GetId()) + } + + return device.GetNetworkPorts() +}