From 90f80d55b9c376e3a965eb7fd07f47de792a9d58 Mon Sep 17 00:00:00 2001 From: Ruslan Aliev Date: Fri, 24 Apr 2020 02:18:10 -0500 Subject: [PATCH] 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 --- pkg/config/config.go | 4 ++++ pkg/config/config_test.go | 6 +++--- testutil/testconfig.go | 6 ++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 00dcd9a62..15658cfa3 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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] diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index aa37293eb..c7da04523 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -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) diff --git a/testutil/testconfig.go b/testutil/testconfig.go index a67b85825..3f7b1eed7 100644 --- a/testutil/testconfig.go +++ b/testutil/testconfig.go @@ -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