Skip to content
Permalink
Browse files

[JENKINS-30551] Added a test case.

  • Loading branch information
uhafner committed May 19, 2018
1 parent 3f9f18f commit 0315547bc1a3084c0ef34a9153a7ac2d81ac87a4
@@ -89,7 +89,7 @@ private WarningsPublisher enableWarnings(final FreeStyleProject job, final Consu

private FreeStyleProject createJobWithWorkspaceFile(final String fileName) throws IOException {
FreeStyleProject job = j.createFreeStyleProject();
copyFilesToWorkspaceWithSuffix(job, fileName);
copyMultipleFilesToWorkspaceWithSuffix(job, fileName);
return job;
}

@@ -135,7 +135,7 @@ private FreeStyleProject createJob() {
*/
private FreeStyleProject createJobWithWorkspaceFile(final String... fileNames) {
FreeStyleProject job = createJob();
copyFilesToWorkspaceWithSuffix(job, fileNames);
copyMultipleFilesToWorkspaceWithSuffix(job, fileNames);
return job;
}

@@ -1,25 +1,22 @@
package io.jenkins.plugins.analysis.warnings;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Objects;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;

import static edu.hm.hafner.analysis.assertj.Assertions.*;
import io.jenkins.plugins.analysis.core.model.AnalysisResult;
import io.jenkins.plugins.analysis.core.model.StaticAnalysisTool;
import io.jenkins.plugins.analysis.core.steps.PublishIssuesStep;
import io.jenkins.plugins.analysis.core.steps.ScanForIssuesStep;
import io.jenkins.plugins.analysis.core.testutil.IntegrationTest;
import io.jenkins.plugins.analysis.core.views.ResultAction;

import static edu.hm.hafner.analysis.assertj.Assertions.*;

import hudson.FilePath;
import hudson.model.Result;
import hudson.model.TopLevelItem;

/**
* Integration tests of the warnings plug-in in pipelines.
@@ -84,7 +81,7 @@ protected String createScanForIssuesStep(final Class<? extends StaticAnalysisToo
*/
protected WorkflowJob createJobWithWorkspaceFiles(final String... fileNames) {
WorkflowJob job = createJob();
copyFilesToWorkspaceWithSuffix(job, fileNames);
copyMultipleFilesToWorkspaceWithSuffix(job, fileNames);
return job;
}

@@ -162,33 +159,20 @@ protected CpsFlowDefinition asStage(final String... steps) {
script.append(" }\n");
script.append("}\n");

System.out.println("----------------------------------------------------------------------");
System.out.println(script);
System.out.println("----------------------------------------------------------------------");
return new CpsFlowDefinition(script.toString(), true);
String jenkinsFile = script.toString();
logJenkinsFile(jenkinsFile);
return new CpsFlowDefinition(jenkinsFile, true);
}

/**
* Copies the specified files to the workspace using a generated file name.
* Prints the content of the JenkinsFile to StdOut.
*
* @param job
* the job to get the workspace for
* @param fileName
* the files to create
* @param content
* the content of the file
* @param script the script
*/
protected void createFileInWorkspace(final TopLevelItem job, final String fileName, final String content) {
try {
FilePath workspace = j.jenkins.getWorkspaceFor(job);
assertThat(workspace).isNotNull();

FilePath child = workspace.child(fileName);
child.copyFrom(new ByteArrayInputStream(content.getBytes()));
}
catch (IOException | InterruptedException e) {
throw new AssertionError(e);
}
protected void logJenkinsFile(final String script) {
System.out.println("----------------------------------------------------------------------");
System.out.println(script);
System.out.println("----------------------------------------------------------------------");
}

/**
@@ -220,4 +204,10 @@ protected ResultAction getResultAction(final WorkflowRun run) {
assertThat(action).as("No ResultAction found in run %s", run).isNotNull();
return action;
}

protected FlowDefinition readDefinition(final String fileName) {
String script = toString(fileName);
logJenkinsFile(script);
return new CpsFlowDefinition(script, true);
}
}
@@ -38,6 +38,37 @@
* @see PublishIssuesStep
*/
public class StepsITest extends PipelineITest {
/**
*/
@Test
public void shouldRecordOutputOfParallelSteps() {
WorkflowJob job = createJob();

copySingleFileToWorkspace(createAgent("node1"), job, "eclipse.txt", "issues.txt");
copySingleFileToWorkspace(createAgent("node2"), job, "eclipse.txt", "issues.txt");

job.setDefinition(readDefinition("parallel.jenkinsfile"));

WorkflowRun run = runSuccessfully(job);
List<ResultAction> actions = run.getActions(ResultAction.class);

assertThat(actions).hasSize(2);

ResultAction first;
ResultAction second;
if (actions.get(0).getId().equals("java-1")) {
first = actions.get(0);
second = actions.get(1);
}
else {
first = actions.get(1);
second = actions.get(0);
}

assertThat(first.getResult().getIssues()).hasSize(5);
assertThat(second.getResult().getIssues()).hasSize(3);
}

/**
* Runs the Eclipse parser on the console log that contains 8 issues which are decorated with console notes. The
* output file is copied to the console log using a shell cat command.
@@ -155,20 +186,22 @@ public void shouldShowWarningsOfGroovyParser() {
"publishIssues issues:[groovy]"));

ParserConfiguration configuration = ParserConfiguration.getInstance();
String id = "groovy-pep8";
configuration.setParsers(Collections.singletonList(
new GroovyParser("groovy-pep8", "Groovy Pep8",
new GroovyParser(id, "Groovy Pep8",
"(.*):(\\d+):(\\d+): (\\D\\d*) (.*)",
toString("groovy/pep8.groovy"), "")));
WorkflowRun run = runSuccessfully(job);

ResultAction action = getResultAction(run);
assertThat(action.getId()).isEqualTo("groovy-pep8");
assertThat(action.getId()).isEqualTo(id);
assertThat(action.getDisplayName()).contains("Groovy Pep8");

AnalysisResult result = action.getResult();
assertThat(result.getIssues()).hasSize(8);

// FIXME: issues from the action should also have an ID
assertThat(result.getIssues()).hasOrigin(id);
assertThat(result.getIssues().getPropertyCount(Issue::getOrigin)).containsOnly(entry(id, 8));
}

/**
@@ -188,15 +221,14 @@ public void shouldIncludeJustOneFile() {
assertThat(result.getIssues()).hasSize(1);
}

// TODO: check all variants of a reference (non-existing name, no run in job, overallResultMustBeSuccess, ignoreAnalysisResult, etc.)
/**
* Creates a reference job and starts the analysis for this job. Then another job is created that uses the first
* one as reference. Verifies that the association is correctly stored.
*/
@Test
public void shouldUseOtherJobAsReference() {
WorkflowJob reference = createJob("reference");
copyFilesToWorkspaceWithSuffix(reference, "java-start.txt");
copyMultipleFilesToWorkspaceWithSuffix(reference, "java-start.txt");
reference.setDefinition(asStage(createScanForIssuesStep(Java.class), PUBLISH_ISSUES_STEP));

AnalysisResult referenceResult = scheduleBuild(reference, Java.class);
@@ -0,0 +1,22 @@
def tasks = [:]

tasks["task_1"] = {
stage ("task_1") {
node('node1') {
def eclipse = scanForIssues tool: [$class: 'Eclipse'], pattern:'**/*issues.txt', defaultEncoding:'UTF-8'
publishIssues issues:[eclipse], name: 'Java Warnings 1', id : 'java-1',
filters:[[property: [$class: 'IncludeFile'], pattern: '.*SWTTextBuilder.java.*']]
}
}
}
tasks["task_2"] = {
stage ("task_2") {
node('node2') {
def eclipse = scanForIssues tool: [$class: 'Eclipse'], pattern:'**/*issues.txt', defaultEncoding:'UTF-8'
publishIssues issues:[eclipse], name: 'Java Warnings 2', id : 'java-2',
filters:[[property: [$class: 'ExcludeFile'], pattern: '.*SWTTextBuilder.java.*']]
}
}
}

parallel tasks

0 comments on commit 0315547

Please sign in to comment.
You can’t perform that action at this time.