Skip to content

Commit

Permalink
test (config) : Add some unit tests for config settings
Browse files Browse the repository at this point in the history
Extracted some unit tests out of #4451 as I was need them
while working on #3832

These unit tests verify these scenarios for config:
- when we try to set invalid key, throw error
- whether default key values are as expected
- whether we're able to override default values by providing new value

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia authored and praveenkumar committed Jan 2, 2025
1 parent d89df4c commit c08e41a
Showing 1 changed file with 180 additions and 0 deletions.
180 changes: 180 additions & 0 deletions pkg/crc/config/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,183 @@ func TestPath(t *testing.T) {
IsSecret: false,
}, cfg.Get(ProxyCAFile))
}

func TestWhenInvalidKeySetThenErrorIsThrown(t *testing.T) {
// Given
cfg, err := newInMemoryConfig()
require.NoError(t, err)

// When + Then
_, err = cfg.Set("i-dont-exist", "i-should-not-be-set")
assert.Error(t, err, "Configuration property 'i-dont-exist' does not exist")
}

var configDefaultValuesTestArguments = []struct {
key string
defaultValue interface{}
}{
{
KubeAdminPassword, "",
},
{
CPUs, uint(4),
},
{
Memory, uint(10752),
},
{
DiskSize, 31,
},
{
NameServer, "",
},
{
PullSecretFile, "",
},
{
DisableUpdateCheck, false,
},
{
ExperimentalFeatures, false,
},
{
EmergencyLogin, false,
},
{
PersistentVolumeSize, 15,
},
{
HostNetworkAccess, false,
},
{
HTTPProxy, "",
},
{
HTTPSProxy, "",
},
{
NoProxy, "",
},
{
ProxyCAFile, Path(""),
},
{
EnableClusterMonitoring, false,
},
{
ConsentTelemetry, "",
},
{
IngressHTTPPort, 80,
},
{
IngressHTTPSPort, 443,
},
{
EnableBundleQuayFallback, false,
},
{
Preset, "openshift",
},
}

func TestDefaultKeyValuesSetInConfig(t *testing.T) {
for _, tt := range configDefaultValuesTestArguments {
t.Run(tt.key, func(t *testing.T) {
// Given
cfg, err := newInMemoryConfig()
require.NoError(t, err)

// When + Then
assert.Equal(t, SettingValue{
Value: tt.defaultValue,
Invalid: false,
IsDefault: true,
}, cfg.Get(tt.key))
})
}
}

var configProvidedValuesTestArguments = []struct {
key string
providedValue interface{}
}{
{
KubeAdminPassword, "kubeadmin-secret-password",
},
{
CPUs, uint(8),
},
{
Memory, uint(21504),
},
{
DiskSize, 62,
},
{
NameServer, "127.0.0.1",
},
{
DisableUpdateCheck, true,
},
{
ExperimentalFeatures, true,
},
{
EmergencyLogin, true,
},
{
PersistentVolumeSize, 20,
},
{
HTTPProxy, "http://proxy-via-http-proxy-property:3128",
},
{
HTTPSProxy, "https://proxy-via-http-proxy-property:3128",
},
{
NoProxy, "http://no-proxy-property:3128",
},
{
EnableClusterMonitoring, true,
},
{
ConsentTelemetry, "yes",
},
{
IngressHTTPPort, 8080,
},
{
IngressHTTPSPort, 6443,
},
{
EnableBundleQuayFallback, true,
},
{
Preset, "microshift",
},
}

func TestSetProvidedValuesOverrideDefaultValuesInConfig(t *testing.T) {
for _, tt := range configProvidedValuesTestArguments {
t.Run(tt.key, func(t *testing.T) {

// When + Then

// Given
cfg, err := newInMemoryConfig()
require.NoError(t, err)

// When
_, err = cfg.Set(tt.key, tt.providedValue)
require.NoError(t, err)

// Then
assert.Equal(t, SettingValue{
Value: tt.providedValue,
Invalid: false,
IsDefault: false,
}, cfg.Get(tt.key))
})
}
}

0 comments on commit c08e41a

Please sign in to comment.