views:

5529

answers:

6

I am trying to put a maven2 project under continuous integration in hudson. The project uses selenium for some integration testing. Hudson is running on a headless linux. I am using xvfb to start a x server session for selenium.

In order to run the tests, I need to export an environment variable named DISPLAY. e.g.

export DISPLAY=:99

However, I don't want to set the variable on the box since it would affect all builds. I have tried to do a shell execute using the m2 extra steps plugin but it doesnt work since it is executed in a separate bash file, meaning that environment variables are not persisted.

Is there a way to register the environment variable from hudson.

+2  A: 
Rich Seller
I'm not convinced this actually answers the question that was asked - how do I set environment variables in the same shell as Maven is invoked?These parameters are quite useful, but it's not quite what the user was asking for - and it also doesn't allow you to edit environmental variables that are currently there i.e. you can't get it to call itself.For example, export PATH=/usr/bin/foo:$PATH won't work, as it does not recognise $PATH
Spedge
@Spedge you are conflating reading and setting of environment variables. The question asked how to set a variable and the mechanism described does exactly that. In your example you are doing both a set and a read, which was not asked for.
Rich Seller
@Rich Seller - Apologies sir. I have done some work on this now, and will elaborate in an answer beneath (as there's not enough room in a comment).
Spedge
+3  A: 

Hudson's Node Properties would do a good job here. But if you're looking for a more automated way of assigning the screen number, you'll need to do a little more work, perhaps tying into the port-allocator plugin, or writing a new BuildWrapper plugin which starts up xvfb automatically, and sets the SCREEN environment variable on behalf of the build.

If you're willing to switch to a different X server for testing, you can try the Xvnc plugin for Hudson. It will start the vncserver automatically, and set the SCREEN environment variable as well. It also has the ability to take a screenshot when tests are finished, and show that in the job's Hudson page.

Michael Donohue
How do you *add* (not set) to an environment variable such as `PATH`?
Sridhar Ratnakumar
@Sridhar I'd suggest starting an entirely new question for that.
Michael Donohue
A: 

As per my response to the accepted answer, here are my findings.

Beware Un-initialised Variables

When using the Parametrization feature in Hudson, you can append environmental variables to the end of a string - provided that they contain something in the first place.

For example, if the environment variable $FOO is blank, and you were to use the following line in a String Parameter...

/usr/bin/ladeda/:$F00

then the environment variable will read /usr/bin/ladeda/:$F00.

However, if I did

export F00=/usr/bin/fiddledede

then the exported variable would be....

/usr/bin/ladeda/:/usr/bin/fiddledede

When I initially ran this as a test, I had not worked this out - and thus thought the parameter feature was not accepting external environment variables, when it actually was (they just had to contain something).

Spedge
+2  A: 

fyi, I'm releasing the setenv plugin for Hudson today (assuming java.net recovers enough for me to do so!) - it behaves similarly to the parameterized build functionality, but with a simpler UI (just a textarea for input - key/value pairs are separated by newlines) and without the need to provide values for the parameters at build-time.

abayer
Worked perfectly for me.
CoverosGene
+1  A: 

I've found running selenium using xvfb-run to be more reliable than setting DISPLAY, so this might work for you. So:

xvfb-run java -jar selenium-server.jar
Paul Biggar
A: 

Have you tried to use the selenium maven plugin?

The plugin could be configured to start Xvfb, run the tests and then stop it.

using this pom.xml configuration:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>selenium-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>xvfb</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>xvfb</goal>
                </goals>
            </execution>

            <execution>
                <id>selenium</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>start-server</goal>
                </goals>
                <configuration>
                    <background>true</background>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

the details are here: http://mojo.codehaus.org/selenium-maven-plugin/examples/headless-with-xvfb.html

Sigmar