Skip to content

Commit

Permalink
Merge pull request #31 from jenkinsci/jenkinsfile
Browse files Browse the repository at this point in the history
chore: add required Jenkinsfile
  • Loading branch information
dawidmalina authored Mar 9, 2024
2 parents fea34ab + b0ecc04 commit 3af1a6a
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 29 deletions.
7 changes: 7 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
buildPlugin(
forkCount: '1C', // run this number of tests in parallel for faster feedback. If the number terminates with a 'C', the value will be multiplied by the number of available CPU cores
useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests
configurations: [
[platform: 'linux', jdk: 21],
[platform: 'windows', jdk: 17],
])
11 changes: 9 additions & 2 deletions src/main/java/com/lookout/jenkins/EnvironmentScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ private Environment generateEnvironment(AbstractBuild<?, ?> build,
// First we create the script in a temporary directory.
FilePath ws = build.getWorkspace(), scriptFile = null;

if (ws == null) {
listener.error(Messages.EnvironmentScriptWrapper_WorkspaceIsNull());
return null;
}

ByteArrayOutputStream commandOutput = new ByteArrayOutputStream();
int returnCode = -1;
try {
Expand All @@ -132,7 +137,9 @@ private Environment generateEnvironment(AbstractBuild<?, ?> build,
}

// Make sure prefix will always be more than 3 letters
final String prefix = "env-" + build.getProject().getName();
// Replace the equals sign with an underscore because Windows doesn't accept
// this
final String prefix = "env-" + build.getProject().getName().replace("=", "_");
// Create a file in the system temporary directory with our script in it.
scriptFile = ws.createTextTempFile(prefix, extension, script, false);

Expand Down Expand Up @@ -295,7 +302,7 @@ public ListBoxModel doFillScriptTypeItems() {
ListBoxModel items = new ListBoxModel(
new ListBoxModel.Option(Commands.UNIX_SCRIPT_DISPLAY_NAME, Commands.UNIX_SCRIPT),
new ListBoxModel.Option(Commands.BATCH_SCRIPT_DISPLAY_NAME, Commands.BATCH_SCRIPT),
new ListBoxModel.Option(Commands.PROWER_SHELL_DISPLAY_NAME, Commands.POWER_SHELL));
new ListBoxModel.Option(Commands.POWER_SHELL_DISPLAY_NAME, Commands.POWER_SHELL));
return items;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lookout/jenkins/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Commands {
public final static String UNIX_SCRIPT_DISPLAY_NAME = "Unix script";

public final static String POWER_SHELL = "powerShell";
public final static String PROWER_SHELL_DISPLAY_NAME = "Powershell script";
public final static String POWER_SHELL_DISPLAY_NAME = "Powershell script";

public final static String BATCH_SCRIPT = "batchScript";
public final static String BATCH_SCRIPT_DISPLAY_NAME = "Batch script";
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/com/lookout/jenkins/Messages.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
EnvironmentScriptWrapper.UnableToProduceScript=Unable to produce a script file
EnvironmentScriptWrapper.UnableToParseScriptOutput=Unable to parse output from script
EnvironmentScriptWrapper.UnableToExecuteScript=Unable to execute script, return code {0}
EnvironmentScriptWrapper.UnableToExecuteScript=Unable to execute script, return code {0}
EnvironmentScriptWrapper.WorkspaceIsNull=Workspace is null. Cannot generate environment
33 changes: 28 additions & 5 deletions src/test/java/com/lookout/jenkins/EnvironmentScriptMatrixTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jvnet.hudson.test.JenkinsRule;

import hudson.FilePath;
import hudson.Functions;
import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixRun;
Expand Down Expand Up @@ -42,6 +43,11 @@ public MatrixTestJob(String script, boolean onlyRunOnParent) throws Exception {
// race conditions when concurrently updating the 'counter' file.
project.setExecutionStrategy(new DefaultMatrixExecutionStrategyImpl(true, null, null, null));

String scriptType = UNIX_SCRIPT;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
}

project.setAxes(new AxisList(new Axis("axis", "value1", "value2")));
project.getBuildWrappersList()
.add(new EnvironmentScript(script, scriptType, onlyRunOnParent, hideGeneratedValue));
Expand All @@ -52,28 +58,45 @@ public MatrixTestJob(String script, boolean onlyRunOnParent) throws Exception {
}
}

final static String SCRIPT_COUNTER = "file='%s/counter'\n"
final static String SCRIPT_COUNTER_UNIX = "file='%s/counter'\n"
+ "if [ -f $file ]; then\n"
+ " i=$(($(cat $file)+1))\n"
+ "else\n"
+ " i=1\n"
+ "fi\n"
+ "echo 1 >was_run\n"
+ "echo $i >$file\n"
+ "echo 1 > was_run\n"
+ "echo $i > $file\n"
+ "echo seen=yes";

final static String SCRIPT_COUNTER_BATCH = "@echo off\r\n"
+ "set file=%s\\counter\r\n"
+ "if exist %%file%% (\r\n"
+ " set /p i=< %%file%%\r\n"
+ " set /a i+=1\r\n"
+ ") else (\r\n"
+ " set i=1\r\n"
+ ")\r\n"
+ "echo 1 > was_run\r\n"
+ "echo %%i%% > %%file%%\r\n"
+ "echo seen=yes";

// Generate a random directory that we pass to the shell script.
File tempDir;

String script;
String scriptType = "unixScript";
final static String UNIX_SCRIPT = "unixScript";
final static String BATCH_SCRIPT = "batchScript";
boolean hideGeneratedValue = Boolean.TRUE;

@Before
public void setUp() throws IOException {
// Generate a random directory that we pass to the shell script.
tempDir = Files.createTempDirectory("tmp").toFile();
script = String.format(SCRIPT_COUNTER, tempDir.getPath());
String scriptCounter = SCRIPT_COUNTER_UNIX;
if (Functions.isWindows()) {
scriptCounter = SCRIPT_COUNTER_BATCH;
}
script = String.format(scriptCounter, tempDir.getPath());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.nio.file.Files;

import hudson.FilePath;
import hudson.Functions;
import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixRun;
Expand Down Expand Up @@ -43,6 +44,11 @@ public MultiMatrixTestJob(String script, boolean onlyRunOnParent) throws Excepti
// race conditions when concurrently updating the 'counter' file.
project.setExecutionStrategy(new DefaultMatrixExecutionStrategyImpl(true, null, null, null));

String scriptType = UNIX_SCRIPT;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
}

project.setAxes(new AxisList(new Axis("axis", "value1", "value2")));
project.getBuildWrappersList()
.add(new EnvironmentScript(script, scriptType, onlyRunOnParent, hideGeneratedValue));
Expand All @@ -53,28 +59,45 @@ public MultiMatrixTestJob(String script, boolean onlyRunOnParent) throws Excepti
}
}

