Skip to content

Commit

Permalink
fix: updated e2e test cases for VRFS
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Jan 23, 2024
1 parent 2bb181a commit 6f4968c
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 52 deletions.
2 changes: 1 addition & 1 deletion docs/metal_vrf_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ metal vrf delete vrf -i <metal_vrf_UUID> [-f] [flags]
```
-f, --force Skips confirmation for the removal of the VRF.
-h, --help help for delete
-i, --id string UUID of the VRF.
-i, --id string Specify the UUID of the VRF
```

### Options inherited from parent commands
Expand Down
4 changes: 2 additions & 2 deletions docs/metal_vrf_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ metal vrf get -p <project_Id> [flags]

```
# Gets the details of the specified device
metal vrf get -i 3b0795ba-ec9a-4a9e-83a7-043e7e11407c
metal vrf get -v 3b0795ba-ec9a-4a9e-83a7-043e7e11407c
# Lists VRFs for project 3b0795ba-ec9a-4a9e-83a7-043e7e11407c:
metal vrf list -p 3b0795ba-ec9a-4a9e-83a7-043e7e11407c
Expand All @@ -26,7 +26,7 @@ metal vrf get -p <project_Id> [flags]
-h, --help help for get
-m, --metro string Filter by Metro ID (uuid) or Metro Code
-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, --vrfID string VRF UUID
-v, --vrf-id string Specify the VRF UUID.
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion internal/vrf/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Client) Create() *cobra.Command {
data := make([][]string, 1)

// This output block below is probably incorrect but leaving it for now for testing later.
data[0] = []string{vrfRequest.GetName(), vrfRequest.GetDescription(), strconv.Itoa(int(vrfRequest.GetLocalAsn())), strings.Join(vrfRequest.GetIpRanges(), ","), vrfRequest.GetCreatedAt().String()}
data[0] = []string{vrfRequest.GetId(), vrfRequest.GetName(), vrfRequest.GetDescription(), strconv.Itoa(int(vrfRequest.GetLocalAsn())), strings.Join(vrfRequest.GetIpRanges(), ","), vrfRequest.GetCreatedAt().String()}
header := []string{"ID", "Name", "Description", "LocalASN", "IPranges", "Created"}

return c.Out.Output(vrfRequest, header, &data)
Expand Down
2 changes: 1 addition & 1 deletion internal/vrf/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Client) Delete() *cobra.Command {
},
}

deleteVrfCmd.Flags().StringVarP(&vrfID, "id", "i", "", "UUID of the VRF.")
deleteVrfCmd.Flags().StringVarP(&vrfID, "id", "i", "", "Specify the UUID of the VRF")
deleteVrfCmd.Flags().BoolVarP(&force, "force", "f", false, "Skips confirmation for the removal of the VRF.")

_ = deleteVrfCmd.MarkFlagRequired("id")
Expand Down
39 changes: 18 additions & 21 deletions internal/vrf/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strconv"
"strings"

metal "github.com/equinix/equinix-sdk-go/services/metalv1"
"github.com/spf13/cobra"
)

Expand All @@ -24,7 +23,7 @@ func (c *Client) Retrieve() *cobra.Command {
Short: "Lists VRFs.",
Long: "Retrieves a list of all VRFs for the specified project or the details of the specified VRF ID. Either a project ID or a VRF ID is required.",
Example: ` # Gets the details of the specified device
metal vrf get -i 3b0795ba-ec9a-4a9e-83a7-043e7e11407c
metal vrf get -v 3b0795ba-ec9a-4a9e-83a7-043e7e11407c
# Lists VRFs for project 3b0795ba-ec9a-4a9e-83a7-043e7e11407c:
metal vrf list -p 3b0795ba-ec9a-4a9e-83a7-043e7e11407c`,
Expand All @@ -33,26 +32,26 @@ func (c *Client) Retrieve() *cobra.Command {
cmd.SilenceUsage = true
inc := []string{}
exc := []string{}
var (
vrfsList *metal.VrfList
vrf *metal.Vrf
vrfs []metal.Vrf
err error
)
vrfIdFlag, _ := cmd.Flags().GetString("vrfID")
if vrfIdFlag != "" {
vrf, _, err = c.Service.FindVrfById(context.Background(), vrfID).Include(c.Servicer.Includes(inc)).Exclude(c.Servicer.Excludes(exc)).Execute()

// It's a required flag in case of get VRF by ID.
if vrfID != "" {
vrf, _, err := c.Service.FindVrfById(context.Background(), vrfID).Include(c.Servicer.Includes(inc)).Exclude(c.Servicer.Excludes(exc)).Execute()
if err != nil {
return fmt.Errorf("error when calling `VRFsApi.FindVrfById``: %w", err)
}
vrfs[0] = *vrf
} else {
vrfsList, _, err = c.Service.FindVrfs(context.Background(), projectID).Metro(metro).Include(c.Servicer.Includes(inc)).Exclude(c.Servicer.Excludes(exc)).Execute()
if err != nil {
return fmt.Errorf("error when calling `VRFsApi.FindVrfs``: %w", err)
}
vrfs = vrfsList.GetVrfs()
data := make([][]string, 1)

data[0] = []string{vrf.GetId(), vrf.GetName(), vrf.GetDescription(), strings.Join(vrf.GetIpRanges(), ","), strconv.FormatInt(int64(vrf.GetLocalAsn()), 10), vrf.CreatedAt.String()}
header := []string{"ID", "Name", "Description", "IPRanges", "LocalASN", "Created"}

return c.Out.Output(vrf, header, &data)

}
vrfsList, _, err := c.Service.FindVrfs(context.Background(), projectID).Metro(metro).Include(c.Servicer.Includes(inc)).Exclude(c.Servicer.Excludes(exc)).Execute()
if err != nil {
return fmt.Errorf("error when calling `VRFsApi.FindVrfs``: %w", err)
}
vrfs := vrfsList.GetVrfs()

