Skip to content

Commit

Permalink
#28 Fix e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszsek committed Sep 3, 2019
1 parent 1c147e1 commit 1e03f63
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cicd/jobs/e2e.jenkins
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env groovy

pipelineJob('e2e-jenkins-operator') {
displayName('Dummy job to e2e testing')
displayName('Dummy job for e2e testing')

logRotator {
numToKeep(10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import jenkins.model.JenkinsLocationConfiguration
import hudson.model.Node.Mode
def jenkins = Jenkins.instance
//Number of jobs that run simultaneously on master, currently only SeedJob.
//Number of jobs that run simultaneously on master.
jenkins.setNumExecutors(%d)
//Jobs must specify that they want to run on master
jenkins.setMode(Mode.EXCLUSIVE)
jenkins.save()
`

const enableCSRF = `
Expand Down
4 changes: 0 additions & 4 deletions pkg/controller/jenkins/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ const (
SeedJobSuffix = "job-dsl-seed"
// DefaultJenkinsMasterImage is the default Jenkins master docker image
DefaultJenkinsMasterImage = "jenkins/jenkins:lts"
// UserConfigurationJobName is the Jenkins job name used to configure Jenkins by groovy scripts provided by user
UserConfigurationJobName = OperatorName + "-user-configuration"
// UserConfigurationCASCJobName is the Jenkins job name used to configure Jenkins by Configuration as code yaml configs provided by user
UserConfigurationCASCJobName = OperatorName + "-user-configuration-casc"
// DefaultHTTPPortInt32 is the default Jenkins HTTP port
DefaultHTTPPortInt32 = int32(8080)
// DefaultSlavePortInt32 is the default Jenkins port for slaves
Expand Down
1 change: 1 addition & 0 deletions test/e2e/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestJenkinsMasterPodRestart(t *testing.T) {
waitForJenkinsBaseConfigurationToComplete(t, jenkins)
}

// FIXME java.lang.Exception: AuthorizationStrategy.Unsecured is not set
func TestSafeRestart(t *testing.T) {
t.Parallel()
namespace, ctx := setupTest(t)
Expand Down
50 changes: 37 additions & 13 deletions test/e2e/restorebackup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package e2e
import (
"context"
"testing"
"time"

"github.com/jenkinsci/kubernetes-operator/internal/try"
"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/client"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/configuration/base/resources"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants"

framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -25,28 +25,41 @@ func TestBackupAndRestore(t *testing.T) {
// Deletes test namespace
defer ctx.Cleanup()

jobID := "e2e-jenkins-operator"
createPVC(t, namespace)
jenkins := createJenkinsWithBackupAndRestoreConfigured(t, "e2e", namespace)
waitForJenkinsUserConfigurationToComplete(t, jenkins)

jenkinsClient := verifyJenkinsAPIConnection(t, jenkins)
waitForJob(t, jenkinsClient, jobID)
job, err := jenkinsClient.GetJob(jobID)
require.NoError(t, err, job)
i, err := job.InvokeSimple(map[string]string{})
require.NoError(t, err, i)
time.Sleep(60 * time.Second) // wait for the build to complete

restartJenkinsMasterPod(t, jenkins)
waitForRecreateJenkinsMasterPod(t, jenkins)
waitForJenkinsUserConfigurationToComplete(t, jenkins)
jenkinsClient := verifyJenkinsAPIConnection(t, jenkins)
verifyJobBuildsAfterRestoreBackup(t, jenkinsClient)
jenkinsClient = verifyJenkinsAPIConnection(t, jenkins)
waitForJob(t, jenkinsClient, jobID)
verifyJobBuildsAfterRestoreBackup(t, jenkinsClient, jobID)
}

func verifyJobBuildsAfterRestoreBackup(t *testing.T, jenkins client.Jenkins) {
job, err := jenkins.GetJob(constants.UserConfigurationJobName)
require.NoError(t, err)
build, err := job.GetLastBuild()
require.NoError(t, err)
assert.Equal(t, int64(2), build.GetBuildNumber())
func waitForJob(t *testing.T, jenkinsClient client.Jenkins, jobID string) {
err := try.Until(func() (end bool, err error) {
_, err = jenkinsClient.GetJob(jobID)
return err == nil, err
}, time.Second*2, time.Minute*2)
require.NoErrorf(t, err, "Jenkins job '%s' not created by seed job", jobID)
}

job, err = jenkins.GetJob(constants.UserConfigurationCASCJobName)
func verifyJobBuildsAfterRestoreBackup(t *testing.T, jenkinsClient client.Jenkins, jobID string) {
job, err := jenkinsClient.GetJob(jobID)
require.NoError(t, err)
build, err = job.GetLastBuild()
build, err := job.GetLastBuild()
require.NoError(t, err)
assert.Equal(t, int64(2), build.GetBuildNumber())
assert.Equal(t, int64(1), build.GetBuildNumber())
}

func createPVC(t *testing.T, namespace string) {
Expand Down Expand Up @@ -136,6 +149,17 @@ func createJenkinsWithBackupAndRestoreConfigured(t *testing.T, name, namespace s
},
},
},
SeedJobs: []v1alpha2.SeedJob{
{
ID: "jenkins-operator",
CredentialID: "jenkins-operator",
JenkinsCredentialType: v1alpha2.NoJenkinsCredentialCredentialType,
Targets: "cicd/jobs/*.jenkins",
Description: "Jenkins Operator repository",
RepositoryBranch: "master",
RepositoryURL: "https://github.com/jenkinsci/kubernetes-operator.git",
},
},
},
}

Expand Down

0 comments on commit 1e03f63

Please sign in to comment.