Skip to content

Commit

Permalink
1) Fixing platform verification logic to match platform family only
Browse files Browse the repository at this point in the history
2) Implementing a CLI option that disables platform verification
  • Loading branch information
barancev committed Dec 22, 2017
1 parent af370f5 commit f2eec92
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public static RegistrationRequest build(GridNodeConfiguration configuration, Str
pendingRequest.configuration.fixUpHost();
// make sure the capabilities are updated with required fields
pendingRequest.configuration.fixUpCapabilities();
pendingRequest.configuration.dropCapabilitiesThatDoenNotMatchCurrentPlatform();
pendingRequest.configuration.dropCapabilitiesThatDoesNotMatchCurrentPlatform();

return pendingRequest;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,17 @@ private static class HostPort {
)
public Integer unregisterIfStillDownAfter = DEFAULT_UNREGISTER_DELAY;

/**
* Whether or not to drop capabilities that does not belong to the current platform family
*/
@Expose
@Parameter(
names = "-enablePlatformVerification",
arity = 1,
description = "<Boolean>: Whether or not to drop capabilities that does not belong to the current platform family. Defaults to true."
)
public boolean enablePlatformVerification = true;

/**
* Creates a new configuration using the default values.
*/
Expand Down Expand Up @@ -532,22 +543,33 @@ public void fixUpCapabilities() {
capabilities = capabilities.stream()
.peek(cap -> cap.setCapability(
CapabilityType.PLATFORM,
Optional.ofNullable(cap.getCapability(CapabilityType.PLATFORM_NAME)).orElse(current)))
Optional.ofNullable(cap.getCapability(CapabilityType.PLATFORM_NAME)).orElse(
Optional.ofNullable(cap.getCapability(CapabilityType.PLATFORM)).orElse(current))))
.peek(cap -> cap.setCapability(
CapabilityType.PLATFORM_NAME,
Optional.ofNullable(cap.getCapability(CapabilityType.PLATFORM_NAME)).orElse(
Optional.ofNullable(cap.getCapability(CapabilityType.PLATFORM)).orElse(current))))
.peek(cap -> cap.setCapability(RegistrationRequest.SELENIUM_PROTOCOL,
Optional.ofNullable(cap.getCapability(RegistrationRequest.SELENIUM_PROTOCOL))
.orElse(SeleniumProtocol.WebDriver.toString())))
.peek(cap -> cap.setCapability(CONFIG_UUID_CAPABILITY, UUID.randomUUID().toString()))
.collect(Collectors.toList());
}

public void dropCapabilitiesThatDoenNotMatchCurrentPlatform() {
public void dropCapabilitiesThatDoesNotMatchCurrentPlatform() {
if (!enablePlatformVerification) {
return;
}

if (capabilities == null) {
return; // assumes the caller set it/wants it this way
}

Platform current = Platform.getCurrent();
Platform currentFamily = Optional.ofNullable(current.family()).orElse(current);
capabilities = capabilities.stream()
.filter(cap -> current.is(cap.getPlatform()))
.filter(cap -> cap.getPlatform() != null
&& (cap.getPlatform() == Platform.ANY || cap.getPlatform().is(currentFamily)))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.beust.jcommander.JCommander;

import org.junit.Assume;
import org.junit.Test;
import org.openqa.grid.common.exception.GridConfigurationException;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
Expand Down Expand Up @@ -268,7 +269,7 @@ public void testConstructorDoesNotPruneCapabilitiesWithUnknownPlatform() {
}

@Test
public void testBuilderPrunesCapabilitiesWithUnknownPlatform() {
public void testBuilderPrunesCapabilitiesWithUnknownPlatformName() {
GridNodeConfiguration config = new GridNodeConfiguration();
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("browserName", "firefox");
Expand All @@ -278,6 +279,86 @@ public void testBuilderPrunesCapabilitiesWithUnknownPlatform() {
assertEquals(req.getConfiguration().capabilities.size(), 0);
}

@Test
public void testBuilderPrunesCapabilitiesWithUnknownPlatform() {
GridNodeConfiguration config = new GridNodeConfiguration();
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("browserName", "firefox");
capabilities.setCapability("platform", "cheese");
config.capabilities = Arrays.asList(capabilities);
RegistrationRequest req = RegistrationRequest.build(config);
assertEquals(req.getConfiguration().capabilities.size(), 0);
}

@Test
public void testBuilderPrunesCapabilitiesWithPlatformThatDoesNotMatchCurrent() {
Platform current = Platform.getCurrent().family();
Platform platform = Arrays.stream(Platform.values())
.filter(p -> ! p.is(current)).findFirst().get();
GridNodeConfiguration config = new GridNodeConfiguration();
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("browserName", "firefox");
capabilities.setCapability("platform", platform);
config.capabilities = Arrays.asList(capabilities);
RegistrationRequest req = RegistrationRequest.build(config);
assertEquals(req.getConfiguration().capabilities.size(), 0);
}

@Test
public void testBuilderDoesNotPruneCapabilitiesWithPlatformThatIsEqualToCurrentFamily() {
Platform platform = Platform.getCurrent().family();
GridNodeConfiguration config = new GridNodeConfiguration();
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("browserName", "firefox");
capabilities.setCapability("platform", platform);
config.capabilities = Arrays.asList(capabilities);
RegistrationRequest req = RegistrationRequest.build(config);
assertEquals(req.getConfiguration().capabilities.size(), 1);
}

@Test
public void testBuilderDoesNotPruneCapabilitiesWithPlatformThatBelongsToCurrentFamily() {
Platform current = Platform.getCurrent().family();
Platform platform = Arrays.stream(Platform.values())
.filter(p -> p.is(current) && p != current).findFirst().orElse(null);
Assume.assumeTrue(platform != null);
GridNodeConfiguration config = new GridNodeConfiguration();
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("browserName", "firefox");
capabilities.setCapability("platform", platform);
config.capabilities = Arrays.asList(capabilities);
RegistrationRequest req = RegistrationRequest.build(config);
assertEquals(req.getConfiguration().capabilities.size(), 1);
}

@Test
public void testBuilderFixesUpPlatform() {
Platform platform = Platform.getCurrent();
GridNodeConfiguration config = new GridNodeConfiguration();
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("browserName", "firefox");
capabilities.setCapability("platformName", platform);
config.capabilities = Arrays.asList(capabilities);
RegistrationRequest req = RegistrationRequest.build(config);
assertEquals(req.getConfiguration().capabilities.size(), 1);
assertEquals(req.getConfiguration().capabilities.get(0).getCapability("platform"), platform);
assertEquals(req.getConfiguration().capabilities.get(0).getCapability("platformName"), platform);
}

@Test
public void testBuilderFixesUpPlatformName() {
Platform platform = Platform.getCurrent();
GridNodeConfiguration config = new GridNodeConfiguration();
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("browserName", "firefox");
capabilities.setCapability("platform", platform);
config.capabilities = Arrays.asList(capabilities);
RegistrationRequest req = RegistrationRequest.build(config);
assertEquals(req.getConfiguration().capabilities.size(), 1);
assertEquals(req.getConfiguration().capabilities.get(0).getCapability("platform"), platform);
assertEquals(req.getConfiguration().capabilities.get(0).getCapability("platformName"), platform);
}

private void assertConstruction(RegistrationRequest req) {
assertNotNull(req);
assertNotNull(req.getConfiguration());
Expand Down

0 comments on commit f2eec92

Please sign in to comment.