Skip to content

Commit

Permalink
Cherry-pick tests fixes to 1.3.x (#952)
Browse files Browse the repository at this point in the history
* force junit 5.7.0 (#4)

* ui tests - fix selenium tests when run in k8s testsuite (#936)

* Increase retries on all tests and increase timeouts in some UI tests (#938)
  • Loading branch information
famarting authored Oct 23, 2020
1 parent da05a64 commit 4c8bd00
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
5 changes: 5 additions & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<test.app.name>
../app/target/apicurio-registry-app-${project.version}-runner
</test.app.name>
<junit.version>5.7.0</junit.version>
<junit.platform-launcher.version>1.7.0</junit.platform-launcher.version>
<debezium.version>1.3.0.Final</debezium.version>
<version.okhttp>4.9.0</version.okhttp>
<kafka.version>2.3.0</kafka.version>
Expand Down Expand Up @@ -85,16 +87,19 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform-launcher.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@
*/
package io.apicurio.tests.selenium;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.Testcontainers;
Expand Down Expand Up @@ -67,23 +74,78 @@ public void beforeTestExecution(ExtensionContext extensionContext) throws Except
}
}

private void deployChrome() {
private void deployChrome() throws Exception {
LOGGER.info("Deploying chrome browser");
if (!TestUtils.isExternalRegistry()) {
String uiUrl;
WebDriver driver;
if (TestUtils.isExternalRegistry()) {
// we are supposing that if registry is deployed externally selenium will be as well
driver = getRemoteChromeDriver();
String registrySeleniumHost = System.getenv().getOrDefault("REGISTRY_SELENIUM_HOST", TestUtils.getRegistryHost());
String registrySeleniumPort = System.getenv().getOrDefault("REGISTRY_SELENIUM_PORT", Integer.toString(TestUtils.getRegistryPort()));
uiUrl = String.format("http://%s:%s/ui", registrySeleniumHost, registrySeleniumPort);
} else {
Testcontainers.exposeHostPorts(TestUtils.getRegistryPort());
}
chrome = new BrowserWebDriverContainer()
uiUrl = TestUtils.getRegistryUIUrl().replace("localhost", "host.testcontainers.internal");
chrome = new BrowserWebDriverContainer()
.withCapabilities(new ChromeOptions());
chrome.start();
SeleniumProvider.getInstance().setupDriver(chrome.getWebDriver());
SeleniumProvider.getInstance().setUiUrl(TestUtils.getRegistryUIUrl().replace("localhost", "host.testcontainers.internal"));
chrome.start();
driver = chrome.getWebDriver();
}
SeleniumProvider.getInstance().setupDriver(driver);
SeleniumProvider.getInstance().setUiUrl(uiUrl);
deployed = true;
}

private void deleteChrome() {
SeleniumProvider.getInstance().tearDownDrivers();
LOGGER.info("Stopping chrome browser");
chrome.stop();
if (!TestUtils.isExternalRegistry()) {
chrome.stop();
}
deployed = false;
}

public static RemoteWebDriver getRemoteChromeDriver() throws Exception {
String seleniumHost = System.getenv().getOrDefault("SELENIUM_HOST", "localhost");
String seleniumPort = System.getenv().getOrDefault("SELENIUM_PORT", "80");
ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
options.addArguments("test-type", "--headless", "--no-sandbox", "--disable-dev-shm-usage", "--disable-extensions");
return getRemoteDriver(seleniumHost, seleniumPort, options);
}

private static RemoteWebDriver getRemoteDriver(String host, String port, Capabilities options) throws Exception {
int attempts = 60;
URL hubUrl = new URL(String.format("http://%s:%s/wd/hub", host, port));
LOGGER.info("Using remote selenium " + hubUrl);
for (int i = 0; i < attempts; i++) {
try {
testReachable(hubUrl);
return new RemoteWebDriver(hubUrl, options);
} catch (IOException e) {
if (i == attempts - 1) {
LOGGER.warn("Cannot connect to hub", e);
} else {
LOGGER.warn("Cannot connect to hub: {}", e.getMessage());
}
}
Thread.sleep(2000);
}
throw new IllegalStateException("Selenium webdriver cannot connect to selenium container");
}

private static void testReachable(URL url) throws IOException {
LOGGER.info("Trying to connect to {}", url.toString());
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.getContent();
LOGGER.info("Client is able to connect to the selenium hub");
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.Duration;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -73,7 +74,7 @@ void testDeleteArtifacts(RegistryService service) throws Exception {

page.deleteArtifact(artifactId1);

TestUtils.waitFor("Artifacts list updated", Constants.POLL_INTERVAL, Constants.TIMEOUT_GLOBAL, () -> {
TestUtils.waitFor("Artifacts list updated", Constants.POLL_INTERVAL, Duration.ofSeconds(60).toMillis(), () -> {
try {
return page.getArtifactsList().size() == 1;
} catch (Exception e) {
Expand All @@ -86,7 +87,7 @@ void testDeleteArtifacts(RegistryService service) throws Exception {

page.deleteArtifact(artifactId2);

TestUtils.waitFor("Artifacts list updated", Constants.POLL_INTERVAL, Constants.TIMEOUT_GLOBAL, () -> {
TestUtils.waitFor("Artifacts list updated", Constants.POLL_INTERVAL, Duration.ofSeconds(60).toMillis(), () -> {
try {
return page.getArtifactsList().size() == 0;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static void retry(RunnableExc runnable) throws Exception {
}

public static <T> T retry(Callable<T> callable) throws Exception {
return retry(callable, "Action #" + System.currentTimeMillis(), 5);
return retry(callable, "Action #" + System.currentTimeMillis(), 15);
}

public static void retry(RunnableExc runnable, String name, int maxRetries) throws Exception {
Expand Down

0 comments on commit 4c8bd00

Please sign in to comment.