Improved notification tests, refactor ConfigurationType to Phase

This commit is contained in:
Jakub Al-Khalili
2019-10-03 15:53:39 +02:00
parent ca3508ef49
commit 4e8b0fa72e
13 changed files with 222 additions and 207 deletions
@@ -8,9 +8,6 @@ import (
"strings"
"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
"github.com/jenkinsci/kubernetes-operator/pkg/log"
"github.com/go-logr/logr"
stackerr "github.com/pkg/errors"
"github.com/robfig/cron"
"k8s.io/api/core/v1"
@@ -22,41 +19,39 @@ import (
func (s *SeedJobs) ValidateSeedJobs(jenkins v1alpha2.Jenkins) ([]string, error) {
var messages []string
if msg := s.validateIfIDIsUnique(jenkins.Spec.SeedJobs); msg != nil {
if msg := s.validateIfIDIsUnique(jenkins.Spec.SeedJobs); len(msg) > 0 {
messages = append(messages, msg...)
}
for _, seedJob := range jenkins.Spec.SeedJobs {
logger := s.logger.WithValues("seedJob", seedJob.ID).V(log.VWarn)
if len(seedJob.ID) == 0 {
messages = append(messages, "id can't be empty")
messages = append(messages, fmt.Sprintf("seedJob `%s` id can't be empty", seedJob.ID))
}
if len(seedJob.RepositoryBranch) == 0 {
messages = append(messages, "repository branch can't be empty")
messages = append(messages, fmt.Sprintf("seedJob `%s` repository branch can't be empty", seedJob.ID))
}
if len(seedJob.RepositoryURL) == 0 {
messages = append(messages, "repository URL branch can't be empty")
messages = append(messages, fmt.Sprintf("seedJob `%s` repository URL branch can't be empty", seedJob.ID))
}
if len(seedJob.Targets) == 0 {
messages = append(messages, "targets can't be empty")
messages = append(messages, fmt.Sprintf("seedJob `%s` targets can't be empty", seedJob.ID))
}
if _, ok := v1alpha2.AllowedJenkinsCredentialMap[string(seedJob.JenkinsCredentialType)]; !ok {
messages = append(messages, "unknown credential type")
messages = append(messages, fmt.Sprintf("seedJob `%s` unknown credential type", seedJob.ID))
}
if (seedJob.JenkinsCredentialType == v1alpha2.BasicSSHCredentialType ||
seedJob.JenkinsCredentialType == v1alpha2.UsernamePasswordCredentialType) && len(seedJob.CredentialID) == 0 {
messages = append(messages, "credential ID can't be empty")
messages = append(messages, fmt.Sprintf("seedJob `%s` credential ID can't be empty", seedJob.ID))
}
// validate repository url match private key
if strings.Contains(seedJob.RepositoryURL, "git@") && seedJob.JenkinsCredentialType == v1alpha2.NoJenkinsCredentialCredentialType {
messages = append(messages, "Jenkins credential must be set while using ssh repository url")
messages = append(messages, fmt.Sprintf("seedJob `%s` Jenkins credential must be set while using ssh repository url", seedJob.ID))
}
if seedJob.JenkinsCredentialType == v1alpha2.BasicSSHCredentialType || seedJob.JenkinsCredentialType == v1alpha2.UsernamePasswordCredentialType {
@@ -64,38 +59,48 @@ func (s *SeedJobs) ValidateSeedJobs(jenkins v1alpha2.Jenkins) ([]string, error)
namespaceName := types.NamespacedName{Namespace: jenkins.Namespace, Name: seedJob.CredentialID}
err := s.k8sClient.Get(context.TODO(), namespaceName, secret)
if err != nil && apierrors.IsNotFound(err) {
messages = append(messages, fmt.Sprintf("required secret '%s' with Jenkins credential not found", seedJob.CredentialID))
messages = append(messages, fmt.Sprintf("seedJob `%s` required secret '%s' with Jenkins credential not found", seedJob.ID, seedJob.CredentialID))
} else if err != nil {
return nil, stackerr.WithStack(err)
}
if seedJob.JenkinsCredentialType == v1alpha2.BasicSSHCredentialType {
if msg := validateBasicSSHSecret(logger, *secret); msg != nil {
messages = append(messages, msg...)
if msg := validateBasicSSHSecret(*secret); len(msg) > 0 {
for _, m := range msg {
messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m))
}
}
}
if seedJob.JenkinsCredentialType == v1alpha2.UsernamePasswordCredentialType {
if msg := validateUsernamePasswordSecret(logger, *secret); msg != nil {
messages = append(messages, msg...)
if msg := validateUsernamePasswordSecret(*secret); len(msg) > 0 {
for _, m := range msg {
messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m))
}
}
}
}
if len(seedJob.BuildPeriodically) > 0 {
if msg := s.validateSchedule(seedJob, seedJob.BuildPeriodically, "buildPeriodically"); msg != nil {
messages = append(messages, msg...)
if msg := s.validateSchedule(seedJob, seedJob.BuildPeriodically, "buildPeriodically"); len(msg) > 0 {
for _, m := range msg {
messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m))
}
}
}
if len(seedJob.PollSCM) > 0 {
if msg := s.validateSchedule(seedJob, seedJob.PollSCM, "pollSCM"); msg != nil {
messages = append(messages, msg...)
if msg := s.validateSchedule(seedJob, seedJob.PollSCM, "pollSCM"); len(msg) > 0 {
for _, m := range msg {
messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m))
}
}
}
if seedJob.GitHubPushTrigger {
if msg := s.validateGitHubPushTrigger(jenkins); msg != nil {
messages = append(messages, msg...)
if msg := s.validateGitHubPushTrigger(jenkins); len(msg) > 0 {
for _, m := range msg {
messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m))
}
}
}
}
@@ -146,7 +151,7 @@ func (s *SeedJobs) validateIfIDIsUnique(seedJobs []v1alpha2.SeedJob) []string {
return messages
}
func validateBasicSSHSecret(logger logr.InfoLogger, secret v1.Secret) []string {
func validateBasicSSHSecret(secret v1.Secret) []string {
var messages []string
username, exists := secret.Data[UsernameSecretKey]
if !exists {
@@ -170,7 +175,7 @@ func validateBasicSSHSecret(logger logr.InfoLogger, secret v1.Secret) []string {
return messages
}
func validateUsernamePasswordSecret(logger logr.InfoLogger, secret v1.Secret) []string {
func validateUsernamePasswordSecret(secret v1.Secret) []string {
var messages []string
username, exists := secret.Data[UsernameSecretKey]
if !exists {
@@ -96,7 +96,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `` id can't be empty"})
})
t.Run("Valid with private key and secret", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -162,7 +163,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` private key 'privateKey' invalid in secret 'deploy-keys': failed to decode PEM block"})
})
t.Run("Invalid with PrivateKey and empty Secret data", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -195,7 +197,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` required data 'privateKey' not found in secret 'deploy-keys'", "seedJob `example` private key 'privateKey' invalid in secret 'deploy-keys': failed to decode PEM block"})
})
t.Run("Invalid with ssh RepositoryURL and empty PrivateKey", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -217,7 +220,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string([]string{"seedJob `example` required secret 'jenkins-operator-e2e' with Jenkins credential not found", "seedJob `example` required data 'username' not found in secret ''", "seedJob `example` required data 'username' is empty in secret ''", "seedJob `example` required data 'privateKey' not found in secret ''", "seedJob `example` required data 'privateKey' not found in secret ''", "seedJob `example` private key 'privateKey' invalid in secret '': failed to decode PEM block"}))
})
t.Run("Invalid without targets", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -237,7 +241,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` targets can't be empty"})
})
t.Run("Invalid without repository URL", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -257,7 +262,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` repository URL branch can't be empty"})
})
t.Run("Invalid without repository branch", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -277,7 +283,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` repository branch can't be empty"})
})
t.Run("Valid with username and password", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -343,7 +350,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` required data 'username' is empty in secret 'deploy-keys'"})
})
t.Run("Invalid with empty password", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -376,7 +384,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` required data 'password' is empty in secret 'deploy-keys'"})
})
t.Run("Invalid without username", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -408,7 +417,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` required data 'username' not found in secret 'deploy-keys'", "seedJob `example` required data 'username' is empty in secret 'deploy-keys'"})
})
t.Run("Invalid without password", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -440,7 +450,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` required data 'password' not found in secret 'deploy-keys'", "seedJob `example` required data 'password' is empty in secret 'deploy-keys'"})
})
t.Run("Invalid with wrong cron spec", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -463,7 +474,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` `buildPeriodically` schedule 'invalid-cron-spec' is invalid cron spec in `example`"})
})
t.Run("Valid with good cron spec", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -510,7 +522,8 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, result, []string{"seedJob `example` githubPushTrigger is set. This function requires `github` plugin installed in .Spec.Master.Plugins because seed jobs Push Trigger function needs it"})
})
t.Run("Invalid with set githubPushTrigger and not installed github plugin", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@@ -557,6 +570,7 @@ func TestValidateIfIDIsUnique(t *testing.T) {
}
ctrl := New(nil, nil, logf.ZapLogger(false))
got := ctrl.validateIfIDIsUnique(seedJobs)
assert.NotNil(t, got)
assert.Equal(t, got, []string{"'first' seed job ID is not unique"})
})
}