From 4b4cebadfab472f516dcded8f0594977c187c8c8 Mon Sep 17 00:00:00 2001 From: Jakub Al-Khalili Date: Thu, 12 Sep 2019 09:50:29 +0200 Subject: [PATCH] #96 Improve pod unit tests --- .../configuration/base/resources/pod_test.go | 123 +++++++++++++++++- 1 file changed, 120 insertions(+), 3 deletions(-) diff --git a/pkg/controller/jenkins/configuration/base/resources/pod_test.go b/pkg/controller/jenkins/configuration/base/resources/pod_test.go index 29133b24..e3788ba2 100644 --- a/pkg/controller/jenkins/configuration/base/resources/pod_test.go +++ b/pkg/controller/jenkins/configuration/base/resources/pod_test.go @@ -11,7 +11,7 @@ func TestGetJenkinsMasterPodBaseVolumes(t *testing.T) { configMapName := "config-map" jenkins := &v1alpha2.Jenkins{ Spec: v1alpha2.JenkinsSpec{ - ConfigurationAsCode:v1alpha2.ConfigurationAsCode{ + ConfigurationAsCode: v1alpha2.ConfigurationAsCode{ Customization: v1alpha2.Customization{ Configurations: []v1alpha2.ConfigMapRef{ { @@ -23,7 +23,7 @@ func TestGetJenkinsMasterPodBaseVolumes(t *testing.T) { }, }, }, - GroovyScripts:v1alpha2.GroovyScripts{ + GroovyScripts: v1alpha2.GroovyScripts{ Customization: v1alpha2.Customization{ Configurations: []v1alpha2.ConfigMapRef{ { @@ -49,6 +49,123 @@ func TestGetJenkinsMasterPodBaseVolumes(t *testing.T) { } } - assert.True(t, groovyExists && cascExists) + assert.True(t, groovyExists) + assert.True(t, cascExists) + }) + t.Run("groovy script without secret name", func(t *testing.T) { + jenkins := &v1alpha2.Jenkins{ + Spec: v1alpha2.JenkinsSpec{ + ConfigurationAsCode: v1alpha2.ConfigurationAsCode{ + Customization: v1alpha2.Customization{ + Configurations: []v1alpha2.ConfigMapRef{ + { + Name: "casc-scripts", + }, + }, + Secret: v1alpha2.SecretRef{ + Name: "jenkins-secret", + }, + }, + }, + GroovyScripts: v1alpha2.GroovyScripts{ + Customization: v1alpha2.Customization{ + Configurations: []v1alpha2.ConfigMapRef{ + { + Name: "groovy-scripts", + }, + }, + }, + }, + }, + } + + volumeExists := false + for _, volume := range GetJenkinsMasterPodBaseVolumes(jenkins) { + if volume.Name == ("casc-" + jenkins.Spec.ConfigurationAsCode.Secret.Name) { + volumeExists = true + } + } + + assert.True(t, volumeExists) + }) + t.Run("casc without secret name", func(t *testing.T) { + jenkins := &v1alpha2.Jenkins{ + Spec: v1alpha2.JenkinsSpec{ + ConfigurationAsCode: v1alpha2.ConfigurationAsCode{ + Customization: v1alpha2.Customization{ + Configurations: []v1alpha2.ConfigMapRef{ + { + Name: "casc-scripts", + }, + }, + }, + }, + GroovyScripts: v1alpha2.GroovyScripts{ + Customization: v1alpha2.Customization{ + Configurations: []v1alpha2.ConfigMapRef{ + { + Name: "groovy-scripts", + }, + }, + Secret: v1alpha2.SecretRef{ + Name: "jenkins-secret", + }, + }, + }, + }, + } + + volumeExists := false + for _, volume := range GetJenkinsMasterPodBaseVolumes(jenkins) { + if volume.Name == ("gs-" + jenkins.Spec.GroovyScripts.Secret.Name) { + volumeExists = true + } + } + + assert.True(t, volumeExists) + }) + t.Run("casc and groovy script shared secret name", func(t *testing.T) { + jenkins := &v1alpha2.Jenkins{ + Spec: v1alpha2.JenkinsSpec{ + ConfigurationAsCode: v1alpha2.ConfigurationAsCode{ + Customization: v1alpha2.Customization{ + Configurations: []v1alpha2.ConfigMapRef{ + { + Name: "casc-scripts", + }, + }, + Secret: v1alpha2.SecretRef{ + Name: "jenkins-secret", + }, + }, + }, + GroovyScripts: v1alpha2.GroovyScripts{ + Customization: v1alpha2.Customization{ + Configurations: []v1alpha2.ConfigMapRef{ + { + Name: "groovy-scripts", + }, + }, + Secret: v1alpha2.SecretRef{ + Name: "jenkins-secret", + }, + }, + }, + }, + } + + groovyExists := false + cascExists := false + + for _, volume := range GetJenkinsMasterPodBaseVolumes(jenkins) { + if volume.Name == ("gs-" + jenkins.Spec.GroovyScripts.Secret.Name) { + groovyExists = true + } else if volume.Name == ("casc-" + jenkins.Spec.ConfigurationAsCode.Secret.Name) { + cascExists = true + } + } + + assert.True(t, groovyExists) + assert.True(t, cascExists) }) }