I have a JUnit test using Assumption
to skip the test if the developer's computer doesn't have the pre-requisite software for running it. Despite being "junit", it's an integration test. Something like this:
int isSoftwarePresent = new ProcessBuilder("check software presence").start().waitFor();
Assume.assumeThat("Software not present", isSoftwarePresent, is(equalTo(0)));
However, at one point I realized the test had stopped running on the automated build on Jenkins, due to that assumption, and eventually a regression was introduced which the test was supposed to stop.
To put in other words, the required software went missing from Jenkins slave environment, which caused the test to be skipped.
The automated test is run by maven with the FailSafe plugin, on a Jenkins Pipeline build plan. How can I detect that my environment is Jenkins so that I can make the assumption condition more strict?
That is, I want the condition to be something like this:
boolean isJenkinsBuild = /* true if this is being run inside a Jenkins build, false otherwise */;
boolean isSoftwarePresent = new ProcessBuilder("check software presence").start().waitFor() == 0;
Assume.assumeTrue("Software not present", isSoftwarePresent || isJenkinsBuild);
Or even,
@Test
void testJenkinsEnvironment() {
...
Assume.assumeTrue(isJenkinsBuild);
Assert.assertTrue(isSoftwarePresent);
}
@Test
void testFeature() {
...
Assume.assumeTrue(isSoftwarePresent);
...
}
You get to the Test Result page by clicking a build link on the Status or History page of your Jenkins project, or by clicking Test Result in the menu on the left. Click the image to enlarge it. At the top of the page, Jenkins displays summary information about the executed tests and their total execution time.
You can use the Jenkins plugins for performance and Web UI testing to run tests on a Jenkins server by using a Jenkins build step.
Since they are easy to test for and passed to every program that gets executed by the build, I opted to base the check on environment variables.
Though rohit answer shows one way of setting variables on Jenkinsfile, I'd rather rely on things that Jenkins itself does, so I checked the environment variables set on my Jenkins jobs, and there's many options to pick from:
These seem to be constant on a Jenkins instance.
HUDSON_URL
JENKINS_URL
Not Jenkins-specific, but the following is also useful:
USER
Our Jenkins runs under user jenkins
, so testing that value is also a possibility.
These have constant values across builds for the same job.
JOB_URL
JOB_NAME
JOB_BASE_NAME
JOB_DISPLAY_URL
These have constant values across the same build (that is, for different sh
invocations).
BUILD_URL
BUILD_TAG
RUN_CHANGES_DISPLAY
RUN_DISPLAY
BUILD_DISPLAY_NAME
BUILD_ID
BUILD_NUMBER
These are related to the node on which the current command is being executed.
JENKINS_HOME
JENKINS_NODE_COOKIE
NODE_LABELS
NODE_NAME
This one changes with each build, may change from node to node, and may even change with Jenkinsfile configuration:
WORKSPACE
I'm not sure about these. They are definitely from Jenkins, but I don't know what their lifecycle is.
HUDSON_SERVER_COOKIE
JENKINS_SERVER_COOKIE
HUDSON_COOKIE
For my own purposes, I decided to go with JENKINS_HOME
. The name is very Jenkins-specific, and it seems more fundamental than, say, JENKINS_URL
. While it doesn't imply the build is being run by Jenkins, it will always be set in that case. I don't mind false positives, as long as I don't get false negatives.
Couple of ways you can achieve this:
You can make you application accept arguments and then pass a TRUE/FALSE value indicating it is running from Jenkins or not.
You can also read the system properties of os
e.g. System.getProperty("os.arch");
But this will not work if your Jenkins environment and your workspace are on the same machine
Like so :
Pipeline- option1
pipeline {
environment {
FROM_JENKINS= "TRUE"
} stage('test'){
sh "mvn test"
}
}
Pipeline- option2
pipeline {
stage('test'){
sh '''FROM_JENKINS="TRUE" // setting the env variable in the same shell where you are running mvn
mvn test'''
}
}
Application
boolean isJenkinsBuild = Boolean.valueOf(System.getenv("FROM_JENKINS"));
boolean isSoftwarePresent = new ProcessBuilder("check software presence").start().waitFor() == 0;
Assume.assumeTrue("Software not present", isSoftwarePresent || isJenkinsBuild);
Hope it helps :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With