Ensure map is initialized before assignment

Attempts to write to a nil map will cause a runtime panic,
since we don't have explicit checks whether the map is nil
or not - they were added with proper initialization using make.
Appropriate test configuration was added.

Change-Id: Ib50686de44b782fdc881bd4608dd74588fce93bd
Relates-To: #190
Closes: #190
This commit is contained in:
Ruslan Aliev 2020-04-24 02:18:10 -05:00
parent 7d18489bc9
commit 90f80d55b9
3 changed files with 13 additions and 3 deletions

View File

@ -180,6 +180,10 @@ func (c *Config) reconcileClusters() (map[string]string, bool) {
c.Clusters[clusterComplexName.Name] = NewClusterPurpose()
}
if c.Clusters[clusterComplexName.Name].ClusterTypes[clusterComplexName.Type] == nil {
// We have to make sure that ClusterTypes map is initialized properly before assignment
if c.Clusters[clusterComplexName.Name].ClusterTypes == nil {
c.Clusters[clusterComplexName.Name].ClusterTypes = make(map[string]*Cluster)
}
c.Clusters[clusterComplexName.Name].ClusterTypes[clusterComplexName.Type] = NewCluster()
}
configCluster := c.Clusters[clusterComplexName.Name].ClusterTypes[clusterComplexName.Type]

View File

@ -117,7 +117,7 @@ func TestLoadConfig(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
assert.Len(t, conf.Clusters, 5)
assert.Len(t, conf.Clusters, 6)
require.Contains(t, conf.Clusters, "def")
assert.Len(t, conf.Clusters["def"].ClusterTypes, 2)
assert.Len(t, conf.Contexts, 3)
@ -345,7 +345,7 @@ func TestGetClusters(t *testing.T) {
defer cleanup(t)
clusters := conf.GetClusters()
assert.Len(t, clusters, 5)
assert.Len(t, clusters, 6)
}
func TestGetContexts(t *testing.T) {
@ -673,7 +673,7 @@ users:
// Verify that only 3 clusters have been added (original 5 plus 3 new clusters)
// This is important since the above kubeconfig actually has 4
// clusters, but one was already defined in the airship config
assert.Len(t, conf.Clusters, 5+3)
assert.Len(t, conf.Clusters, 6+3)
// verify that the new clusters have been added to the config
_, err := conf.GetCluster("cluster", config.Target)

View File

@ -291,6 +291,8 @@ clusters:
target:
bootstrapInfo: ""
clusterKubeconf: wrongonlyinkubeconf_target
clustertypenil:
clusterType: null
contexts:
def_ephemeral:
contextKubeconf: def_ephemeral
@ -329,6 +331,10 @@ clusters:
insecure-skip-tls-verify: true
server: http://9.10.11.12
name: invalidName
- cluster:
insecure-skip-tls-verify: true
server: http://9.10.11.12
name: clustertypenil_target
contexts:
- context:
cluster: def_ephemeral