How do I detect my test is running on a Jenkins environment?

Daniel C. Sobral picture Daniel C. Sobral · Jul 2, 2018 · Viewed 8.3k times · Source

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);
    ...
}

Answer

Daniel C. Sobral picture Daniel C. Sobral · Aug 30, 2018

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:

General Jenkins Information

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.

Job Information

These have constant values across builds for the same job.

  • JOB_URL
  • JOB_NAME
  • JOB_BASE_NAME
  • JOB_DISPLAY_URL

Build Information

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

Node Information

These are related to the node on which the current command is being executed.

  • JENKINS_HOME
  • JENKINS_NODE_COOKIE
  • NODE_LABELS
  • NODE_NAME

Special

This one changes with each build, may change from node to node, and may even change with Jenkinsfile configuration:

  • WORKSPACE

Miscellaneous

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

Final Considerations

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.