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 ctreatma committed Nov 28, 2023
1 parent 099001c commit 79f5808
Show file tree
Hide file tree
Showing 2 changed files with 290 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)
})
}
}
56 changes: 56 additions & 0 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package helper

import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -345,6 +347,7 @@ func SetupProjectAndDevice(t *testing.T, projectId, deviceId *string) *openapicl
return device
}

//nolint:staticcheck
func CleanTestGateway(gatewayId string) error {
TestApiClient := TestClient()
_, _, err := TestApiClient.MetalGatewaysApi.
Expand All @@ -355,6 +358,59 @@ func CleanTestGateway(gatewayId string) error {
fmt.Fprintf(os.Stderr, "Error when calling `MetalGatewaysApi.DeleteMetalGateway``: %v\n", err)
return err
}
return nil
}

//nolint:staticcheck
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
}

//nolint:staticcheck
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
}

//nolint:staticcheck
func GenerateRandomString(length int) (string, error) {
// Calculate the number of bytes needed for the given string length
numBytes := (length * 3) / 4

// Create a byte slice to store the random bytes
randomBytes := make([]byte, numBytes)

// Read random bytes from the crypto/rand package
_, err := rand.Read(randomBytes)
if err != nil {
return "", err
}

// Encode the random bytes to base64 to get a string
randomString := base64.URLEncoding.EncodeToString(randomBytes)

// Trim any padding characters
randomString = randomString[:length]

return randomString, nil
}

0 comments on commit 79f5808

Please sign in to comment.