final static String SCRIPT_COUNTER = "file='%s/counter'\n"
final static String SCRIPT_COUNTER_UNIX = "file='%s/counter'\n"
+ "if [ -f $file ]; then\n"
+ " i=$(($(cat $file)+1))\n"
+ "else\n"
+ " i=1\n"
+ "fi\n"
+ "echo 1 >was_run\n"
+ "echo $i >$file\n"
+ "echo 1 > was_run\n"
+ "echo $i > $file\n"
+ "echo seen=yes";

final static String SCRIPT_COUNTER_BATCH = "@echo off\r\n"
+ "set file=%s\\counter\r\n"
+ "if exist %%file%% (\r\n"
+ " set /p i=< %%file%%\r\n"
+ " set /a i+=1\r\n"
+ ") else (\r\n"
+ " set i=1\r\n"
+ ")\r\n"
+ "echo 1 > was_run\r\n"
+ "echo %%i%% > %%file%%\r\n"
+ "echo seen=yes";

// Generate a random directory that we pass to the shell script.
File tempDir;

String script;
String scriptType = "unixScript";
final static String UNIX_SCRIPT = "unixScript";
final static String BATCH_SCRIPT = "batchScript";
boolean hideGeneratedValue = Boolean.TRUE;

@Before
public void setUp() throws IOException {
// Generate a random directory that we pass to the shell script.
tempDir = Files.createTempDirectory("tmp").toFile();
script = String.format(SCRIPT_COUNTER, tempDir);
String scriptCounter = SCRIPT_COUNTER_UNIX;
if (Functions.isWindows()) {
scriptCounter = SCRIPT_COUNTER_BATCH;
}
script = String.format(scriptCounter, tempDir);
}

