-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af2db53
commit 43f05cc
Showing
2 changed files
with
64 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,63 @@ | ||
package command | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
kcp "skr-tester/pkg/kcp" | ||
"skr-tester/pkg/logger" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type AssertCommand struct { | ||
cobraCmd *cobra.Command | ||
log logger.Logger | ||
instanceID string | ||
machineType string | ||
} | ||
|
||
func NewAsertCmd() *cobra.Command { | ||
cmd := AssertCommand{} | ||
cobraCmd := &cobra.Command{ | ||
Use: "assert", | ||
Aliases: []string{"a"}, | ||
Short: "Does an assertion", | ||
Long: "Does an assertion", | ||
Example: "skr-tester assert -i instanceID -m m6i.large Asserts the instance has the machine type m6i.large.", | ||
|
||
PreRunE: func(_ *cobra.Command, _ []string) error { return cmd.Validate() }, | ||
RunE: func(_ *cobra.Command, _ []string) error { return cmd.Run() }, | ||
} | ||
cmd.cobraCmd = cobraCmd | ||
|
||
cobraCmd.Flags().StringVarP(&cmd.instanceID, "instanceID", "i", "", "InstanceID of the specific instance.") | ||
cobraCmd.Flags().StringVarP(&cmd.machineType, "machineType", "m", "", "MachineType of the specific instance.") | ||
|
||
return cobraCmd | ||
} | ||
|
||
func (cmd *AssertCommand) Run() error { | ||
cmd.log = logger.New() | ||
if cmd.machineType != "" { | ||
kcpClient := kcp.NewKCPClient() | ||
currentMachineType, err := kcpClient.GetCurrentMachineType(cmd.instanceID) | ||
if err != nil { | ||
return fmt.Errorf("failed to get current machine type: %v", err) | ||
} | ||
if cmd.machineType != *currentMachineType { | ||
return fmt.Errorf("machine types are not equal: expected %s, got %s", cmd.machineType, *currentMachineType) | ||
} else { | ||
fmt.Println("Machine type assertion passed: expected and got", cmd.machineType) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (cmd *AssertCommand) Validate() error { | ||
if cmd.instanceID != "" { | ||
return nil | ||
} else { | ||
return errors.New("at least one of the following options have to be specified: instanceID") | ||
} | ||
} |
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