Pax考试如何启动多个容器

Pax Exam how to start multiple containers

对于我正在进行的项目,我们有必要编写 PaxExam 集成测试,这些测试 运行 在多个 Karaf 容器上进行。

想法是找到一种方法 extend/configure PaxExam 来启动一个 Karaf 容器(或更多)并在那里部署一个 bundle 的弹跳,然后启动测试 Karaf 容器,然后测试功能。

我们需要它来验证性能测试和其他事情。

有人知道吗?这在 PaxExam 中真的可行吗?

发现这篇有趣的文章后,我自己写下答案。

请特别查看使用 Karaf ShellKaraf 中的分布式集成测试部分

http://planet.jboss.org/post/advanced_integration_testing_with_pax_exam_karaf

这篇文章基本上就是这样说的:

首先你必须改变测试探针头,允许动态包

@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
    probe.setHeader(Constants.DYNAMICIMPORT_PACKAGE, "*;status=provisional");
    return probe;
}

之后,文章建议可以在Karaf中执行命令的代码如下shell

@Inject 
CommandProcessor commandProcessor;

protected String executeCommands(final String ...commands) {
    String response;
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final PrintStream printStream = new PrintStream(byteArrayOutputStream);
    final CommandSession commandSession = commandProcessor.createSession(System.in, printStream, System.err);
    FutureTask<string> commandFuture = new FutureTask<string>(
            new Callable<string>() {
                public String call() {
                    try {
                        for(String command:commands) {
                         System.err.println(command);
                         commandSession.execute(command);
                        }
                    } catch (Exception e) {
                        e.printStackTrace(System.err);
                    }
                    return byteArrayOutputStream.toString();
                }
            });

    try {
        executor.submit(commandFuture);
        response =  commandFuture.get(COMMAND_TIMEOUT, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        e.printStackTrace(System.err);
        response = "SHELL COMMAND TIMED OUT: ";
    }

    return response;
}

然后,剩下的就有点微不足道了,你将不得不实现一个能够启动 Karaf 子实例的层

public void createInstances() {
    //Install broker feature that is provided by FuseESB
    executeCommands("admin:create --feature broker brokerChildInstance");
    //Install producer feature that provided by imaginary feature repo.
    executeCommands("admin:create --featureURL mvn:imaginary/repo/1.0/xml/features --feature producer producerChildInstance");
    //Install producer feature that provided by imaginary feature repo.
    executeCommands("admin:create --featureURL mvn:imaginary/repo/1.0/xml/features --feature consumer consumerChildInstance");

    //start child instances
    executeCommands("admin:start brokerChildInstance");
    executeCommands("admin:start producerChildInstance");
    executeCommands("admin:start consumerChildInstance");

    //You will need to destroy the child instances once you are done.
    //Using @After seems the right place to do that.
}