@Test
Expand Down
93 changes: 78 additions & 15 deletions src/test/java/com/lookout/jenkins/EnvironmentScriptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import static org.junit.Assert.*;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;

import hudson.EnvVars;
import hudson.Functions;
import hudson.model.FreeStyleBuild;
import hudson.model.TaskListener;
import hudson.model.FreeStyleProject;
Expand Down Expand Up @@ -39,33 +41,54 @@ public TestJob(String script, String scriptType, boolean hideEnvironmentVariable
}

final static String UNIX_SCRIPT = "unixScript";
final static String BATCH_SCRIPT = "batchScript";

final static String SCRIPT_SIMPLE_VARIABLES = "echo var1=one\n"
+ "echo var2=two\n"
+ "echo var3=three";

final static String SCRIPT_DEPENDENT_VARIABLES = "echo var1=one\n"
final static String SCRIPT_DEPENDENT_VARIABLES_UNIX = "echo var1=one\n"
+ "echo var2='$var1 two'\n"
+ "echo var3='yo $var4'\n"
+ "echo var4='three ${var2}'";

final static String SCRIPT_OVERRIDDEN_VARIABLES = "echo var1=one\n"
final static String SCRIPT_DEPENDENT_VARIABLES_BATCH = "echo var1=one\n"
+ "echo var2=$var1 two\n"
+ "echo var3=yo $var4\n"
+ "echo var4=three ${var2}";

final static String SCRIPT_OVERRIDDEN_VARIABLES_UNIX = "echo var1=one\n"
+ "echo var1+something='not one'\n"
+ "echo var2+something='two'";

final static String SCRIPT_OVERRIDDEN_VARIABLES_BATCH = "echo var1=one\n"
+ "echo var1+something=not one\n"
+ "echo var2+something=two";

final static String SCRIPT_UTF8 = "echo UTFstr=mąż";

final static String SCRIPT_SHEBANG = "#!/bin/cat\n"
final static String SCRIPT_SHEBANG_UNIX = "#!/bin/cat\n"
+ "hello=world";

// batch script does not have shebang
final static String SCRIPT_SHEBANG_BATCH = "echo hello=world";

public void testWithEmptyScript() throws Exception {
TestJob job = new TestJob("", UNIX_SCRIPT, true);
String scriptType = UNIX_SCRIPT;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
}
TestJob job = new TestJob("", scriptType, true);
assertEquals(Result.SUCCESS, job.build.getResult());
}

@Test
public void testWithSimpleVariables() throws Exception {
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, UNIX_SCRIPT, true);
String scriptType = UNIX_SCRIPT;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
}
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, scriptType, true);
assertEquals(Result.SUCCESS, job.build.getResult());

