简体   繁体   English

如何检测我的测试是否在Jenkins环境中运行?

[英]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. 如果开发人员的计算机没有运行它的必备软件,我使用Assumption跳过测试的JUnit测试。 Despite being "junit", it's an integration test. 尽管是“junit”,但它是一个集成测试。 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. 但是,有一次我意识到测试已经停止在Jenkins的自动构建上运行,由于这个假设,并且最终引入了测试应该停止的回归。

To put in other words, the required software went missing from Jenkins slave environment, which caused the test to be skipped. 换句话说,Jenkins从属环境中缺少所需的软件,导致测试被跳过。

The automated test is run by maven with the FailSafe plugin, on a Jenkins Pipeline build plan. 在Jenkins Pipeline构建计划中,maven使用FailSafe插件运行自动化测试。 How can I detect that my environment is Jenkins so that I can make the assumption condition more strict? 如何检测我的环境是Jenkins,以便我可以使假设条件更严格?

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

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: 虽然rohit回答显示了在Jenkinsfile上设置变量的一种方法,但我宁愿依赖Jenkins本身所做的事情,所以我检查了我的Jenkins作业上设置的环境变量,并且有很多选择可供选择:

General Jenkins Information 詹金斯将军信息

These seem to be constant on a Jenkins instance. 这些似乎在Jenkins实例上是不变的。

  • HUDSON_URL
  • JENKINS_URL

Not Jenkins-specific, but the following is also useful: 不是Jenkins特有的,但以下内容也很有用:

  • USER

Our Jenkins runs under user jenkins , so testing that value is also a possibility. 我们的Jenkins在用户jenkins下运行,因此测试该值也是可能的。

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). 它们在同一个构建中具有常量值(即,对于不同的sh调用)。

  • 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: 这个随每个构建而变化,可能会在节点之间变化,甚至可能随Jenkinsfile配置而变化:

  • 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 . 出于我自己的目的,我决定和JENKINS_HOME一起去。 The name is very Jenkins-specific, and it seems more fundamental than, say, JENKINS_URL . 这个名字非常特定于Jenkins,它似乎比JENKINS_URL更基础。 While it doesn't imply the build is being run by Jenkins, it will always be set in that case. 虽然它并不意味着构建是由Jenkins运行的,但在这种情况下它总是会被设置。 I don't mind false positives, as long as I don't get false negatives. 我不介意误报,只要我没有得到假阴性。

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. 您可以使应用程序接受参数,然后传递一个TRUE / FALSE值,表明它是从Jenkins运行的。

  2. You can also read the system properties of os 您还可以读取操作系统的系统属性

eg System.getProperty("os.arch"); 例如System.getProperty(“os.arch”);

But this will not work if your Jenkins environment and your workspace are on the same machine 但是,如果您的Jenkins环境和工作区位于同一台计算机上,则无法使用此功能

  1. You can just set an env variable in your pipeline(exists only in pipeline) and in the application you can read that value 您可以在管道中设置一个env变量(仅存在于管道中),在应用程序中可以读取该值

Like so : 像这样:

Pipeline- option1 Pipeline- option1

     pipeline {
            environment {
                FROM_JENKINS= "TRUE" 
            } stage('test'){ 

                   sh "mvn test"
             }
        }

Pipeline- option2 管道 - 选项2

     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 :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:如果我的程序实例正在运行,我该如何检测它,然后关闭旧的程序 - Java: If I have a instance of my program running, how do I detect that, and then close the old one(s) 如何在分布式Jenkins构建环境中管理Java证书? - How do I manage java certificates on a distributed Jenkins build environment? 运行Selenium测试对Jenkins不起作用 - Running Selenium test do not work on Jenkins 如何在 Mac 上使用 Jenkins 运行 selenium 测试用例? - How do I run a selenium test case using Jenkins, on a mac? 如何让我的Spring-JUnit测试认为它在GenericApplicationContext中运行? - How do I get my Spring-JUnit test to think its running in a GenericApplicationContext? 为什么在ActionBarActivity上运行测试时会出现NoClassDefFoundError? - Why do I get a NoClassDefFoundError when running my test on an ActionBarActivity? 如何识别TestNG测试是否在Jenkins上运行? - How to identify whether TestNG test is running on Jenkins or not? Jenkins和Maven:如何在运行测试之前将一些文件打包到JAR中并解压缩? - Jenkins and Maven : how to package some files in my JAR and unpack them before running a test? 如何检测测试是否在 GitLab CI 服务器上运行 - How to detect if test is running on GitLab CI server 如何对MainActivity进行单元测试? - How do I unit test my MainActivity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM