diff --git a/pkg/crc/config/validations.go b/pkg/crc/config/validations.go index cc024ebae7..0465b5d44d 100644 --- a/pkg/crc/config/validations.go +++ b/pkg/crc/config/validations.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "runtime" "strings" "github.com/crc-org/crc/v2/pkg/crc/constants" @@ -134,11 +135,18 @@ func validateYesNo(value interface{}) (bool, string) { return false, "must be yes or no" } +// validatePreset checks if given Preset is valid for CRC cluster creation +// There is an additional check to throw unsupported operation exception in +// case of using 'okd' preset value on top of ARM architecture. func validatePreset(value interface{}) (bool, string) { - _, err := crcpreset.ParsePresetE(cast.ToString(value)) + parsedPreset, err := crcpreset.ParsePresetE(cast.ToString(value)) if err != nil { return false, fmt.Sprintf("Unknown preset. Only %s are valid.", crcpreset.AllPresets()) } + arch := runtime.GOARCH + if parsedPreset == crcpreset.OKD && (arch == "arm" || arch == "arm64") { + return false, fmt.Sprintf("preset '%s' is not supported on %s architecture, please use different preset value", parsedPreset, runtime.GOARCH) + } return true, "" } diff --git a/pkg/crc/config/validations_test.go b/pkg/crc/config/validations_test.go new file mode 100644 index 0000000000..5eb60847e3 --- /dev/null +++ b/pkg/crc/config/validations_test.go @@ -0,0 +1,55 @@ +package config + +import ( + "fmt" + "runtime" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestValidatePresetWithVariousPresetValues(t *testing.T) { + tests := []struct { + presetValue string + validationPassed bool + validationMessage string + }{ + {"openshift", true, ""}, + {"microshift", true, ""}, + {"unknown", false, "Unknown preset"}, + } + for _, tt := range tests { + t.Run(tt.presetValue, func(t *testing.T) { + // When + validationPass, validationMessage := validatePreset(tt.presetValue) + + // Then + assert.Equal(t, tt.validationPassed, validationPass) + assert.Contains(t, validationMessage, tt.validationMessage) + }) + } +} + +func TestValidationPreset_WhenOKDProvidedOnNonArmArchitecture_thenValidationSuccessful(t *testing.T) { + // Given + if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" { + t.Skip("Skipping test; running on ARM architecture") + } + // When + validationPass, validationMessage := validatePreset("okd") + // Then + assert.Equal(t, true, validationPass) + assert.Equal(t, "", validationMessage) +} + +func TestValidationPreset_WhenOKDProvidedOnArmArchitecture_thenValidationFailure(t *testing.T) { + // Given + if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" { + t.Skip("Skipping test; not running on ARM architecture") + } + // When + validationPass, validationMessage := validatePreset("okd") + // Then + assert.Equal(t, false, validationPass) + assert.Equal(t, fmt.Sprintf("preset 'okd' is not supported on %s architecture, please use different preset value", runtime.GOARCH), validationMessage) +}