【问题标题】:Jenkins job failed due to tests failed. Source code for tests include AWT GUI tests, how to use Xvfb pluginJenkins 作业因测试失败而失败。测试源代码包括 AWT GUI 测试,如何使用 Xvfb 插件
【发布时间】:2013-09-05 02:10:13
【问题描述】:
  • 环境 - Linux。 sudo su - jenkins”
  • 语言 - Java
  • 项目结构
    src/java -- Java 源代码

    test/java -- Junit 单元测试

    src/java-test -- 集成测试

  • 构建系统 - Gradle

build.gradle sourceSets 定义:

sourceSets {
   main {
      java {
         srcDir 'src/java'
      }
   }
   test {
      java {
         srcDir 'test/java'
      }
   }
   integrationTest {
      java {
         srcDir 'src/java-test'
      }
   }
}
  • 假设我在某处成功签出了项目“ProjectABC”的源代码。
  • 当我运行“gradle clean build”时,我的本地机器上一切正常(Windows Win7 桌面) 使用 Cygwin 会话。 Java编译测试运行成功。

  • 当我在 Linux 终端上运行“gradle clean build”(即使用 putty Session)时,它在测试任务期间失败,但 Java 编译部分成功。

  • 当我运行“gralde clean build -x test”时,它可以工作(因为我们不包括测试任务调用)。

我在 putty 会话上使用“gradle clean build”时收到以下错误消息:

:compileTestJava
Download http://artifactory_server2:8081/artifactory/libs-thirdparty-local/junit/junit/4.11/junit-4.11.pom
Download http://artifactory_server2:8081/artifactory/libs-thirdparty-local/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom
Download http://artifactory_server2:8081/artifactory/libs-thirdparty-local/junit/junit/4.11/junit-4.11.jar
Download http://artifactory_server2:8081/artifactory/libs-thirdparty-local/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
/production/jenkinsAKS/jobs/ProjectABCUtilities/workspace/test/java/com/tr/ids/util/test/tree/TestMultiParentTree.java:5: warning: unmappable character for encoding UTF8
 * Copyright � 2005 Thomson MICROMEDEX.  All Rights Reserved.
             ^
/production/jenkinsAKS/jobs/ProjectABCUtilities/workspace/test/java/com/tr/ids/util/test/tree/TestSingleParentTree.java:5: warning: unmappable character for encoding UTF8
 * Copyright � 2005 Thomson MICROMEDEX.  All Rights Reserved.
             ^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:processTestResources UP-TO-DATE
:testClasses
:test
Download http://artifactory_server2:8081/artifactory/jcenter-cache/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002.pom
Download http://artifactory_server2:8081/artifactory/jcenter-cache/org/jacoco/org.jacoco.build/0.6.2.201302030002/org.jacoco.build-0.6.2.201302030002.pom
Download http://artifactory_server2:8081/artifactory/jcenter-cache/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002.jar
Xlib: connection to "localhost:12.0" refused by server
Xlib: PuTTY X11 proxy: wrong authorisation protocol attempted

com.tr.ids.util.test.chart.TestChartUtilities > getPieChart FAILED
    java.lang.InternalError at TestChartUtilities.java:89

com.tr.ids.util.test.chart.TestChartUtilities > getLegend FAILED
    java.lang.NoClassDefFoundError at TestChartUtilities.java:103

com.tr.ids.util.test.chart.TestChartUtilities > useString FAILED
    java.lang.NoClassDefFoundError at TestChartUtilities.java:143

140 tests completed, 3 failed
:test FAILED

FAILURE: Build failed with an exception.

正如你在上面看到的,":test" 任务最后被调用。只有这个项目 ProjectABC 有这个测试用例,其中测试源代码有一个 .java 文件,其中包含以下代码:

即/test/java/com/tr/ids/util/test/...*.java, *.html

出现问题的文件是:TestChartUtilities.java

