Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (config) : Display helpful message when user tries to set okd preset on arm (#3389) #4485

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading