mirror of
https://github.com/jenkinsci/kubernetes-operator.git
synced 2026-10-17 21:35:38 +02:00
Improved notification tests, refactor ConfigurationType to Phase
This commit is contained in:
@@ -6,13 +6,11 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
docker "github.com/docker/distribution/reference"
|
||||
"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
|
||||
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/configuration/base/resources"
|
||||
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants"
|
||||
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/plugins"
|
||||
"github.com/jenkinsci/kubernetes-operator/pkg/log"
|
||||
|
||||
docker "github.com/docker/distribution/reference"
|
||||
stackerr "github.com/pkg/errors"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -27,38 +25,38 @@ var (
|
||||
func (r *ReconcileJenkinsBaseConfiguration) Validate(jenkins *v1alpha2.Jenkins) ([]string, error) {
|
||||
var messages []string
|
||||
|
||||
if msg := r.validateReservedVolumes(); msg != nil {
|
||||
if msg := r.validateReservedVolumes(); len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
|
||||
if msg, err := r.validateVolumes(); err != nil {
|
||||
return nil, err
|
||||
} else if msg != nil {
|
||||
} else if len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
|
||||
for _, container := range jenkins.Spec.Master.Containers {
|
||||
if msg := r.validateContainer(container); msg != nil {
|
||||
if msg := r.validateContainer(container); len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
}
|
||||
|
||||
if msg := r.validatePlugins(plugins.BasePlugins(), jenkins.Spec.Master.BasePlugins, jenkins.Spec.Master.Plugins); msg != nil {
|
||||
if msg := r.validatePlugins(plugins.BasePlugins(), jenkins.Spec.Master.BasePlugins, jenkins.Spec.Master.Plugins); len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
|
||||
if msg := r.validateJenkinsMasterPodEnvs(); msg != nil {
|
||||
if msg := r.validateJenkinsMasterPodEnvs(); len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
|
||||
if msg, err := r.validateCustomization(r.jenkins.Spec.GroovyScripts.Customization, "spec.groovyScripts"); err != nil {
|
||||
return nil, err
|
||||
} else if msg != nil {
|
||||
} else if len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
if msg, err := r.validateCustomization(r.jenkins.Spec.ConfigurationAsCode.Customization, "spec.configurationAsCode"); err != nil {
|
||||
return nil, err
|
||||
} else if msg != nil {
|
||||
} else if len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
|
||||
@@ -72,7 +70,7 @@ func (r *ReconcileJenkinsBaseConfiguration) validateImagePullSecrets() ([]string
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if msg != nil {
|
||||
if len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
}
|
||||
@@ -112,19 +110,19 @@ func (r *ReconcileJenkinsBaseConfiguration) validateVolumes() ([]string, error)
|
||||
case volume.ConfigMap != nil:
|
||||
if msg, err := r.validateConfigMapVolume(volume); err != nil {
|
||||
return nil, err
|
||||
} else if msg != nil {
|
||||
} else if len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
case volume.Secret != nil:
|
||||
if msg, err := r.validateSecretVolume(volume); err != nil {
|
||||
return nil, err
|
||||
} else if msg != nil {
|
||||
} else if len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
case volume.PersistentVolumeClaim != nil:
|
||||
if msg, err := r.validatePersistentVolumeClaim(volume); err != nil {
|
||||
return nil, err
|
||||
} else if msg != nil {
|
||||
} else if len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
default: //TODO add support for rest of volumes
|
||||
@@ -211,7 +209,7 @@ func (r *ReconcileJenkinsBaseConfiguration) validateContainer(container v1alpha2
|
||||
messages = append(messages, "Image pull policy not set")
|
||||
}
|
||||
|
||||
if msg := r.validateContainerVolumeMounts(container); msg != nil {
|
||||
if msg := r.validateContainerVolumeMounts(container); len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
|
||||
@@ -307,19 +305,19 @@ func (r *ReconcileJenkinsBaseConfiguration) validatePlugins(requiredBasePlugins
|
||||
}
|
||||
}
|
||||
|
||||
if !plugins.VerifyDependencies(allPlugins) {
|
||||
messages = append(messages, "Invalid dependencies")
|
||||
if msg := plugins.VerifyDependencies(allPlugins); len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
|
||||
if !r.verifyBasePlugins(requiredBasePlugins, basePlugins) {
|
||||
messages = append(messages, "Failed to verify base plugins")
|
||||
if msg := r.verifyBasePlugins(requiredBasePlugins, basePlugins); len(msg) > 0 {
|
||||
messages = append(messages, msg...)
|
||||
}
|
||||
|
||||
return messages
|
||||
}
|
||||
|
||||
func (r *ReconcileJenkinsBaseConfiguration) verifyBasePlugins(requiredBasePlugins []plugins.Plugin, basePlugins []v1alpha2.Plugin) bool {
|
||||
valid := true
|
||||
func (r *ReconcileJenkinsBaseConfiguration) verifyBasePlugins(requiredBasePlugins []plugins.Plugin, basePlugins []v1alpha2.Plugin) []string {
|
||||
var messages []string
|
||||
|
||||
for _, requiredBasePlugin := range requiredBasePlugins {
|
||||
found := false
|
||||
@@ -330,12 +328,11 @@ func (r *ReconcileJenkinsBaseConfiguration) verifyBasePlugins(requiredBasePlugin
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
valid = false
|
||||
r.logger.V(log.VWarn).Info(fmt.Sprintf("Missing plugin '%s' in spec.master.basePlugins", requiredBasePlugin.Name))
|
||||
messages = append(messages, fmt.Sprintf("Missing plugin '%s' in spec.master.basePlugins", requiredBasePlugin.Name))
|
||||
}
|
||||
}
|
||||
|
||||
return valid
|
||||
return messages
|
||||
}
|
||||
|
||||
func (r *ReconcileJenkinsBaseConfiguration) validateCustomization(customization v1alpha2.Customization, name string) ([]string, error) {
|
||||
|
||||
@@ -48,7 +48,7 @@ func TestValidatePlugins(t *testing.T) {
|
||||
|
||||
got := baseReconcileLoop.validatePlugins(requiredBasePlugins, basePlugins, userPlugins)
|
||||
|
||||
assert.NotNil(t, got)
|
||||
assert.Equal(t, got, []string{"invalid plugin name 'INVALID?:0.0.1', must follow pattern '(?i)^[0-9a-z-_]+$'"})
|
||||
})
|
||||
t.Run("invalid user plugin version", func(t *testing.T) {
|
||||
var requiredBasePlugins []plugins.Plugin
|
||||
@@ -57,7 +57,7 @@ func TestValidatePlugins(t *testing.T) {
|
||||
|
||||
got := baseReconcileLoop.validatePlugins(requiredBasePlugins, basePlugins, userPlugins)
|
||||
|
||||
assert.NotNil(t, got)
|
||||
assert.Equal(t, got, []string{"invalid plugin version 'simple-plugin:invalid', must follow pattern '^[0-9\\\\.]+$'"})
|
||||
})
|
||||
t.Run("valid base plugin", func(t *testing.T) {
|
||||
var requiredBasePlugins []plugins.Plugin
|
||||
@@ -75,7 +75,7 @@ func TestValidatePlugins(t *testing.T) {
|
||||
|
||||
got := baseReconcileLoop.validatePlugins(requiredBasePlugins, basePlugins, userPlugins)
|
||||
|
||||
assert.NotNil(t, got)
|
||||
assert.Equal(t, got, []string{"invalid plugin name 'INVALID?:0.0.1', must follow pattern '(?i)^[0-9a-z-_]+$'"})
|
||||
})
|
||||
t.Run("invalid base plugin version", func(t *testing.T) {
|
||||
var requiredBasePlugins []plugins.Plugin
|
||||
@@ -84,7 +84,7 @@ func TestValidatePlugins(t *testing.T) {
|
||||
|
||||
got := baseReconcileLoop.validatePlugins(requiredBasePlugins, basePlugins, userPlugins)
|
||||
|
||||
assert.NotNil(t, got)
|
||||
assert.Equal(t, got, []string{"invalid plugin version 'simple-plugin:invalid', must follow pattern '^[0-9\\\\.]+$'"})
|
||||
})
|
||||
t.Run("valid user and base plugin version", func(t *testing.T) {
|
||||
var requiredBasePlugins []plugins.Plugin
|
||||
@@ -129,7 +129,7 @@ func TestValidatePlugins(t *testing.T) {
|
||||
|
||||
got := baseReconcileLoop.validatePlugins(requiredBasePlugins, basePlugins, userPlugins)
|
||||
|
||||
assert.NotNil(t, got)
|
||||
assert.Equal(t, got, []string{"Missing plugin 'simple-plugin' in spec.master.basePlugins"})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -186,7 +186,8 @@ func TestReconcileJenkinsBaseConfiguration_validateImagePullSecrets(t *testing.T
|
||||
&jenkins, false, false, nil, nil)
|
||||
|
||||
got, _ := baseReconcileLoop.validateImagePullSecrets()
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Secret test-ref not found defined in spec.master.imagePullSecrets", "Secret 'test-ref' defined in spec.master.imagePullSecrets doesn't have 'docker-server' key.", "Secret 'test-ref' defined in spec.master.imagePullSecrets doesn't have 'docker-username' key.", "Secret 'test-ref' defined in spec.master.imagePullSecrets doesn't have 'docker-password' key.", "Secret 'test-ref' defined in spec.master.imagePullSecrets doesn't have 'docker-email' key."})
|
||||
})
|
||||
|
||||
t.Run("no docker email", func(t *testing.T) {
|
||||
@@ -219,7 +220,8 @@ func TestReconcileJenkinsBaseConfiguration_validateImagePullSecrets(t *testing.T
|
||||
&jenkins, false, false, nil, nil)
|
||||
|
||||
got, _ := baseReconcileLoop.validateImagePullSecrets()
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Secret 'test-ref' defined in spec.master.imagePullSecrets doesn't have 'docker-email' key."})
|
||||
})
|
||||
|
||||
t.Run("no docker password", func(t *testing.T) {
|
||||
@@ -252,7 +254,8 @@ func TestReconcileJenkinsBaseConfiguration_validateImagePullSecrets(t *testing.T
|
||||
&jenkins, false, false, nil, nil)
|
||||
|
||||
got, _ := baseReconcileLoop.validateImagePullSecrets()
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Secret 'test-ref' defined in spec.master.imagePullSecrets doesn't have 'docker-password' key."})
|
||||
})
|
||||
|
||||
t.Run("no docker username", func(t *testing.T) {
|
||||
@@ -285,7 +288,8 @@ func TestReconcileJenkinsBaseConfiguration_validateImagePullSecrets(t *testing.T
|
||||
&jenkins, false, false, nil, nil)
|
||||
|
||||
got, _ := baseReconcileLoop.validateImagePullSecrets()
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Secret 'test-ref' defined in spec.master.imagePullSecrets doesn't have 'docker-username' key."})
|
||||
})
|
||||
|
||||
t.Run("no docker server", func(t *testing.T) {
|
||||
@@ -318,7 +322,8 @@ func TestReconcileJenkinsBaseConfiguration_validateImagePullSecrets(t *testing.T
|
||||
&jenkins, false, false, nil, nil)
|
||||
|
||||
got, _ := baseReconcileLoop.validateImagePullSecrets()
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Secret 'test-ref' defined in spec.master.imagePullSecrets doesn't have 'docker-server' key."})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -374,7 +379,8 @@ func TestValidateJenkinsMasterPodEnvs(t *testing.T) {
|
||||
baseReconcileLoop := New(nil, nil, logf.ZapLogger(false),
|
||||
&jenkins, false, false, nil, nil)
|
||||
got := baseReconcileLoop.validateJenkinsMasterPodEnvs()
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Jenkins Master container env 'JENKINS_HOME' cannot be overridden"})
|
||||
})
|
||||
t.Run("missing -Djava.awt.headless=true in JAVA_OPTS env", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
@@ -396,7 +402,8 @@ func TestValidateJenkinsMasterPodEnvs(t *testing.T) {
|
||||
baseReconcileLoop := New(nil, nil, logf.ZapLogger(false),
|
||||
&jenkins, false, false, nil, nil)
|
||||
got := baseReconcileLoop.validateJenkinsMasterPodEnvs()
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Jenkins Master container env 'JAVA_OPTS' doesn't have required flag '-Djava.awt.headless=true'"})
|
||||
})
|
||||
t.Run("missing -Djenkins.install.runSetupWizard=false in JAVA_OPTS env", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
@@ -418,7 +425,8 @@ func TestValidateJenkinsMasterPodEnvs(t *testing.T) {
|
||||
baseReconcileLoop := New(nil, nil, logf.ZapLogger(false),
|
||||
&jenkins, false, false, nil, nil)
|
||||
got := baseReconcileLoop.validateJenkinsMasterPodEnvs()
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Jenkins Master container env 'JAVA_OPTS' doesn't have required flag '-Djenkins.install.runSetupWizard=false'"})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -455,7 +463,8 @@ func TestValidateReservedVolumes(t *testing.T) {
|
||||
baseReconcileLoop := New(nil, nil, logf.ZapLogger(false),
|
||||
&jenkins, false, false, nil, nil)
|
||||
got := baseReconcileLoop.validateReservedVolumes()
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Jenkins Master pod volume 'jenkins-home' is reserved please choose different one"})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -545,7 +554,8 @@ func TestValidateContainerVolumeMounts(t *testing.T) {
|
||||
baseReconcileLoop := New(nil, nil, logf.ZapLogger(false),
|
||||
&jenkins, false, false, nil, nil)
|
||||
got := baseReconcileLoop.validateContainerVolumeMounts(jenkins.Spec.Master.Containers[0])
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Not found volume for 'missing-volume' volume mount in container ''"})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -618,7 +628,8 @@ func TestValidateConfigMapVolume(t *testing.T) {
|
||||
got, err := baseReconcileLoop.validateConfigMapVolume(volume)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"ConfigMap 'configmap-name' not found for volume '{volume-name {nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil &ConfigMapVolumeSource{LocalObjectReference:LocalObjectReference{Name:configmap-name,},Items:[],DefaultMode:nil,Optional:*false,} nil nil nil nil nil nil nil nil}}'"})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -687,7 +698,8 @@ func TestValidateSecretVolume(t *testing.T) {
|
||||
got, err := baseReconcileLoop.validateSecretVolume(volume)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Secret 'secret-name' not found for volume '{volume-name {nil nil nil nil nil &SecretVolumeSource{SecretName:secret-name,Items:[],DefaultMode:nil,Optional:*false,} nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil}}'"})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -731,7 +743,8 @@ func TestValidateCustomization(t *testing.T) {
|
||||
got, err := baseReconcileLoop.validateCustomization(customization, "spec.groovyScripts")
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"spec.groovyScripts.secret.name is set but spec.groovyScripts.configurations is empty"})
|
||||
})
|
||||
t.Run("secret and configmap exists", func(t *testing.T) {
|
||||
customization := v1alpha2.Customization{
|
||||
@@ -784,7 +797,8 @@ func TestValidateCustomization(t *testing.T) {
|
||||
got, err := baseReconcileLoop.validateCustomization(customization, "spec.groovyScripts")
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"Secret 'secretName' configured in spec.groovyScripts.secret.name not found"})
|
||||
})
|
||||
t.Run("secret exists and configmap not exists", func(t *testing.T) {
|
||||
customization := v1alpha2.Customization{
|
||||
@@ -806,6 +820,7 @@ func TestValidateCustomization(t *testing.T) {
|
||||
got, err := baseReconcileLoop.validateCustomization(customization, "spec.groovyScripts")
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, got)
|
||||
|
||||
assert.Equal(t, got, []string{"ConfigMap 'configmap-name' configured in spec.groovyScripts.configurations[0] not found"})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user