Skip to content

Commit

Permalink
Fixing processing of -hub, -hubHost and -hubPort options, -hub should…
Browse files Browse the repository at this point in the history
… have precedence. Fixes #5219
  • Loading branch information
barancev committed Dec 14, 2017
1 parent 2e5358a commit 779ed15
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ static List<MutableCapabilities> getCapabilities() {
}
}

private static class HostPort {
final String host;
final int port;

HostPort(String host, int port) {
this.host = host;
this.port = port;
}
}

private HostPort hubHostPort;

/*
* config parameters which do not serialize or de-serialize
*/
Expand Down Expand Up @@ -228,11 +240,13 @@ static List<MutableCapabilities> getCapabilities() {
/**
* The hub url. Defaults to {@code http://localhost:4444}.
*/
@Expose
@Parameter(
names = "-hub",
description = "<String> : the url that will be used to post the registration request. This option takes precedence over -hubHost and -hubPort options."
)
private String hubOption;

This comment has been minimized.

Copy link
@kool79

kool79 Dec 14, 2017

Contributor

@barancev, hubOption is redundant. It was never set.

This comment has been minimized.

Copy link
@barancev

barancev Dec 15, 2017

Author Member

JCommander sets this property when it's parsing CLI args.


@Expose
public String hub = DEFAULT_HUB;

/**
Expand Down Expand Up @@ -307,23 +321,42 @@ public GridNodeConfiguration() {
}

public String getHubHost() {
if (hubHost == null) {
if (hub == null) {
throw new RuntimeException("You must specify either a hubHost or hub parameter.");
}
parseHubUrl();
}
return hubHost;
return getHubHostPort().host;
}

public Integer getHubPort() {
if (hubPort == null) {
if (hub == null) {
throw new RuntimeException("You must specify either a hubPort or hub parameter.");
return getHubHostPort().port;
}

private HostPort getHubHostPort() {
if (hubHostPort == null) { // parse options
// -hub has precedence
if (hubOption != null) {
hub = hubOption;
try {
URL u = new URL(hub);
hubHostPort = new HostPort(u.getHost(), u.getPort());
} catch (MalformedURLException mURLe) {
throw new RuntimeException("-hub must be a valid url: " + hub, mURLe);
}
} else if (hubHost != null || hubPort != null) {
if (hubHost == null) {
throw new RuntimeException("You must specify either a -hubHost or -hub parameter.");
}
if (hubPort == null) {
throw new RuntimeException("You must specify either a -hubPort or -hub parameter.");
}
hubHostPort = new HostPort(hubHost, hubPort);
} else {
try {
URL u = new URL(hub);
hubHostPort = new HostPort(u.getHost(), u.getPort());
} catch (MalformedURLException mURLe) {
throw new RuntimeException("-hub must be a valid url: " + hub, mURLe);

This comment has been minimized.

Copy link
@kool79

kool79 Dec 15, 2017

Contributor

With current implementation when parameter hub is defined inside json, it still have no precedence over hubHost and hubPort (when both defined). It is expected?
Also I think we'll get wrong RuntimeException("You must specify either ...") when json has hub and one of the hubHost or hubPort.

This comment has been minimized.

Copy link
@barancev

barancev Dec 15, 2017

Author Member

CLI options has precedence over JSON file, it's expected.
To be honest, we'd better deprecate and remove -hubHost and -hubPort CLI options.
But it takes time to delete legacy features...

This comment has been minimized.

Copy link
@kool79

kool79 Dec 15, 2017

Contributor

On the other hand we can have ambiguous situation when we define hubPort, hubHost, hub in different combination in json and CLI. Which one has higher priority?

CLI json who wins?
hub, hubHost, hubPort <none> hub
<none> hub, hubHost, hubPort ?? hub (I think)
hubHost, hubPort hub ?? CLI should win (I think)

This comment has been minimized.

Copy link
@barancev

barancev Dec 15, 2017

Author Member

That's why I want to remove them :)))

}
}
parseHubUrl();
}
return hubPort;
return hubHostPort;
}

public String getRemoteHost() {
Expand All @@ -339,16 +372,6 @@ public String getRemoteHost() {
return remoteHost;
}

private void parseHubUrl() {
try {
URL u = new URL(hub);
hubHost = u.getHost();
hubPort = u.getPort();
} catch (MalformedURLException mURLe) {
throw new RuntimeException("-hub must be a valid url: " + hub, mURLe);
}
}

public void merge(GridNodeConfiguration other) {
if (other == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableMap;
Expand All @@ -30,10 +31,12 @@

import com.beust.jcommander.JCommander;

import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.openqa.grid.common.exception.GridConfigurationException;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.testing.TestUtilities;

import java.util.Arrays;

Expand Down Expand Up @@ -146,7 +149,6 @@ public void testConstructorEqualsDefaultConfig() {
assertEquals(expected.nodeConfigFile, actual.nodeConfigFile);
assertEquals(expected.unregisterIfStillDownAfter, actual.unregisterIfStillDownAfter);


assertEquals(expected.cleanUpCycle, actual.cleanUpCycle);
assertEquals(expected.host, actual.host);
assertEquals(expected.maxSession, actual.maxSession);
Expand Down Expand Up @@ -217,16 +219,74 @@ public void testWithCapabilitiesArgsWithExtraSpacing() {

@Test
public void testGetHubHost() {
final String[] args = new String[]{"-hubHost", "dummyhost", "-hubPort", "1234"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
gnc.hub = "http://dummyhost:4444/wd/hub";
new JCommander(gnc, args);
assertEquals("dummyhost", gnc.getHubHost());
}

@Test
public void testGetHubHostFromHubOption() {
final String[] args = new String[]{"-hub", "http://dummyhost:1234/wd/hub"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
new JCommander(gnc, args);
assertEquals("dummyhost", gnc.getHubHost());
}

@Test
public void testOneOfHubOrHubHostShouldBePresent() {
final String[] args = new String[]{"-hubPort", "1234"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
new JCommander(gnc, args);
Throwable t = TestUtilities.catchThrowable(gnc::getHubHost);
assertThat(t, CoreMatchers.instanceOf(RuntimeException.class));
t = TestUtilities.catchThrowable(gnc::getHubPort);
assertThat(t, CoreMatchers.instanceOf(RuntimeException.class));
}

@Test
public void testHubOptionHasPrecedenceOverHubHost() {
final String[] args = new String[]{"-hub", "http://smarthost:4321/wd/hub",
"-hubHost", "dummyhost", "-hubPort", "1234"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
new JCommander(gnc, args);
assertEquals("smarthost", gnc.getHubHost());
}

@Test
public void testGetHubPort() {
final String[] args = new String[]{"-hubHost", "dummyhost", "-hubPort", "1234"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
gnc.hub = "http://dummyhost:4444/wd/hub";
assertEquals(4444, gnc.getHubPort().intValue());
new JCommander(gnc, args);
assertEquals(1234, gnc.getHubPort().intValue());
}

@Test
public void testGetHubPortFromHubOption() {
final String[] args = new String[]{"-hub", "http://dummyhost:1234/wd/hub"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
new JCommander(gnc, args);
assertEquals(1234, gnc.getHubPort().intValue());
}

@Test
public void testOneOfHubOrHubPortShouldBePresent() {
final String[] args = new String[]{"-hubHost", "dummyhost"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
new JCommander(gnc, args);
Throwable t = TestUtilities.catchThrowable(gnc::getHubHost);
assertThat(t, CoreMatchers.instanceOf(RuntimeException.class));
t = TestUtilities.catchThrowable(gnc::getHubPort);
assertThat(t, CoreMatchers.instanceOf(RuntimeException.class));
}

@Test
public void testHubOptionHasPrecedenceOverHubPort() {
final String[] args = new String[]{"-hub", "http://smarthost:4321/wd/hub",
"-hubHost", "dummyhost", "-hubPort", "1234"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
new JCommander(gnc, args);
assertEquals(4321, gnc.getHubPort().intValue());
}

@Test
Expand Down

0 comments on commit 779ed15

Please sign in to comment.