Skip to content

Commit

Permalink
resolves JENKINS-30883 by retrieving port from EnvVars
Browse files Browse the repository at this point in the history
  • Loading branch information
apemberton committed Oct 12, 2015
1 parent 6616bbb commit bead956
Showing 1 changed file with 25 additions and 12 deletions.
Expand Up @@ -28,6 +28,8 @@
import com.saucelabs.ci.sauceconnect.AbstractSauceTunnelManager;
import com.saucelabs.hudson.HudsonSauceConnectFourManager;
import com.saucelabs.hudson.HudsonSauceManagerFactory;

import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.Util;
Expand All @@ -37,6 +39,8 @@
import hudson.tasks.BuildWrapper;
import hudson.util.Secret;
import hudson.util.VariableResolver;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.jenkins_ci.plugins.run_condition.RunCondition;
Expand Down Expand Up @@ -308,13 +312,26 @@ public Environment setUp(final AbstractBuild build, Launcher launcher, final Bui
listener.getLogger().println("Error checking Sauce Connect run condition");
throw new IOException(e);
}

EnvVars env = new EnvVars();
try {
env = build.getEnvironment(listener);
} catch (IOException e) {
listener.getLogger().println("Error getting environment variables");
throw e;
} catch (InterruptedException e) {
listener.getLogger().println("Error getting environment variables");
throw e;
}

if (canRun) {
if (launchSauceConnectOnSlave) {
listener.getLogger().println("Starting Sauce Connect on slave node using tunnel identifier: " + tunnelIdentifier);

Computer.currentComputer().getChannel().call
(new SauceConnectHandler(
this,
env,
listener,
workingDirectory,
resolvedOptions
Expand All @@ -323,7 +340,7 @@ public Environment setUp(final AbstractBuild build, Launcher launcher, final Bui
} else {
listener.getLogger().println("Starting Sauce Connect on master node using identifier: " + AbstractSauceTunnelManager.getTunnelIdentifier(resolvedOptions, "default"));
//launch Sauce Connect on the master
SauceConnectHandler sauceConnectStarter = new SauceConnectHandler(this, listener, workingDirectory, resolvedOptions);
SauceConnectHandler sauceConnectStarter = new SauceConnectHandler(this, env, listener, workingDirectory, resolvedOptions);
sauceConnectStarter.call();

}
Expand Down Expand Up @@ -384,7 +401,7 @@ public void buildEnvVars(Map<String, String> env) {
SauceEnvironmentUtil.outputEnvironmentVariable(env, SAUCE_USE_CHROME, String.valueOf(isUseChromeForAndroid()), true, verboseLogging, listener.getLogger());

DecimalFormat myFormatter = new DecimalFormat("####");
SauceEnvironmentUtil.outputEnvironmentVariable(env, SELENIUM_PORT, myFormatter.format(getPort()), true, verboseLogging, listener.getLogger());
SauceEnvironmentUtil.outputEnvironmentVariable(env, SELENIUM_PORT, myFormatter.format(getPort(env)), true, verboseLogging, listener.getLogger());

}

Expand Down Expand Up @@ -549,16 +566,12 @@ private static String getCurrentHostName() {
/**
* @return the port to be used
*/
private int getPort() {
private int getPort(Map<String, String> envVars) {
if (StringUtils.isNotBlank(seleniumPort) && !seleniumPort.equals("0")) {
Matcher matcher = ENVIRONMENT_VARIABLE_PATTERN.matcher(seleniumPort);
if (matcher.matches()) {
String variableName = matcher.group(1);
String value = System.getenv(variableName);
if (value == null) {
value = "0";
}
return Integer.parseInt(value);
return MapUtils.getInteger(envVars, variableName, 0);
} else {
return Integer.parseInt(seleniumPort);
}
Expand Down Expand Up @@ -792,12 +805,12 @@ private static final class SauceConnectHandler implements Callable<SauceConnectH
* @param workingDirectory
* @param resolvedOptions
*/
public SauceConnectHandler(SauceOnDemandBuildWrapper sauceOnDemandBuildWrapper, BuildListener listener, String workingDirectory, String resolvedOptions) {
public SauceConnectHandler(SauceOnDemandBuildWrapper sauceOnDemandBuildWrapper, EnvVars env, BuildListener listener, String workingDirectory, String resolvedOptions) {
this.options = resolvedOptions;
this.workingDirectory = workingDirectory;
this.listener = listener;
this.username = sauceOnDemandBuildWrapper.getUserName();
this.port = sauceOnDemandBuildWrapper.getPort();
this.port = sauceOnDemandBuildWrapper.getPort(env);
this.key = sauceOnDemandBuildWrapper.getApiKey();
this.verboseLogging = sauceOnDemandBuildWrapper.isVerboseLogging();
this.sauceConnectPath = sauceOnDemandBuildWrapper.getSauceConnectPath();
Expand All @@ -810,8 +823,8 @@ public SauceConnectHandler(SauceOnDemandBuildWrapper sauceOnDemandBuildWrapper,
* @param resolvedOptions
* @param sauceConnectJar
*/
public SauceConnectHandler(SauceOnDemandBuildWrapper sauceOnDemandBuildWrapper, BuildListener listener, String workingDirectory, String resolvedOptions, File sauceConnectJar) {
this(sauceOnDemandBuildWrapper, listener, workingDirectory, resolvedOptions);
public SauceConnectHandler(SauceOnDemandBuildWrapper sauceOnDemandBuildWrapper, EnvVars env, BuildListener listener, String workingDirectory, String resolvedOptions, File sauceConnectJar) {
this(sauceOnDemandBuildWrapper, env, listener, workingDirectory, resolvedOptions);
this.sauceConnectJar = sauceConnectJar;
}

Expand Down

0 comments on commit bead956

Please sign in to comment.