-
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.
Migrate metal ports subcommand cli to metal-go from packngo
Signed-off-by: Ayush Rangwala <[email protected]>
- Loading branch information
1 parent
de1378f
commit ebaebc0
Showing
8 changed files
with
474 additions
and
33 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
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
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
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
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,120 @@ | ||
package convert | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"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" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func TestPorts_Convert(t *testing.T) { | ||
var projectId, deviceId *string | ||
subCommand := "port" | ||
consumerToken := "" | ||
apiURL := "" | ||
Version := "devel" | ||
rootClient := root.NewClient(consumerToken, apiURL, Version) | ||
tests := []struct { | ||
name string | ||
cmd *cobra.Command | ||
want *cobra.Command | ||
cmdFunc func(*testing.T, *cobra.Command) | ||
}{ | ||
{ | ||
name: "convert port", | ||
cmd: ports.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), | ||
want: &cobra.Command{}, | ||
cmdFunc: func(t *testing.T, c *cobra.Command) { | ||
root := c.Root() | ||
projId, err := helper.CreateTestProject("metal-cli-test-ports-project") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
projectId = &projId | ||
|
||
devId, err := helper.CreateTestDevice(*projectId, "metal-cli-test-ports-device") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
deviceId = &devId | ||
|
||
device, err := helper.GetDeviceById(*deviceId) | ||
if len(device.NetworkPorts) < 3 { | ||
t.Errorf("All 3 ports doesnot exist for the created device: %s", device.GetId()) | ||
} | ||
port := device.GetNetworkPorts()[2] | ||
|
||
active, err := helper.IsDeviceStateActive(*deviceId) | ||
if err == nil && active { | ||
root.SetArgs([]string{subCommand, "convert", "-i", port.GetId(), "--layer2", "--bonded", "--force"}) | ||
|
||
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[:]), port.GetId()) { | ||
t.Errorf("cmd output should contain ID of the port: %s", port.GetId()) | ||
} | ||
|
||
if !strings.Contains(string(out[:]), port.GetName()) { | ||
t.Errorf("cmd output should contain name of the port: %s", port.GetName()) | ||
} | ||
|
||
if !strings.Contains(string(out[:]), string(port.GetType())) { | ||
t.Errorf("cmd output should contain type of the port: %s", string(port.GetType())) | ||
} | ||
|
||
if !strings.Contains(string(out[:]), strconv.FormatBool(port.Data.GetBonded())) { | ||
t.Errorf("cmd output should contain if port is bonded: %s", strconv.FormatBool(port.Data.GetBonded())) | ||
} | ||
|
||
if err := helper.DeletePort(port.GetId()); err != nil { | ||
t.Error(err) | ||
} | ||
if err := helper.DeleteVLAN(port.GetId()); err != nil { | ||
t.Error(err) | ||
} | ||
if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rootCmd := rootClient.NewCommand() | ||
rootCmd.AddCommand(tt.cmd) | ||
tt.cmdFunc(t, tt.cmd) | ||
}) | ||
} | ||
} | ||
|
||
func cleanup(t *testing.T, deviceId, projectId *string) { | ||
resp, err := helper.IsDeviceStateActive(*deviceId) | ||
if err == nil && resp == true { | ||
err = helper.CleanTestDevice(*deviceId) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
err = helper.CleanTestProject(*projectId) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
} |
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,102 @@ | ||
package retrieve | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"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" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func TestPorts_Retrieve(t *testing.T) { | ||
var projectId, deviceId *string | ||
subCommand := "port" | ||
consumerToken := "" | ||
apiURL := "" | ||
Version := "devel" | ||
rootClient := root.NewClient(consumerToken, apiURL, Version) | ||
tests := []struct { | ||
name string | ||
cmd *cobra.Command | ||
want *cobra.Command | ||
cmdFunc func(*testing.T, *cobra.Command) | ||
}{ | ||
{ | ||
name: "retrieve port", | ||
cmd: ports.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), | ||
want: &cobra.Command{}, | ||
cmdFunc: func(t *testing.T, c *cobra.Command) { | ||
root := c.Root() | ||
projId, err := helper.CreateTestProject("metal-cli-test-ports-project") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
projectId = &projId | ||
|
||
devId, err := helper.CreateTestDevice(*projectId, "metal-cli-test-ports-device") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
deviceId = &devId | ||
|
||
device, err := helper.GetDeviceById(*deviceId) | ||
|
||
if len(device.NetworkPorts) == 0 { | ||
t.Errorf("ports doesnot exist for the created device: %s", device.GetId()) | ||
} | ||
|
||
port := device.GetNetworkPorts()[0] | ||
root.SetArgs([]string{subCommand, "get", "-i", port.GetId()}) | ||
|
||
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[:]), port.GetId()) { | ||
t.Errorf("cmd output should contain ID of the port: %s", port.GetId()) | ||
} | ||
|
||
if !strings.Contains(string(out[:]), port.GetName()) { | ||
t.Errorf("cmd output should contain name of the port: %s", port.GetName()) | ||
} | ||
|
||
if !strings.Contains(string(out[:]), string(port.GetType())) { | ||
t.Errorf("cmd output should contain type of the port: %s", string(port.GetType())) | ||
} | ||
|
||
if !strings.Contains(string(out[:]), port.Data.GetMac()) { | ||
t.Errorf("cmd output should contain MAC address of the port: %s", port.Data.GetMac()) | ||
} | ||
|
||
if !strings.Contains(string(out[:]), strconv.FormatBool(port.Data.GetBonded())) { | ||
t.Errorf("cmd output should contain if port is bonded: %s", strconv.FormatBool(port.Data.GetBonded())) | ||
} | ||
|
||
if err := helper.CleanupProjectAndDevice(deviceId, projectId); err != nil { | ||
t.Error(err) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rootCmd := rootClient.NewCommand() | ||
rootCmd.AddCommand(tt.cmd) | ||
tt.cmdFunc(t, tt.cmd) | ||
}) | ||
} | ||
} |
Oops, something went wrong.