EnvVars vars = job.build.getEnvironment(job.listener);
Expand All @@ -76,7 +99,13 @@ public void testWithSimpleVariables() throws Exception {

@Test
public void testWithDependentVariables() throws Exception {
TestJob job = new TestJob(SCRIPT_DEPENDENT_VARIABLES, UNIX_SCRIPT, true);
String scriptType = UNIX_SCRIPT;
String script = SCRIPT_DEPENDENT_VARIABLES_UNIX;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
script = SCRIPT_DEPENDENT_VARIABLES_BATCH;
}
TestJob job = new TestJob(script, scriptType, true);
assertEquals(Result.SUCCESS, job.build.getResult());

EnvVars vars = job.build.getEnvironment(job.listener);
Expand All @@ -87,41 +116,67 @@ public void testWithDependentVariables() throws Exception {
}

@Test
public void testWithOverridenVariables() throws Exception {
TestJob job = new TestJob(SCRIPT_OVERRIDDEN_VARIABLES, UNIX_SCRIPT, true);
public void testWithOverriddenVariables() throws Exception {
String scriptType = UNIX_SCRIPT;
String script = SCRIPT_OVERRIDDEN_VARIABLES_UNIX;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
script = SCRIPT_OVERRIDDEN_VARIABLES_BATCH;
}
TestJob job = new TestJob(script, scriptType, true);
assertEquals(Result.SUCCESS, job.build.getResult());

EnvVars vars = job.build.getEnvironment(job.listener);
assertEquals("not one:one", vars.get("var1"));
assertEquals("not one" + File.pathSeparatorChar + "one", vars.get("var1"));
assertEquals("two", vars.get("var2"));
}

@Test
public void testReadingFileFromSCM() throws Exception {
TestJob job = new TestJob("cat envs", UNIX_SCRIPT, true);
String scriptType = UNIX_SCRIPT;
String script = "cat envs";
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
script = "type envs";
}
TestJob job = new TestJob(script, scriptType, true);
assertEquals(Result.SUCCESS, job.build.getResult());
assertEquals("bar", job.build.getEnvironment(job.listener).get("foo_var"));
}

@Test
public void testWorkingDirectory() throws Exception {
TestJob job = new TestJob("echo hi >was_run", UNIX_SCRIPT, true);
String scriptType = UNIX_SCRIPT;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
}
TestJob job = new TestJob("echo hi >was_run", scriptType, true);

// Make sure that the $PWD of the script is $WORKSPACE.
assertTrue(job.build.getWorkspace().child("was_run").exists());
}

@Test
public void testWithShebang() throws Exception {
TestJob job = new TestJob(SCRIPT_SHEBANG, UNIX_SCRIPT, true);
String scriptType = UNIX_SCRIPT;
String script = SCRIPT_SHEBANG_UNIX;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
script = SCRIPT_SHEBANG_BATCH;
}
TestJob job = new TestJob(script, scriptType, true);

assertEquals(Result.SUCCESS, job.build.getResult());
EnvVars vars = job.build.getEnvironment(job.listener);
assertEquals("world", vars.get("hello"));
}

public void testUTFHandling() throws Exception {
TestJob job = new TestJob(SCRIPT_UTF8, UNIX_SCRIPT, true);
String scriptType = UNIX_SCRIPT;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
}
TestJob job = new TestJob(SCRIPT_UTF8, scriptType, true);
assertEquals(Result.SUCCESS, job.build.getResult());

EnvVars vars = job.build.getEnvironment(job.listener);
Expand All @@ -130,7 +185,11 @@ public void testUTFHandling() throws Exception {

@Test
public void testHideEnvironmentVariablesValues() throws Exception {
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, UNIX_SCRIPT, true);
String scriptType = UNIX_SCRIPT;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
}
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, scriptType, true);
assertEquals(Result.SUCCESS, job.build.getResult());
List<String> logs = job.build.getLog(10);

Expand All @@ -141,7 +200,11 @@ public void testHideEnvironmentVariablesValues() throws Exception {

@Test
public void testShowEnvironmentVariablesValues() throws Exception {
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, UNIX_SCRIPT, false);
String scriptType = UNIX_SCRIPT;
if (Functions.isWindows()) {
scriptType = BATCH_SCRIPT;
}
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, scriptType, false);
assertEquals(Result.SUCCESS, job.build.getResult());
List<String> logs = job.build.getLog(10);

Expand Down

0 comments on commit 3af1a6a

Please sign in to comment.