Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
    ...
}
like image 735
Daniel C. Sobral Avatar asked Jul 02 '18 20:07

Daniel C. Sobral


People also ask

Where can we view the summary of the test run in Jenkins?

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.

Which method can be used to execute test cases from Jenkins?

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.


2 Answers

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.

like image 175
Daniel C. Sobral Avatar answered Sep 24 '22 06:09

Daniel C. Sobral


Couple of ways you can achieve this:

  1. You can make you application accept arguments and then pass a TRUE/FALSE value indicating it is running from Jenkins or not.

  2. 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

  1. You can just set an env variable in your pipeline(exists only in pipeline) and in the application you can read that value

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 :)

like image 24
rohit thomas Avatar answered Sep 22 '22 06:09

rohit thomas