测试源代码 - Java 文件代码快照为:。 请参见第 89、103 和 143 行/行,其中行包含:“result = ChartUtil”。关键词

package com.tr.ids.util.test.chart;

import java.awt.Color;
import java.io.UnsupportedEncodingException;

import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

import com.tr.ids.util.api.chart.ChartData;
import com.tr.ids.util.api.chart.ChartUtil;
import com.tr.ids.util.test.BaseTestCase;

..
....
......more code here ...
....
...

// #############################################################################
// Test.
// #############################################################################
/**
 * Test simple string with ampersand character.
 */
   public void getPieChart() {
      ChartData chartData = new ChartData();
      try {
         chartData.addChartItem(25, new Color(255,255,0));
         chartData.addChartItem(25, new Color(255,0,255));
         chartData.addChartItem(25, new Color(0,255,255));
      }
      catch (Exception e) {
         fail("Exception occured while building the ChartData object (probably 0 amount)");
      }
      byte[] result = null;
      try {
         result = ChartUtil.drawPieChart(chartData, 300);          // <--- line# 89 which is failing.
      } catch (Exception e) {
         fail("Exception occured trying to drawPieChart.");
      }
      assertNotNull("No pie chart returned", result);

   }

..
....
......more code here ...
....
...

/**
 * Test simple string with apostrophe and quote characters.
 */
   public void getLegend() {
      byte[] result = null;
      try {
         result = ChartUtil.drawLegend("ff0000", 40);          // <--- line# 103
      } catch (Exception e) {
         fail("Exception occured while drawing a legend box");
      }
      assertNotNull("No legend image returned", result);
   }

/**
 * Test simple string with less than and greater than characters.
 */
   public void useString() {
      ChartData chartData = new ChartData();
      try {
         chartData.addChartItem(25, new Color(255,255,0));
         chartData.addChartItem(25, new Color(255,0,255));
         chartData.addChartItem(25, new Color(0,255,255));
      }
      catch (Exception e) {
         fail("Exception occured while building the ChartData object (probably 0 amount)");
      }

      String graphDesc = null;
      try {
         graphDesc = chartData.generateStringRepresentaion(false);
      } catch (UnsupportedEncodingException e1) {
         fail("Exception occured while building the ChartData object (probably 0 amount)");
      }

      ChartData chartData2 = new ChartData();
      try {
         chartData2.loadFromString(graphDesc);
      } catch (NumberFormatException e) {
         fail("NumberFormatException thrown loading from string");
      } catch (Exception e) {
         e.printStackTrace();
         fail("Exception thrown");
      }

      byte[] result = null;
      try {
         result = ChartUtil.drawPieChart(chartData2, 300);      // <--- line# 143
      } catch (Exception e) {
         fail("Exception occured trying to drawPieChart.");
      }
      assertNotNull("No pie chart returned for chart loaded from string", result);
   }

现在,正如我之前提到的,在 Cygwin(Windows 本地计算机)中,一切正常,因为我可能通过 Cygwin 在本地计算机上拥有所有可用的图形/AWT 设置/工具。

现在,我还能错过什么???

由于我在 Jenkins 作业中也遇到了同样的错误(当通过 Putty 会话运行时),我认为安装/使用 Jenkins“Xvfb 插件”会有所帮助:

让您在每次构建时控制 Xvfb 虚拟帧缓冲区 X11 服务器。它在构建开始之前启动 Xvfb,并在构建时停止它。如果您的构建需要 X11 访问权限,例如运行需要 GUI 的测试,这将非常有用。

我按照说明配置了这个插件:https://wiki.jenkins-ci.org/display/JENKINS/Xvfb+Plugin

但是,它仍然给我一个错误。

Jenkins 日志(当我不使用 Jenkins Xvfb 插件时)显示与我进入 Putty 会话时相同的错误。

同样,当调用“gradle clean build -x test”时,Jenkins 作业成功但我必须运行“gradle clean build”(失败)。

现在,在 Jenkins 全局配置(在 Manager Jenkins > 配置系统下)和 Job 的配置级别启用 Jenkins“Xvfb”插件时,我在执行期间在 Jenkins 日志中收到以下错误:

..
....
15:30:11 Xvfb starting$ Xvfb :2 -screen 0 1024x768x24 -fbdir /production/jenkinsAKS/2013-08-23_15-30-072456509552045645846xvfb
..
...
...
:compileTestJava
Download http://artifactory_server2:8081/artifactory/libs-thirdparty-local/junit/junit/4.11/junit-4.11.pom
Download http://artifactory_server2:8081/artifactory/libs-thirdparty-local/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom
Download http://artifactory_server2:8081/artifactory/libs-thirdparty-local/junit/junit/4.11/junit-4.11.jar
Download http://artifactory_server2:8081/artifactory/libs-thirdparty-local/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
/production/jenkinsAKS/jobs/ProjectABCUtilities/workspace/test/java/com/tr/ids/util/test/tree/TestMultiParentTree.java:5: warning: unmappable character for encoding UTF8
* Copyright � 2005 Thomson MICROMEDEX.  All Rights Reserved.
                 ^
/production/jenkinsAKS/jobs/ProjectABCUtilities/workspace/test/java/com/tr/ids/util/test/tree/TestSingleParentTree.java:5: warning: unmappable character for encoding UTF8
* Copyright � 2005 Thomson MICROMEDEX.  All Rights Reserved.
                 ^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:processTestResources UP-TO-DATE
:testClasses
:test
Download http://artifactory_server2:8081/artifactory/jcenter-cache/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002.pom
Download http://artifactory_server2:8081/artifactory/jcenter-cache/org/jacoco/org.jacoco.build/0.6.2.201302030002/org.jacoco.build-0.6.2.201302030002.pom
Download http://artifactory_server2:8081/artifactory/jcenter-cache/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002.jar
15:30:51 Xlib: connection to "localhost:16.0" refused by server
15:30:51 Xlib: PuTTY X11 proxy: wrong authorisation protocol attempted
15:30:52 
com.tr.ids.util.test.chart.TestChartUtilities > getPieChart FAILED
java.lang.InternalError at TestChartUtilities.java:89

com.tr.ids.util.test.chart.TestChartUtilities > getLegend FAILED
java.lang.NoClassDefFoundError at TestChartUtilities.java:103

com.tr.ids.util.test.chart.TestChartUtilities > useString FAILED
java.lang.NoClassDefFoundError at TestChartUtilities.java:143

140 tests completed, 3 failed
:test FAILED

FAILURE: Build failed with an exception.

如你所见,这次我要排队了。

15:30:51 Xlib: connection to "localhost:16.0" refused by server
15:30:51 Xlib: PuTTY X11 proxy: wrong authorisation protocol attempted
15:30:52 

在 Xvfb 的高级配置的作业级别配置中,我在“Xvfb 特定显示名称”框中提到的值为 13 或 16(根据插件),我在测试部分看到相同的错误(正如我之前得到的),但这次与 X11 相关的错误为:

15:31:42 Xvfb starting$ Xvfb :16 -screen 0 1024x768x24 -fbdir /production/jenkinsAKS/2013-08-23_15-31-388454769980302593302xvfb
15:31:42 _XSERVTransSocketINETCreateListener: ...SocketCreateListener() failed
15:31:42 _XSERVTransMakeAllCOTSServerListeners: server already running
15:31:42 
15:31:42 Fatal server error:
15:31:42 Cannot establish any listening sockets - Make sure an X server isn't already running
15:31:42 unlink: No such file or directory
15:31:42 unlink  failed, errno 2
15:31:44 ERROR: Xvfb failed to start, consult the lines above for errors

ps -eAf|grep -i xvfb - 不显示任何在 Linux 服务器上运行的东西。

