|
| 1 | +package com.cloudbees.jenkins.plugins; |
| 2 | + |
| 3 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 4 | +import hudson.EnvVars; |
| 5 | +import hudson.console.AnnotatedLargeText; |
| 6 | +import hudson.model.*; |
| 7 | +import jenkins.branch.BranchSource; |
| 8 | +import jenkins.branch.MultiBranchProject; |
| 9 | +import jenkins.plugins.git.GitSCMSource; |
| 10 | +import jenkins.plugins.git.GitSampleRepoRule; |
| 11 | +import jenkins.plugins.git.traits.BranchDiscoveryTrait; |
| 12 | +import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject; |
| 13 | +import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
| 14 | +import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
| 15 | +import org.jvnet.hudson.test.JenkinsRule; |
| 16 | +import org.junit.Rule; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.Objects; |
| 23 | + |
| 24 | +import static org.junit.Assert.*; |
| 25 | + |
| 26 | + |
| 27 | +@SuppressWarnings({"rawtypes", "ResultOfMethodCallIgnored"}) |
| 28 | +public class BitbucketMultibranchTest { |
| 29 | + |
| 30 | + @Rule |
| 31 | + public JenkinsRule jenkinsRule = new JenkinsRule(); |
| 32 | + |
| 33 | + @Rule |
| 34 | + public GitSampleRepoRule sampleRepo = new GitSampleRepoRule(); |
| 35 | + |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testWorkflowMultiBranchProject() throws Exception{ |
| 39 | + BitbucketEnvironmentContributor instance = |
| 40 | + jenkinsRule.jenkins.getExtensionList(EnvironmentContributor.class).get(BitbucketEnvironmentContributor.class); |
| 41 | + assertNotNull(instance); |
| 42 | + |
| 43 | + |
| 44 | + // Initialize a Git repository |
| 45 | + sampleRepo.init(); |
| 46 | + sampleRepo.write("Jenkinsfile", "pipeline { agent any; triggers { bitbucketPush } stages { stage('Build') { steps { echo 'Building...' } } } }"); |
| 47 | + sampleRepo.git("add", "Jenkinsfile"); |
| 48 | + sampleRepo.git("commit", "--message=Initial commit"); |
| 49 | + |
| 50 | + // Create a new WorkflowMultiBranchProject |
| 51 | + WorkflowMultiBranchProject workflowMultiBranchProject = jenkinsRule.jenkins.createProject(WorkflowMultiBranchProject.class, "my-project"); |
| 52 | + |
| 53 | + // Add a GitSCMSource to the project |
| 54 | + GitSCMSource gitSource = new GitSCMSource(sampleRepo.toString()); |
| 55 | + gitSource.getTraits().add(new BranchDiscoveryTrait()); |
| 56 | + |
| 57 | + workflowMultiBranchProject.getSourcesList().add(new BranchSource(gitSource)); |
| 58 | + |
| 59 | + // Schedule and find a branch project |
| 60 | + WorkflowJob job = scheduleAndFindBranchProject(workflowMultiBranchProject); |
| 61 | + |
| 62 | + |
| 63 | + // Get the last build and perform assertions |
| 64 | + WorkflowRun build = job.getLastBuild(); |
| 65 | + assertNotNull(build); |
| 66 | + assertEquals(1, build.getNumber()); |
| 67 | + jenkinsRule.assertBuildStatusSuccess(build); |
| 68 | + jenkinsRule.assertLogContains("Branch indexing", build); |
| 69 | + } |
| 70 | + |
| 71 | + private static class Bla extends CauseAction implements EnvironmentContributingAction { |
| 72 | + |
| 73 | + @Override |
| 74 | + public void buildEnvironment(@NonNull Run<?, ?> run, @NonNull EnvVars env) { |
| 75 | + EnvironmentContributingAction.super.buildEnvironment(run, env); |
| 76 | + env.put("BITBUCKET_PAYLOAD", "checking_payload"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private WorkflowJob scheduleAndFindBranchProject(WorkflowMultiBranchProject workflowMultiBranchProject) throws Exception { |
| 81 | + |
| 82 | + // Schedule indexing and wait for completion |
| 83 | + Queue.Item queueItem = workflowMultiBranchProject.scheduleBuild2(0, |
| 84 | + new CauseAction(new BitBucketPushCause("tzachs")), new Bla()); |
| 85 | + |
| 86 | + Queue.Executable executable = Objects.requireNonNull(queueItem).getFuture().get(); |
| 87 | + if ( executable instanceof MultiBranchProject.BranchIndexing){ |
| 88 | + MultiBranchProject.BranchIndexing branchIndexing = (MultiBranchProject.BranchIndexing) executable; |
| 89 | + String multiBranchLog = getLog(branchIndexing.getLogText()); |
| 90 | + jenkinsRule.assertStringContains(multiBranchLog, "Starting branch indexing"); |
| 91 | + jenkinsRule.assertStringContains(multiBranchLog, "Started by BitBucket push by tzachs"); |
| 92 | + } |
| 93 | + jenkinsRule.waitUntilNoActivity(); |
| 94 | + |
| 95 | + return workflowMultiBranchProject.getItem("master"); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + private String getLog(AnnotatedLargeText logText) throws IOException { |
| 100 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 101 | + |
| 102 | + logText.writeLogTo(0, baos); |
| 103 | + |
| 104 | + return baos.toString(StandardCharsets.UTF_8); |
| 105 | + } |
| 106 | +} |
0 commit comments