diff --git a/internal/cmd/auth/activate-service-account/activate_service_account_test.go b/internal/cmd/auth/activate-service-account/activate_service_account_test.go index 5b0799a58..a9e12b30c 100644 --- a/internal/cmd/auth/activate-service-account/activate_service_account_test.go +++ b/internal/cmd/auth/activate-service-account/activate_service_account_test.go @@ -3,8 +3,10 @@ package activateserviceaccount import ( "testing" + "github.com/stackitcloud/stackit-cli/internal/pkg/auth" "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/zalando/go-keyring" "github.com/google/go-cmp/cmp" ) @@ -120,3 +122,62 @@ func TestParseInput(t *testing.T) { }) } } + +func TestStoreFlags(t *testing.T) { + tests := []struct { + description string + model *inputModel + isValid bool + }{ + { + description: "base", + model: fixtureInputModel(), + isValid: true, + }, + { + description: "no values", + model: &inputModel{ + ServiceAccountToken: "", + ServiceAccountKeyPath: "", + PrivateKeyPath: "", + TokenCustomEndpoint: "", + JwksCustomEndpoint: "", + }, + isValid: true, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + // Initialize an empty keyring + keyring.MockInit() + + err := storeFlags(tt.model) + if !tt.isValid { + if err == nil { + t.Fatalf("did not fail on invalid input") + } + return + } + if err != nil { + t.Fatalf("store flags: %v", err) + } + + value, err := auth.GetAuthField(auth.TOKEN_CUSTOM_ENDPOINT) + if err != nil { + t.Errorf("Failed to get value of auth field: %v", err) + } + if value != tt.model.TokenCustomEndpoint { + t.Errorf("Value of \"%s\" does not match: expected \"%s\", got \"%s\"", auth.TOKEN_CUSTOM_ENDPOINT, tt.model.TokenCustomEndpoint, value) + } + + value, err = auth.GetAuthField(auth.JWKS_CUSTOM_ENDPOINT) + if err != nil { + t.Errorf("Failed to get value of auth field: %v", err) + } + if value != tt.model.JwksCustomEndpoint { + t.Errorf("Value of \"%s\" does not match: expected \"%s\", got \"%s\"", auth.JWKS_CUSTOM_ENDPOINT, tt.model.TokenCustomEndpoint, value) + } + }) + } +} diff --git a/internal/cmd/root_test.go b/internal/cmd/root_test.go new file mode 100644 index 000000000..71861a46f --- /dev/null +++ b/internal/cmd/root_test.go @@ -0,0 +1,75 @@ +package cmd + +import ( + "errors" + "testing" + + "github.com/spf13/cobra" + pkgErrors "github.com/stackitcloud/stackit-cli/internal/pkg/errors" +) + +var cmd *cobra.Command +var service *cobra.Command +var resource *cobra.Command +var operation *cobra.Command + +func setupCmd() { + cmd = &cobra.Command{ + Use: "stackit", + } + service = &cobra.Command{ + Use: "service", + } + resource = &cobra.Command{ + Use: "resource", + } + operation = &cobra.Command{ + Use: "operation", + } + cmd.AddCommand(service) + service.AddCommand(resource) + resource.AddCommand(operation) +} + +func TestBeautifyUnknownAndMissingCommandsError(t *testing.T) { + tests := []struct { + description string + inputError error + command *cobra.Command + expectedMsg string + isNotUnknownFlagError bool + }{ + { + description: "root command, extra input is a flag", + inputError: errors.New("unknown flag: --something"), + command: cmd, + expectedMsg: pkgErrors.SUBCOMMAND_MISSING, + }, + { + description: "non unknown flag error, return the same", + inputError: errors.New("some error"), + command: cmd, + expectedMsg: "some error", + isNotUnknownFlagError: true, + }, + } + + setupCmd() + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + actualError := beautifyUnknownAndMissingCommandsError(cmd, tt.inputError) + + if tt.isNotUnknownFlagError { + if actualError.Error() != tt.expectedMsg { + t.Fatalf("expected error message to be %s, got %s", tt.expectedMsg, actualError.Error()) + } + return + } + + appendedErr := pkgErrors.AppendUsageTip(errors.New(tt.expectedMsg), cmd) + if actualError.Error() != appendedErr.Error() { + t.Fatalf("expected error to be %s, got %s", appendedErr.Error(), actualError.Error()) + } + }) + } +} diff --git a/internal/pkg/config/config_test.go b/internal/pkg/config/config_test.go new file mode 100644 index 000000000..194ba8047 --- /dev/null +++ b/internal/pkg/config/config_test.go @@ -0,0 +1,71 @@ +package config + +import ( + "os" + "path/filepath" + "testing" + + "github.com/spf13/viper" +) + +func TestWrite(t *testing.T) { + tests := []struct { + description string + folderName string + folderExists bool + }{ + { + description: "write config file", + folderName: "", + }, + { + description: "write config file to new folder", + folderName: "new-folder", + }, + { + description: "write config file to existing folder", + folderName: "existing-folder", + folderExists: true, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + configPath := filepath.Join(os.TempDir(), tt.folderName, "config.json") + viper.SetConfigFile(configPath) + folderPath = filepath.Dir(configPath) + + if tt.folderExists { + err := os.MkdirAll(folderPath, os.ModePerm) + if err != nil { + t.Fatalf("expected error to be nil, got %v", err) + } + } + + err := Write() + if err != nil { + t.Fatalf("expected error to be nil, got %v", err) + } + + // Check if the file was created + _, err = os.Stat(configPath) + if os.IsNotExist(err) { + t.Fatalf("expected file to exist, got %v", err) + } + + // Delete the file + err = os.Remove(configPath) + if err != nil { + t.Fatalf("expected error to be nil, got %v", err) + } + + // Delete the folder + if tt.folderName != "" { + err = os.Remove(folderPath) + if err != nil { + t.Fatalf("expected error to be nil, got %v", err) + } + } + }) + } +} diff --git a/internal/pkg/errors/errors_test.go b/internal/pkg/errors/errors_test.go new file mode 100644 index 000000000..8e7999d23 --- /dev/null +++ b/internal/pkg/errors/errors_test.go @@ -0,0 +1,629 @@ +package errors + +import ( + "errors" + "fmt" + "testing" + + "github.com/spf13/cobra" +) + +var cmd *cobra.Command +var service *cobra.Command +var resource *cobra.Command +var operation *cobra.Command + +func setupCmd() { + cmd = &cobra.Command{ + Use: "stackit", + } + service = &cobra.Command{ + Use: "service", + } + resource = &cobra.Command{ + Use: "resource", + } + operation = &cobra.Command{ + Use: "operation", + } + cmd.AddCommand(service) + service.AddCommand(resource) + resource.AddCommand(operation) +} + +func TestSimpleErrors(t *testing.T) { + tests := []struct { + description string + err error + expectedMsg string + }{ + { + description: "Test ProjectIdError", + err: &ProjectIdError{}, + expectedMsg: MISSING_PROJECT_ID, + }, + { + description: "Test EmptyUpdateError", + err: &EmptyUpdateError{}, + expectedMsg: EMPTY_UPDATE, + }, + { + description: "Test AuthError", + err: &AuthError{}, + expectedMsg: FAILED_AUTH, + }, + { + description: "Test ActivateServiceAccountError", + err: &ActivateServiceAccountError{}, + expectedMsg: FAILED_SERVICE_ACCOUNT_ACTIVATION, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + if tt.err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, tt.err.Error()) + } + }) + } +} + +func TestArgusInputPlanError(t *testing.T) { + tests := []struct { + description string + args []string + expectedMsg string + }{ + { + description: "base", + args: []string{"arg1", "arg2"}, + expectedMsg: fmt.Sprintf(ARGUS_INVALID_INPUT_PLAN, "stackit service resource operation arg1 arg2", "service"), + }, + { + description: "no args", + args: []string{}, + expectedMsg: fmt.Sprintf(ARGUS_INVALID_INPUT_PLAN, "stackit service resource operation", "service"), + }, + } + + setupCmd() + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &ArgusInputPlanError{ + Cmd: operation, + Args: tt.args, + } + + if err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestArgusInvalidPlanError(t *testing.T) { + tests := []struct { + description string + details string + service string + expectedMsg string + }{ + { + description: "base", + details: "details", + service: "service", + expectedMsg: fmt.Sprintf(ARGUS_INVALID_PLAN, "details", "service"), + }, + { + description: "no details", + details: "", + service: "service", + expectedMsg: fmt.Sprintf(ARGUS_INVALID_PLAN, "", "service"), + }, + { + description: "no service", + details: "details", + service: "", + expectedMsg: fmt.Sprintf(ARGUS_INVALID_PLAN, "details", ""), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &ArgusInvalidPlanError{ + Service: tt.service, + Details: tt.details, + } + + if err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestDSAInputPlanError(t *testing.T) { + tests := []struct { + description string + args []string + expectedMsg string + }{ + { + description: "base", + args: []string{"arg1", "arg2"}, + expectedMsg: fmt.Sprintf(DSA_INVALID_INPUT_PLAN, "stackit service resource operation arg1 arg2", "service"), + }, + { + description: "no args", + args: []string{}, + expectedMsg: fmt.Sprintf(DSA_INVALID_INPUT_PLAN, "stackit service resource operation", "service"), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + setupCmd() + err := &DSAInputPlanError{ + Cmd: operation, + Args: tt.args, + } + + if err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestDSAInvalidPlanError(t *testing.T) { + tests := []struct { + description string + details string + service string + expectedMsg string + }{ + { + description: "base", + details: "details", + service: "service", + expectedMsg: fmt.Sprintf(DSA_INVALID_PLAN, "details", "service"), + }, + { + description: "no details", + details: "", + service: "service", + expectedMsg: fmt.Sprintf(DSA_INVALID_PLAN, "", "service"), + }, + { + description: "no service", + details: "details", + service: "", + expectedMsg: fmt.Sprintf(DSA_INVALID_PLAN, "details", ""), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &DSAInvalidPlanError{ + Service: tt.service, + Details: tt.details, + } + + if err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestDatabaseInputFlavorError(t *testing.T) { + tests := []struct { + description string + args []string + operation string + expectedMsg string + }{ + { + description: "base", + args: []string{"arg1", "arg2"}, + operation: "operation", + expectedMsg: fmt.Sprintf(DATABASE_INVALID_INPUT_FLAVOR, "stackit service resource operation arg1 arg2", "service"), + }, + } + + setupCmd() + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &DatabaseInputFlavorError{ + Cmd: operation, + Args: tt.args, + Operation: tt.operation, + } + + if err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestDatabaseInvalidFlavorError(t *testing.T) { + tests := []struct { + description string + details string + service string + expectedMsg string + }{ + { + description: "base", + details: "details", + service: "service", + expectedMsg: fmt.Sprintf(DATABASE_INVALID_FLAVOR, "details", "service"), + }, + { + description: "no details", + details: "", + service: "service", + expectedMsg: fmt.Sprintf(DATABASE_INVALID_FLAVOR, "", "service"), + }, + { + description: "no service", + details: "details", + service: "", + expectedMsg: fmt.Sprintf(DATABASE_INVALID_FLAVOR, "details", ""), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &DatabaseInvalidFlavorError{ + Service: tt.service, + Details: tt.details, + } + + if err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestDatabaseInvalidStorageError(t *testing.T) { + tests := []struct { + description string + details string + service string + flavorId string + expectedMsg string + }{ + { + description: "base", + details: "details", + service: "service", + flavorId: "flavorId", + expectedMsg: fmt.Sprintf(DATABASE_INVALID_STORAGE, "details", "service", "flavorId"), + }, + { + description: "no details", + details: "", + service: "service", + flavorId: "flavorId", + expectedMsg: fmt.Sprintf(DATABASE_INVALID_STORAGE, "", "service", "flavorId"), + }, + { + description: "no service", + details: "details", + service: "", + flavorId: "flavorId", + expectedMsg: fmt.Sprintf(DATABASE_INVALID_STORAGE, "details", "", "flavorId"), + }, + { + description: "no flavorId", + details: "details", + service: "service", + flavorId: "", + expectedMsg: fmt.Sprintf(DATABASE_INVALID_STORAGE, "details", "service", ""), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &DatabaseInvalidStorageError{ + Service: tt.service, + Details: tt.details, + FlavorId: tt.flavorId, + } + + if err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestFlagValidationError(t *testing.T) { + tests := []struct { + description string + flag string + details string + expectedMsg string + }{ + { + description: "base", + flag: "flag", + details: "details", + expectedMsg: fmt.Sprintf(FLAG_VALIDATION, "flag", "details"), + }, + { + description: "no flag", + flag: "", + details: "details", + expectedMsg: fmt.Sprintf(FLAG_VALIDATION, "", "details"), + }, + { + description: "no details", + flag: "flag", + details: "", + expectedMsg: fmt.Sprintf(FLAG_VALIDATION, "flag", ""), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &FlagValidationError{ + Flag: tt.flag, + Details: tt.details, + } + + if err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestRequiredMutuallyExclusiveFlagsError(t *testing.T) { + tests := []struct { + description string + flags []string + expectedMsg string + }{ + { + description: "base", + flags: []string{"flag1", "flag2"}, + expectedMsg: fmt.Sprintf(REQUIRED_MUTUALLY_EXCLUSIVE_FLAGS, "flag1, flag2"), + }, + { + description: "no flags", + flags: []string{}, + expectedMsg: fmt.Sprintf(REQUIRED_MUTUALLY_EXCLUSIVE_FLAGS, ""), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &RequiredMutuallyExclusiveFlagsError{ + Flags: tt.flags, + } + + if err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestArgValidationError(t *testing.T) { + tests := []struct { + description string + arg string + details string + expectedMsg string + }{ + { + description: "base", + arg: "arg", + details: "details", + expectedMsg: fmt.Sprintf(ARG_VALIDATION, "arg", "details"), + }, + { + description: "no arg", + arg: "", + details: "details", + expectedMsg: fmt.Sprintf(ARG_VALIDATION, "", "details"), + }, + { + description: "no details", + arg: "arg", + details: "", + expectedMsg: fmt.Sprintf(ARG_VALIDATION, "arg", ""), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &ArgValidationError{ + Arg: tt.arg, + Details: tt.details, + } + + if err.Error() != tt.expectedMsg { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestSingleArgExpectedError(t *testing.T) { + tests := []struct { + description string + expected string + count int + expectedMsg string + }{ + { + description: "base", + expected: "expected", + count: 1, + expectedMsg: fmt.Sprintf(ARG_MISSING, "expected"), + }, + { + description: "multiple", + expected: "expected", + count: 2, + expectedMsg: fmt.Sprintf(SINGLE_ARG_EXPECTED, "expected", 2), + }, + } + + setupCmd() + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &SingleArgExpectedError{ + Expected: tt.expected, + Count: tt.count, + Cmd: operation, + } + + appendedErr := AppendUsageTip(errors.New(tt.expectedMsg), operation) + + if err.Error() != appendedErr.Error() { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestSingleOptionalArgExpectedError(t *testing.T) { + tests := []struct { + description string + expected string + count int + expectedMsg string + }{ + { + description: "base", + expected: "expected", + count: 1, + expectedMsg: fmt.Sprintf(SINGLE_OPTIONAL_ARG_EXPECTED, "expected", 1), + }, + { + description: "multiple", + expected: "expected", + count: 2, + expectedMsg: fmt.Sprintf(SINGLE_OPTIONAL_ARG_EXPECTED, "expected", 2), + }, + { + description: "no count", + expected: "expected", + count: 0, + expectedMsg: fmt.Sprintf(SINGLE_OPTIONAL_ARG_EXPECTED, "expected", 0), + }, + } + + setupCmd() + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &SingleOptionalArgExpectedError{ + Expected: tt.expected, + Count: tt.count, + Cmd: operation, + } + + appendedErr := AppendUsageTip(errors.New(tt.expectedMsg), operation) + + if err.Error() != appendedErr.Error() { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestInputUnknownError(t *testing.T) { + tests := []struct { + description string + input string + command *cobra.Command + expectedMsg string + }{ + { + description: "extra argument, not a subcommand", + input: "extra", + command: operation, + expectedMsg: fmt.Sprintf(ARG_UNKNOWN, "extra"), + }, + { + description: "extra subcommand", + input: "extra", + command: service, + expectedMsg: fmt.Sprintf(SUBCOMMAND_UNKNOWN, "extra"), + }, + } + + setupCmd() + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &InputUnknownError{ + ProvidedInput: tt.input, + Cmd: tt.command, + } + + appendedErr := AppendUsageTip(errors.New(tt.expectedMsg), tt.command) + + if err.Error() != appendedErr.Error() { + t.Fatalf("expected error to be %s, got %s", appendedErr.Error(), err.Error()) + } + }) + } +} + +func TestSubcommandMissingError(t *testing.T) { + tests := []struct { + description string + expectedMsg string + }{ + { + description: "base", + expectedMsg: SUBCOMMAND_MISSING, + }, + } + + setupCmd() + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := &SubcommandMissingError{ + Cmd: cmd, + } + + appendedErr := AppendUsageTip(errors.New(tt.expectedMsg), cmd) + + if err.Error() != appendedErr.Error() { + t.Fatalf("expected error to be %s, got %s", tt.expectedMsg, err.Error()) + } + }) + } +} + +func TestAppendUsageTip(t *testing.T) { + tests := []struct { + description string + err error + expectedError error + }{ + { + description: "base", + err: fmt.Errorf("error"), + expectedError: fmt.Errorf("%w.\n\n%s", fmt.Errorf("error"), fmt.Sprintf(USAGE_TIP, "stackit")), + }, + } + + setupCmd() + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + err := AppendUsageTip(tt.err, cmd) + + if err.Error() != tt.expectedError.Error() { + t.Fatalf("expected error to be %s, got %s", tt.expectedError, err.Error()) + } + }) + } +} diff --git a/internal/pkg/projectname/project_name_test.go b/internal/pkg/projectname/project_name_test.go new file mode 100644 index 000000000..25239c8f4 --- /dev/null +++ b/internal/pkg/projectname/project_name_test.go @@ -0,0 +1,61 @@ +package projectname + +import ( + "context" + "testing" + + "github.com/google/uuid" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/stackitcloud/stackit-cli/internal/pkg/config" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" +) + +var testProjectId = uuid.NewString() + +func TestGetProjectName(t *testing.T) { + tests := []struct { + description string + projectName string + projectId string + isValid bool + }{ + { + description: "Project name from config", + projectName: "project-name", + projectId: testProjectId, + isValid: true, + }, + { + description: "empty project name and id", + projectName: "", + projectId: "", + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + viper.Set(config.ProjectNameKey, tt.projectName) + viper.Set(config.ProjectIdKey, tt.projectId) + defer viper.Reset() + p := print.NewPrinter() + cmd := &cobra.Command{} + + projectName, err := GetProjectName(context.Background(), p, cmd) + if err != nil { + if tt.isValid { + t.Fatalf("unexpected error: %v", err) + } + return + } + if !tt.isValid { + t.Fatalf("expected error, got project name %q", projectName) + } + + if projectName != tt.projectName { + t.Fatalf("expected project name %q, got %q", tt.projectName, projectName) + } + }) + } +} diff --git a/internal/pkg/services/mongodbflex/utils/utils_test.go b/internal/pkg/services/mongodbflex/utils/utils_test.go index bdea1a34b..1024c5710 100644 --- a/internal/pkg/services/mongodbflex/utils/utils_test.go +++ b/internal/pkg/services/mongodbflex/utils/utils_test.go @@ -644,3 +644,55 @@ func TestGetRestoreStatus(t *testing.T) { }) } } + +func TestGetInstanceType(t *testing.T) { + tests := []struct { + description string + numReplicas int64 + expectedOutput string + isValid bool + }{ + { + description: "single", + numReplicas: 1, + expectedOutput: "Single", + isValid: true, + }, + { + description: "replica set", + numReplicas: 3, + expectedOutput: "Replica", + isValid: true, + }, + { + description: "sharded cluster", + numReplicas: 9, + expectedOutput: "Sharded", + isValid: true, + }, + { + description: "invalid", + numReplicas: 0, + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + output, err := GetInstanceType(tt.numReplicas) + if !tt.isValid { + if err == nil { + t.Fatalf("did not fail on invalid input") + } + return + } + if err != nil { + t.Fatalf("failed on valid input: %v", err) + } + + if output != tt.expectedOutput { + t.Fatalf("expected output to be %s, got %s", tt.expectedOutput, output) + } + }) + } +} diff --git a/internal/pkg/services/postgresflex/utils/utils_test.go b/internal/pkg/services/postgresflex/utils/utils_test.go index c25d4a1d3..250295e79 100644 --- a/internal/pkg/services/postgresflex/utils/utils_test.go +++ b/internal/pkg/services/postgresflex/utils/utils_test.go @@ -569,3 +569,49 @@ func TestGetUserName(t *testing.T) { }) } } + +func TestGetInstanceType(t *testing.T) { + tests := []struct { + description string + numReplicas int64 + expectedOutput string + isValid bool + }{ + { + description: "single", + numReplicas: 1, + expectedOutput: "Single", + isValid: true, + }, + { + description: "replica set", + numReplicas: 3, + expectedOutput: "Replica", + isValid: true, + }, + { + description: "invalid", + numReplicas: 0, + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + output, err := GetInstanceType(tt.numReplicas) + if !tt.isValid { + if err == nil { + t.Fatalf("did not fail on invalid input") + } + return + } + if err != nil { + t.Fatalf("failed on valid input: %v", err) + } + + if output != tt.expectedOutput { + t.Fatalf("expected output to be %s, got %s", tt.expectedOutput, output) + } + }) + } +} diff --git a/internal/pkg/services/ske/utils/utils_test.go b/internal/pkg/services/ske/utils/utils_test.go index fe977e1f2..2158e175a 100644 --- a/internal/pkg/services/ske/utils/utils_test.go +++ b/internal/pkg/services/ske/utils/utils_test.go @@ -610,3 +610,30 @@ func TestWriteConfigFile(t *testing.T) { t.Errorf("failed cleaning test data") } } + +func TestGetDefaultKubeconfigPath(t *testing.T) { + tests := []struct { + description string + }{ + { + description: "base", + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + output, err := GetDefaultKubeconfigPath() + + if err != nil { + t.Errorf("failed on valid input") + } + userHome, err := os.UserHomeDir() + if err != nil { + t.Errorf("could not get user home directory") + } + if output != filepath.Join(userHome, ".kube", "config") { + t.Errorf("expected output to be %s, got %s", filepath.Join(userHome, ".kube", "config"), output) + } + }) + } +}