Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Improved device unit test cases and fixed issue #340

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/metal_device_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ metal device create -p <project_id> (-m <metro> | -f <facility>) -P <plan> -H <h
### Options

```
-a, --always-pxe Sets whether the device always PXE boots on reboot.
-a, --always-pxe string Sets whether the device always PXE boots on reboot.
-b, --billing-cycle string Billing cycle (default "hourly")
-c, --customdata string Custom data to be included with your device's metadata.
-f, --facility string Code of the facility where the device will be created
Expand Down
2 changes: 1 addition & 1 deletion docs/metal_device_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ metal device update -i <device_id> [-H <hostname>] [-d <description>] [--locked
### Options

```
-a, --always-pxe Sets the device to always iPXE on reboot.
-a, --always-pxe string Sets the device to always iPXE on reboot.
-c, --customdata string Adds or updates custom data to be included with your device's metadata.
-d, --description string Adds or updates the description for the device.
-h, --help help for update
Expand Down
22 changes: 16 additions & 6 deletions internal/devices/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *Client) Create() *cobra.Command {
tags []string
ipxescripturl string
publicIPv4SubnetSize int
alwaysPXE bool
alwaysPXE string
hardwareReservationID string
spotInstance bool
spotPriceMax float64
Expand Down Expand Up @@ -125,8 +125,13 @@ func (c *Client) Create() *cobra.Command {
if billingCycle != "" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetBillingCycle(*validBillingCycle)
}
if alwaysPXE {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(alwaysPXE)
if alwaysPXE != "" {
Copy link
Member

@displague displague Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if alwaysPXE == "true" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(true)
}
if alwaysPXE == "false" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(false)
}
}

