Skip to content

Commit a5eb86c

Browse files
committed
fix (config) : Display message when user tries to set okd preset on arm (#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]>
1 parent 8a1d173 commit a5eb86c

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

pkg/crc/config/validations.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"fmt"
5+
"runtime"
56
"strings"
67

78
"github.com/crc-org/crc/v2/pkg/crc/constants"
@@ -134,11 +135,18 @@ func validateYesNo(value interface{}) (bool, string) {
134135
return false, "must be yes or no"
135136
}
136137

138+
// validatePreset checks if given Preset is valid for CRC cluster creation
139+
// There is an additional check to throw unsupported operation exception in
140+
// case of using 'okd' preset value on top of ARM architecture.
137141
func validatePreset(value interface{}) (bool, string) {
138-
_, err := crcpreset.ParsePresetE(cast.ToString(value))
142+
parsedPreset, err := crcpreset.ParsePresetE(cast.ToString(value))
139143
if err != nil {
140144
return false, fmt.Sprintf("Unknown preset. Only %s are valid.", crcpreset.AllPresets())
141145
}
146+
arch := runtime.GOARCH
147+
if parsedPreset == crcpreset.OKD && (arch == "arm" || arch == "arm64") {
148+
return false, fmt.Sprintf("preset '%s' is not supported on %s architecture, please use different preset value", parsedPreset, runtime.GOARCH)
149+
}
142150
return true, ""
143151
}
144152

pkg/crc/config/validations_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestValidatePresetWithVariousPresetValues(t *testing.T) {
12+
tests := []struct {
13+
presetValue string
14+
validationPassed bool
15+
validationMessage string
16+
}{
17+
{"openshift", true, ""},
18+
{"microshift", true, ""},
19+
{"unknown", false, "Unknown preset"},
20+
}
21+
for _, tt := range tests {
22+
t.Run(tt.presetValue, func(t *testing.T) {
23+
// When
24+
validationPass, validationMessage := validatePreset(tt.presetValue)
25+
26+
// Then
27+
assert.Equal(t, tt.validationPassed, validationPass)
28+
assert.Contains(t, validationMessage, tt.validationMessage)
29+
})
30+
}
31+
}
32+
33+
func TestValidationPreset_WhenOKDProvidedOnNonArmArchitecture_thenValidationSuccessful(t *testing.T) {
34+
// Given
35+
if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
36+
t.Skip("Skipping test; running on ARM architecture")
37+
}
38+
// When
39+
validationPass, validationMessage := validatePreset("okd")
40+
// Then
41+
assert.Equal(t, true, validationPass)
42+
assert.Equal(t, "", validationMessage)
43+
}
44+
45+
func TestValidationPreset_WhenOKDProvidedOnArmArchitecture_thenValidationFailure(t *testing.T) {
46+
// Given
47+
if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" {
48+
t.Skip("Skipping test; not running on ARM architecture")
49+
}
50+
// When
51+
validationPass, validationMessage := validatePreset("okd")
52+
// Then
53+
assert.Equal(t, false, validationPass)
54+
assert.Equal(t, fmt.Sprintf("preset 'okd' is not supported on %s architecture, please use different preset value", runtime.GOARCH), validationMessage)
55+
}

0 commit comments

Comments
 (0)