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 authored and cprivitere committed Jan 19, 2024
1 parent 3b9a618 commit 3a6090b
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/metal_vrf_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, --vrfID string Specify the VRF UUID.
```

### Options inherited from parent commands
Expand Down
32 changes: 16 additions & 16 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 @@ -33,26 +32,27 @@ 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")
// It's a required flag in case of get VRF by ID.
if vrfIdFlag != "" {
vrf, _, err = c.Service.FindVrfById(context.Background(), vrfID).Include(c.Servicer.Includes(inc)).Exclude(c.Servicer.Excludes(exc)).Execute()
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,7 +67,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.Flags().StringVarP(&vrfID, "vrfID", "v", "", "Specify the VRF UUID.")

_ = retrieveVrfsCmd.MarkFlagRequired("project-id")

Expand Down
145 changes: 145 additions & 0 deletions test/e2e/vrfstest/vrf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
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"
consumerToken := ""
apiURL := ""
Version := "metal"
rootClient := root.NewClient(consumerToken, apiURL, 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-get-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)
})
}
}
35 changes: 35 additions & 0 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,38 @@ func ExecuteAndCaptureOutput(t *testing.T, root *cobra.Command) []byte {

return out
}

//nolint:staticcheck
func CreateTestVrfs(t *testing.T, projectId, name string) *metalv1.Vrf {
t.Helper()
TestApiClient := TestClient()

var IpRanges []string

vrfCreateInput := *metalv1.NewVrfCreateInput("da", name)
vrfCreateInput.SetLocalAsn(5678)
IpRanges = append(IpRanges, "10.10.1.0/24")
vrfCreateInput.SetIpRanges(IpRanges)

resp, _, err := TestApiClient.VRFsApi.CreateVrf(context.Background(), projectId).VrfCreateInput(vrfCreateInput).Execute()
if err != nil {
t.Fatalf("Error when calling `VRFsApi.CreateVrf``: %v\n", err)
}

t.Cleanup(func() {
CleanTestVrfs(t, resp.GetId())
})

return resp
}

//nolint:staticcheck
func CleanTestVrfs(t *testing.T, vrfId string) {
t.Helper()
TestApiClient := TestClient()

resp, err := TestApiClient.VRFsApi.DeleteVrf(context.Background(), vrfId).Execute()
if err != nil && resp.StatusCode != http.StatusNotFound {
t.Fatalf("Error when calling `VRFsApi.DeleteVrf`` for %v: %v\n", vrfId, err)
}
}

0 comments on commit 3a6090b

Please sign in to comment.