if ipxescripturl != "" {
Expand Down Expand Up @@ -165,8 +170,13 @@ func (c *Client) Create() *cobra.Command {
metroDeviceRequest.DeviceCreateInMetroInput.SetBillingCycle(*validBillingCycle)
}

if alwaysPXE {
metroDeviceRequest.DeviceCreateInMetroInput.SetAlwaysPxe(alwaysPXE)
if alwaysPXE != "" {
if alwaysPXE == "true" {
metroDeviceRequest.DeviceCreateInMetroInput.SetAlwaysPxe(true)
}
if alwaysPXE == "false" {
metroDeviceRequest.DeviceCreateInMetroInput.SetAlwaysPxe(false)
}
}

if ipxescripturl != "" {
Expand Down Expand Up @@ -219,7 +229,7 @@ func (c *Client) Create() *cobra.Command {
createDeviceCmd.Flags().IntVarP(&publicIPv4SubnetSize, "public-ipv4-subnet-size", "S", 0, "Size of the public IPv4 subnet.")
createDeviceCmd.Flags().StringVarP(&hardwareReservationID, "hardware-reservation-id", "r", "", "The UUID of a hardware reservation, if you are provisioning a server from your reserved hardware.")
createDeviceCmd.Flags().StringVarP(&billingCycle, "billing-cycle", "b", "hourly", "Billing cycle ")
createDeviceCmd.Flags().BoolVarP(&alwaysPXE, "always-pxe", "a", false, "Sets whether the device always PXE boots on reboot.")
createDeviceCmd.Flags().StringVarP(&alwaysPXE, "always-pxe", "a", "", "Sets whether the device always PXE boots on reboot.")
createDeviceCmd.Flags().BoolVarP(&spotInstance, "spot-instance", "s", false, "Provisions the device as a spot instance.")
createDeviceCmd.Flags().Float64VarP(&spotPriceMax, "spot-price-max", "", 0, `Sets the maximum spot market price for the device: --spot-price-max=1.2`)
createDeviceCmd.Flags().StringVarP(&terminationTime, "termination-time", "T", "", `Device termination time: --termination-time="2023-08-24T15:04:05Z"`)
Expand Down
15 changes: 10 additions & 5 deletions internal/devices/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Client) Update() *cobra.Command {
userdata string
hostname string
tags []string
alwaysPXE bool
alwaysPXE string
ipxescripturl string
customdata string
deviceID string
Expand Down Expand Up @@ -74,8 +74,13 @@ func (c *Client) Update() *cobra.Command {
deviceUpdate.Tags = tags
}

if alwaysPXE {
deviceUpdate.SetAlwaysPxe(alwaysPXE)
if alwaysPXE != "" {
if alwaysPXE == "true" {
deviceUpdate.SetAlwaysPxe(true)
}
if alwaysPXE == "false" {
deviceUpdate.SetAlwaysPxe(false)
}
}

if ipxescripturl != "" {
Expand All @@ -93,7 +98,7 @@ func (c *Client) Update() *cobra.Command {
}
device, _, err := c.Service.UpdateDevice(context.Background(), deviceID).DeviceUpdateInput(*deviceUpdate).Execute()
if err != nil {
return fmt.Errorf("Could not update Device: %w", err)
return fmt.Errorf("could not update Device: %w", err)
}

header := []string{"ID", "Hostname", "OS", "State"}
Expand All @@ -110,7 +115,7 @@ func (c *Client) Update() *cobra.Command {
updateDeviceCmd.Flags().StringVarP(&userdata, "userdata", "u", "", "Adds or updates the userdata for the device.")
updateDeviceCmd.Flags().BoolVarP(&locked, "locked", "l", false, "Locks or unlocks the device for future changes.")
updateDeviceCmd.Flags().StringSliceVarP(&tags, "tags", "t", []string{}, `Adds or updates the tags for the device --tags="tag1,tag2".`)
updateDeviceCmd.Flags().BoolVarP(&alwaysPXE, "always-pxe", "a", false, "Sets the device to always iPXE on reboot.")
updateDeviceCmd.Flags().StringVarP(&alwaysPXE, "always-pxe", "a", "", "Sets the device to always iPXE on reboot.")
updateDeviceCmd.Flags().StringVarP(&ipxescripturl, "ipxe-script-url", "s", "", "Add or update the URL of the iPXE script.")
updateDeviceCmd.Flags().StringVarP(&customdata, "customdata", "c", "", "Adds or updates custom data to be included with your device's metadata.")
_ = updateDeviceCmd.MarkFlagRequired("id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
root "github.com/equinix/metal-cli/internal/cli"
"github.com/equinix/metal-cli/internal/devices"
outputPkg "github.com/equinix/metal-cli/internal/outputs"
"github.com/equinix/metal-cli/test/helper"

Check failure on line 13 in test/e2e/devices/devicecreateflagstest/device_create_flags_test.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/equinix/metal-cli/test/helper (-: # github.com/equinix/metal-cli/test/helper
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -72,7 +72,7 @@
// Extract the ID from the match
if len(match) > 1 {
deviceId = strings.TrimSpace(match[1])
resp, err = helper.IsDeviceStateActive(deviceId)
resp, err = helper.IsDeviceStateActive(deviceId, "active")
if err == nil && resp == true {
err = helper.CleanTestDevice(deviceId)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/devices/devicecreatetest/device_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
root "github.com/equinix/metal-cli/internal/cli"
"github.com/equinix/metal-cli/internal/devices"
outputPkg "github.com/equinix/metal-cli/internal/outputs"
"github.com/equinix/metal-cli/test/helper"

Check failure on line 13 in test/e2e/devices/devicecreatetest/device_create_test.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/equinix/metal-cli/test/helper (-: # github.com/equinix/metal-cli/test/helper
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -72,10 +72,10 @@
// Extract the ID from the match
if len(match) > 1 {
deviceId = strings.TrimSpace(match[1])
resp, err = helper.IsDeviceStateActive(deviceId)
resp, err = helper.IsDeviceStateActive(deviceId, "active")
if err != nil || resp {
if !resp {
resp, err = helper.IsDeviceStateActive(deviceId)
resp, err = helper.IsDeviceStateActive(deviceId, "active")
}
err = helper.CleanTestDevice(deviceId)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/devices/devicegettest/device_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
root "github.com/equinix/metal-cli/internal/cli"
"github.com/equinix/metal-cli/internal/devices"
outputPkg "github.com/equinix/metal-cli/internal/outputs"
"github.com/equinix/metal-cli/test/helper"

Check failure on line 12 in test/e2e/devices/devicegettest/device_get_test.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/equinix/metal-cli/test/helper (-: # github.com/equinix/metal-cli/test/helper
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -63,7 +63,7 @@
t.Error("expected output should include " + deviceId)
}
if len(projectId) != 0 && len(deviceId) != 0 {
resp, err = helper.IsDeviceStateActive(deviceId)
resp, err = helper.IsDeviceStateActive(deviceId, "active")
if err == nil && resp == true {
err = helper.CleanTestDevice(deviceId)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/devices/devicereinstalltest/device_reinstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func TestCli_Devices_Update(t *testing.T) {
if err != nil {
t.Error(err)
}
status, err = helper.IsDeviceStateActive(deviceId)
status, err = helper.IsDeviceStateActive(deviceId, "active")
if err != nil {
status, err = helper.IsDeviceStateActive(deviceId)
status, err = helper.IsDeviceStateActive(deviceId, "active")
if err != nil {
t.Error(err)
}
Expand All @@ -60,11 +60,11 @@ func TestCli_Devices_Update(t *testing.T) {
if err != nil {
t.Error(err)
} else {
status, err = helper.IsDeviceStateActive(deviceId)
status, err = helper.IsDeviceStateActive(deviceId, "active")
// The below case will excute in both Device Active and Non-active states.
if err != nil || status {
if !status {
_, err = helper.IsDeviceStateActive(deviceId)
_, err = helper.IsDeviceStateActive(deviceId, "active")
if err != nil {
t.Error(err)
}
Expand Down
37 changes: 19 additions & 18 deletions test/e2e/devices/devicestarttest/device_start_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package devicestarttest

import (
"fmt"
"io"
"os"
"strings"
Expand Down Expand Up @@ -46,28 +45,30 @@ func TestCli_Devices_Update(t *testing.T) {
if err != nil {
t.Error(err)
}

deviceId, err = helper.CreateTestDevice(projectId, "metal-cli-start-dev")
if err != nil {
t.Error(err)
}
status, err = helper.IsDeviceStateActive(deviceId)

status, err = helper.IsDeviceStateActive(deviceId, "active")
if err != nil {
_, err := helper.IsDeviceStateActive(deviceId)
status, err = helper.IsDeviceStateActive(deviceId, "active")
if err != nil {
t.Error(err)
} else {
err = helper.StopTestDevice(deviceId)
if err != nil {
t.Error(err)
}
status, err = helper.IsDeviceStateActive(deviceId)
if err == nil {
t.Error(err)
}
}
}

if len(projectId) != 0 && len(deviceId) != 0 && !status {
err = helper.StopTestDevice(deviceId)
if err != nil {
t.Error(err)
}

status, err = helper.IsDeviceStateActive(deviceId, "inactive")
if err != nil {
t.Error(err)
}
if len(projectId) != 0 && len(deviceId) != 0 && status {
root.SetArgs([]string{subCommand, "start", "--id", deviceId})
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
Expand All @@ -81,25 +82,25 @@ func TestCli_Devices_Update(t *testing.T) {
if !strings.Contains(string(out[:]), "Device "+deviceId+" successfully started.") {
t.Error("expected output should include" + "Device " + deviceId + " successfully started." + "in the out string ")
} else {
status, _ = helper.IsDeviceStateActive(deviceId)
status, _ = helper.IsDeviceStateActive(deviceId, "active")
if err != nil || status {
if !status {
_, err = helper.IsDeviceStateActive(deviceId)
_, err = helper.IsDeviceStateActive(deviceId, "active")
if err != nil {
t.Error(err)
}
}
fmt.Print("Device is Active")

err = helper.CleanTestDevice(deviceId)
if err != nil {
t.Error(err)
}
fmt.Print("Cleaned Test Device")

err = helper.CleanTestProject(projectId)
if err != nil {
t.Error(err)
}
fmt.Print("Cleaned Test Project")

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/devices/devicestoptest/device_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestCli_Devices_Update(t *testing.T) {
if err != nil {
t.Error(err)
}
status, err := helper.IsDeviceStateActive(deviceId)
status, err := helper.IsDeviceStateActive(deviceId, "active")
if err != nil {
t.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/devices/deviceupdatetest/device_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestCli_Devices_Update(t *testing.T) {
if err != nil {
t.Error(err)
}
status, err := helper.IsDeviceStateActive(deviceId)
status, err := helper.IsDeviceStateActive(deviceId, "active")
if err != nil {
t.Error(err)
}
Expand Down
5 changes: 2 additions & 3 deletions test/e2e/events/deviceeventstest/device_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestCli_Events_Get(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()

projectId, err = helper.CreateTestProject("metal-cli-events-pro")
if err != nil {
t.Error(err)
Expand All @@ -50,9 +49,9 @@ func TestCli_Events_Get(t *testing.T) {
if err != nil {
t.Error(err)
}
status, err = helper.IsDeviceStateActive(deviceId)
status, err = helper.IsDeviceStateActive(deviceId, "active")
if err != nil {
status, err = helper.IsDeviceStateActive(deviceId)
status, err = helper.IsDeviceStateActive(deviceId, "active")
if err != nil || !status {
t.Error(err)
}
Expand Down
8 changes: 4 additions & 4 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helper

Check failure on line 1 in test/helper/helper.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/equinix/metal-cli/test/helper

import (
"context"
Expand Down Expand Up @@ -51,7 +51,7 @@
return deviceResp.GetId(), nil
}

func IsDeviceStateActive(deviceId string) (bool, error) {
func IsDeviceStateActive(deviceId string, state string) (bool, error) {
TestApiClient := TestClient()
predefinedTime := 500 * time.Second // Adjust this as needed
retryInterval := 10 * time.Second // Adjust this as needed
Expand All @@ -60,16 +60,16 @@
resp, _, err := TestApiClient.DevicesApi.FindDeviceById(context.Background(), deviceId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.FindDeviceById``: %v\n", err)
return false, err
return false, fmt.Errorf("timed out waiting for device %v to become %v", deviceId, state)
}
if resp.GetState() == "active" {
if resp.GetState() == state {

Check failure on line 65 in test/helper/helper.go

View workflow job for this annotation

GitHub Actions / test

invalid operation: resp.GetState() == state (mismatched types v1.DeviceState and string)

Check failure on line 65 in test/helper/helper.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: resp.GetState() == state (mismatched types v1.DeviceState and string) (typecheck)

Check failure on line 65 in test/helper/helper.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: resp.GetState() == state (mismatched types v1.DeviceState and string)) (typecheck)

Check failure on line 65 in test/helper/helper.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: resp.GetState() == state (mismatched types v1.DeviceState and string)) (typecheck)

Check failure on line 65 in test/helper/helper.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: resp.GetState() == state (mismatched types v1.DeviceState and string)) (typecheck)
return true, nil
}

// Sleep for the specified interval
time.Sleep(retryInterval)
}
return false, fmt.Errorf("timed out waiting for device %v to become active", deviceId)
return false, fmt.Errorf("timed out waiting for device %v to become %v", deviceId, state)
}

func StopTestDevice(deviceId string) error {
Expand Down
Loading