mirror of
https://github.com/jenkinsci/kubernetes-operator.git
synced 2026-10-17 21:35:38 +02:00
#69 Add e2e test
This commit is contained in:
@@ -40,6 +40,13 @@ func TestConfiguration(t *testing.T) {
|
||||
Description: "Jenkins Operator repository",
|
||||
RepositoryBranch: "master",
|
||||
RepositoryURL: "https://github.com/jenkinsci/kubernetes-operator.git",
|
||||
PollSCM: "1 1 1 1 1",
|
||||
UnstableOnDeprecation: true,
|
||||
BuildPeriodically: "1 1 1 1 1",
|
||||
FailOnMissingPlugin: true,
|
||||
IgnoreMissingFiles: true,
|
||||
//AdditionalClasspath: can fail with the seed job agent
|
||||
GitHubPushTrigger: true,
|
||||
},
|
||||
}
|
||||
groovyScripts := v1alpha2.GroovyScripts{
|
||||
|
||||
@@ -124,6 +124,7 @@ func createJenkinsCR(t *testing.T, name, namespace string, seedJob *[]v1alpha2.S
|
||||
Plugins: []v1alpha2.Plugin{
|
||||
{Name: "audit-trail", Version: "2.4"},
|
||||
{Name: "simple-theme-plugin", Version: "0.5.1"},
|
||||
{Name: "github", Version: "1.29.4"},
|
||||
},
|
||||
NodeSelector: map[string]string{"kubernetes.io/hostname": "minikube"},
|
||||
},
|
||||
|
||||
@@ -7,12 +7,15 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/jenkinsci/kubernetes-operator/internal/render"
|
||||
"github.com/jenkinsci/kubernetes-operator/internal/try"
|
||||
"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
|
||||
jenkinsclient "github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/client"
|
||||
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/configuration/user/seedjobs"
|
||||
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants"
|
||||
|
||||
framework "github.com/operator-framework/operator-sdk/pkg/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -113,6 +116,8 @@ func verifyJenkinsSeedJobs(t *testing.T, jenkinsClient jenkinsclient.Jenkins, se
|
||||
assert.NoErrorf(t, err, "Jenkins credential '%s' not created for seed job ID '%s'", seedJob.CredentialID, seedJob.ID)
|
||||
}
|
||||
|
||||
verifySeedJobProperties(t, jenkinsClient, seedJob)
|
||||
|
||||
for _, requireJobName := range seedJob.JobNames {
|
||||
err = try.Until(func() (end bool, err error) {
|
||||
_, err = jenkinsClient.GetJob(requireJobName)
|
||||
@@ -123,6 +128,46 @@ func verifyJenkinsSeedJobs(t *testing.T, jenkinsClient jenkinsclient.Jenkins, se
|
||||
}
|
||||
}
|
||||
|
||||
func verifySeedJobProperties(t *testing.T, jenkinsClient jenkinsclient.Jenkins, seedJob seedJobConfig) {
|
||||
data := struct {
|
||||
ID string
|
||||
CredentialID string
|
||||
Targets string
|
||||
RepositoryBranch string
|
||||
RepositoryURL string
|
||||
GitHubPushTrigger bool
|
||||
BuildPeriodically string
|
||||
PollSCM string
|
||||
IgnoreMissingFiles bool
|
||||
AdditionalClasspath string
|
||||
FailOnMissingPlugin bool
|
||||
UnstableOnDeprecation bool
|
||||
SeedJobSuffix string
|
||||
AgentName string
|
||||
}{
|
||||
ID: seedJob.ID,
|
||||
CredentialID: seedJob.CredentialID,
|
||||
Targets: seedJob.Targets,
|
||||
RepositoryBranch: seedJob.RepositoryBranch,
|
||||
RepositoryURL: seedJob.RepositoryURL,
|
||||
GitHubPushTrigger: seedJob.GitHubPushTrigger,
|
||||
BuildPeriodically: seedJob.BuildPeriodically,
|
||||
PollSCM: seedJob.PollSCM,
|
||||
IgnoreMissingFiles: seedJob.IgnoreMissingFiles,
|
||||
AdditionalClasspath: seedJob.AdditionalClasspath,
|
||||
FailOnMissingPlugin: seedJob.FailOnMissingPlugin,
|
||||
UnstableOnDeprecation: seedJob.UnstableOnDeprecation,
|
||||
SeedJobSuffix: constants.SeedJobSuffix,
|
||||
AgentName: seedjobs.AgentName,
|
||||
}
|
||||
|
||||
groovyScript, err := render.Render(verifySeedJobPropertiesGroovyScriptTemplate, data)
|
||||
assert.NoError(t, err, groovyScript)
|
||||
|
||||
logs, err := jenkinsClient.ExecuteScript(groovyScript)
|
||||
assert.NoError(t, err, logs, groovyScript)
|
||||
}
|
||||
|
||||
func verifyIfJenkinsCredentialExists(jenkinsClient jenkinsclient.Jenkins, credentialName string) error {
|
||||
groovyScriptFmt := `import com.cloudbees.plugins.credentials.Credentials
|
||||
|
||||
@@ -151,3 +196,100 @@ if(!found) {
|
||||
_, err := jenkinsClient.ExecuteScript(groovyScript)
|
||||
return err
|
||||
}
|
||||
|
||||
var verifySeedJobPropertiesGroovyScriptTemplate = template.Must(template.New("test-e2e-verify-job-properties").Parse(`
|
||||
import hudson.model.FreeStyleProject;
|
||||
import hudson.plugins.git.GitSCM;
|
||||
import hudson.plugins.git.BranchSpec;
|
||||
import hudson.triggers.SCMTrigger;
|
||||
import hudson.triggers.TimerTrigger;
|
||||
import hudson.util.Secret;
|
||||
import javaposse.jobdsl.plugin.*;
|
||||
import jenkins.model.Jenkins;
|
||||
import jenkins.model.JenkinsLocationConfiguration;
|
||||
import com.cloudbees.plugins.credentials.CredentialsScope;
|
||||
import com.cloudbees.plugins.credentials.domains.Domain;
|
||||
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
|
||||
import jenkins.model.JenkinsLocationConfiguration;
|
||||
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
|
||||
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
|
||||
{{ if .GitHubPushTrigger }}
|
||||
import com.cloudbees.jenkins.GitHubPushTrigger;
|
||||
{{ end }}
|
||||
import hudson.model.FreeStyleProject;
|
||||
import hudson.model.labels.LabelAtom;
|
||||
import hudson.plugins.git.BranchSpec;
|
||||
import hudson.plugins.git.GitSCM;
|
||||
import hudson.plugins.git.SubmoduleConfig;
|
||||
import hudson.plugins.git.extensions.impl.CloneOption;
|
||||
import javaposse.jobdsl.plugin.ExecuteDslScripts;
|
||||
import javaposse.jobdsl.plugin.LookupStrategy;
|
||||
import javaposse.jobdsl.plugin.RemovedJobAction;
|
||||
import javaposse.jobdsl.plugin.RemovedViewAction;
|
||||
import hudson.tasks.BuildStep;
|
||||
|
||||
Jenkins jenkins = Jenkins.instance
|
||||
|
||||
def jobDslSeedName = "{{ .ID }}-{{ .SeedJobSuffix }}"
|
||||
def jobRef = jenkins.getItem(jobDslSeedName)
|
||||
|
||||
if (jobRef == null) {
|
||||
throw new Exception("Job with given name not found")
|
||||
}
|
||||
|
||||
if (!jobRef.getDisplayName().equals("Seed Job from {{ .ID }}")) {
|
||||
throw new Exception("Display name is not equal")
|
||||
}
|
||||
|
||||
if (jobRef.getScm() == null) {
|
||||
throw new Exception("No SCM found")
|
||||
}
|
||||
|
||||
if (jobRef.getScm().getBranches().find { val -> val.getName() == "{{ .RepositoryBranch }}" } == null) {
|
||||
throw new Exception("Specified SCM branch not found")
|
||||
}
|
||||
|
||||
if(jobRef.getScm().getRepositories().find { it.getURIs().find { uri -> uri.toString().equals("https://github.com/jenkinsci/kubernetes-operator.git") } } == null) {
|
||||
throw new Exception("Specified SCM repositories are invalid")
|
||||
}
|
||||
|
||||
{{ if .PollSCM }}
|
||||
if (jobRef.getTriggers().find { key, val -> val.getClass().getSimpleName() == "SCMTrigger" && val.getSpec() == "{{ .PollSCM }}" } == null) {
|
||||
throw new Exception("SCMTrigger not found but set")
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{ if .GitHubPushTrigger }}
|
||||
if (jobRef.getTriggers().find { key, val -> val.getClass().getSimpleName() == "GitHubPushTrigger" } == null) {
|
||||
throw new Exception("GitHubPushTrigger not found but set")
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{ if .BuildPeriodically }}
|
||||
if (jobRef.getTriggers().find { key, val -> val.getClass().getSimpleName() == "TimerTrigger" && val.getSpec() == "{{ .BuildPeriodically }}" } == null) {
|
||||
throw new Exception("BuildPeriodically not found but set")
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
for (BuildStep step : jobRef.getBuildersList()) {
|
||||
if (!step.getTargets().equals("{{ .Targets }}")) {
|
||||
throw new Exception("Targets are not equals'")
|
||||
}
|
||||
|
||||
if (!step.getAdditionalClasspath().equals(null)) {
|
||||
throw new Exception("AdditionalClasspath is not equal")
|
||||
}
|
||||
|
||||
if (!step.isFailOnMissingPlugin().equals({{ .FailOnMissingPlugin }})) {
|
||||
throw new Exception("FailOnMissingPlugin is not equal")
|
||||
}
|
||||
|
||||
if (!step.isUnstableOnDeprecation().equals({{ .UnstableOnDeprecation }})) {
|
||||
throw new Exception("UnstableOnDeprecation is not equal")
|
||||
}
|
||||
|
||||
if (!step.isIgnoreMissingFiles().equals({{ .IgnoreMissingFiles }})) {
|
||||
throw new Exception("IgnoreMissingFiles is not equal")
|
||||
}
|
||||
}
|
||||
`))
|
||||
|
||||
Reference in New Issue
Block a user