Skip to content

Commit

Permalink
chore: rewrite some tests (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
bguerin authored Mar 6, 2023
1 parent fe5ddf0 commit b2a0a88
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 309 deletions.
7 changes: 7 additions & 0 deletions jenkins-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
<version>1166.va_436e268e972</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-stage-step</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package org.jenkinsci.plugins.pipeline.maven;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import org.jenkinsci.plugins.pipeline.maven.dao.PipelineMavenPluginDao;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;

import hudson.FilePath;
import hudson.model.Fingerprint;
import hudson.tasks.Fingerprinter;
import hudson.tasks.Maven;
import jenkins.model.Jenkins;
import jenkins.mvn.DefaultGlobalSettingsProvider;
Expand Down Expand Up @@ -69,62 +79,10 @@ protected void loadMavenJarProjectInGitRepo(GitSampleRepoRule gitRepo) throws Ex
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_jar_project/");
}

protected void loadMavenPomProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_pom_project/");
}

protected void loadMavenJarWithParentPomProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_jar_with_parent_pom_project/");
}

protected void loadMavenJarWithJacocoInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_jar_with_jacoco_project/");
}

protected void loadMavenWarProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_war_project/");
}

protected void loadMavenJarWithFlattenPomProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_jar_with_flatten_pom_project/");
}

protected void loadOsgiBundleProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/multi_module_bundle_project/");
}

protected void loadJenkinsPluginProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_hpi_project/");
}

protected void loadMultiModuleProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/multi_module_maven_project/");
}

protected void loadNbmBaseMavenProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_nbm_base_project/");
}

protected void loadNbmDependencyMavenJarProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_nbm_dependency_project/");
}

protected void loadDockerBaseMavenProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_docker_base_project/");
}

protected void loadDockerDependencyMavenJarProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_docker_dependency_project/");
}

protected void loadDeployFileBaseMavenProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_deployfile_base_project/");
}

protected void loadDeployFileDependencyMavenJarProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_deployfile_dependency_project/");
}