当 Jenkins 作业使用 Xvfb 插件运行时,它会在其执行期间自动启动/停止 X Display 的调用(根据 Jenkins Xvfb 帮助页面上的 Xvfb 插件功能)。

我还发现Linux机器上启用了X11转发:

# grep X11F /etc/ssh/sshd_config
X11Forwarding yes                          
#

我注意到的一件事是,我用来运行 Jenkins 的用户是:“jenkins”,一旦我以我的身份(c123456 id)登录到 Linux 机器,然后执行“sudo su - jenkins”,就会出现没有 .XAuthority 文件。

Doing: ... 确实表明它存在: ls -ltra ~c123456

-rw------- 1 c123456 devgroup   406 Aug 23 13:07 .Xauthority

但是,用户 jenkins 不存在同样的情况,即 ls -ltra ~jenkins(没有任何 .XAuthority 文件)。

Linux $DISPLAY 变量已设置,但“xclock”不起作用。我为 X11 转发启用/检查了 X11 设置(在客户端,即在目标 Linux 机器会话的 Windows 本地机器 putty 设置上)。

[c1234563@devserver1 ~]$ echo $DISPLAY 本地主机:15.0 [c1234563@devserver1 ~]$ xclock 与 localhost:15.0 的 X 连接断开(显式终止或服务器关闭)。 [c1234563@devserver1 ~]$

我尝试了这个链接,但仍然无法解决问题,按照它所说的解决它。

http://froebe.net/blog/2008/11/14/getting-xlib-putty-x11-proxy-wrong-authentication-protocol-attempted-i-have-the-answer/

此时我可能缺少什么可以帮助我

1) 使用 Putty 会话或通过 Jenkins 作业方式解决测试失败问题。

2) 我想知道 Xvfb 插件是否正在使用内存帧缓冲区执行 X Display,那么我不应该在我的本地 Windows 机器/目标 Linux 机器上安装任何 X Display 服务器/客户端,例如 XMing、XVnc/TightVnc 等。

【问题讨论】:

    标签: linux testing jenkins x11 xlib


    【解决方案1】:

    Putty 会话和 jenkins 作业可能会有 2 个略有不同的运行环境。由于它的配置方式,Putty 想使用你的远程 X 服务器,而 jenkins 可能是无头的,需要 xfvb 或类似的环境。

    为了使您的暂定槽 Putty 正常工作,您可能需要在服务器上的 /etc/ssh/sshd_configuse xauth 中添加“ForwardX11Trusted yes”。但这会使您的 GUI 出现在您的 Windows 客户端上。可能不是您真正想要的:您不希望必须将显示导出到远程服务器来运行您的自动构建。

    要修复 jenkins,我还不能 100% 确定。 Xfvb 插件失败是因为您已经在它尝试使用的端口上运行了一个服务器。您可能需要检查 netstat -ln 并搜索端口 (potentially around 6000+)。让我们开始吧。

    【讨论】:

    • 我同意。非常感谢。
    【解决方案2】:

    这是一个奇怪的答案,但我做了以下事情来解决这个问题。我知道我迟早会需要 Xvfb 插件,但是..

    1. 删除了我的工作区/jenkins 作业 - 或重命名了 jenkins 作业。删除后进行备份。
    2. 在 putty 上,gradle clean build 和 gradle clean build jacocoTestReport 有效。
    3. 在 Jenkins 中仍然失败。 一个。删除了 Jenkins Xvfb 插件 湾。重启 Jenkins 实例
    4. 问题已解决。

    正如我所说,对于 GUI 测试,我需要 Xvfb,但在执行上述步骤后,我遇到的问题现在没有出现。

    最终更新:解决方案是删除 Jenkins 作业的工作区文件夹的数据。

    【讨论】:

    • 好的,找到了解决方案。清理 Jenkins 作业的工作区(源文件被签出的地方)。 rm -fr * 在那里工作了。
    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 2012-06-10
    • 1970-01-01
    • 2017-05-21
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    相关资源
    最近更新 更多