Skip to content

Commit f1c876e

Browse files
committed
Merge pull request #27 from nmusienko/JFG-398
JFG-398 Jagger Jenkins plugin is unusable on Jenkins slave
2 parents 093eee0 + 5ccac42 commit f1c876e

File tree

5 files changed

+40
-34
lines changed

5 files changed

+40
-34
lines changed

src/main/java/com/griddynamics/jagger/jenkins/plugin/JaggerComparisonPlugin.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.griddynamics.jagger.jenkins.plugin;
22

33
import hudson.Extension;
4+
import hudson.FilePath;
45
import hudson.Launcher;
56
import hudson.model.*;
67
import hudson.tasks.BuildStepDescriptor;
@@ -17,40 +18,38 @@
1718
public class JaggerComparisonPlugin extends Builder {
1819

1920
private final String path;
20-
private final boolean ignoreErrors; //ignoring errors in plugin
21+
private final boolean stopOnErrors; //ignoring errors in plugin
2122

2223
@DataBoundConstructor
23-
public JaggerComparisonPlugin(String path,boolean ignoreErrors) {
24+
public JaggerComparisonPlugin(String path, boolean stopOnErrors) {
2425
this.path = path;
25-
this.ignoreErrors = ignoreErrors;
26+
this.stopOnErrors = stopOnErrors;
2627
}
2728

2829
public String getPath() {
2930
return path;
3031
}
3132

32-
public boolean getIgnoreErrors() {
33-
return ignoreErrors;
33+
public boolean isStopOnErrors() {
34+
return stopOnErrors;
3435
}
3536

3637
@Override
3738
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
3839
SessionDecision decision;
3940
try{
4041
String filePath = build.getEnvironment(listener).expand(getPath());
41-
File file=new File(filePath);
42-
if(file.isAbsolute()){
43-
decision=SessionDecision.create(file);
44-
} else {
45-
decision=SessionDecision.create(new File(String.valueOf(build.getWorkspace()),filePath));
46-
}
42+
FilePath file = new FilePath(build.getExecutor().getCurrentWorkspace(), filePath);
43+
listener.getLogger().println("Reading file: " + file.absolutize());
44+
decision = SessionDecision.create(file);
45+
4746
} catch (Exception e){
4847
listener.getLogger().println("Plugin exception: " + e.toString());
49-
if (getIgnoreErrors()){
50-
listener.getLogger().println("Ignoring error");
51-
return true;
48+
if (isStopOnErrors()){
49+
return false;
5250
}
53-
return false;
51+
listener.getLogger().println("Ignoring error");
52+
return true;
5453
}
5554
Result currentBuildResult=build.getResult();
5655
Decision result=decision.makeDecision();

src/main/java/com/griddynamics/jagger/jenkins/plugin/JaggerEasyDeployPlugin.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.floreysoft.jmte.Engine;
44
import hudson.Extension;
5+
import hudson.FilePath;
56
import hudson.Launcher;
67
import hudson.model.*;
78
import hudson.tasks.BuildStepDescriptor;
@@ -683,7 +684,8 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
683684

684685
PrintStream logger = listener.getLogger();
685686
logger.println(getLineSeparator() + "______Jagger_Easy_Deploy_Started______" + getLineSeparator());
686-
String pathToDeploymentScript = build.getWorkspace() + File.separator + "deploy-script.sh";
687+
FilePath pathToDeploymentScript =
688+
new FilePath(build.getExecutor().getCurrentWorkspace(), "deploy-script.sh");
687689

688690
try{
689691

@@ -714,15 +716,17 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
714716

715717
return true;
716718
}
717-
} catch (IOException e) {
719+
} catch (Exception e) {
718720

719721
logger.println("!!!" + getLineSeparator() + "Exception in perform " + e +
720722
"can't create script file or run script");
721-
722-
if(new File(pathToDeploymentScript).delete()) {
723+
try {
724+
pathToDeploymentScript.delete();
723725
logger.println(pathToDeploymentScript + " has been deleted");
724-
} else {
725-
logger.println(pathToDeploymentScript + " haven't been created");
726+
} catch (Exception exception) {
727+
logger.println("Exception during delete file '"
728+
+ pathToDeploymentScript.absolutize() + "': " + exception);
729+
exception.printStackTrace(logger);
726730
}
727731
}
728732

@@ -735,11 +739,11 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
735739
* @throws IOException if can't create file or ru cmds.
736740
* @param file 5
737741
*/
738-
private void createScriptFile(String file) throws IOException {
742+
private void createScriptFile(FilePath file) throws Exception {
739743

740744
PrintWriter fw = null;
741745
try{
742-
fw = new PrintWriter(new FileOutputStream(file));
746+
fw = new PrintWriter(file.write());
743747
fw.write(getDeploymentScript());
744748

745749
} finally {

src/main/java/com/griddynamics/jagger/jenkins/plugin/SessionDecision.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.griddynamics.jagger.jenkins.plugin;
22

3+
import hudson.FilePath;
34
import org.w3c.dom.Document;
45
import org.xml.sax.SAXException;
56

@@ -33,12 +34,12 @@ public class SessionDecision {
3334
* @throws IOException
3435
* @throws SAXException
3536
*/
36-
public static SessionDecision create (File path) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
37+
public static SessionDecision create (FilePath path) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
3738
SessionDecision decision=new SessionDecision();
3839
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
3940
factory.setValidating(true);
4041
DocumentBuilder builder = factory.newDocumentBuilder();
41-
Document doc = builder.parse(new FileInputStream(path));
42+
Document doc = builder.parse(path.read());
4243
decision.setComparisonDecision(readDecision(doc, XPATH_COMPARISON_DECISION));
4344
Decision sessionDecision=readDecision(doc, XPATH_SESSION_DECISION);
4445
if(sessionDecision==null){

src/main/resources/com/griddynamics/jagger/jenkins/plugin/JaggerComparisonPlugin/config.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<f:entry title="Path to Jagger output xml file" field="path">
33
<f:textbox />
44
</f:entry>
5-
<f:entry title="Stop and set the build to 'failed' status if there are errors when processing a result file" field="ignoreErrors">
5+
<f:entry title="Stop and set the build to 'failed' status if there are errors when processing a result file" field="stopOnErrors">
66
<f:checkbox default="checked"/>
77
</f:entry>
88
</j:jelly>

src/test/java/com/griddynamics/jagger/jenkins/plugin/JaggerComparisonPluginTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
import static org.testng.Assert.*;
5+
6+
import hudson.FilePath;
57
import org.testng.annotations.Test;
68
import org.xml.sax.SAXParseException;
79

@@ -17,42 +19,42 @@
1719
public class JaggerComparisonPluginTest {
1820
@Test(expectedExceptions = SAXParseException.class)
1921
public void brokenTest() throws Exception{
20-
SessionDecision.create(new File("src/test/resources/broken.xml")).makeDecision();
22+
SessionDecision.create(new FilePath(new File("src/test/resources/broken.xml"))).makeDecision();
2123
}
2224

2325
@Test(expectedExceptions = IllegalArgumentException.class)
2426
public void wrongXPathTest() throws Exception{
25-
SessionDecision.create(new File("src/test/resources/wrongXPath.xml")).makeDecision();
27+
SessionDecision.create(new FilePath(new File("src/test/resources/wrongXPath.xml"))).makeDecision();
2628
}
2729

2830
@Test(expectedExceptions = IllegalArgumentException.class)
2931
public void wrongDecisionTest() throws Exception{
30-
SessionDecision.create(new File("src/test/resources/wrongDecision.xml")).makeDecision();
32+
SessionDecision.create(new FilePath(new File("src/test/resources/wrongDecision.xml"))).makeDecision();
3133
}
3234

3335
@Test
3436
public void okTest() throws Exception{
35-
assertEquals(SessionDecision.create(new File("src/test/resources/ok.xml")).makeDecision(),Decision.OK);
37+
assertEquals(SessionDecision.create(new FilePath(new File("src/test/resources/ok.xml"))).makeDecision(),Decision.OK);
3638
}
3739

3840
@Test
3941
public void fatalTest() throws Exception{
40-
assertEquals(SessionDecision.create(new File("src/test/resources/fatal.xml")).makeDecision(),Decision.FATAL);
42+
assertEquals(SessionDecision.create(new FilePath(new File("src/test/resources/fatal.xml"))).makeDecision(),Decision.FATAL);
4143
}
4244

4345
@Test
4446
public void warningTest() throws Exception{
45-
assertEquals(SessionDecision.create(new File("src/test/resources/warning.xml")).makeDecision(),Decision.WARNING);
47+
assertEquals(SessionDecision.create(new FilePath(new File("src/test/resources/warning.xml"))).makeDecision(),Decision.WARNING);
4648
}
4749

4850
@Test(expectedExceptions = IllegalArgumentException.class)
4951
public void onlyComparisonTest() throws Exception{
50-
assertEquals(SessionDecision.create(new File("src/test/resources/onlyComparison.xml")).makeDecision(),Decision.WARNING);
52+
assertEquals(SessionDecision.create(new FilePath(new File("src/test/resources/onlyComparison.xml"))).makeDecision(),Decision.WARNING);
5153
}
5254

5355
@Test
5456
public void onlySummaryTest() throws Exception{
55-
assertEquals(SessionDecision.create(new File("src/test/resources/onlySummary.xml")).makeDecision(),Decision.OK);
57+
assertEquals(SessionDecision.create(new FilePath(new File("src/test/resources/onlySummary.xml"))).makeDecision(),Decision.OK);
5658
}
5759
}
5860

0 commit comments

Comments
 (0)