Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] start the secure server only when needed in unit tests #14717

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions java/test/org/openqa/selenium/CookieImplementationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
import org.openqa.selenium.environment.DomainHelper;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsSecureServer;
import org.openqa.selenium.testing.NotWorkingInRemoteBazelBuilds;
import org.openqa.selenium.testing.NotYetImplemented;
import org.openqa.selenium.testing.SwitchToTopAfterTest;

@NeedsSecureServer
class CookieImplementationTest extends JupiterTestBase {

private DomainHelper domainHelper;
Expand Down
2 changes: 2 additions & 0 deletions java/test/org/openqa/selenium/PageLoadingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsFreshDriver;
import org.openqa.selenium.testing.NeedsSecureServer;
import org.openqa.selenium.testing.NoDriverAfterTest;
import org.openqa.selenium.testing.NotYetImplemented;
import org.openqa.selenium.testing.SwitchToTopAfterTest;

@NeedsSecureServer
class PageLoadingTest extends JupiterTestBase {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class InProcessTestEnvironment implements TestEnvironment {

private final AppServer appServer;

public InProcessTestEnvironment() {
appServer = new NettyAppServer();
public InProcessTestEnvironment(boolean secureServer) {
appServer = new NettyAppServer(secureServer);
appServer.start();
}

Expand All @@ -40,6 +40,6 @@ public void stop() {
}

public static void main(String[] args) {
new InProcessTestEnvironment();
new InProcessTestEnvironment(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public class NettyAppServer implements AppServer {
LOG.log(
Level.WARNING,
String.format("NettyAppServer retry #%s. ", e.getAttemptCount()));
initValues();
initValues(secure != null);
})
.onRetriesExceeded(e -> LOG.log(Level.WARNING, "NettyAppServer start aborted."))
.build();

public NettyAppServer() {
initValues();
public NettyAppServer(boolean secureServer) {
initValues(secureServer);
}

public NettyAppServer(HttpHandler handler) {
Expand Down Expand Up @@ -116,7 +116,7 @@ public static void main(String[] args) {
System.out.printf("Server started. Root URL: %s%n", server.whereIs("/"));
}

private void initValues() {
private void initValues(boolean secureServer) {
Config config = createDefaultConfig();
BaseServerOptions options = new BaseServerOptions(config);

Expand All @@ -128,16 +128,18 @@ private void initValues() {

server = new NettyServer(options, handler);

Config secureConfig = new CompoundConfig(sslConfig, createDefaultConfig());
BaseServerOptions secureOptions = new BaseServerOptions(secureConfig);
if (secureServer) {
Config secureConfig = new CompoundConfig(sslConfig, createDefaultConfig());
BaseServerOptions secureOptions = new BaseServerOptions(secureConfig);

HttpHandler secureHandler =
new HandlersForTests(
secureOptions.getHostname().orElse("localhost"),
secureOptions.getPort(),
tempDir.toPath());
HttpHandler secureHandler =
new HandlersForTests(
secureOptions.getHostname().orElse("localhost"),
secureOptions.getPort(),
tempDir.toPath());

secure = new NettyServer(secureOptions, secureHandler);
secure = new NettyServer(secureOptions, secureHandler);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class NettyAppServerTest extends AppServerTestBase {

@Override
protected AppServer createAppServer() {
return new NettyAppServer();
return new NettyAppServer(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsSecureServer;
import org.openqa.selenium.testing.NotYetImplemented;

@NeedsSecureServer
class FederatedCredentialManagementTest extends JupiterTestBase {

private JavascriptExecutor jsAwareDriver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class RemoteWebDriverBiDiTest {

@BeforeAll
static void serverSetup() {
server = new NettyAppServer();
server = new NettyAppServer(false);
server.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void setupServers() {
tearDowns.add(deployment);

server = deployment.getServer();
appServer = new NettyAppServer();
appServer = new NettyAppServer(false);
tearDowns.add(() -> appServer.stop());
appServer.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static boolean isBazel() {
@BeforeEach
public void setup() {
// this field is actually in use, javascript test do access it
testEnvironment = GlobalTestEnvironment.getOrCreate(InProcessTestEnvironment::new);
testEnvironment = GlobalTestEnvironment.getOrCreate(() -> new InProcessTestEnvironment(true));
}

@AfterEach
Expand Down
2 changes: 1 addition & 1 deletion java/test/org/openqa/selenium/safari/CrossDomainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CrossDomainTest extends JupiterTestBase {

@BeforeAll
public static void startSecondServer() {
otherServer = new NettyAppServer();
otherServer = new NettyAppServer(false);
otherServer.start();

otherPages = new Pages(otherServer);
Expand Down
1 change: 1 addition & 0 deletions java/test/org/openqa/selenium/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ java_library(
"Ignore.java",
"IgnoreList.java",
"NeedsFreshDriver.java",
"NeedsSecureServer.java",
"NoDriverAfterTest.java",
"NoDriverBeforeTest.java",
"NotWorkingInRemoteBazelBuilds.java",
Expand Down
24 changes: 23 additions & 1 deletion java/test/org/openqa/selenium/testing/JupiterTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.Optional;
import java.util.logging.Logger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -37,6 +39,8 @@

public abstract class JupiterTestBase {

private static final Logger LOG = Logger.getLogger(JupiterTestBase.class.getName());

@RegisterExtension protected static SeleniumExtension seleniumExtension = new SeleniumExtension();

protected TestEnvironment environment;
Expand All @@ -54,9 +58,27 @@ public static void shouldTestBeRunAtAll() {

@BeforeEach
public void prepareEnvironment() {
environment = GlobalTestEnvironment.getOrCreate(InProcessTestEnvironment::new);
boolean needsSecureServer =
Optional.ofNullable(this.getClass().getAnnotation(NeedsSecureServer.class))
.map(NeedsSecureServer::value)
.orElse(false);

environment =
GlobalTestEnvironment.getOrCreate(() -> new InProcessTestEnvironment(needsSecureServer));
appServer = environment.getAppServer();

if (needsSecureServer) {
try {
appServer.whereIsSecure("/");
} catch (IllegalStateException ex) {
// this should not happen with bazel, a new JVM is used for each class
// the annotation is on class level, so we should never see this
LOG.info("appServer is restarted with secureServer=true");
environment.stop();
environment = new InProcessTestEnvironment(true);
}
}

pages = new Pages(appServer);

driver = seleniumExtension.getDriver();
Expand Down
30 changes: 30 additions & 0 deletions java/test/org/openqa/selenium/testing/NeedsSecureServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.testing;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NeedsSecureServer {

boolean value() default true;
}
Loading