protected void loadSourceCodeInGitRepository(GitSampleRepoRule gitRepo, String name) throws Exception {
gitRepo.init();
Path mavenProjectRoot = Paths.get(WithMavenStepOnMasterTest.class.getResource(name).toURI());
Expand All @@ -140,12 +98,24 @@ protected Maven.MavenInstallation configureDefaultMaven(String mavenVersion, int
File buildDirectory = new File(System.getProperty("buildDirectory", "target")); // TODO relative path
File mvnHome = new File(buildDirectory, "apache-maven-" + mavenVersion);
if (!mvnHome.exists()) {
FilePath mvn = Jenkins.getInstance().getRootPath().createTempFile("maven", "zip");
FilePath mvn = Jenkins.get().getRootPath().createTempFile("maven", "zip");
mvn.copyFrom(new URL("https://dlcdn.apache.org/maven/maven-3/" + mavenVersion + "/binaries/apache-maven-" + mavenVersion + "-bin.tar.gz"));
mvn.untar(new FilePath(buildDirectory), FilePath.TarCompression.GZIP);
}
Maven.MavenInstallation mavenInstallation = new Maven.MavenInstallation("default", mvnHome.getAbsolutePath(), JenkinsRule.NO_PROPERTIES);
Jenkins.getInstance().getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(mavenInstallation);
Jenkins.get().getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(mavenInstallation);
return mavenInstallation;
}

protected void verifyFileIsFingerPrinted(WorkflowJob pipeline, WorkflowRun build, String fileName) throws java.io.IOException {
Fingerprinter.FingerprintAction fingerprintAction = build.getAction(Fingerprinter.FingerprintAction.class);
Map<String, String> records = fingerprintAction.getRecords();
String jarFileMd5sum = records.get(fileName);
assertThat(jarFileMd5sum, not(nullValue()));

Fingerprint jarFileFingerPrint = jenkinsRule.getInstance().getFingerprintMap().get(jarFileMd5sum);
assertThat(jarFileFingerPrint.getFileName(), is(fileName));
assertThat(jarFileFingerPrint.getOriginal().getJob().getName(), is(pipeline.getName()));
assertThat(jarFileFingerPrint.getOriginal().getNumber(), is(build.getNumber()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import jenkins.plugins.git.traits.BranchDiscoveryTrait;
import jenkins.scm.api.trait.SCMSourceTrait;

import org.eclipse.collections.impl.block.factory.Predicates;
import org.hamcrest.Matchers;
import org.jenkinsci.plugins.pipeline.maven.dao.PipelineMavenPluginDao;
import org.jenkinsci.plugins.pipeline.maven.publishers.PipelineGraphPublisher;
Expand Down Expand Up @@ -41,12 +40,6 @@ public class DependencyGraphTest extends AbstractIntegrationTest {
@Rule
public GitSampleRepoRule downstreamArtifactRepoRule = new GitSampleRepoRule();

/*
Does not work
@Inject
public GlobalPipelineMavenConfig globalPipelineMavenConfig;
*/

@Before
@Override
public void setup() throws Exception {
Expand Down Expand Up @@ -185,8 +178,7 @@ public void verify_downstream_multi_branch_pipeline_trigger() throws Exception {

@Test
public void verify_osgi_bundle_recorded_as_bundle_and_as_jar() throws Exception {
loadOsgiBundleProjectInGitRepo(gitRepoRule);

loadSourceCodeInGitRepository(this.gitRepoRule, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/multi_module_bundle_project/");

String pipelineScript = "node() {\n" +
" git($/" + gitRepoRule.toString() + "/$)\n" +
Expand All @@ -210,8 +202,8 @@ public void verify_osgi_bundle_recorded_as_bundle_and_as_jar() throws Exception
{skip_downstream_triggers=TRUE, type=jar, gav=jenkins.mvn.test.bundle:print-api:0.0.1-SNAPSHOT},
{skip_downstream_triggers=TRUE, type=pom, gav=jenkins.mvn.test.bundle:print-api:0.0.1-SNAPSHOT},
{skip_downstream_triggers=TRUE, type=pom, gav=jenkins.mvn.test.bundle:print-impl:0.0.1-SNAPSHOT}]
*/

System.out.println("generated artifacts" + generatedArtifacts);

Iterable<String> matchingArtifactTypes = generatedArtifacts.stream()
Expand All @@ -225,12 +217,86 @@ public void verify_osgi_bundle_recorded_as_bundle_and_as_jar() throws Exception
assertThat(matchingArtifactTypes, Matchers.containsInAnyOrder("jar", "bundle", "pom"));
}

@Test
public void verify_downstream_pipeline_triggered_on_parent_pom_build() throws Exception {
loadSourceCodeInGitRepository(this.gitRepoRule, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_pom_project/");

String pipelineScript = "node() {\n" +
" git($/" + gitRepoRule.toString() + "/$)\n" +
" withMaven() {\n" +
" sh 'mvn package'\n" +
" }\n" +
"}";

// TRIGGER maven-jar#1 to record that "build-maven-jar"
WorkflowJob multiModuleBundleProjectPipeline = jenkinsRule.createProject(WorkflowJob.class, "build-multi-module-bundle");
multiModuleBundleProjectPipeline.setDefinition(new CpsFlowDefinition(pipelineScript, true));
WorkflowRun build = jenkinsRule.assertBuildStatus(Result.SUCCESS, multiModuleBundleProjectPipeline.scheduleBuild2(0));

PipelineMavenPluginDao dao = GlobalPipelineMavenConfig.get().getDao();
List<MavenArtifact> generatedArtifacts = dao.getGeneratedArtifacts(multiModuleBundleProjectPipeline.getFullName(), build.getNumber());

Iterable<String> matchingArtifactTypes = generatedArtifacts.stream()
.filter(input -> input != null &&
input.getGroupId().equals("com.example") &&
input.getArtifactId().equals("my-pom") &&
input.getVersion().equals("0.1-SNAPSHOT"))
.map(MavenArtifact::getType)
.collect(Collectors.toList());

assertThat(matchingArtifactTypes, Matchers.containsInAnyOrder("pom"));
}

@Test
public void verify_downstream_pipeline_triggered_on_jar_having_parent_pom_dependency() throws Exception {
System.out.println("gitRepoRule: " + gitRepoRule);
loadSourceCodeInGitRepository(this.gitRepoRule, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_pom_project/");
System.out.println("downstreamArtifactRepoRule: " + downstreamArtifactRepoRule);
loadSourceCodeInGitRepository(this.downstreamArtifactRepoRule, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_jar_with_parent_pom_project/");

String mavenParentPipelineScript = "node() {\n" +
" git($/" + gitRepoRule.toString() + "/$)\n" +
" withMaven() {\n" +
" sh 'mvn install'\n" +
" }\n" +
"}";
String mavenJarPipelineScript = "node() {\n" +
" git($/" + downstreamArtifactRepoRule.toString() + "/$)\n" +
" withMaven() {\n" +
" sh 'mvn install'\n" +
" }\n" +
"}";


WorkflowJob mavenParentPipeline = jenkinsRule.createProject(WorkflowJob.class, "build-maven-parent");
mavenParentPipeline.setDefinition(new CpsFlowDefinition(mavenParentPipelineScript, true));
mavenParentPipeline.addTrigger(new WorkflowJobDependencyTrigger());

WorkflowRun mavenParentPipelineFirstRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenParentPipeline.scheduleBuild2(0));
// TODO check in DB that the generated artifact is recorded

WorkflowJob mavenJarPipeline = jenkinsRule.createProject(WorkflowJob.class, "build-maven-jar");
mavenJarPipeline.setDefinition(new CpsFlowDefinition(mavenJarPipelineScript, true));
mavenJarPipeline.addTrigger(new WorkflowJobDependencyTrigger());
WorkflowRun mavenJarPipelineFirstRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenJarPipeline.scheduleBuild2(0));
// TODO check in DB that the dependency on the war project is recorded

WorkflowRun mavenParentPipelineSecondRun = jenkinsRule.assertBuildStatus(Result.SUCCESS, mavenParentPipeline.scheduleBuild2(0));

jenkinsRule.waitUntilNoActivity();

WorkflowRun mavenJarPipelineLastRun = mavenJarPipeline.getLastBuild();

assertThat(mavenJarPipelineLastRun.getNumber(), is(mavenJarPipelineFirstRun.getNumber() + 1));
Cause.UpstreamCause upstreamCause = mavenJarPipelineLastRun.getCause(Cause.UpstreamCause.class);
assertThat(upstreamCause, notNullValue());
}

/**
* The maven-war-app has a dependency on the maven-jar-app
*/
@Test
public void verify_downstream_pipeline_triggered_on_parent_pom_build() throws Exception {
public void verify_downstream_pipeline_triggered_on_war_having_jar_dependency() throws Exception {
System.out.println("gitRepoRule: " + gitRepoRule);
loadMavenJarProjectInGitRepo(this.gitRepoRule);
System.out.println("downstreamArtifactRepoRule: " + downstreamArtifactRepoRule);
Expand Down Expand Up @@ -276,16 +342,14 @@ public void verify_downstream_pipeline_triggered_on_parent_pom_build() throws Ex
assertThat(mavenWarPipelineLastRun.getNumber(), is(mavenWarPipelineFirstRun.getNumber() + 1));
Cause.UpstreamCause upstreamCause = mavenWarPipelineLastRun.getCause(Cause.UpstreamCause.class);
assertThat(upstreamCause, notNullValue());


}

@Test
public void verify_nbm_downstream_simple_pipeline_trigger() throws Exception {
System.out.println("gitRepoRule: " + gitRepoRule);
loadNbmDependencyMavenJarProjectInGitRepo(this.gitRepoRule);
loadSourceCodeInGitRepository(this.gitRepoRule, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_nbm_dependency_project/");
System.out.println("downstreamArtifactRepoRule: " + downstreamArtifactRepoRule);
loadNbmBaseMavenProjectInGitRepo(this.downstreamArtifactRepoRule);
loadSourceCodeInGitRepository(this.downstreamArtifactRepoRule, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_nbm_base_project/");

String mavenNbmDependencyPipelineScript = "node() {\n"
+ " git($/" + gitRepoRule.toString() + "/$)\n"
Expand Down Expand Up @@ -330,9 +394,9 @@ public void verify_nbm_downstream_simple_pipeline_trigger() throws Exception {
@Test
public void verify_docker_downstream_simple_pipeline_trigger() throws Exception {
System.out.println("gitRepoRule: " + gitRepoRule);
loadDockerDependencyMavenJarProjectInGitRepo(this.gitRepoRule);
loadSourceCodeInGitRepository(this.gitRepoRule, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_docker_dependency_project/");
System.out.println("downstreamArtifactRepoRule: " + downstreamArtifactRepoRule);
loadDockerBaseMavenProjectInGitRepo(this.downstreamArtifactRepoRule);
loadSourceCodeInGitRepository(this.downstreamArtifactRepoRule, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_docker_base_project/");

String mavenDockerDependencyPipelineScript = "node() {\n"
+ " git($/" + gitRepoRule.toString() + "/$)\n"
Expand Down Expand Up @@ -384,9 +448,9 @@ public void verify_deployfile_downstream_simple_pipeline_trigger() throws Except
publisher.setLifecycleThreshold("deploy");

System.out.println("gitRepoRule: " + gitRepoRule);
loadDeployFileDependencyMavenJarProjectInGitRepo(this.gitRepoRule);
loadSourceCodeInGitRepository(this.gitRepoRule, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_deployfile_dependency_project/");
System.out.println("downstreamArtifactRepoRule: " + downstreamArtifactRepoRule);
loadDeployFileBaseMavenProjectInGitRepo(this.downstreamArtifactRepoRule);
loadSourceCodeInGitRepository(this.downstreamArtifactRepoRule, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_deployfile_base_project/");

String mavenDeployFileDependencyPipelineScript = "node() {\n"
+ " git($/" + gitRepoRule.toString() + "/$)\n"
Expand Down
Loading

0 comments on commit b2a0a88

Please sign in to comment.