data := make([][]string, len(vrfs))

Expand All @@ -67,9 +66,7 @@ func (c *Client) Retrieve() *cobra.Command {
}
retrieveVrfsCmd.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.")
retrieveVrfsCmd.Flags().StringVarP(&metro, "metro", "m", "", "Filter by Metro ID (uuid) or Metro Code")
retrieveVrfsCmd.Flags().StringVarP(&vrfID, "vrfID", "v", "", "VRF UUID")

_ = retrieveVrfsCmd.MarkFlagRequired("project-id")
retrieveVrfsCmd.Flags().StringVarP(&vrfID, "vrf-id", "v", "", "Specify the VRF UUID.")

return retrieveVrfsCmd
}
27 changes: 3 additions & 24 deletions test/e2e/ports/convert_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package ports

import (
"strconv"
"strings"
"testing"

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/equinix-sdk-go/services/metalv1"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -45,7 +42,7 @@ func TestPorts_Convert(t *testing.T) {

out := helper.ExecuteAndCaptureOutput(t, root)

assertPortCmdOutput(t, port, string(out[:]), "layer2-individual", false)
helper.AssertPortCmdOutput(t, port, string(out[:]), "layer2-individual", false)
},
},
{
Expand All @@ -59,7 +56,7 @@ func TestPorts_Convert(t *testing.T) {

out := helper.ExecuteAndCaptureOutput(t, root)

assertPortCmdOutput(t, port, string(out[:]), "layer2-bonded", true)
helper.AssertPortCmdOutput(t, port, string(out[:]), "layer2-bonded", true)
},
},
{
Expand All @@ -73,7 +70,7 @@ func TestPorts_Convert(t *testing.T) {

out := helper.ExecuteAndCaptureOutput(t, root)

assertPortCmdOutput(t, port, string(out[:]), "layer3", true)
helper.AssertPortCmdOutput(t, port, string(out[:]), "layer3", true)
},
},
}
Expand All @@ -86,21 +83,3 @@ func TestPorts_Convert(t *testing.T) {
})
}
}

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())
}

if !strings.Contains(out, port.GetName()) {
t.Errorf("cmd output should contain name of the port: %s", port.GetName())
}

if !strings.Contains(out, networkType) {
t.Errorf("cmd output should contain type of the port: %s", string(port.GetNetworkType()))
}

if !strings.Contains(out, strconv.FormatBool(bonded)) {
t.Errorf("cmd output should contain if port is bonded: %s", strconv.FormatBool(port.Data.GetBonded()))
}
}
2 changes: 1 addition & 1 deletion test/e2e/ports/retrieve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestPorts_Retrieve(t *testing.T) {
t.Errorf("cmd output should contain MAC address of the port: %s", port.Data.GetMac())
}

assertPortCmdOutput(t, port, string(out[:]), string(port.GetNetworkType()), port.Data.GetBonded())
helper.AssertPortCmdOutput(t, port, string(out[:]), string(port.GetNetworkType()), port.Data.GetBonded())
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/ports/vlans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestPorts_VLANs(t *testing.T) {
t.Fatal(err)
}

assertPortCmdOutput(t, port, string(out[:]), "layer2-bonded", true)
helper.AssertPortCmdOutput(t, port, string(out[:]), "layer2-bonded", true)
},
},
}
Expand Down
168 changes: 168 additions & 0 deletions test/e2e/vrfstest/vrf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package vrfstest

import (
"strings"
"testing"

root "github.com/equinix/metal-cli/internal/cli"
outputPkg "github.com/equinix/metal-cli/internal/outputs"
"github.com/equinix/metal-cli/internal/vrf"
"github.com/equinix/metal-cli/test/helper"
"github.com/spf13/cobra"
)

