-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: updated e2e test cases for VRFS
- Loading branch information
1 parent
099001c
commit 79f5808
Showing
2 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters