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 Nov 23, 2023
1 parent a051f74 commit c722f40
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 0 deletions.
234 changes: 234 additions & 0 deletions test/e2e/vrfstest/vrf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
package vrfstest

import (
"io"
"os"
"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"
)

// setupTestProject initializes a test project and returns its ID along with a cleanup function.
func setupTestProject(t *testing.T, projectName string) (string, func()) {
projectId, err := helper.CreateTestProject(projectName)
if err != nil {
t.Fatal(err)
}

teardown := func() {
err := helper.CleanTestProject(projectId)
if err != nil {
t.Error(err)
}
}

return projectId, teardown
}

// setupTestVRF initializes a test VRF within the given project and returns its ID along with a cleanup function.
func setupTestVRF(t *testing.T, projectId, vrfName string) (string, func()) {
vrfId, err := helper.CreateTestVrfs(projectId, vrfName)
if err != nil {
t.Fatal(err)
}

teardown := func() {
err := helper.CleanTestVrfs(vrfId)
if err != nil {
t.Error(err)
}
}

return vrfId, teardown
}

func TestCli_Vrf_Create(t *testing.T) {
subCommand := "vrf"
consumerToken := ""
apiURL := ""
Version := "metal"
rootClient := root.NewClient(consumerToken, apiURL, Version)

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

tests := []struct {
name string
fields fields
want *cobra.Command
cmdFunc func(*testing.T, *cobra.Command, *cobra.Command, string)
}{
{
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, rootCmd *cobra.Command, projectID string) {
root := c.Root()
RandName, _ := helper.GenerateRandomString(32)
projName := "metal-cli-" + RandName + "-vrf-create-test"
projectId, cleanupProject := setupTestProject(t, projName)
defer cleanupProject()

if projectId != "" {
root.SetArgs([]string{subCommand, "create", "-p", projectId, "-m", "da", "-n", "metal-cli-vrf-create-test", "-a", "3456", "-r", "10.0.1.0/24"})
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
if !strings.Contains(string(out[:]), "metal-cli-vrf-create-test") {
t.Error("expected output should include metal-cli-vrf-create-test, 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, rootCmd, "")
})
}
}

func TestCli_Vrf_Delete(t *testing.T) {
subCommand := "vrf"
consumerToken := ""
apiURL := ""
Version := "metal"
rootClient := root.NewClient(consumerToken, apiURL, Version)

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

tests := []struct {
name string
fields fields
want *cobra.Command
cmdFunc func(*testing.T, *cobra.Command, *cobra.Command, string, 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, rootCmd *cobra.Command, projID, vrfId string) {
root := c.Root()
RandName, _ := helper.GenerateRandomString(32)
projName := "metal-cli-" + RandName + "-vrf-delete-test"
projectID, cleanupProject := setupTestProject(t, projName)

if projectID != "" {
vrfId, _ := setupTestVRF(t, projectID, "metal-cli-vrf-delete-test")

root.SetArgs([]string{subCommand, "delete", "-i", vrfId, "-f"})
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
if !strings.Contains(string(out[:]), "VRF deletion initiated. Please check 'metal vrf get -i "+vrfId+" ' for status") {
t.Error("expected output should include VRF deletion initiated. Please check 'metal vrf get -i " + vrfId + " ' for status, in the out string")
}
}
defer cleanupProject()
},
},
}

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, rootCmd, "", "")
})
}
}

func TestCli_Vrf_Get(t *testing.T) {
subCommand := "vrf"
consumerToken := ""
apiURL := ""
Version := "metal"
rootClient := root.NewClient(consumerToken, apiURL, Version)

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-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()
RandName, _ := helper.GenerateRandomString(32)
projName := "metal-cli-" + RandName + "-vrf-get-test"
projectID, cleanupProject := setupTestProject(t, projName)

if projectID != "" {
vrfId, _ := setupTestVRF(t, projectID, "metal-cli-vrf-get-test")
if vrfId != "" {
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
if !strings.Contains(string(out[:]), vrfId) &&
!strings.Contains(string(out[:]), "metal-cli-vrf-get-test") {
t.Error("expected output should include " + vrfId + ", in the out string ")
}
}
}
defer cleanupProject()
},
},
}

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)
})
}
}
29 changes: 29 additions & 0 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,32 @@ func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) *openapicl

return device
}

func CreateTestVrfs(projectId, name string) (string, error) {
TestApiClient := TestClient()

var IpRanges []string

vrfCreateInput := *openapiclient.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 {
fmt.Fprintf(os.Stderr, "Error when calling `VRFsApi.CreateVrf``: %v\n", err)
}

return resp.GetId(), nil
}

func CleanTestVrfs(vrfId string) error {
TestApiClient := TestClient()

_, err := TestApiClient.VRFsApi.DeleteVrf(context.Background(), vrfId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `VRFsApi.DeleteVrf``: %v\n", err)
}

return nil
}

0 comments on commit c722f40

Please sign in to comment.