#69 Improve schedule tests

This commit is contained in:
Jakub Al-Khalili 2019-09-02 14:10:59 +02:00
parent 134c298358
commit b0f1ee88cd
6 changed files with 32 additions and 25 deletions

View File

@ -530,24 +530,31 @@ type SeedJob struct {
JenkinsCredentialType JenkinsCredentialType `json:"credentialType,omitempty"`
// GitHubPushTrigger is used for GitHub web hooks
// +optional
GitHubPushTrigger bool `json:"githubPushTrigger,omitempty"`
// BuildPeriodically is setting build trigger
// +optional
BuildPeriodically string `json:"buildPeriodically,omitempty"`
// PollSCM is setting build trigger
// +optional
PollSCM string `json:"pollSCM,omitempty"`
// IgnoreMissingFiles is setting for Job DSL API plugin
// +optional
IgnoreMissingFiles bool `json:"ignoreMissingFiles,omitempty"`
// AdditionalClasspath is setting for Job DSL API plugin
// +optional
AdditionalClasspath string `json:"additionalClasspath,omitempty"`
// FailOnMissingPlugin is setting for Job DSL API plugin
// +optional
FailOnMissingPlugin bool `json:"failOnMissingPlugin,omitempty"`
// UnstableOnDeprecation is setting for Job DSL API plugin
// +optional
UnstableOnDeprecation bool `json:"unstableOnDeprecation,omitempty"`
}

View File

@ -2,11 +2,11 @@ package resources
import (
"fmt"
"github.com/jenkinsci/kubernetes-operator/internal/render"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants"
"text/template"
"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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

View File

@ -2,11 +2,11 @@ package resources
import (
"fmt"
"github.com/jenkinsci/kubernetes-operator/internal/render"
"text/template"
"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants"
"github.com/jenkinsci/kubernetes-operator/internal/render"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

View File

@ -45,7 +45,7 @@ const (
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.plugins.git.GitSCM;
import hudson.plugins.git.BranchSpec;
@ -79,14 +79,14 @@ import static com.google.common.collect.Lists.newArrayList;
Jenkins jenkins = Jenkins.instance
def jobDslSeedName = " {{ .ID }} - {{ .SeedJobSuffix }}";
def jobDslSeedName = "{{ .ID }} - {{ .SeedJobSuffix }}";
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 scm = new GitSCM(
repoList,
newArrayList(new BranchSpec(" {{ .RepositoryBranch }} ")),
newArrayList(new BranchSpec("{{ .RepositoryBranch }}")),
false,
Collections.<SubmoduleConfig>emptyList(),
null,
@ -95,15 +95,15 @@ def scm = new GitSCM(
)
def executeDslScripts = new ExecuteDslScripts()
executeDslScripts.setTargets(" {{ .Targets }} ")
executeDslScripts.setTargets("{{ .Targets }}")
executeDslScripts.setSandbox(false)
executeDslScripts.setRemovedJobAction(RemovedJobAction.DELETE)
executeDslScripts.setRemovedViewAction(RemovedViewAction.DELETE)
executeDslScripts.setLookupStrategy(LookupStrategy.SEED_JOB)
executeDslScripts.setAdditionalClasspath(" {{ .AdditionalClasspath }} ")
executeDslScripts.setFailOnMissingPlugin( {{ .FailOnMissingPlugin }} )
executeDslScripts.setUnstableOnDeprecation( {{ .UnstableOnDeprecation }} )
executeDslScripts.setIgnoreMissingFiles( {{ .IgnoreMissingFiles }} )
executeDslScripts.setAdditionalClasspath("{{ .AdditionalClasspath }}")
executeDslScripts.setFailOnMissingPlugin({{ .FailOnMissingPlugin }})
executeDslScripts.setUnstableOnDeprecation({{ .UnstableOnDeprecation }})
executeDslScripts.setIgnoreMissingFiles({{ .IgnoreMissingFiles }})
if (jobRef == null) {
jobRef = jenkins.createProject(FreeStyleProject, jobDslSeedName)
@ -114,16 +114,16 @@ jobRef.getBuildersList().add(executeDslScripts)
jobRef.setDisplayName("Seed Job from {{ .ID }}")
jobRef.setScm(scm)
{{ $length := len .PollSCM }} {{ if gt $length 0 }}
jobRef.addTrigger(new SCMTrigger(" {{ .PollSCM }} "))
{{ if .PollSCM }}
jobRef.addTrigger(new SCMTrigger("{{ .PollSCM }}"))
{{ end }}
{{ if .GitHubPushTrigger }}
jobRef.addTrigger(new GitHubPushTrigger())
{{ end }}
{{ $length := len .BuildPeriodically }} {{ if gt $length 0 }}
jobRef.addTrigger(new TimerTrigger(" {{ .BuildPeriodically }} "))
{{ if .BuildPeriodically }}
jobRef.addTrigger(new TimerTrigger("{{ .BuildPeriodically }}"))
{{ end}}
jobRef.setAssignedLabel(new LabelAtom("{{ .AgentName }}"))
jenkins.getQueue().schedule(jobRef)
@ -440,7 +440,7 @@ func seedJobCreatingGroovyScript(seedJob v1alpha2.SeedJob) (string, error) {
AgentName: AgentName,
}
output, err := render.Render(seedJobGroovyScript, data)
output, err := render.Render(seedJobGroovyScriptTemplate, data)
if err != nil {
return "", err
}

View File

@ -5,7 +5,6 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/robfig/cron"
"strings"
"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
@ -13,6 +12,7 @@ import (
"github.com/go-logr/logr"
stackerr "github.com/pkg/errors"
"github.com/robfig/cron"
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
@ -90,13 +90,13 @@ func (r *SeedJobs) ValidateSeedJobs(jenkins v1alpha2.Jenkins) (bool, error) {
}
if len(seedJob.BuildPeriodically) > 0 {
if ok := r.validateSchedule(seedJob, seedJob.BuildPeriodically, "buildPeriodically"); !ok {
if !r.validateSchedule(seedJob, seedJob.BuildPeriodically, "buildPeriodically") {
valid = false
}
}
if len(seedJob.PollSCM) > 0 {
if ok := r.validateSchedule(seedJob, seedJob.PollSCM, "pollSCM"); !ok {
if !r.validateSchedule(seedJob, seedJob.PollSCM, "pollSCM") {
valid = false
}
}
@ -136,7 +136,7 @@ func (r *SeedJobs) validateGitHubPushTrigger(jenkins v1alpha2.Jenkins) bool {
}
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 true

View File

@ -463,7 +463,7 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.Equal(t, false, result)
assert.False(t, result)
})
t.Run("Valid with good cron spec", func(t *testing.T) {
jenkins := v1alpha2.Jenkins{
@ -487,7 +487,7 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
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) {
jenkins := v1alpha2.Jenkins{
@ -510,7 +510,7 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
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) {
jenkins := v1alpha2.Jenkins{
@ -538,7 +538,7 @@ func TestValidateSeedJobs(t *testing.T) {
result, err := seedJobs.ValidateSeedJobs(jenkins)
assert.NoError(t, err)
assert.Equal(t, true, result)
assert.True(t, result)
})
}