Skip to content

Commit

Permalink
Improved device unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Aug 31, 2023
1 parent c7eed25 commit b04a49d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 28 deletions.
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 @@ -114,8 +114,13 @@ func (c *Client) Create() *cobra.Command {
if billingCycle != "" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetBillingCycle(billingCycle)
}
if alwaysPXE {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(alwaysPXE)
if alwaysPXE != "" {
if alwaysPXE == "true" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(true)
}
if alwaysPXE == "false" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(false)
}
}

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

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 @@ -208,7 +218,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
31 changes: 16 additions & 15 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)
if err != nil {
_, err := helper.IsDeviceStateActive(deviceId)
status, err = helper.IsDeviceStateActive(deviceId)
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.IsDeviceStateInActive(deviceId)
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 @@ -89,17 +90,17 @@ func TestCli_Devices_Update(t *testing.T) {
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
23 changes: 23 additions & 0 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ func IsDeviceStateActive(deviceId string) (bool, error) {
return false, err
}

func IsDeviceStateInActive(deviceId string) (bool, error) {
var err error
var resp *openapiclient.Device
TestApiClient := TestClient()
predefinedTime := 200 * time.Second // Adjust this as needed
retryInterval := 10 * time.Second // Adjust this as needed
startTime := time.Now()
for time.Since(startTime) < predefinedTime {
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
}
if resp.GetState() == "inactive" {
return true, nil
}

// Sleep for the specified interval
time.Sleep(retryInterval)
}
return false, err
}

func StopTestDevice(deviceId string) error {

deviceActionInput := *openapiclient.NewDeviceActionInput("power_off")
Expand Down

0 comments on commit b04a49d

Please sign in to comment.