Skip to content

Commit 779ed15

Browse files
committed
Fixing processing of -hub, -hubHost and -hubPort options, -hub should have precedence. Fixes #5219
1 parent 2e5358a commit 779ed15

File tree

2 files changed

+110
-27
lines changed

2 files changed

+110
-27
lines changed

java/server/src/org/openqa/grid/internal/utils/configuration/GridNodeConfiguration.java

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ static List<MutableCapabilities> getCapabilities() {
132132
}
133133
}
134134

135+
private static class HostPort {
136+
final String host;
137+
final int port;
138+
139+
HostPort(String host, int port) {
140+
this.host = host;
141+
this.port = port;
142+
}
143+
}
144+
145+
private HostPort hubHostPort;
146+
135147
/*
136148
* config parameters which do not serialize or de-serialize
137149
*/
@@ -228,11 +240,13 @@ static List<MutableCapabilities> getCapabilities() {
228240
/**
229241
* The hub url. Defaults to {@code http://localhost:4444}.
230242
*/
231-
@Expose
232243
@Parameter(
233244
names = "-hub",
234245
description = "<String> : the url that will be used to post the registration request. This option takes precedence over -hubHost and -hubPort options."
235246
)
247+
private String hubOption;
248+
249+
@Expose
236250
public String hub = DEFAULT_HUB;
237251

238252
/**
@@ -307,23 +321,42 @@ public GridNodeConfiguration() {
307321
}
308322

309323
public String getHubHost() {
310-
if (hubHost == null) {
311-
if (hub == null) {
312-
throw new RuntimeException("You must specify either a hubHost or hub parameter.");
313-
}
314-
parseHubUrl();
315-
}
316-
return hubHost;
324+
return getHubHostPort().host;
317325
}
318326

319327
public Integer getHubPort() {
320-
if (hubPort == null) {
321-
if (hub == null) {
322-
throw new RuntimeException("You must specify either a hubPort or hub parameter.");
328+
return getHubHostPort().port;
329+
}
330+
331+
private HostPort getHubHostPort() {
332+
if (hubHostPort == null) { // parse options
333+
// -hub has precedence
334+
if (hubOption != null) {
335+
hub = hubOption;
336+
try {
337+
URL u = new URL(hub);
338+
hubHostPort = new HostPort(u.getHost(), u.getPort());
339+
} catch (MalformedURLException mURLe) {
340+
throw new RuntimeException("-hub must be a valid url: " + hub, mURLe);
341+
}
342+
} else if (hubHost != null || hubPort != null) {
343+
if (hubHost == null) {
344+
throw new RuntimeException("You must specify either a -hubHost or -hub parameter.");
345+
}
346+
if (hubPort == null) {
347+
throw new RuntimeException("You must specify either a -hubPort or -hub parameter.");
348+
}
349+
hubHostPort = new HostPort(hubHost, hubPort);
350+
} else {
351+
try {
352+
URL u = new URL(hub);
353+
hubHostPort = new HostPort(u.getHost(), u.getPort());
354+
} catch (MalformedURLException mURLe) {
355+
throw new RuntimeException("-hub must be a valid url: " + hub, mURLe);
356+
}
323357
}
324-
parseHubUrl();
325358
}
326-
return hubPort;
359+
return hubHostPort;
327360
}
328361

329362
public String getRemoteHost() {
@@ -339,16 +372,6 @@ public String getRemoteHost() {
339372
return remoteHost;
340373
}
341374

342-
private void parseHubUrl() {
343-
try {
344-
URL u = new URL(hub);
345-
hubHost = u.getHost();
346-
hubPort = u.getPort();
347-
} catch (MalformedURLException mURLe) {
348-
throw new RuntimeException("-hub must be a valid url: " + hub, mURLe);
349-
}
350-
}
351-
352375
public void merge(GridNodeConfiguration other) {
353376
if (other == null) {
354377
return;

java/server/test/org/openqa/grid/internal/utils/configuration/GridNodeConfigurationTest.java

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertNotNull;
2323
import static org.junit.Assert.assertNull;
2424
import static org.junit.Assert.assertSame;
25+
import static org.junit.Assert.assertThat;
2526
import static org.junit.Assert.assertTrue;
2627

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

3132
import com.beust.jcommander.JCommander;
3233

34+
import org.hamcrest.CoreMatchers;
3335
import org.junit.Test;
3436
import org.openqa.grid.common.exception.GridConfigurationException;
3537
import org.openqa.selenium.Platform;
3638
import org.openqa.selenium.remote.DesiredCapabilities;
39+
import org.openqa.selenium.testing.TestUtilities;
3740

3841
import java.util.Arrays;
3942

@@ -146,7 +149,6 @@ public void testConstructorEqualsDefaultConfig() {
146149
assertEquals(expected.nodeConfigFile, actual.nodeConfigFile);
147150
assertEquals(expected.unregisterIfStillDownAfter, actual.unregisterIfStillDownAfter);
148151

149-
150152
assertEquals(expected.cleanUpCycle, actual.cleanUpCycle);
151153
assertEquals(expected.host, actual.host);
152154
assertEquals(expected.maxSession, actual.maxSession);
@@ -217,16 +219,74 @@ public void testWithCapabilitiesArgsWithExtraSpacing() {
217219

218220
@Test
219221
public void testGetHubHost() {
222+
final String[] args = new String[]{"-hubHost", "dummyhost", "-hubPort", "1234"};
220223
GridNodeConfiguration gnc = new GridNodeConfiguration();
221-
gnc.hub = "http://dummyhost:4444/wd/hub";
224+
new JCommander(gnc, args);
222225
assertEquals("dummyhost", gnc.getHubHost());
223226
}
224227

228+
@Test
229+
public void testGetHubHostFromHubOption() {
230+
final String[] args = new String[]{"-hub", "http://dummyhost:1234/wd/hub"};
231+
GridNodeConfiguration gnc = new GridNodeConfiguration();
232+
new JCommander(gnc, args);
233+
assertEquals("dummyhost", gnc.getHubHost());
234+
}
235+
236+
@Test
237+
public void testOneOfHubOrHubHostShouldBePresent() {
238+
final String[] args = new String[]{"-hubPort", "1234"};
239+
GridNodeConfiguration gnc = new GridNodeConfiguration();
240+
new JCommander(gnc, args);
241+
Throwable t = TestUtilities.catchThrowable(gnc::getHubHost);
242+
assertThat(t, CoreMatchers.instanceOf(RuntimeException.class));
243+
t = TestUtilities.catchThrowable(gnc::getHubPort);
244+
assertThat(t, CoreMatchers.instanceOf(RuntimeException.class));
245+
}
246+
247+
@Test
248+
public void testHubOptionHasPrecedenceOverHubHost() {
249+
final String[] args = new String[]{"-hub", "http://smarthost:4321/wd/hub",
250+
"-hubHost", "dummyhost", "-hubPort", "1234"};
251+
GridNodeConfiguration gnc = new GridNodeConfiguration();
252+
new JCommander(gnc, args);
253+
assertEquals("smarthost", gnc.getHubHost());
254+
}
255+
225256
@Test
226257
public void testGetHubPort() {
258+
final String[] args = new String[]{"-hubHost", "dummyhost", "-hubPort", "1234"};
227259
GridNodeConfiguration gnc = new GridNodeConfiguration();
228-
gnc.hub = "http://dummyhost:4444/wd/hub";
229-
assertEquals(4444, gnc.getHubPort().intValue());
260+
new JCommander(gnc, args);
261+
assertEquals(1234, gnc.getHubPort().intValue());
262+
}
263+
264+
@Test
265+
public void testGetHubPortFromHubOption() {
266+
final String[] args = new String[]{"-hub", "http://dummyhost:1234/wd/hub"};
267+
GridNodeConfiguration gnc = new GridNodeConfiguration();
268+
new JCommander(gnc, args);
269+
assertEquals(1234, gnc.getHubPort().intValue());
270+
}
271+
272+
@Test
273+
public void testOneOfHubOrHubPortShouldBePresent() {
274+
final String[] args = new String[]{"-hubHost", "dummyhost"};
275+
GridNodeConfiguration gnc = new GridNodeConfiguration();
276+
new JCommander(gnc, args);
277+
Throwable t = TestUtilities.catchThrowable(gnc::getHubHost);
278+
assertThat(t, CoreMatchers.instanceOf(RuntimeException.class));
279+
t = TestUtilities.catchThrowable(gnc::getHubPort);
280+
assertThat(t, CoreMatchers.instanceOf(RuntimeException.class));
281+
}
282+
283+
@Test
284+
public void testHubOptionHasPrecedenceOverHubPort() {
285+
final String[] args = new String[]{"-hub", "http://smarthost:4321/wd/hub",
286+
"-hubHost", "dummyhost", "-hubPort", "1234"};
287+
GridNodeConfiguration gnc = new GridNodeConfiguration();
288+
new JCommander(gnc, args);
289+
assertEquals(4321, gnc.getHubPort().intValue());
230290
}
231291

232292
@Test

0 commit comments

Comments
 (0)