Skip to content

Commit

Permalink
[grid]: Capability se:vncEnabled value based on list of vnc-env-var
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <[email protected]>
  • Loading branch information
VietND96 committed Oct 10, 2024
1 parent 2cc7a5b commit 877e0c5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
9 changes: 6 additions & 3 deletions java/src/org/openqa/selenium/grid/node/config/NodeFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static org.openqa.selenium.grid.node.config.NodeOptions.DEFAULT_REGISTER_PERIOD;
import static org.openqa.selenium.grid.node.config.NodeOptions.DEFAULT_SESSION_TIMEOUT;
import static org.openqa.selenium.grid.node.config.NodeOptions.DEFAULT_USE_SELENIUM_MANAGER;
import static org.openqa.selenium.grid.node.config.NodeOptions.DEFAULT_VNC_ENV_VAR;
import static org.openqa.selenium.grid.node.config.NodeOptions.DEFAULT_VNC_ENV_VARS;
import static org.openqa.selenium.grid.node.config.NodeOptions.NODE_SECTION;
import static org.openqa.selenium.grid.node.config.NodeOptions.OVERRIDE_MAX_SESSIONS;

Expand Down Expand Up @@ -202,8 +202,11 @@ public class NodeFlags implements HasRoles {
description =
"Environment variable to check in order to determine if a vnc stream is "
+ "available or not.")
@ConfigValue(section = NODE_SECTION, name = "vnc-env-var", example = "SE_START_XVFB")
public String vncEnvVar = DEFAULT_VNC_ENV_VAR;
@ConfigValue(
section = NODE_SECTION,
name = "vnc-env-var",
example = "[\"SE_START_XVFB\", \"SE_START_VNC\", \"SE_START_NO_VNC\"]")
public List<String> vncEnvVar = DEFAULT_VNC_ENV_VARS;

@Parameter(
names = "--no-vnc-port",
Expand Down
15 changes: 12 additions & 3 deletions java/src/org/openqa/selenium/grid/node/config/NodeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -78,7 +79,8 @@ public class NodeOptions {
static final boolean DEFAULT_DETECT_DRIVERS = true;
static final boolean DEFAULT_USE_SELENIUM_MANAGER = false;
static final boolean OVERRIDE_MAX_SESSIONS = false;
static final String DEFAULT_VNC_ENV_VAR = "SE_START_XVFB";
static final List<String> DEFAULT_VNC_ENV_VARS =
Arrays.asList("SE_START_XVFB", "SE_START_VNC", "SE_START_NO_VNC");
static final int DEFAULT_NO_VNC_PORT = 7900;
static final int DEFAULT_REGISTER_CYCLE = 10;
static final int DEFAULT_REGISTER_PERIOD = 120;
Expand Down Expand Up @@ -286,9 +288,16 @@ public int getDrainAfterSessionCount() {

@VisibleForTesting
boolean isVncEnabled() {
String vncEnvVar = config.get(NODE_SECTION, "vnc-env-var").orElse(DEFAULT_VNC_ENV_VAR);
List<String> vncEnvVars = DEFAULT_VNC_ENV_VARS;
if (config.getAll(NODE_SECTION, "vnc-env-var").isPresent()) {
vncEnvVars = config.getAll(NODE_SECTION, "vnc-env-var").get();
}
if (!vncEnabledValueSet.getAndSet(true)) {
vncEnabled.set(Boolean.parseBoolean(System.getenv(vncEnvVar)));
boolean allEnabled =
vncEnvVars.stream()
.allMatch(
env -> "true".equalsIgnoreCase(System.getProperty(env, System.getenv(env))));
vncEnabled.set(allEnabled);
}
return vncEnabled.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,50 @@ void settingSlotMatcherAvailable() {
assertThat(nodeOptions.getSlotMatcher()).isExactlyInstanceOf(YesSlotMatcher.class);
}

@Test
void testIsVncEnabledAcceptListEnvVarsAndReturnTrue() {
System.setProperty("SE_START_XVFB", "true");
System.setProperty("SE_START_VNC", "true");
System.setProperty("SE_START_NO_VNC", "true");
String[] rawConfig =
new String[] {
"[node]", "vnc-env-var = [\"SE_START_XVFB\", \"SE_START_VNC\", \"SE_START_NO_VNC\"]",
};
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
NodeOptions nodeOptionsEnabled = new NodeOptions(config);
assertThat(config.getAll("node", "vnc-env-var").get())
.containsExactly("SE_START_XVFB", "SE_START_VNC", "SE_START_NO_VNC");
assertThat(nodeOptionsEnabled.isVncEnabled()).isTrue();
}

@Test
void testIsVncEnabledAcceptListEnvVarsAndReturnFalse() {
System.setProperty("SE_START_XVFB", "true");
System.setProperty("SE_START_VNC", "false");
String[] rawConfig =
new String[] {
"[node]", "vnc-env-var = [\"SE_START_XVFB\", \"SE_START_VNC\", \"SE_START_NO_VNC\"]",
};
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
NodeOptions nodeOptionsEnabled = new NodeOptions(config);
assertThat(config.getAll("node", "vnc-env-var").get())
.containsExactly("SE_START_XVFB", "SE_START_VNC", "SE_START_NO_VNC");
assertThat(nodeOptionsEnabled.isVncEnabled()).isFalse();
}

@Test
void testIsVncEnabledAcceptSingleEnvVar() {
System.setProperty("SE_START_XVFB", "false");
String[] rawConfig =
new String[] {
"[node]", "vnc-env-var = \"SE_START_XVFB\"",
};
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
NodeOptions nodeOptionsEnabled = new NodeOptions(config);
assertThat(config.getAll("node", "vnc-env-var").get()).containsExactly("SE_START_XVFB");
assertThat(nodeOptionsEnabled.isVncEnabled()).isFalse();
}

private Condition<? super List<? extends Capabilities>> supporting(String name) {
return new Condition<>(
caps -> caps.stream().anyMatch(cap -> name.equals(cap.getBrowserName())),
Expand Down

0 comments on commit 877e0c5

Please sign in to comment.