Skip to content

Commit

Permalink
fix (config) : Display message when user tries to set okd preset on a…
Browse files Browse the repository at this point in the history
…rm (#3389)

Add additional handling in validatePreset method for failing the
validation when `okd` preset is provided on arm architecture. Validation
would fail with message directing user to use other preset values.

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Nov 26, 2024
1 parent f038d75 commit 1cb8203
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/crc/config/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"runtime"
"strings"

"github.com/crc-org/crc/v2/pkg/crc/constants"
Expand Down Expand Up @@ -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, ""
}

Expand Down
55 changes: 55 additions & 0 deletions pkg/crc/config/validations_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 1cb8203

Please sign in to comment.