Skip to content

Commit

Permalink
[JENKINS-43094] Add ATH unit test tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrille Le Clerc committed Jun 10, 2017
1 parent c4ec6ad commit b3e48c2
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
@@ -0,0 +1,123 @@
package org.jenkinsci.plugins.pipeline.maven;

import hudson.model.Fingerprint;
import hudson.model.Job;
import hudson.model.Result;
import hudson.tasks.Maven;
import jenkins.mvn.DefaultGlobalSettingsProvider;
import jenkins.mvn.DefaultSettingsProvider;
import jenkins.mvn.GlobalMavenConfig;
import jenkins.plugins.git.GitSampleRepoRule;
import jenkins.scm.impl.mock.GitSampleRepoRuleUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
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.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.ExtendedToolInstallations;
import org.jvnet.hudson.test.JenkinsRule;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Hashtable;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

/**
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
public class DependencyFingerprintPublisherTest {
@ClassRule
public static BuildWatcher buildWatcher = new BuildWatcher();

@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();

@Rule
public GitSampleRepoRule gitRepoRule = new GitSampleRepoRule();

String mavenInstallationName;

@Before
public void setup() throws Exception {
// Maven.MavenInstallation maven3 = ToolInstallations.configureMaven35();
Maven.MavenInstallation maven3 = ExtendedToolInstallations.configureMaven35();

mavenInstallationName = maven3.getName();

GlobalMavenConfig globalMavenConfig = jenkinsRule.get(GlobalMavenConfig.class);
globalMavenConfig.setGlobalSettingsProvider(new DefaultGlobalSettingsProvider());
globalMavenConfig.setSettingsProvider(new DefaultSettingsProvider());

}

/**
* Two (2) pipeline maven jobs consume the same commons-lang3-3.5.jar dependency.
* Verify that withMaven fingerprints commons-lang3-3.5.jar on each build
* @throws Exception
*/
@Test
public void verify_fingerprinting_of_dependencies() throws Exception {

loadMonoDependencyMavenProjectInGitRepo(this.gitRepoRule);

String pipelineScript = "node('master') {\n" +
" git($/" + gitRepoRule.toString() + "/$)\n" +
" withMaven(options:[dependenciesFingerprintPublisher(includeReleaseVersions:true)]) {\n" +
" sh 'mvn package'\n" +
" }\n" +
"}";

String commonsLang3version35Md5 = "780b5a8b72eebe6d0dbff1c11b5658fa";

WorkflowJob firstPipeline;
{ // first job using commons-lang3:3.5
firstPipeline = jenkinsRule.createProject(WorkflowJob.class, "build-mono-dependency-maven-project-1");
firstPipeline.setDefinition(new CpsFlowDefinition(pipelineScript, true));
WorkflowRun build = jenkinsRule.assertBuildStatus(Result.SUCCESS, firstPipeline.scheduleBuild2(0));

Fingerprint fingerprint = jenkinsRule.jenkins.getFingerprintMap().get(commonsLang3version35Md5);
assertThat(fingerprint, not(nullValue()));

Fingerprint.BuildPtr original = fingerprint.getOriginal();
assertThat(fingerprint.getFileName(), is("org/apache/commons/commons-lang3/3.5/commons-lang3-3.5.jar"));
assertThat(original.getName(), is(firstPipeline.getName()));
assertThat(original.getNumber(), is(1));
Hashtable<String, Fingerprint.RangeSet> usages = fingerprint.getUsages();
assertThat(usages.size(), is(1));
assertThat(usages.containsKey(firstPipeline.getName()), is(true));
}
{ // second job using commons-lang3:3.5
WorkflowJob secondPipeline = jenkinsRule.createProject(WorkflowJob.class, "build-mono-dependency-maven-project-2");
secondPipeline.setDefinition(new CpsFlowDefinition(pipelineScript, true));
WorkflowRun build = jenkinsRule.assertBuildStatus(Result.SUCCESS, secondPipeline.scheduleBuild2(0));

Fingerprint fingerprint = jenkinsRule.jenkins.getFingerprintMap().get(commonsLang3version35Md5);
assertThat(fingerprint, not(nullValue()));
Hashtable<String, Fingerprint.RangeSet> usages = fingerprint.getUsages();
assertThat(usages.size(), is(2));
assertThat(usages.containsKey(firstPipeline.getName()), is(true));
assertThat(usages.containsKey(secondPipeline.getName()), is(true));
}

}

private void loadMonoDependencyMavenProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/mono_dependency_maven_jar_project/");
}

private void loadSourceCodeInGitRepository(GitSampleRepoRule gitRepo, String name) throws Exception {
gitRepo.init();
Path mavenProjectRoot = Paths.get(WithMavenStepOnMasterTest.class.getResource(name).toURI());
if (!Files.exists(mavenProjectRoot)) {
throw new IllegalStateException("Folder '" + mavenProjectRoot + "' not found");
}
GitSampleRepoRuleUtils.addFilesAndCommit(mavenProjectRoot, this.gitRepoRule);
}
}
@@ -0,0 +1,9 @@


*.log

target/
work/

*.iml
.idea/
@@ -0,0 +1,15 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jenkins.mvn.test</groupId>
<artifactId>mono-dependency-maven-project</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
</project>

@@ -0,0 +1,10 @@
package com.example;

/**
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
public class App {
public static void main(String[] args) {
System.out.println("App");
}
}

0 comments on commit b3e48c2

Please sign in to comment.