Skip to content

Commit 8905eb2

Browse files
committed
[grid] Making only name and stereotype be mandatory in nodeConfig
This is preparation work to implement #9592, as not all fields should be mandatory.
1 parent e571f89 commit 8905eb2

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

java/src/org/openqa/selenium/grid/node/config/NodeOptions.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.ArrayList;
4848
import java.util.Collection;
4949
import java.util.Comparator;
50+
import java.util.HashMap;
5051
import java.util.List;
5152
import java.util.Map;
5253
import java.util.Optional;
@@ -243,11 +244,16 @@ private void addDriverConfigs(
243244
Function<Capabilities, Collection<SessionFactory>> factoryFactory,
244245
ImmutableMultimap.Builder<Capabilities, SessionFactory> sessionFactories) {
245246
Multimap<WebDriverInfo, SessionFactory> driverConfigs = HashMultimap.create();
246-
int configElements = 3;
247247
config.getAll(NODE_SECTION, "driver-configuration").ifPresent(drivers -> {
248-
if (drivers.size() % configElements != 0) {
249-
throw new ConfigException("Expected each driver config to have three elements " +
250-
"(name, stereotype and max-sessions)");
248+
/*
249+
The four accepted keys are: name, max-sessions, stereotype, webdriver-executable. The
250+
mandatory keys are name and stereotype. When configs are read, they keys always come
251+
alphabetically ordered. This means that we know a new config is present when we find
252+
the "name" key again.
253+
*/
254+
255+
if (drivers.size() == 0) {
256+
throw new ConfigException("No driver configs were found!");
251257
}
252258

253259
drivers.stream()
@@ -260,19 +266,24 @@ private void addDriverConfigs(
260266
"required 'key=value' structure");
261267
});
262268

269+
// Find all indexes where "name" is present, as it marks the start of a config
270+
int[] configIndexes = IntStream.range(0, drivers.size())
271+
.filter(index -> drivers.get(index).startsWith("name")).toArray();
272+
273+
if (configIndexes.length == 0) {
274+
throw new ConfigException("No 'name' keyword was found in the provided configs!");
275+
}
276+
263277
List<Map<String, String>> driversMap = new ArrayList<>();
264-
IntStream.range(0, drivers.size()/configElements).boxed()
265-
.forEach(i -> {
266-
ImmutableMap<String, String> configMap = ImmutableMap.of(
267-
drivers.get(i*configElements).split("=")[0],
268-
drivers.get(i*configElements).split("=")[1],
269-
drivers.get(i*configElements+1).split("=")[0],
270-
drivers.get(i*configElements+1).split("=")[1],
271-
drivers.get(i*configElements+2).split("=")[0],
272-
drivers.get(i*configElements+2).split("=")[1]
273-
);
274-
driversMap.add(configMap);
278+
for (int i = 0; i < configIndexes.length; i++) {
279+
int fromIndex = configIndexes[i];
280+
int toIndex = (i + 1) >= configIndexes.length ? drivers.size() : configIndexes[i + 1];
281+
Map<String, String> configMap = new HashMap<>();
282+
drivers.subList(fromIndex, toIndex).forEach(keyValue -> {
283+
configMap.put(keyValue.split("=")[0], keyValue.split("=")[1]);
275284
});
285+
driversMap.add(configMap);
286+
}
276287

277288
List<DriverService.Builder<?, ?>> builders = new ArrayList<>();
278289
ServiceLoader.load(DriverService.Builder.class).forEach(builders::add);

java/test/org/openqa/selenium/grid/node/config/NodeOptionsTest.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717

1818
package org.openqa.selenium.grid.node.config;
1919

20+
import static java.util.Collections.emptyMap;
21+
import static java.util.Collections.emptySet;
22+
import static java.util.Collections.singletonMap;
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.junit.Assert.fail;
25+
import static org.junit.Assume.assumeFalse;
26+
import static org.junit.Assume.assumeTrue;
27+
2028
import com.google.common.collect.ImmutableMap;
2129

2230
import org.assertj.core.api.Condition;
@@ -47,14 +55,6 @@
4755
import java.util.List;
4856
import java.util.Map;
4957

50-
import static java.util.Collections.emptyMap;
51-
import static java.util.Collections.emptySet;
52-
import static java.util.Collections.singletonMap;
53-
import static org.assertj.core.api.Assertions.assertThat;
54-
import static org.junit.Assert.fail;
55-
import static org.junit.Assume.assumeFalse;
56-
import static org.junit.Assume.assumeTrue;
57-
5858
@SuppressWarnings("DuplicatedCode")
5959
public class NodeOptionsTest {
6060

@@ -334,6 +334,31 @@ public void driversConfigNeedsStereotypeField() {
334334
assertThat(reported).isEmpty();
335335
}
336336

337+
@Test
338+
public void maxSessionsFieldIsOptionalInDriversConfig() {
339+
String[] rawConfig = new String[]{
340+
"[node]",
341+
"detect-drivers = false",
342+
"[[node.driver-configuration]]",
343+
"name = \"Chrome Beta\"",
344+
"stereotype = '{\"browserName\": \"chrome\"}'",
345+
"[[node.driver-configuration]]",
346+
"name = \"Firefox Nightly\"",
347+
"stereotype = '{\"browserName\": \"firefox\"}'",
348+
};
349+
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
350+
351+
List<Capabilities> reported = new ArrayList<>();
352+
new NodeOptions(config).getSessionFactories(capabilities -> {
353+
reported.add(capabilities);
354+
return Collections.singleton(HelperFactory.create(config, capabilities));
355+
});
356+
357+
assertThat(reported).is(supporting("chrome"));
358+
assertThat(reported).is(supporting("firefox"));
359+
}
360+
361+
337362
@Test
338363
public void shouldNotOverrideMaxSessionsByDefault() {
339364
assumeTrue("ChromeDriver needs to be available", new ChromeDriverInfo().isAvailable());

0 commit comments

Comments
 (0)