Skip to content

Commit

Permalink
fix goimports and vlan test
Browse files Browse the repository at this point in the history
Signed-off-by: Ayush Rangwala <[email protected]>
  • Loading branch information
aayushrangwala committed Nov 1, 2023
1 parent 3e85c48 commit ea8cf16
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 55 deletions.
3 changes: 2 additions & 1 deletion internal/ports/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
44 changes: 4 additions & 40 deletions test/e2e/ports/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -26,8 +24,8 @@ func TestPorts_Convert(t *testing.T) {
Version := "devel"
rootClient := root.NewClient(consumerToken, apiURL, Version)

portList := setupProjectAndDevice(t, &projectId, &deviceId)
port := &portList[2]
device := helper.SetupProjectAndDevice(t, &projectId, &deviceId)
port := &device.GetNetworkPorts()[2]
defer func() {
if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil {
t.Error(err)
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/ports/retrieve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestPorts_Retrieve(t *testing.T) {
Version := "devel"
rootClient := root.NewClient(consumerToken, apiURL, Version)

portList := setupProjectAndDevice(t, &projectId, &deviceId)
port := &portList[2]
device := helper.SetupProjectAndDevice(t, &projectId, &deviceId)
port := &device.GetNetworkPorts()[2]
defer func() {
if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil {
t.Error(err)
Expand Down
38 changes: 29 additions & 9 deletions test/e2e/ports/vlans_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ports

import (
"context"
"io"
"os"
"strconv"
Expand All @@ -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"
)

Expand All @@ -22,26 +24,35 @@ func TestPorts_VLANs(t *testing.T) {
Version := "devel"
rootClient := root.NewClient(consumerToken, apiURL, Version)

portList := setupProjectAndDevice(t, &projectId, &deviceId)
port := &portList[2]
device := helper.SetupProjectAndDevice(t, &projectId, &deviceId)
port := &device.GetNetworkPorts()[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
Expand All @@ -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
Expand All @@ -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)
},
},
}
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion test/e2e/vlan/vlan_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/vlan/vlan_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 37 additions & 1 deletion test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"testing"
"time"

openapiclient "github.com/equinix-labs/metal-go/metal/v1"
Expand Down Expand Up @@ -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)
Expand All @@ -256,6 +257,7 @@ func CleanTestVlan(vlanId string) error {

return nil
}

func UnAssignPortVlan(portId, vlanId string) error {
testClient := TestClient()
_, _, err := testClient.PortsApi.
Expand All @@ -280,3 +282,37 @@ func CleanupProjectAndDevice(deviceId, projectId string) error {

return nil
}

//nolint:staticcheck
func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) *openapiclient.Device {
projId, err := CreateTestProject("metal-cli-test-project")
if err != nil {
t.Error(err)
}
*projectId = projId

devId, err := CreateTestDevice(*projectId, "metal-cli-test-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
}

0 comments on commit ea8cf16

Please sign in to comment.