From 10efbb55b11fafe0c47dd891b139d8d5cad67d36 Mon Sep 17 00:00:00 2001 From: Ayush Rangwala Date: Fri, 27 Oct 2023 18:59:55 +0530 Subject: [PATCH 1/6] feat: Migrate metal cli from packngo to metal-go for gateways subcommand Add tests for gateway subcommand Signed-off-by: Ayush Rangwala --- internal/gateway/create.go | 44 ++++++++--- internal/gateway/delete.go | 8 +- internal/gateway/gateway.go | 13 ++-- internal/gateway/retrieve.go | 28 +++++-- test/e2e/gateways/create_test.go | 114 +++++++++++++++++++++++++++++ test/e2e/gateways/delete_test.go | 109 +++++++++++++++++++++++++++ test/e2e/gateways/retrieve_test.go | 95 ++++++++++++++++++++++++ test/helper/helper.go | 35 +++++++++ 8 files changed, 422 insertions(+), 24 deletions(-) create mode 100644 test/e2e/gateways/create_test.go create mode 100644 test/e2e/gateways/delete_test.go create mode 100644 test/e2e/gateways/retrieve_test.go diff --git a/internal/gateway/create.go b/internal/gateway/create.go index 303700dd..f99d17bf 100644 --- a/internal/gateway/create.go +++ b/internal/gateway/create.go @@ -21,16 +21,18 @@ package gateway import ( + "context" + "errors" "fmt" "strconv" - "github.com/packethost/packngo" + metal "github.com/equinix-labs/metal-go/metal/v1" "github.com/spf13/cobra" ) func (c *Client) Create() *cobra.Command { var projectID, vnID, reservationID string - var netSize int + var netSize int32 // createMetalGatewayCmd represents the createMetalGateway command createMetalGatewayCmd := &cobra.Command{ @@ -45,14 +47,29 @@ func (c *Client) Create() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true + includes := []string{"virtual_network", "ip_reservation"} - req := &packngo.MetalGatewayCreateRequest{ - VirtualNetworkID: vnID, - IPReservationID: reservationID, - PrivateIPv4SubnetSize: netSize, + if reservationID == "" && netSize == 0 { + return errors.New("Invalid input. Provide either 'private-subnet-size' or 'ip-reservation-id'") } - n, _, err := c.Service.Create(projectID, req) + req := metal.CreateMetalGatewayRequest{ + MetalGatewayCreateInput: &metal.MetalGatewayCreateInput{ + VirtualNetworkId: vnID, + }, + } + if reservationID != "" { + req.MetalGatewayCreateInput.SetIpReservationId(reservationID) + } else { + req.MetalGatewayCreateInput.SetPrivateIpv4SubnetSize(netSize) + } + + n, _, err := c.Service. + CreateMetalGateway(context.Background(), projectID). + Include(c.Servicer.Includes(includes)). + Exclude(c.Servicer.Excludes(nil)). + CreateMetalGatewayRequest(req). + Execute() if err != nil { return fmt.Errorf("Could not create Metal Gateway: %w", err) } @@ -60,22 +77,25 @@ func (c *Client) Create() *cobra.Command { data := make([][]string, 1) address := "" - if n.IPReservation != nil { - address = n.IPReservation.Address + "/" + strconv.Itoa(n.IPReservation.CIDR) + gway := n.MetalGateway + ipReservation := gway.IpReservation + if ipReservation != nil { + address = ipReservation.GetAddress() + "/" + strconv.Itoa(int(ipReservation.GetCidr())) } - data[0] = []string{n.ID, n.VirtualNetwork.MetroCode, strconv.Itoa(n.VirtualNetwork.VXLAN), address, string(n.State), n.CreatedAt} + data[0] = []string{gway.GetId(), gway.VirtualNetwork.GetMetroCode(), + strconv.Itoa(int(gway.VirtualNetwork.GetVxlan())), address, string(gway.GetState()), gway.GetCreatedAt().String()} header := []string{"ID", "Metro", "VXLAN", "Addresses", "State", "Created"} - return c.Out.Output(n, header, &data) + return c.Out.Output(gway, header, &data) }, } createMetalGatewayCmd.Flags().StringVarP(&projectID, "project-id", "p", "", "The project's UUID. This flag is required, unless specified in the config created by metal init or set as METAL_PROJECT_ID environment variable.") createMetalGatewayCmd.Flags().StringVarP(&reservationID, "ip-reservation-id", "r", "", "UUID of the Public or VRF IP Reservation to assign.") createMetalGatewayCmd.Flags().StringVarP(&vnID, "virtual-network", "v", "", "UUID of the Virtual Network to assign.") - createMetalGatewayCmd.Flags().IntVarP(&netSize, "private-subnet-size", "s", 0, "Size of the private subnet to request (8 for /29)") + createMetalGatewayCmd.Flags().Int32VarP(&netSize, "private-subnet-size", "s", 0, "Size of the private subnet to request (8 for /29)") _ = createMetalGatewayCmd.MarkFlagRequired("project-id") _ = createMetalGatewayCmd.MarkFlagRequired("virtual-network") diff --git a/internal/gateway/delete.go b/internal/gateway/delete.go index 0269ed33..6d5bd7a3 100644 --- a/internal/gateway/delete.go +++ b/internal/gateway/delete.go @@ -21,6 +21,7 @@ package gateway import ( + "context" "fmt" "github.com/manifoldco/promptui" @@ -32,9 +33,14 @@ func (c *Client) Delete() *cobra.Command { gwayID string force bool ) + includes := []string{"virtual_network", "ip_reservation"} deleteGway := func(id string) error { - _, err := c.Service.Delete(id) + _, _, err := c.Service. + DeleteMetalGateway(context.Background(), id). + Include(c.Servicer.Includes(includes)). + Exclude(c.Servicer.Excludes(nil)). + Execute() if err != nil { return err } diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 7a8d0814..19ec0b5e 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -22,13 +22,14 @@ package gateway import ( "github.com/equinix/metal-cli/internal/outputs" - "github.com/packethost/packngo" + + metal "github.com/equinix-labs/metal-go/metal/v1" "github.com/spf13/cobra" ) type Client struct { Servicer Servicer - Service packngo.MetalGatewayService + Service *metal.MetalGatewaysApiService Out outputs.Outputer } @@ -45,7 +46,7 @@ func (c *Client) NewCommand() *cobra.Command { root.PersistentPreRun(cmd, args) } } - c.Service = c.Servicer.API(cmd).MetalGateways + c.Service = c.Servicer.MetalAPI(cmd).MetalGatewaysApi }, } @@ -58,8 +59,10 @@ func (c *Client) NewCommand() *cobra.Command { } type Servicer interface { - API(*cobra.Command) *packngo.Client - ListOptions(defaultIncludes, defaultExcludes []string) *packngo.ListOptions + MetalAPI(*cobra.Command) *metal.APIClient + Filters() map[string]string + Includes(defaultIncludes []string) (incl []string) + Excludes(defaultExcludes []string) (excl []string) } func NewClient(s Servicer, out outputs.Outputer) *Client { diff --git a/internal/gateway/retrieve.go b/internal/gateway/retrieve.go index e61feb28..f8094d1a 100644 --- a/internal/gateway/retrieve.go +++ b/internal/gateway/retrieve.go @@ -21,9 +21,12 @@ package gateway import ( + "context" "fmt" "strconv" + metal "github.com/equinix-labs/metal-go/metal/v1" + "github.com/spf13/cobra" ) @@ -42,26 +45,39 @@ func (c *Client) Retrieve() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true - listOpts := c.Servicer.ListOptions(nil, nil).Including("virtual_network", "ip_reservation") - gways, _, err := c.Service.List(projectID, listOpts) + includes := []string{"virtual_network", "ip_reservation"} + + gwayList, _, err := c.Service. + FindMetalGatewaysByProject(context.Background(), projectID). + Include(c.Servicer.Includes(includes)). + Exclude(c.Servicer.Excludes(nil)). + Execute() if err != nil { return fmt.Errorf("Could not list Project Metal Gateways: %w", err) } + gways := gwayList.GetMetalGateways() + data := make([][]string, len(gways)) + metalGways := make([]*metal.MetalGateway, len(gways)) for i, n := range gways { + gway := n.MetalGateway + metalGways = append(metalGways, gway) + address := "" - if n.IPReservation != nil { - address = n.IPReservation.Address + "/" + strconv.Itoa(n.IPReservation.CIDR) + ipReservation := gway.IpReservation + if ipReservation != nil { + address = ipReservation.GetAddress() + "/" + strconv.Itoa(int(ipReservation.GetCidr())) } - data[i] = []string{n.ID, n.VirtualNetwork.MetroCode, strconv.Itoa(n.VirtualNetwork.VXLAN), address, string(n.State), n.CreatedAt} + data[i] = []string{gway.GetId(), gway.VirtualNetwork.GetMetroCode(), strconv.Itoa(int(gway.VirtualNetwork.GetVxlan())), + address, string(gway.GetState()), gway.GetCreatedAt().String()} } header := []string{"ID", "Metro", "VXLAN", "Addresses", "State", "Created"} - return c.Out.Output(gways, header, &data) + return c.Out.Output(metalGways, header, &data) }, } retrieveMetalGatewaysCmd.Flags().StringVarP(&projectID, "project-id", "p", "", "The project's UUID. This flag is required, unless specified in the config created by metal init or set as METAL_PROJECT_ID environment variable.") diff --git a/test/e2e/gateways/create_test.go b/test/e2e/gateways/create_test.go new file mode 100644 index 00000000..b9b3701f --- /dev/null +++ b/test/e2e/gateways/create_test.go @@ -0,0 +1,114 @@ +package gateways + +import ( + "context" + "errors" + "io" + "os" + "strconv" + "strings" + "testing" + + "github.com/spf13/cobra" + + root "github.com/equinix/metal-cli/internal/cli" + "github.com/equinix/metal-cli/internal/gateway" + outputPkg "github.com/equinix/metal-cli/internal/outputs" + "github.com/equinix/metal-cli/test/helper" +) + +func TestGateways_Create(t *testing.T) { + var projectId, deviceId string + subCommand := "gateways" + consumerToken := "" + apiURL := "" + Version := "devel" + rootClient := root.NewClient(consumerToken, apiURL, Version) + + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + defer func() { + if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + t.Error(err) + } + }() + + vlan, err := helper.CreateTestVLAN(projectId) + if err != nil { + t.Error(err) + return + } + defer func() { + if err := helper.CleanTestVlan(vlan.GetId()); err != nil { + t.Error(err) + } + }() + + tests := []struct { + name string + cmd *cobra.Command + want *cobra.Command + cmdFunc func(*testing.T, *cobra.Command) + }{ + { + name: "create gateways", + cmd: gateway.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + root := c.Root() + + root.SetArgs([]string{subCommand, "create", "-p", projectId, "-v", vlan.GetId(), "-s", "8"}) + + rescueStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + if err := root.Execute(); err != nil { + t.Error(err) + } + w.Close() + out, _ := io.ReadAll(r) + os.Stdout = rescueStdout + + apiClient := helper.TestClient() + gateways, _, err := apiClient.MetalGatewaysApi. + FindMetalGatewaysByProject(context.Background(), projectId). + Execute() + if err != nil { + t.Error(err) + return + } + if len(gateways.MetalGateways) != 1 { + t.Error(errors.New("Gateway Not Found. Failed to create gateway")) + return + } + + assertGatewaysCmdOutput(t, string(out[:]), gateways.MetalGateways[0].MetalGateway.GetId(), device.Metro.GetCode(), strconv.Itoa(int(vlan.GetVxlan()))) + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rootCmd := rootClient.NewCommand() + rootCmd.AddCommand(tt.cmd) + tt.cmdFunc(t, tt.cmd) + }) + } +} + +func assertGatewaysCmdOutput(t *testing.T, out, gatewayId, metro, vxlan string) { + if !strings.Contains(out, gatewayId) { + t.Errorf("cmd output should contain ID of the gateway: %s", gatewayId) + } + + if !strings.Contains(out, metro) { + t.Errorf("cmd output should contain metro same as device: %s", metro) + } + + if !strings.Contains(out, vxlan) { + t.Errorf("cmd output should contain vxlan, gateway is attached with: %s", vxlan) + } + + if !strings.Contains(out, "ready") { + t.Errorf("cmd output should contain 'ready' state of the gateway") + } +} diff --git a/test/e2e/gateways/delete_test.go b/test/e2e/gateways/delete_test.go new file mode 100644 index 00000000..79313e94 --- /dev/null +++ b/test/e2e/gateways/delete_test.go @@ -0,0 +1,109 @@ +package gateways + +import ( + "context" + "fmt" + "io" + "os" + "strings" + "testing" + + "github.com/spf13/cobra" + + root "github.com/equinix/metal-cli/internal/cli" + "github.com/equinix/metal-cli/internal/gateway" + outputPkg "github.com/equinix/metal-cli/internal/outputs" + "github.com/equinix/metal-cli/test/helper" +) + +func TestGateways_Delete(t *testing.T) { + var projectId, deviceId string + subCommand := "gateways" + consumerToken := "" + apiURL := "" + Version := "devel" + rootClient := root.NewClient(consumerToken, apiURL, Version) + + _ = helper.SetupProjectAndDevice(t, &projectId, &deviceId) + defer func() { + if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + t.Error(err) + } + }() + + vlan, err := helper.CreateTestVLAN(projectId) + if err != nil { + t.Error(err) + return + } + defer func() { + if err := helper.CleanTestVlan(vlan.GetId()); err != nil { + t.Error(err) + } + }() + + subnetSize := int32(8) + metalGateway, err := helper.CreateTestGateway(projectId, vlan.GetId(), &subnetSize) + if err != nil { + t.Error(err) + return + } + defer func() { + if err := helper.CleanTestGateway(metalGateway.GetId()); err != nil && + !strings.Contains(err.Error(), "Not Found") { + t.Error(err) + } + }() + + tests := []struct { + name string + cmd *cobra.Command + want *cobra.Command + cmdFunc func(*testing.T, *cobra.Command) + }{ + { + name: "delete gateways", + cmd: gateway.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + root := c.Root() + + root.SetArgs([]string{subCommand, "delete", "-f", "-i", metalGateway.GetId()}) + + rescueStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + if err := root.Execute(); err != nil { + t.Error(err) + } + w.Close() + out, _ := io.ReadAll(r) + os.Stdout = rescueStdout + + apiClient := helper.TestClient() + gateways, _, err := apiClient.MetalGatewaysApi. + FindMetalGatewayById(context.Background(), metalGateway.GetId()). + Execute() + if err != nil && !strings.Contains(err.Error(), "Not Found") { + t.Error(err) + } + if gateways != nil && gateways.MetalGateway != nil { + t.Error(fmt.Errorf("not able to delete metal gateway with ID: [%s]", metalGateway.GetId())) + } + + expectedOut := fmt.Sprintf("Metal Gateway %s successfully deleted.", metalGateway.GetId()) + if !strings.Contains(string(out[:]), expectedOut) { + t.Error(fmt.Errorf("expected output: '%s' but got '%s'", expectedOut, string(out))) + } + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rootCmd := rootClient.NewCommand() + rootCmd.AddCommand(tt.cmd) + tt.cmdFunc(t, tt.cmd) + }) + } +} diff --git a/test/e2e/gateways/retrieve_test.go b/test/e2e/gateways/retrieve_test.go new file mode 100644 index 00000000..520e5156 --- /dev/null +++ b/test/e2e/gateways/retrieve_test.go @@ -0,0 +1,95 @@ +package gateways + +import ( + "io" + "os" + "strconv" + "strings" + "testing" + + root "github.com/equinix/metal-cli/internal/cli" + "github.com/equinix/metal-cli/internal/gateway" + outputPkg "github.com/equinix/metal-cli/internal/outputs" + "github.com/spf13/cobra" + + "github.com/equinix/metal-cli/test/helper" +) + +func TestGateways_Retrieve(t *testing.T) { + var projectId, deviceId string + subCommand := "gateways" + consumerToken := "" + apiURL := "" + Version := "devel" + rootClient := root.NewClient(consumerToken, apiURL, Version) + + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + defer func() { + if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + t.Error(err) + } + }() + + vlan, err := helper.CreateTestVLAN(projectId) + if err != nil { + t.Error(err) + return + } + defer func() { + if err := helper.CleanTestVlan(vlan.GetId()); err != nil { + t.Error(err) + } + }() + + subnetSize := int32(8) + metalGateway, err := helper.CreateTestGateway(projectId, vlan.GetId(), &subnetSize) + if err != nil { + t.Error(err) + return + } + defer func() { + if err := helper.CleanTestGateway(metalGateway.GetId()); err != nil && + !strings.Contains(err.Error(), "Not Found") { + t.Error(err) + } + }() + + tests := []struct { + name string + cmd *cobra.Command + want *cobra.Command + cmdFunc func(*testing.T, *cobra.Command) + }{ + { + name: "retrieve gateways by projectId", + cmd: gateway.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + root := c.Root() + + // get using projectId + root.SetArgs([]string{subCommand, "get", "-p", projectId}) + + rescueStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + if err := root.Execute(); err != nil { + t.Error(err) + } + w.Close() + out, _ := io.ReadAll(r) + os.Stdout = rescueStdout + + assertGatewaysCmdOutput(t, string(out[:]), metalGateway.GetId(), device.Metro.GetCode(), strconv.Itoa(int(vlan.GetVxlan()))) + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rootCmd := rootClient.NewCommand() + rootCmd.AddCommand(tt.cmd) + tt.cmdFunc(t, tt.cmd) + }) + } +} diff --git a/test/helper/helper.go b/test/helper/helper.go index c2e3ea68..59398263 100644 --- a/test/helper/helper.go +++ b/test/helper/helper.go @@ -72,6 +72,27 @@ func CreateTestVLAN(projectId string) (*openapiclient.VirtualNetwork, error) { return vlan, nil } +func CreateTestGateway(projectId, vlanId string, privateIPv4SubnetSize *int32) (*openapiclient.MetalGateway, error) { + TestApiClient := TestClient() + + gatewayCreateInput := openapiclient.CreateMetalGatewayRequest{ + MetalGatewayCreateInput: &openapiclient.MetalGatewayCreateInput{ + VirtualNetworkId: vlanId, + PrivateIpv4SubnetSize: privateIPv4SubnetSize, + }, + } + gateway, _, err := TestApiClient.MetalGatewaysApi. + CreateMetalGateway(context.Background(), projectId). + Include([]string{"ip_reservation"}). + CreateMetalGatewayRequest(gatewayCreateInput). + Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "Error when calling `MetalGatewaysApi.CreateMetalGateway`: %v\n", err) + return nil, err + } + return gateway.MetalGateway, nil +} + func GetDeviceById(deviceId string) (*openapiclient.Device, error) { TestApiClient := TestClient() includes := []string{"network_ports"} @@ -316,3 +337,17 @@ func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) *openapicl return device } + +func CleanTestGateway(gatewayId string) error { + 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 nil +} From 4686ba27e652cc8b696bbb28739b45de70a48be1 Mon Sep 17 00:00:00 2001 From: Ayush Rangwala Date: Wed, 1 Nov 2023 21:55:49 +0530 Subject: [PATCH 2/6] Update docs Signed-off-by: Ayush Rangwala --- docs/metal_gateway_create.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/metal_gateway_create.md b/docs/metal_gateway_create.md index d350c4ec..5dbed1ce 100644 --- a/docs/metal_gateway_create.md +++ b/docs/metal_gateway_create.md @@ -23,11 +23,11 @@ metal gateway create -p --virtual-network ### Options ``` - -h, --help help for create - -r, --ip-reservation-id string UUID of the Public or VRF IP Reservation to assign. - -s, --private-subnet-size int Size of the private subnet to request (8 for /29) - -p, --project-id string The project's UUID. This flag is required, unless specified in the config created by metal init or set as METAL_PROJECT_ID environment variable. - -v, --virtual-network string UUID of the Virtual Network to assign. + -h, --help help for create + -r, --ip-reservation-id string UUID of the Public or VRF IP Reservation to assign. + -s, --private-subnet-size int32 Size of the private subnet to request (8 for /29) + -p, --project-id string The project's UUID. This flag is required, unless specified in the config created by metal init or set as METAL_PROJECT_ID environment variable. + -v, --virtual-network string UUID of the Virtual Network to assign. ``` ### Options inherited from parent commands From c8b87d4e55e97f9c612f3f8ac94574415ec1ddbf Mon Sep 17 00:00:00 2001 From: Ayush Rangwala Date: Wed, 15 Nov 2023 16:09:23 +0530 Subject: [PATCH 3/6] fixed helper setup function return signature Signed-off-by: Ayush Rangwala --- test/e2e/gateways/create_test.go | 6 +++++- test/e2e/gateways/delete_test.go | 6 +++++- test/e2e/gateways/retrieve_test.go | 6 +++++- test/e2e/ports/convert_test.go | 8 ++++++-- test/e2e/ports/retrieve_test.go | 8 ++++++-- test/e2e/ports/vlans_test.go | 15 ++++++++++----- test/helper/helper.go | 19 ++++++++++--------- 7 files changed, 47 insertions(+), 21 deletions(-) diff --git a/test/e2e/gateways/create_test.go b/test/e2e/gateways/create_test.go index b9b3701f..ad31e239 100644 --- a/test/e2e/gateways/create_test.go +++ b/test/e2e/gateways/create_test.go @@ -25,13 +25,17 @@ func TestGateways_Create(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + device, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) defer func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } }() + if err != nil { + return + } + vlan, err := helper.CreateTestVLAN(projectId) if err != nil { t.Error(err) diff --git a/test/e2e/gateways/delete_test.go b/test/e2e/gateways/delete_test.go index 79313e94..13ceab33 100644 --- a/test/e2e/gateways/delete_test.go +++ b/test/e2e/gateways/delete_test.go @@ -24,13 +24,17 @@ func TestGateways_Delete(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - _ = helper.SetupProjectAndDevice(t, &projectId, &deviceId) + _, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) defer func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } }() + if err != nil { + return + } + vlan, err := helper.CreateTestVLAN(projectId) if err != nil { t.Error(err) diff --git a/test/e2e/gateways/retrieve_test.go b/test/e2e/gateways/retrieve_test.go index 520e5156..09b69ea5 100644 --- a/test/e2e/gateways/retrieve_test.go +++ b/test/e2e/gateways/retrieve_test.go @@ -23,13 +23,17 @@ func TestGateways_Retrieve(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + device, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) defer func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } }() + if err != nil { + return + } + vlan, err := helper.CreateTestVLAN(projectId) if err != nil { t.Error(err) diff --git a/test/e2e/ports/convert_test.go b/test/e2e/ports/convert_test.go index 05b9ae14..f7d5013d 100644 --- a/test/e2e/ports/convert_test.go +++ b/test/e2e/ports/convert_test.go @@ -24,13 +24,17 @@ func TestPorts_Convert(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) - port := &device.GetNetworkPorts()[2] + device, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) defer func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } }() + if err != nil { + return + } + + port := &device.GetNetworkPorts()[2] if port == nil { t.Error("bond0 Port not found on device") return diff --git a/test/e2e/ports/retrieve_test.go b/test/e2e/ports/retrieve_test.go index 2084f099..c25c942c 100644 --- a/test/e2e/ports/retrieve_test.go +++ b/test/e2e/ports/retrieve_test.go @@ -22,13 +22,17 @@ func TestPorts_Retrieve(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) - port := &device.GetNetworkPorts()[2] + device, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) defer func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } }() + if err != nil { + return + } + + port := &device.GetNetworkPorts()[2] if port == nil { t.Error("bond0 Port not found on device") return diff --git a/test/e2e/ports/vlans_test.go b/test/e2e/ports/vlans_test.go index 1600c435..a2ab1a9c 100644 --- a/test/e2e/ports/vlans_test.go +++ b/test/e2e/ports/vlans_test.go @@ -24,7 +24,16 @@ func TestPorts_VLANs(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + device, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + defer func() { + if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + t.Error(err) + } + }() + if err != nil { + return + } + port := &device.GetNetworkPorts()[2] if port == nil { t.Error("bond0 Port not found on device") @@ -41,7 +50,6 @@ func TestPorts_VLANs(t *testing.T) { t.Error(err) return } - defer func() { if err := helper.UnAssignPortVlan(port.GetId(), vlan.GetId()); err != nil { t.Error(err) @@ -49,9 +57,6 @@ func TestPorts_VLANs(t *testing.T) { if err := helper.CleanTestVlan(vlan.GetId()); err != nil { t.Error(err) } - if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { - t.Error(err) - } }() tests := []struct { diff --git a/test/helper/helper.go b/test/helper/helper.go index 59398263..db920a21 100644 --- a/test/helper/helper.go +++ b/test/helper/helper.go @@ -305,37 +305,38 @@ 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) (*openapiclient.Device, error) { + t.Helper() projId, err := CreateTestProject("metal-cli-test-project") if err != nil { - t.Error(err) + return nil, err } *projectId = projId devId, err := CreateTestDevice(*projectId, "metal-cli-test-device") if err != nil { - t.Error(err) + return nil, err } *deviceId = devId active, err := IsDeviceStateActive(*deviceId) if err != nil { - t.Error(err) + return nil, err } if !active { - t.Errorf("Timeout while waiting for device: %s to be active", *deviceId) + return nil, fmt.Errorf("Timeout while waiting for device: %s to be active", *deviceId) + } device, err := GetDeviceById(*deviceId) if err != nil { - t.Error(err) - return nil + return nil, err } if len(device.NetworkPorts) < 3 { - t.Errorf("All 3 ports doesnot exist for the created device: %s", device.GetId()) + return nil, fmt.Errorf("All 3 ports doesnot exist for the created device: %s", device.GetId()) } - return device + return device, nil } func CleanTestGateway(gatewayId string) error { From d7b015fab72b567c55263a1427098e9e75a4c3b2 Mon Sep 17 00:00:00 2001 From: Ayush Rangwala Date: Wed, 15 Nov 2023 18:27:17 +0530 Subject: [PATCH 4/6] fix: output for convert layer2 to layer 3 command Signed-off-by: Ayush Rangwala --- internal/ports/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/ports/convert.go b/internal/ports/convert.go index a6cb7d38..ade72812 100644 --- a/internal/ports/convert.go +++ b/internal/ports/convert.go @@ -95,7 +95,7 @@ func (c *Client) Convert() *cobra.Command { } convToL3 := func(portID string) (*metal.Port, *http.Response, error) { - log.Printf("Converting port %s to layer-3 with addresses %v", portID, addrs) + log.Printf("Converting port %s to layer-3", portID) return c.PortService. ConvertLayer3(context.Background(), portID). PortConvertLayer3Input(metal.PortConvertLayer3Input{ From f5b1b0adc1c80e48a600c225d1815d2e6b9d1b0c Mon Sep 17 00:00:00 2001 From: Ayush Rangwala Date: Fri, 17 Nov 2023 22:57:23 +0530 Subject: [PATCH 5/6] fix: Test case Signed-off-by: Ayush Rangwala --- test/e2e/gateways/create_test.go | 19 +++++++++--------- test/e2e/gateways/delete_test.go | 32 +++++++++++++++++------------- test/e2e/gateways/retrieve_test.go | 31 ++++++++++++++--------------- test/e2e/ports/convert_test.go | 8 ++++---- test/e2e/ports/retrieve_test.go | 8 ++++---- test/e2e/ports/vlans_test.go | 20 +++++++++---------- test/helper/helper.go | 23 ++++++++++++--------- 7 files changed, 74 insertions(+), 67 deletions(-) diff --git a/test/e2e/gateways/create_test.go b/test/e2e/gateways/create_test.go index ad31e239..0e93316c 100644 --- a/test/e2e/gateways/create_test.go +++ b/test/e2e/gateways/create_test.go @@ -25,27 +25,26 @@ func TestGateways_Create(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) - defer func() { + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + t.Cleanup(func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } - }() - - if err != nil { + }) + if device == nil { return } vlan, err := helper.CreateTestVLAN(projectId) + t.Cleanup(func() { + if err := helper.CleanTestVlan(vlan.GetId()); err != nil { + t.Error(err) + } + }) if err != nil { t.Error(err) return } - defer func() { - if err := helper.CleanTestVlan(vlan.GetId()); err != nil { - t.Error(err) - } - }() tests := []struct { name string diff --git a/test/e2e/gateways/delete_test.go b/test/e2e/gateways/delete_test.go index 13ceab33..f02822fe 100644 --- a/test/e2e/gateways/delete_test.go +++ b/test/e2e/gateways/delete_test.go @@ -24,40 +24,44 @@ func TestGateways_Delete(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - _, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) defer func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } }() - - if err != nil { + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + t.Cleanup(func() { + if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + t.Error(err) + } + }) + if device == nil { return } vlan, err := helper.CreateTestVLAN(projectId) - if err != nil { - t.Error(err) - return - } - defer func() { + t.Cleanup(func() { if err := helper.CleanTestVlan(vlan.GetId()); err != nil { t.Error(err) } - }() - - subnetSize := int32(8) - metalGateway, err := helper.CreateTestGateway(projectId, vlan.GetId(), &subnetSize) + }) if err != nil { t.Error(err) return } - defer func() { + + subnetSize := int32(8) + metalGateway, err := helper.CreateTestGateway(projectId, vlan.GetId(), &subnetSize) + t.Cleanup(func() { if err := helper.CleanTestGateway(metalGateway.GetId()); err != nil && !strings.Contains(err.Error(), "Not Found") { t.Error(err) } - }() + }) + if err != nil { + t.Error(err) + return + } tests := []struct { name string diff --git a/test/e2e/gateways/retrieve_test.go b/test/e2e/gateways/retrieve_test.go index 09b69ea5..c633edd4 100644 --- a/test/e2e/gateways/retrieve_test.go +++ b/test/e2e/gateways/retrieve_test.go @@ -23,40 +23,39 @@ func TestGateways_Retrieve(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) - defer func() { + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + t.Cleanup(func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } - }() - - if err != nil { + }) + if device == nil { return } vlan, err := helper.CreateTestVLAN(projectId) - if err != nil { - t.Error(err) - return - } - defer func() { + t.Cleanup(func() { if err := helper.CleanTestVlan(vlan.GetId()); err != nil { t.Error(err) } - }() - - subnetSize := int32(8) - metalGateway, err := helper.CreateTestGateway(projectId, vlan.GetId(), &subnetSize) + }) if err != nil { t.Error(err) return } - defer func() { + + subnetSize := int32(8) + metalGateway, err := helper.CreateTestGateway(projectId, vlan.GetId(), &subnetSize) + t.Cleanup(func() { if err := helper.CleanTestGateway(metalGateway.GetId()); err != nil && !strings.Contains(err.Error(), "Not Found") { t.Error(err) } - }() + }) + if err != nil { + t.Error(err) + return + } tests := []struct { name string diff --git a/test/e2e/ports/convert_test.go b/test/e2e/ports/convert_test.go index f7d5013d..4cbddaa2 100644 --- a/test/e2e/ports/convert_test.go +++ b/test/e2e/ports/convert_test.go @@ -24,13 +24,13 @@ func TestPorts_Convert(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) - defer func() { + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + t.Cleanup(func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } - }() - if err != nil { + }) + if device == nil { return } diff --git a/test/e2e/ports/retrieve_test.go b/test/e2e/ports/retrieve_test.go index c25c942c..50a4a76c 100644 --- a/test/e2e/ports/retrieve_test.go +++ b/test/e2e/ports/retrieve_test.go @@ -22,13 +22,13 @@ func TestPorts_Retrieve(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) - defer func() { + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + t.Cleanup(func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } - }() - if err != nil { + }) + if device == nil { return } diff --git a/test/e2e/ports/vlans_test.go b/test/e2e/ports/vlans_test.go index a2ab1a9c..e134e7ca 100644 --- a/test/e2e/ports/vlans_test.go +++ b/test/e2e/ports/vlans_test.go @@ -24,13 +24,13 @@ func TestPorts_VLANs(t *testing.T) { Version := "devel" rootClient := root.NewClient(consumerToken, apiURL, Version) - device, err := helper.SetupProjectAndDevice(t, &projectId, &deviceId) - defer func() { + device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) + t.Cleanup(func() { if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { t.Error(err) } - }() - if err != nil { + }) + if device == nil { return } @@ -46,18 +46,18 @@ func TestPorts_VLANs(t *testing.T) { } vlan, err := helper.CreateTestVLAN(projectId) - if err != nil { - t.Error(err) - return - } - defer func() { + t.Cleanup(func() { if err := helper.UnAssignPortVlan(port.GetId(), vlan.GetId()); err != nil { t.Error(err) } if err := helper.CleanTestVlan(vlan.GetId()); err != nil { t.Error(err) } - }() + }) + if err != nil { + t.Error(err) + return + } tests := []struct { name string diff --git a/test/helper/helper.go b/test/helper/helper.go index db920a21..85b10d2d 100644 --- a/test/helper/helper.go +++ b/test/helper/helper.go @@ -305,38 +305,43 @@ func CleanupProjectAndDevice(deviceId, projectId string) error { } //nolint:staticcheck -func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) (*openapiclient.Device, error) { +func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) *openapiclient.Device { t.Helper() projId, err := CreateTestProject("metal-cli-test-project") if err != nil { - return nil, err + t.Fatal(err) + return nil } *projectId = projId devId, err := CreateTestDevice(*projectId, "metal-cli-test-device") if err != nil { - return nil, err + t.Fatal(err) + return nil } *deviceId = devId active, err := IsDeviceStateActive(*deviceId) if err != nil { - return nil, err + t.Fatal(err) + return nil } if !active { - return nil, fmt.Errorf("Timeout while waiting for device: %s to be active", *deviceId) - + t.Fatal("Timeout while waiting for device: %s to be active", *deviceId) + return nil } device, err := GetDeviceById(*deviceId) if err != nil { - return nil, err + t.Fatal(err) + return nil } if len(device.NetworkPorts) < 3 { - return nil, fmt.Errorf("All 3 ports doesnot exist for the created device: %s", device.GetId()) + t.Fatal("All 3 ports doesnot exist for the created device: %s", device.GetId()) + return nil } - return device, nil + return device } func CleanTestGateway(gatewayId string) error { From 937c4670b7b714d9a4b04a30201174640b3c111c Mon Sep 17 00:00:00 2001 From: Ayush Rangwala Date: Mon, 20 Nov 2023 20:47:36 +0530 Subject: [PATCH 6/6] fix: lint issues Signed-off-by: Ayush Rangwala --- go.mod | 1 + go.sum | 5 +---- test/e2e/gateways/create_test.go | 14 ++++++++------ test/e2e/gateways/delete_test.go | 8 +++++--- test/helper/helper.go | 7 ++++--- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index a1c6cc24..8bed1f21 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/manifoldco/promptui v0.9.0 github.com/olekukonko/tablewriter v0.0.5 github.com/packethost/packngo v0.30.0 + github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.17.0 diff --git a/go.sum b/go.sum index 9d4da4c8..c983254f 100644 --- a/go.sum +++ b/go.sum @@ -159,6 +159,7 @@ github.com/packethost/packngo v0.30.0 h1:JVeTwbXXETsLTDQncUbYwIFpkOp/xevXrffM2Hr github.com/packethost/packngo v0.30.0/go.mod h1:BT/XcdwLVmeMtGPbovnxCpnI1s9ylSE1cs/7pq007NE= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -257,8 +258,6 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -412,8 +411,6 @@ golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/test/e2e/gateways/create_test.go b/test/e2e/gateways/create_test.go index 0e93316c..c144d4fd 100644 --- a/test/e2e/gateways/create_test.go +++ b/test/e2e/gateways/create_test.go @@ -27,7 +27,8 @@ func TestGateways_Create(t *testing.T) { device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil && + !strings.Contains(err.Error(), "Not Found") { t.Error(err) } }) @@ -37,7 +38,8 @@ func TestGateways_Create(t *testing.T) { vlan, err := helper.CreateTestVLAN(projectId) t.Cleanup(func() { - if err := helper.CleanTestVlan(vlan.GetId()); err != nil { + if err := helper.CleanTestVlan(vlan.GetId()); err != nil && + !strings.Contains(err.Error(), "Not Found") { t.Error(err) } }) @@ -100,18 +102,18 @@ func TestGateways_Create(t *testing.T) { func assertGatewaysCmdOutput(t *testing.T, out, gatewayId, metro, vxlan string) { if !strings.Contains(out, gatewayId) { - t.Errorf("cmd output should contain ID of the gateway: %s", gatewayId) + t.Errorf("cmd output should contain ID of the gateway: [%s] \n output:\n%s", gatewayId, out) } if !strings.Contains(out, metro) { - t.Errorf("cmd output should contain metro same as device: %s", metro) + t.Errorf("cmd output should contain metro same as device: [%s] \n output:\n%s", metro, out) } if !strings.Contains(out, vxlan) { - t.Errorf("cmd output should contain vxlan, gateway is attached with: %s", vxlan) + t.Errorf("cmd output should contain vxlan, gateway is attached with: [%s] \n output:\n%s", vxlan, out) } if !strings.Contains(out, "ready") { - t.Errorf("cmd output should contain 'ready' state of the gateway") + t.Errorf("cmd output should contain 'ready' state of the gateway, output:\n%s", out) } } diff --git a/test/e2e/gateways/delete_test.go b/test/e2e/gateways/delete_test.go index f02822fe..eb17b862 100644 --- a/test/e2e/gateways/delete_test.go +++ b/test/e2e/gateways/delete_test.go @@ -31,7 +31,8 @@ func TestGateways_Delete(t *testing.T) { }() device := helper.SetupProjectAndDevice(t, &projectId, &deviceId) t.Cleanup(func() { - if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { + if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil && + !strings.Contains(err.Error(), "Not Found") { t.Error(err) } }) @@ -41,8 +42,9 @@ func TestGateways_Delete(t *testing.T) { vlan, err := helper.CreateTestVLAN(projectId) t.Cleanup(func() { - if err := helper.CleanTestVlan(vlan.GetId()); err != nil { - t.Error(err) + if err := helper.CleanTestVlan(vlan.GetId()); err != nil && + !strings.Contains(err.Error(), "Not Found") { + t.Error("Error while cleaning up vLan", err) } }) if err != nil { diff --git a/test/helper/helper.go b/test/helper/helper.go index 85b10d2d..ad39935b 100644 --- a/test/helper/helper.go +++ b/test/helper/helper.go @@ -8,6 +8,7 @@ import ( "time" openapiclient "github.com/equinix-labs/metal-go/metal/v1" + "github.com/pkg/errors" ) func TestClient() *openapiclient.APIClient { @@ -159,7 +160,7 @@ func WaitForAttachVlanToPort(portId string, attach bool) error { case <-ticker.C: port, err := GetPortById(portId) if err != nil { - return err + return errors.Wrapf(err, "Error while fetching the port using ID: %s", portId) } vlans := port.GetVirtualNetworks() @@ -327,7 +328,7 @@ func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) *openapicl return nil } if !active { - t.Fatal("Timeout while waiting for device: %s to be active", *deviceId) + t.Fatalf("Timeout while waiting for device: %s to be active", *deviceId) return nil } @@ -337,7 +338,7 @@ func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) *openapicl return nil } if len(device.NetworkPorts) < 3 { - t.Fatal("All 3 ports doesnot exist for the created device: %s", device.GetId()) + t.Fatalf("All 3 ports doesnot exist for the created device: %s", device.GetId()) return nil }