From 89e67664527c0c48d66e05dbbe626dd860344cba Mon Sep 17 00:00:00 2001 From: Vasubabu <3358152+vasubabu@users.noreply.github.com> Date: Thu, 31 Aug 2023 22:05:41 +0530 Subject: [PATCH] Improved device unit test cases --- internal/devices/create.go | 22 +++++++++---- internal/devices/update.go | 15 ++++++--- .../devicestarttest/device_start_test.go | 31 ++++++++++--------- test/helper/helper.go | 23 ++++++++++++++ 4 files changed, 65 insertions(+), 26 deletions(-) diff --git a/internal/devices/create.go b/internal/devices/create.go index cad04d66..9c809d23 100644 --- a/internal/devices/create.go +++ b/internal/devices/create.go @@ -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 @@ -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 != "" { @@ -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 != "" { @@ -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"`) diff --git a/internal/devices/update.go b/internal/devices/update.go index d0664719..ba817e23 100644 --- a/internal/devices/update.go +++ b/internal/devices/update.go @@ -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 @@ -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 != "" { @@ -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"} @@ -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") diff --git a/test/e2e/devices/devicestarttest/device_start_test.go b/test/e2e/devices/devicestarttest/device_start_test.go index 23136d10..21272c4e 100644 --- a/test/e2e/devices/devicestarttest/device_start_test.go +++ b/test/e2e/devices/devicestarttest/device_start_test.go @@ -1,7 +1,6 @@ package devicestarttest import ( - "fmt" "io" "os" "strings" @@ -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() @@ -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") + } } } diff --git a/test/helper/helper.go b/test/helper/helper.go index 723760e4..14ea0607 100644 --- a/test/helper/helper.go +++ b/test/helper/helper.go @@ -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")