func TestCli_Vrf_Create(t *testing.T) {
subCommand := "vrf"
rootClient := root.NewClient(helper.ConsumerToken, helper.URL, helper.Version)
randName := helper.GenerateRandomString(5)

type fields struct {
MainCmd *cobra.Command
Outputer outputPkg.Outputer
}

tests := []struct {
name string
fields fields
want *cobra.Command
cmdFunc func(*testing.T, *cobra.Command)
}{
{
name: "vrf-create-test",
fields: fields{
MainCmd: vrf.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(),
Outputer: outputPkg.Outputer(&outputPkg.Standard{}),
},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projName := "metal-cli-" + randName + "-vrf-create-test"
projectId := helper.CreateTestProject(t, projName)
if projectId.GetId() != "" {

root.SetArgs([]string{subCommand, "create", "-p", projectId.GetId(), "-m", "da", "-n", projName, "-a", "3456", "-r", "10.0.1.0/24"})

out := helper.ExecuteAndCaptureOutput(t, root)

if !strings.Contains(string(out[:]), projName) {
t.Error("expected output should include " + projName + ", in the out string ")
}
}
},
},
{
name: "vrf-delete-test",
fields: fields{
MainCmd: vrf.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(),
Outputer: outputPkg.Outputer(&outputPkg.Standard{}),
},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()

projName := "metal-cli-" + randName + "-vrf-delete-test"
projectId := helper.CreateTestProject(t, projName)

if projectId.GetId() != "" {
vrf := helper.CreateTestVrfs(t, projectId.GetId(), projName)
if vrf.GetId() != "" {
root.SetArgs([]string{subCommand, "delete", "-i", vrf.GetId(), "-f"})
out := helper.ExecuteAndCaptureOutput(t, root)

if !strings.Contains(string(out[:]), "VRF deletion initiated. Please check 'metal vrf get -i "+vrf.GetId()+" ' for status") {
t.Error("expected output should include VRF deletion initiated. Please check 'metal vrf get -i " + vrf.GetId() + " ' for status, in the out string")
}
}
}
},
},
{
name: "vrf-list-test",
fields: fields{
MainCmd: vrf.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(),
Outputer: outputPkg.Outputer(&outputPkg.Standard{}),
},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()

projName := "metal-cli-" + randName + "-vrf-list-test"
projectId := helper.CreateTestProject(t, projName)

if projectId.GetId() != "" {
vrf := helper.CreateTestVrfs(t, projectId.GetId(), projName)

root.SetArgs([]string{subCommand, "get", "-p", projectId.GetId()})
out := helper.ExecuteAndCaptureOutput(t, root)

if !strings.Contains(string(out[:]), vrf.GetId()) &&
!strings.Contains(string(out[:]), projName) {
t.Error("expected output should include " + vrf.GetId() + ", in the out string ")
}
}
},
},
{
name: "vrf-getByProjectId-test",
fields: fields{
MainCmd: vrf.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(),
Outputer: outputPkg.Outputer(&outputPkg.Standard{}),
},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()

projName := "metal-cli-" + randName + "-vrf-get-test"
projectId := helper.CreateTestProject(t, projName)

if projectId.GetId() != "" {
vrf := helper.CreateTestVrfs(t, projectId.GetId(), projName)

root.SetArgs([]string{subCommand, "get", "-p", projectId.GetId()})
out := helper.ExecuteAndCaptureOutput(t, root)

if !strings.Contains(string(out[:]), vrf.GetId()) &&
!strings.Contains(string(out[:]), projName) {
t.Error("expected output should include " + vrf.GetId() + ", in the out string ")
}

}
},
},
{
name: "vrf-getById-test",
fields: fields{
MainCmd: vrf.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(),
Outputer: outputPkg.Outputer(&outputPkg.Standard{}),
},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()

projName := "metal-cli-" + randName + "-vrf-get-test"
projectId := helper.CreateTestProject(t, projName)

if projectId.GetId() != "" {
vrf := helper.CreateTestVrfs(t, projectId.GetId(), projName)
if vrf.GetId() != "" {
root.SetArgs([]string{subCommand, "get", "-v", vrf.GetId()})
out := helper.ExecuteAndCaptureOutput(t, root)

if !strings.Contains(string(out[:]), vrf.GetId()) &&
!strings.Contains(string(out[:]), projName) {
t.Error("expected output should include " + vrf.GetId() + ", in the out string ")
}
}
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rootCmd := rootClient.NewCommand()
rootCmd.AddCommand(tt.fields.MainCmd)
tt.cmdFunc(t, tt.fields.MainCmd)
})
}
}
Loading

0 comments on commit 6f4968c

Please sign in to comment.