#69 Improve schedule tests
This commit is contained in:
		
							parent
							
								
									134c298358
								
							
						
					
					
						commit
						b0f1ee88cd
					
				|  | @ -530,24 +530,31 @@ type SeedJob struct { | ||||||
| 	JenkinsCredentialType JenkinsCredentialType `json:"credentialType,omitempty"` | 	JenkinsCredentialType JenkinsCredentialType `json:"credentialType,omitempty"` | ||||||
| 
 | 
 | ||||||
| 	// GitHubPushTrigger is used for GitHub web hooks
 | 	// GitHubPushTrigger is used for GitHub web hooks
 | ||||||
|  | 	// +optional
 | ||||||
| 	GitHubPushTrigger bool `json:"githubPushTrigger,omitempty"` | 	GitHubPushTrigger bool `json:"githubPushTrigger,omitempty"` | ||||||
| 
 | 
 | ||||||
| 	// BuildPeriodically is setting build trigger
 | 	// BuildPeriodically is setting build trigger
 | ||||||
|  | 	// +optional
 | ||||||
| 	BuildPeriodically string `json:"buildPeriodically,omitempty"` | 	BuildPeriodically string `json:"buildPeriodically,omitempty"` | ||||||
| 
 | 
 | ||||||
| 	// PollSCM is setting build trigger
 | 	// PollSCM is setting build trigger
 | ||||||
|  | 	// +optional
 | ||||||
| 	PollSCM string `json:"pollSCM,omitempty"` | 	PollSCM string `json:"pollSCM,omitempty"` | ||||||
| 
 | 
 | ||||||
| 	// IgnoreMissingFiles is setting for Job DSL API plugin
 | 	// IgnoreMissingFiles is setting for Job DSL API plugin
 | ||||||
|  | 	// +optional
 | ||||||
| 	IgnoreMissingFiles bool `json:"ignoreMissingFiles,omitempty"` | 	IgnoreMissingFiles bool `json:"ignoreMissingFiles,omitempty"` | ||||||
| 
 | 
 | ||||||
| 	// AdditionalClasspath is setting for Job DSL API plugin
 | 	// AdditionalClasspath is setting for Job DSL API plugin
 | ||||||
|  | 	// +optional
 | ||||||
| 	AdditionalClasspath string `json:"additionalClasspath,omitempty"` | 	AdditionalClasspath string `json:"additionalClasspath,omitempty"` | ||||||
| 
 | 
 | ||||||
| 	// FailOnMissingPlugin is setting for Job DSL API plugin
 | 	// FailOnMissingPlugin is setting for Job DSL API plugin
 | ||||||
|  | 	// +optional
 | ||||||
| 	FailOnMissingPlugin bool `json:"failOnMissingPlugin,omitempty"` | 	FailOnMissingPlugin bool `json:"failOnMissingPlugin,omitempty"` | ||||||
| 
 | 
 | ||||||
| 	// UnstableOnDeprecation is setting for Job DSL API plugin
 | 	// UnstableOnDeprecation is setting for Job DSL API plugin
 | ||||||
|  | 	// +optional
 | ||||||
| 	UnstableOnDeprecation bool `json:"unstableOnDeprecation,omitempty"` | 	UnstableOnDeprecation bool `json:"unstableOnDeprecation,omitempty"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -2,11 +2,11 @@ package resources | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/internal/render" |  | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants" |  | ||||||
| 	"text/template" | 	"text/template" | ||||||
| 
 | 
 | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" | 	"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" | ||||||
|  | 	"github.com/jenkinsci/kubernetes-operator/internal/render" | ||||||
|  | 	"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants" | ||||||
| 
 | 
 | ||||||
| 	corev1 "k8s.io/api/core/v1" | 	corev1 "k8s.io/api/core/v1" | ||||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
|  |  | ||||||
|  | @ -2,11 +2,11 @@ package resources | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/internal/render" |  | ||||||
| 	"text/template" | 	"text/template" | ||||||
| 
 | 
 | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" | 	"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants" | 	"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants" | ||||||
|  | 	"github.com/jenkinsci/kubernetes-operator/internal/render" | ||||||
| 
 | 
 | ||||||
| 	corev1 "k8s.io/api/core/v1" | 	corev1 "k8s.io/api/core/v1" | ||||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
|  |  | ||||||
|  | @ -45,7 +45,7 @@ const ( | ||||||
| 	creatingGroovyScriptName = "seed-job-groovy-script.groovy" | 	creatingGroovyScriptName = "seed-job-groovy-script.groovy" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var seedJobGroovyScript = template.Must(template.New(creatingGroovyScriptName).Parse(` | var seedJobGroovyScriptTemplate = template.Must(template.New(creatingGroovyScriptName).Parse(` | ||||||
| import hudson.model.FreeStyleProject; | import hudson.model.FreeStyleProject; | ||||||
| import hudson.plugins.git.GitSCM; | import hudson.plugins.git.GitSCM; | ||||||
| import hudson.plugins.git.BranchSpec; | import hudson.plugins.git.BranchSpec; | ||||||
|  | @ -79,14 +79,14 @@ import static com.google.common.collect.Lists.newArrayList; | ||||||
| 
 | 
 | ||||||
| Jenkins jenkins = Jenkins.instance | Jenkins jenkins = Jenkins.instance | ||||||
| 
 | 
 | ||||||
| def jobDslSeedName = " {{ .ID }} - {{ .SeedJobSuffix }}"; | def jobDslSeedName = "{{ .ID }} - {{ .SeedJobSuffix }}"; | ||||||
| def jobRef = jenkins.getItem(jobDslSeedName) | def jobRef = jenkins.getItem(jobDslSeedName) | ||||||
| 
 | 
 | ||||||
| def repoList = GitSCM.createRepoList(" {{ .RepositoryURL }} ", " {{ .CredentialID }} ") | def repoList = GitSCM.createRepoList("{{ .RepositoryURL }}", "{{ .CredentialID }}") | ||||||
| def gitExtensions = [new CloneOption(true, true, ";", 10)] | def gitExtensions = [new CloneOption(true, true, ";", 10)] | ||||||
| def scm = new GitSCM( | def scm = new GitSCM( | ||||||
|         repoList, |         repoList, | ||||||
|         newArrayList(new BranchSpec(" {{ .RepositoryBranch }} ")), |         newArrayList(new BranchSpec("{{ .RepositoryBranch }}")), | ||||||
|         false, |         false, | ||||||
|         Collections.<SubmoduleConfig>emptyList(), |         Collections.<SubmoduleConfig>emptyList(), | ||||||
|         null, |         null, | ||||||
|  | @ -95,15 +95,15 @@ def scm = new GitSCM( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| def executeDslScripts = new ExecuteDslScripts() | def executeDslScripts = new ExecuteDslScripts() | ||||||
| executeDslScripts.setTargets(" {{ .Targets }} ") | executeDslScripts.setTargets("{{ .Targets }}") | ||||||
| executeDslScripts.setSandbox(false) | executeDslScripts.setSandbox(false) | ||||||
| executeDslScripts.setRemovedJobAction(RemovedJobAction.DELETE) | executeDslScripts.setRemovedJobAction(RemovedJobAction.DELETE) | ||||||
| executeDslScripts.setRemovedViewAction(RemovedViewAction.DELETE) | executeDslScripts.setRemovedViewAction(RemovedViewAction.DELETE) | ||||||
| executeDslScripts.setLookupStrategy(LookupStrategy.SEED_JOB) | executeDslScripts.setLookupStrategy(LookupStrategy.SEED_JOB) | ||||||
| executeDslScripts.setAdditionalClasspath(" {{ .AdditionalClasspath }} ") | executeDslScripts.setAdditionalClasspath("{{ .AdditionalClasspath }}") | ||||||
| executeDslScripts.setFailOnMissingPlugin( {{ .FailOnMissingPlugin }} ) | executeDslScripts.setFailOnMissingPlugin({{ .FailOnMissingPlugin }}) | ||||||
| executeDslScripts.setUnstableOnDeprecation( {{ .UnstableOnDeprecation }} ) | executeDslScripts.setUnstableOnDeprecation({{ .UnstableOnDeprecation }}) | ||||||
| executeDslScripts.setIgnoreMissingFiles( {{ .IgnoreMissingFiles }} ) | executeDslScripts.setIgnoreMissingFiles({{ .IgnoreMissingFiles }}) | ||||||
| 
 | 
 | ||||||
| if (jobRef == null) { | if (jobRef == null) { | ||||||
|         jobRef = jenkins.createProject(FreeStyleProject, jobDslSeedName) |         jobRef = jenkins.createProject(FreeStyleProject, jobDslSeedName) | ||||||
|  | @ -114,16 +114,16 @@ jobRef.getBuildersList().add(executeDslScripts) | ||||||
| jobRef.setDisplayName("Seed Job from {{ .ID }}") | jobRef.setDisplayName("Seed Job from {{ .ID }}") | ||||||
| jobRef.setScm(scm) | jobRef.setScm(scm) | ||||||
| 
 | 
 | ||||||
| {{ $length := len .PollSCM }} {{ if gt $length 0 }} | {{ if .PollSCM }} | ||||||
| jobRef.addTrigger(new SCMTrigger(" {{ .PollSCM }} ")) | jobRef.addTrigger(new SCMTrigger("{{ .PollSCM }}")) | ||||||
| {{ end }} | {{ end }} | ||||||
| 
 | 
 | ||||||
| {{ if .GitHubPushTrigger }} | {{ if .GitHubPushTrigger }} | ||||||
| jobRef.addTrigger(new GitHubPushTrigger()) | jobRef.addTrigger(new GitHubPushTrigger()) | ||||||
| {{ end }} | {{ end }} | ||||||
| 
 | 
 | ||||||
| {{ $length := len .BuildPeriodically }} {{ if gt $length 0 }} | {{ if .BuildPeriodically }} | ||||||
| jobRef.addTrigger(new TimerTrigger(" {{ .BuildPeriodically }} ")) | jobRef.addTrigger(new TimerTrigger("{{ .BuildPeriodically }}")) | ||||||
| {{ end}} | {{ end}} | ||||||
| jobRef.setAssignedLabel(new LabelAtom("{{ .AgentName }}")) | jobRef.setAssignedLabel(new LabelAtom("{{ .AgentName }}")) | ||||||
| jenkins.getQueue().schedule(jobRef) | jenkins.getQueue().schedule(jobRef) | ||||||
|  | @ -440,7 +440,7 @@ func seedJobCreatingGroovyScript(seedJob v1alpha2.SeedJob) (string, error) { | ||||||
| 		AgentName:             AgentName, | 		AgentName:             AgentName, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	output, err := render.Render(seedJobGroovyScript, data) | 	output, err := render.Render(seedJobGroovyScriptTemplate, data) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -5,7 +5,6 @@ import ( | ||||||
| 	"crypto/x509" | 	"crypto/x509" | ||||||
| 	"encoding/pem" | 	"encoding/pem" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"github.com/robfig/cron" |  | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" | 	"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" | ||||||
|  | @ -13,6 +12,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/go-logr/logr" | 	"github.com/go-logr/logr" | ||||||
| 	stackerr "github.com/pkg/errors" | 	stackerr "github.com/pkg/errors" | ||||||
|  | 	"github.com/robfig/cron" | ||||||
| 	"k8s.io/api/core/v1" | 	"k8s.io/api/core/v1" | ||||||
| 	apierrors "k8s.io/apimachinery/pkg/api/errors" | 	apierrors "k8s.io/apimachinery/pkg/api/errors" | ||||||
| 	"k8s.io/apimachinery/pkg/types" | 	"k8s.io/apimachinery/pkg/types" | ||||||
|  | @ -90,13 +90,13 @@ func (r *SeedJobs) ValidateSeedJobs(jenkins v1alpha2.Jenkins) (bool, error) { | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if len(seedJob.BuildPeriodically) > 0 { | 		if len(seedJob.BuildPeriodically) > 0 { | ||||||
| 			if ok := r.validateSchedule(seedJob, seedJob.BuildPeriodically, "buildPeriodically"); !ok { | 			if !r.validateSchedule(seedJob, seedJob.BuildPeriodically, "buildPeriodically") { | ||||||
| 				valid = false | 				valid = false | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if len(seedJob.PollSCM) > 0 { | 		if len(seedJob.PollSCM) > 0 { | ||||||
| 			if ok := r.validateSchedule(seedJob, seedJob.PollSCM, "pollSCM"); !ok { | 			if !r.validateSchedule(seedJob, seedJob.PollSCM, "pollSCM") { | ||||||
| 				valid = false | 				valid = false | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  | @ -136,7 +136,7 @@ func (r *SeedJobs) validateGitHubPushTrigger(jenkins v1alpha2.Jenkins) bool { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if !exists && !userExists { | 	if !exists && !userExists { | ||||||
| 		r.logger.V(log.VWarn).Info("githubPushTrigger is set. This function requires `github` plugin installed in jenkins.Spec.Master.Plugins") | 		r.logger.V(log.VWarn).Info("githubPushTrigger is set. This function requires `github` plugin installed in .Spec.Master.Plugins because seed jobs Push Trigger function needs it") | ||||||
| 		return false | 		return false | ||||||
| 	} | 	} | ||||||
| 	return true | 	return true | ||||||
|  |  | ||||||
|  | @ -463,7 +463,7 @@ func TestValidateSeedJobs(t *testing.T) { | ||||||
| 		result, err := seedJobs.ValidateSeedJobs(jenkins) | 		result, err := seedJobs.ValidateSeedJobs(jenkins) | ||||||
| 
 | 
 | ||||||
| 		assert.NoError(t, err) | 		assert.NoError(t, err) | ||||||
| 		assert.Equal(t, false, result) | 		assert.False(t, result) | ||||||
| 	}) | 	}) | ||||||
| 	t.Run("Valid with good cron spec", func(t *testing.T) { | 	t.Run("Valid with good cron spec", func(t *testing.T) { | ||||||
| 		jenkins := v1alpha2.Jenkins{ | 		jenkins := v1alpha2.Jenkins{ | ||||||
|  | @ -487,7 +487,7 @@ func TestValidateSeedJobs(t *testing.T) { | ||||||
| 		result, err := seedJobs.ValidateSeedJobs(jenkins) | 		result, err := seedJobs.ValidateSeedJobs(jenkins) | ||||||
| 
 | 
 | ||||||
| 		assert.NoError(t, err) | 		assert.NoError(t, err) | ||||||
| 		assert.Equal(t, true, result) | 		assert.True(t, result) | ||||||
| 	}) | 	}) | ||||||
| 	t.Run("Invalid with set githubPushTrigger and not installed github plugin", func(t *testing.T) { | 	t.Run("Invalid with set githubPushTrigger and not installed github plugin", func(t *testing.T) { | ||||||
| 		jenkins := v1alpha2.Jenkins{ | 		jenkins := v1alpha2.Jenkins{ | ||||||
|  | @ -510,7 +510,7 @@ func TestValidateSeedJobs(t *testing.T) { | ||||||
| 		result, err := seedJobs.ValidateSeedJobs(jenkins) | 		result, err := seedJobs.ValidateSeedJobs(jenkins) | ||||||
| 
 | 
 | ||||||
| 		assert.NoError(t, err) | 		assert.NoError(t, err) | ||||||
| 		assert.Equal(t, false, result) | 		assert.False(t, result) | ||||||
| 	}) | 	}) | ||||||
| 	t.Run("Invalid with set githubPushTrigger and not installed github plugin", func(t *testing.T) { | 	t.Run("Invalid with set githubPushTrigger and not installed github plugin", func(t *testing.T) { | ||||||
| 		jenkins := v1alpha2.Jenkins{ | 		jenkins := v1alpha2.Jenkins{ | ||||||
|  | @ -538,7 +538,7 @@ func TestValidateSeedJobs(t *testing.T) { | ||||||
| 		result, err := seedJobs.ValidateSeedJobs(jenkins) | 		result, err := seedJobs.ValidateSeedJobs(jenkins) | ||||||
| 
 | 
 | ||||||
| 		assert.NoError(t, err) | 		assert.NoError(t, err) | ||||||
| 		assert.Equal(t, true, result) | 		assert.True(t, result) | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue