From 3a04fa77baebd2dbc4bb3b1c0e009914d438ab36 Mon Sep 17 00:00:00 2001 From: Diego Molina Date: Sat, 16 Dec 2017 12:12:41 +0100 Subject: [PATCH] Avoiding port mapping, this should not be needed anymore. (#363) * Avoiding port mapping, this should not be needed anymore. * Mocking files and checking that dashboard addition is done * Initialising registry properly for tests --- .travis.yml | 4 +- run_integration_tests.sh | 8 +- .../container/DockerContainerClient.java | 9 -- .../ep/zalenium/dashboard/Dashboard.java | 1 + .../proxy/CloudTestingRemoteProxy.java | 4 + .../renderer/LiveNodeHtmlRenderer.java | 5 +- .../proxy/BrowserStackRemoteProxyTest.java | 29 +++++- .../proxy/CloudTestingRemoteProxyTest.java | 17 +++- .../proxy/DockerSeleniumRemoteProxyTest.java | 88 +++++++++++++------ .../DockerSeleniumStarterRemoteProxyTest.java | 13 ++- .../proxy/SauceLabsRemoteProxyTest.java | 13 ++- .../proxy/TestingBotRemoteProxyTest.java | 13 ++- .../zalenium/servlet/LiveNodeServletTest.java | 19 +++- .../servlet/VncAuthenticationServletTest.java | 28 +++++- .../servlet/ZaleniumConsoleServletTest.java | 32 ++++++- .../util/KubernetesContainerMock.java | 2 + .../zalando/ep/zalenium/util/TestUtils.java | 2 +- 17 files changed, 230 insertions(+), 57 deletions(-) diff --git a/.travis.yml b/.travis.yml index c0114018b8..30e7c55477 100644 --- a/.travis.yml +++ b/.travis.yml @@ -41,10 +41,10 @@ jobs: - env: step=minikube script: - docker pull elgalu/selenium - - kubernetes/minikube-ci-initialize.sh + - travis_retry ./kubernetes/minikube-ci-initialize.sh - mvn clean package -Pbuild-docker-image -DskipTests=true - cd target && docker build -t dosel/zalenium:minikube . && cd .. - - kubernetes/start-zalenium-in-minikube.sh + - ./kubernetes/start-zalenium-in-minikube.sh - ./run_integration_tests.sh minikube - stage: Push Image (only with a tag) diff --git a/run_integration_tests.sh b/run_integration_tests.sh index 86d14b10a3..51fc0f2a75 100755 --- a/run_integration_tests.sh +++ b/run_integration_tests.sh @@ -14,7 +14,7 @@ else # If the environment var exists, then we run the integration tests. This is to allow external PRs ro tun if [ "$INTEGRATION_TO_TEST" = sauceLabs ]; then if [ -n "${SAUCE_USERNAME}" ]; then - sudo mvn clean + sudo env "PATH=$PATH" mvn clean mvn clean verify -Pintegration-test -DthreadCountProperty=2 -Dskip.surefire.tests=true -DintegrationToTest=${INTEGRATION_TO_TEST} # Check for generated videos ls -la ${VIDEOS_FOLDER}/saucelabs*.mp4 || (echo "No Sauce Labs videos were downloaded." && exit 2) @@ -23,7 +23,7 @@ else fi if [ "$INTEGRATION_TO_TEST" = browserStack ]; then if [ -n "${BROWSER_STACK_USER}" ]; then - sudo mvn clean + sudo env "PATH=$PATH" mvn clean mvn clean package -Pbuild-docker-image -DskipTests=true mkdir -p "${VIDEOS_FOLDER}" cd target && docker build -t dosel/zalenium:latest . && cd .. @@ -38,7 +38,7 @@ else fi if [ "$INTEGRATION_TO_TEST" = testingBot ]; then if [ -n "${TESTINGBOT_KEY}" ]; then - sudo mvn clean + sudo env "PATH=$PATH" mvn clean mvn clean verify -Pintegration-test -DthreadCountProperty=2 -Dskip.surefire.tests=true -DintegrationToTest=${INTEGRATION_TO_TEST} # Check for generated videos ls -la ${VIDEOS_FOLDER}/testingbot*.mp4 || (echo "No TestingBot videos were downloaded." && exit 2) @@ -47,7 +47,7 @@ else fi if [ "$INTEGRATION_TO_TEST" = dockerCompose ]; then if [ -n "${SAUCE_USERNAME}" ]; then - sudo mvn clean + sudo env "PATH=$PATH" mvn clean mvn clean package -Pbuild-docker-image -DskipTests=true mkdir -p "${VIDEOS_FOLDER}" chmod +x target/zalenium_in_docker_compose.sh diff --git a/src/main/java/de/zalando/ep/zalenium/container/DockerContainerClient.java b/src/main/java/de/zalando/ep/zalenium/container/DockerContainerClient.java index cf3a19c5f7..aea957eb5d 100644 --- a/src/main/java/de/zalando/ep/zalenium/container/DockerContainerClient.java +++ b/src/main/java/de/zalando/ep/zalenium/container/DockerContainerClient.java @@ -183,14 +183,6 @@ public ContainerCreationStatus createContainer(String zaleniumContainerName, Str String noVncPort = envVars.get("NOVNC_PORT"); - final Map> portBindings = new HashMap<>(); - List portBindingList = new ArrayList<>(); - portBindingList.add(PortBinding.of("", nodePort)); - portBindings.put(nodePort, portBindingList); - portBindingList = new ArrayList<>(); - portBindingList.add(PortBinding.of("", noVncPort)); - portBindings.put(noVncPort, portBindingList); - String networkMode = getZaleniumNetwork(zaleniumContainerName); List extraHosts = new ArrayList<>(); @@ -215,7 +207,6 @@ public ContainerCreationStatus createContainer(String zaleniumContainerName, Str HostConfig hostConfig = HostConfig.builder() .appendBinds(binds) - .portBindings(portBindings) .networkMode(networkMode) .extraHosts(extraHosts) .autoRemove(true) diff --git a/src/main/java/de/zalando/ep/zalenium/dashboard/Dashboard.java b/src/main/java/de/zalando/ep/zalenium/dashboard/Dashboard.java index a5f4c14f4b..57e5665e49 100644 --- a/src/main/java/de/zalando/ep/zalenium/dashboard/Dashboard.java +++ b/src/main/java/de/zalando/ep/zalenium/dashboard/Dashboard.java @@ -170,6 +170,7 @@ public static void restoreCommonProxyUtilities() { commonProxyUtilities = new CommonProxyUtilities(); } + @VisibleForTesting public static void setCommonProxyUtilities(CommonProxyUtilities commonProxyUtilities) { Dashboard.commonProxyUtilities = commonProxyUtilities; } diff --git a/src/main/java/de/zalando/ep/zalenium/proxy/CloudTestingRemoteProxy.java b/src/main/java/de/zalando/ep/zalenium/proxy/CloudTestingRemoteProxy.java index 52cb7d9a27..f8d12c8e63 100644 --- a/src/main/java/de/zalando/ep/zalenium/proxy/CloudTestingRemoteProxy.java +++ b/src/main/java/de/zalando/ep/zalenium/proxy/CloudTestingRemoteProxy.java @@ -49,6 +49,8 @@ public class CloudTestingRemoteProxy extends DefaultRemoteProxy { @VisibleForTesting public static final long DEFAULT_MAX_TEST_IDLE_TIME_SECS = 90L; + @VisibleForTesting + public static boolean addToDashboardCalled = false; private static final Logger logger = Logger.getLogger(CloudTestingRemoteProxy.class.getName()); private static final GoogleAnalyticsApi defaultGA = new GoogleAnalyticsApi(); private static final CommonProxyUtilities defaultCommonProxyUtilities = new CommonProxyUtilities(); @@ -228,6 +230,7 @@ public boolean convertVideoFileToMP4() { } public void addTestToDashboard(String seleniumSessionId, boolean testCompleted) { + addToDashboardCalled = false; new Thread(() -> { try { TestInformation testInformation = getTestInformation(seleniumSessionId); @@ -248,6 +251,7 @@ public void addTestToDashboard(String seleniumSessionId, boolean testCompleted) } createFeatureNotImplementedFile(testInformation.getLogsFolderPath()); Dashboard.updateDashboard(testInformation); + addToDashboardCalled = true; } catch (Exception e) { logger.log(Level.SEVERE, e.toString(), e); } diff --git a/src/main/java/de/zalando/ep/zalenium/servlet/renderer/LiveNodeHtmlRenderer.java b/src/main/java/de/zalando/ep/zalenium/servlet/renderer/LiveNodeHtmlRenderer.java index 40251db6fe..b86e396995 100644 --- a/src/main/java/de/zalando/ep/zalenium/servlet/renderer/LiveNodeHtmlRenderer.java +++ b/src/main/java/de/zalando/ep/zalenium/servlet/renderer/LiveNodeHtmlRenderer.java @@ -12,6 +12,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; @@ -100,8 +101,8 @@ private String tabBrowsers() { slotTitle = testSlot.getCapabilities().toString(); } Map browserValues = new HashMap<>(); - browserValues.put("{{browserVersion}}", version); - browserValues.put("{{slotIcon}}", icon); + browserValues.put("{{browserVersion}}", Optional.ofNullable(version).orElse("N/A")); + browserValues.put("{{slotIcon}}", Optional.ofNullable(icon).orElse("N/A")); browserValues.put("{{slotClass}}", slotClass); browserValues.put("{{slotTitle}}", slotTitle); browserSection.append(templateRenderer.renderSection("{{tabBrowsers}}", browserValues)); diff --git a/src/test/java/de/zalando/ep/zalenium/proxy/BrowserStackRemoteProxyTest.java b/src/test/java/de/zalando/ep/zalenium/proxy/BrowserStackRemoteProxyTest.java index 959459a664..55724c0342 100644 --- a/src/test/java/de/zalando/ep/zalenium/proxy/BrowserStackRemoteProxyTest.java +++ b/src/test/java/de/zalando/ep/zalenium/proxy/BrowserStackRemoteProxyTest.java @@ -3,21 +3,27 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import de.zalando.ep.zalenium.dashboard.Dashboard; import de.zalando.ep.zalenium.registry.ZaleniumRegistry; import de.zalando.ep.zalenium.util.CommonProxyUtilities; import de.zalando.ep.zalenium.util.Environment; import de.zalando.ep.zalenium.util.TestUtils; import de.zalando.ep.zalenium.dashboard.TestInformation; +import org.awaitility.Duration; import org.hamcrest.CoreMatchers; import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.openqa.grid.common.RegistrationRequest; import org.openqa.grid.internal.ExternalSessionKey; import org.openqa.grid.internal.GridRegistry; import org.openqa.grid.internal.RemoteProxy; import org.openqa.grid.internal.TestSession; +import org.openqa.grid.internal.utils.configuration.GridHubConfiguration; +import org.openqa.grid.web.Hub; import org.openqa.grid.web.servlet.handler.RequestType; import org.openqa.grid.web.servlet.handler.WebDriverRequest; import org.openqa.selenium.Platform; @@ -25,16 +31,19 @@ import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.server.jmx.JMXHelper; +import javax.management.InstanceNotFoundException; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.Callable; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -42,16 +51,27 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.timeout; +import static org.awaitility.Awaitility.await; public class BrowserStackRemoteProxyTest { private BrowserStackRemoteProxy browserStackProxy; private GridRegistry registry; + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @SuppressWarnings("ConstantConditions") @Before public void setUp() { - registry = ZaleniumRegistry.newInstance(); + try { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=Hub"); + ManagementFactory.getPlatformMBeanServer().getObjectInstance(objectName); + new JMXHelper().unregister(objectName); + } catch (MalformedObjectNameException | InstanceNotFoundException e) { + // Might be that the object does not exist, it is ok. Nothing to do, this is just a cleanup task. + } + registry = ZaleniumRegistry.newInstance(new Hub(new GridHubConfiguration())); // Creating the configuration and the registration request of the proxy (node) RegistrationRequest request = TestUtils.getRegistrationRequestForTesting(30002, BrowserStackRemoteProxy.class.getCanonicalName()); @@ -142,13 +162,15 @@ public void testInformationIsRetrievedWhenStoppingSession() throws IOException { requestedCapability.put(CapabilityType.PLATFORM, Platform.WIN10); JsonElement informationSample = TestUtils.getTestInformationSample("browserstack_testinformation.json"); - CommonProxyUtilities commonProxyUtilities = mock(CommonProxyUtilities.class); + TestUtils.ensureRequiredInputFilesExist(temporaryFolder); + CommonProxyUtilities commonProxyUtilities = TestUtils.mockCommonProxyUtilitiesForDashboardTesting(temporaryFolder); Environment env = new Environment(); String mockTestInformationUrl = "https://www.browserstack.com/automate/sessions/77e51cead8e6e37b0a0feb0dfa69325b2c4acf97.json"; when(commonProxyUtilities.readJSONFromUrl(mockTestInformationUrl, env.getStringEnvVariable("BROWSER_STACK_USER", ""), env.getStringEnvVariable("BROWSER_STACK_KEY", ""))).thenReturn(informationSample); BrowserStackRemoteProxy.setCommonProxyUtilities(commonProxyUtilities); + Dashboard.setCommonProxyUtilities(commonProxyUtilities); // Getting a test session in the sauce labs node BrowserStackRemoteProxy bsSpyProxy = spy(browserStackProxy); @@ -166,6 +188,8 @@ public void testInformationIsRetrievedWhenStoppingSession() throws IOException { bsSpyProxy.afterCommand(testSession, request, response); verify(bsSpyProxy, timeout(1000 * 5)).getTestInformation(mockSeleniumSessionId); + Callable callable = () -> BrowserStackRemoteProxy.addToDashboardCalled; + await().pollInterval(Duration.FIVE_HUNDRED_MILLISECONDS).atMost(Duration.TWO_SECONDS).until(callable); TestInformation testInformation = bsSpyProxy.getTestInformation(mockSeleniumSessionId); Assert.assertEquals("loadZalandoPageAndCheckTitle", testInformation.getTestName()); Assert.assertThat(testInformation.getFileName(), @@ -181,6 +205,7 @@ public void testInformationIsRetrievedWhenStoppingSession() throws IOException { BrowserStackRemoteProxy.restoreCommonProxyUtilities(); BrowserStackRemoteProxy.restoreGa(); BrowserStackRemoteProxy.restoreEnvironment(); + Dashboard.restoreCommonProxyUtilities(); } } diff --git a/src/test/java/de/zalando/ep/zalenium/proxy/CloudTestingRemoteProxyTest.java b/src/test/java/de/zalando/ep/zalenium/proxy/CloudTestingRemoteProxyTest.java index 7be87080fa..86d839ccae 100644 --- a/src/test/java/de/zalando/ep/zalenium/proxy/CloudTestingRemoteProxyTest.java +++ b/src/test/java/de/zalando/ep/zalenium/proxy/CloudTestingRemoteProxyTest.java @@ -5,13 +5,28 @@ import org.junit.Test; import org.openqa.grid.common.RegistrationRequest; import org.openqa.grid.internal.GridRegistry; +import org.openqa.grid.internal.utils.configuration.GridHubConfiguration; +import org.openqa.grid.web.Hub; +import org.openqa.selenium.remote.server.jmx.JMXHelper; import org.testng.Assert; +import javax.management.InstanceNotFoundException; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; +import java.lang.management.ManagementFactory; + public class CloudTestingRemoteProxyTest { @Test public void defaultValuesAreAlwaysNull() { - GridRegistry registry = ZaleniumRegistry.newInstance(); + try { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=Hub"); + ManagementFactory.getPlatformMBeanServer().getObjectInstance(objectName); + new JMXHelper().unregister(objectName); + } catch (MalformedObjectNameException | InstanceNotFoundException e) { + // Might be that the object does not exist, it is ok. Nothing to do, this is just a cleanup task. + } + GridRegistry registry = ZaleniumRegistry.newInstance(new Hub(new GridHubConfiguration())); RegistrationRequest request = TestUtils.getRegistrationRequestForTesting(30003, CloudTestingRemoteProxy.class.getCanonicalName()); CloudTestingRemoteProxy proxy = CloudTestingRemoteProxy.getNewInstance(request, registry); diff --git a/src/test/java/de/zalando/ep/zalenium/proxy/DockerSeleniumRemoteProxyTest.java b/src/test/java/de/zalando/ep/zalenium/proxy/DockerSeleniumRemoteProxyTest.java index c4990a3195..430d551d34 100644 --- a/src/test/java/de/zalando/ep/zalenium/proxy/DockerSeleniumRemoteProxyTest.java +++ b/src/test/java/de/zalando/ep/zalenium/proxy/DockerSeleniumRemoteProxyTest.java @@ -3,8 +3,10 @@ import de.zalando.ep.zalenium.container.ContainerClient; import de.zalando.ep.zalenium.container.ContainerFactory; import de.zalando.ep.zalenium.container.kubernetes.KubernetesContainerClient; +import de.zalando.ep.zalenium.dashboard.Dashboard; import de.zalando.ep.zalenium.dashboard.TestInformation; import de.zalando.ep.zalenium.registry.ZaleniumRegistry; +import de.zalando.ep.zalenium.util.CommonProxyUtilities; import de.zalando.ep.zalenium.util.DockerContainerMock; import de.zalando.ep.zalenium.util.Environment; import de.zalando.ep.zalenium.util.KubernetesContainerMock; @@ -13,13 +15,17 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import org.openqa.grid.common.RegistrationRequest; import org.openqa.grid.internal.GridRegistry; import org.openqa.grid.internal.TestSession; +import org.openqa.grid.internal.utils.configuration.GridHubConfiguration; +import org.openqa.grid.web.Hub; import org.openqa.grid.web.servlet.handler.RequestType; import org.openqa.grid.web.servlet.handler.WebDriverRequest; import org.openqa.selenium.Dimension; @@ -28,9 +34,12 @@ import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.server.jmx.JMXHelper; +import javax.management.InstanceNotFoundException; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.lang.management.ManagementFactory; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -62,6 +71,9 @@ public class DockerSeleniumRemoteProxyTest { private Supplier originalIsKubernetesValue; private Supplier currentIsKubernetesValue; + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + public DockerSeleniumRemoteProxyTest(ContainerClient containerClient, Supplier isKubernetes) { this.containerClient = containerClient; this.currentIsKubernetesValue = isKubernetes; @@ -94,7 +106,15 @@ public void setUp() { } ContainerFactory.setIsKubernetes(this.currentIsKubernetesValue); - registry = ZaleniumRegistry.newInstance(); + try { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=Hub"); + ManagementFactory.getPlatformMBeanServer().getObjectInstance(objectName); + new JMXHelper().unregister(objectName); + } catch (MalformedObjectNameException | InstanceNotFoundException e) { + // Might be that the object does not exist, it is ok. Nothing to do, this is just a cleanup task. + } + + registry = ZaleniumRegistry.newInstance(new Hub(new GridHubConfiguration())); // Creating the configuration and the registration request of the proxy (node) RegistrationRequest request = TestUtils.getRegistrationRequestForTesting(40000, @@ -253,35 +273,43 @@ public void testIdleTimeoutUsesValuePassedAsCapability() { } @Test - public void pollerThreadTearsDownNodeAfterTestIsCompleted() { + public void pollerThreadTearsDownNodeAfterTestIsCompleted() throws IOException { - // Supported desired capability for the test session - Map requestedCapability = getCapabilitySupportedByDockerSelenium(); + try { + CommonProxyUtilities commonProxyUtilities = TestUtils.mockCommonProxyUtilitiesForDashboardTesting(temporaryFolder); + TestUtils.ensureRequiredInputFilesExist(temporaryFolder); + Dashboard.setCommonProxyUtilities(commonProxyUtilities); - // Start poller thread - proxy.startPolling(); + // Supported desired capability for the test session + Map requestedCapability = getCapabilitySupportedByDockerSelenium(); - // Get a test session - TestSession newSession = proxy.getNewSession(requestedCapability); - Assert.assertNotNull(newSession); + // Start poller thread + proxy.startPolling(); - // The node should be busy since there is a session in it - Assert.assertTrue(proxy.isBusy()); + // Get a test session + TestSession newSession = proxy.getNewSession(requestedCapability); + Assert.assertNotNull(newSession); - // We release the session, the node should be free - WebDriverRequest request = mock(WebDriverRequest.class); - HttpServletResponse response = mock(HttpServletResponse.class); - when(request.getMethod()).thenReturn("DELETE"); - when(request.getRequestType()).thenReturn(RequestType.STOP_SESSION); + // The node should be busy since there is a session in it + Assert.assertTrue(proxy.isBusy()); - newSession.getSlot().doFinishRelease(); - proxy.afterCommand(newSession, request, response); - proxy.afterSession(newSession); + // We release the session, the node should be free + WebDriverRequest request = mock(WebDriverRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getMethod()).thenReturn("DELETE"); + when(request.getRequestType()).thenReturn(RequestType.STOP_SESSION); - // After running one test, the node shouldn't be busy and also down - Assert.assertFalse(proxy.isBusy()); - Callable callable = () -> registry.getProxyById(proxy.getId()) == null; - await().pollInterval(Duration.FIVE_HUNDRED_MILLISECONDS).atMost(Duration.TWO_SECONDS).until(callable); + newSession.getSlot().doFinishRelease(); + proxy.afterCommand(newSession, request, response); + proxy.afterSession(newSession); + + // After running one test, the node shouldn't be busy and also down + Assert.assertFalse(proxy.isBusy()); + Callable callable = () -> registry.getProxyById(proxy.getId()) == null; + await().pollInterval(Duration.FIVE_HUNDRED_MILLISECONDS).atMost(Duration.TWO_SECONDS).until(callable); + } finally { + Dashboard.restoreCommonProxyUtilities(); + } } @Test @@ -398,7 +426,7 @@ public void fallbackToDefaultValueWhenEnvVariableIsNotABoolean() { } @Test - public void videoRecordingIsStartedAndStopped() throws MalformedObjectNameException { + public void videoRecordingIsStartedAndStopped() throws MalformedObjectNameException, IOException { try { @@ -411,6 +439,10 @@ public void videoRecordingIsStartedAndStopped() throws MalformedObjectNameExcept DockerSeleniumStarterRemoteProxy.setContainerClient(containerClient); dsProxy.getNewSession(getCapabilitySupportedByDockerSelenium()); + CommonProxyUtilities commonProxyUtilities = TestUtils.mockCommonProxyUtilitiesForDashboardTesting(temporaryFolder); + TestUtils.ensureRequiredInputFilesExist(temporaryFolder); + Dashboard.setCommonProxyUtilities(commonProxyUtilities); + // Creating a spy proxy to verify the invoked methods DockerSeleniumRemoteProxy spyProxy = spy(proxy); @@ -456,11 +488,12 @@ public void videoRecordingIsStartedAndStopped() throws MalformedObjectNameExcept } finally { ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://localhost:30000\""); new JMXHelper().unregister(objectName); + Dashboard.restoreCommonProxyUtilities(); } } @Test - public void videoRecordingIsDisabled() throws MalformedObjectNameException { + public void videoRecordingIsDisabled() throws MalformedObjectNameException, IOException { try { // Create a docker-selenium container @@ -481,10 +514,14 @@ public void videoRecordingIsDisabled() throws MalformedObjectNameException { .thenReturn(1); // Creating a spy proxy to verify the invoked methods + CommonProxyUtilities commonProxyUtilities = TestUtils.mockCommonProxyUtilitiesForDashboardTesting(temporaryFolder); + TestUtils.ensureRequiredInputFilesExist(temporaryFolder); + Dashboard.setCommonProxyUtilities(commonProxyUtilities); DockerSeleniumRemoteProxy spyProxy = spy(proxy); DockerSeleniumRemoteProxy.setEnv(environment); DockerSeleniumRemoteProxy.readEnvVars(); + // Start poller thread spyProxy.startPolling(); @@ -524,6 +561,7 @@ public void videoRecordingIsDisabled() throws MalformedObjectNameException { verify(spyProxy, never()).copyVideos(""); } finally { DockerSeleniumRemoteProxy.restoreEnvironment(); + Dashboard.restoreCommonProxyUtilities(); ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://localhost:30000\""); new JMXHelper().unregister(objectName); } diff --git a/src/test/java/de/zalando/ep/zalenium/proxy/DockerSeleniumStarterRemoteProxyTest.java b/src/test/java/de/zalando/ep/zalenium/proxy/DockerSeleniumStarterRemoteProxyTest.java index dcc1bf88c1..b79ae5b39d 100644 --- a/src/test/java/de/zalando/ep/zalenium/proxy/DockerSeleniumStarterRemoteProxyTest.java +++ b/src/test/java/de/zalando/ep/zalenium/proxy/DockerSeleniumStarterRemoteProxyTest.java @@ -19,14 +19,18 @@ import org.openqa.grid.common.RegistrationRequest; import org.openqa.grid.internal.GridRegistry; import org.openqa.grid.internal.TestSession; +import org.openqa.grid.internal.utils.configuration.GridHubConfiguration; +import org.openqa.grid.web.Hub; import org.openqa.selenium.Dimension; import org.openqa.selenium.Platform; import org.openqa.selenium.remote.BrowserType; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.server.jmx.JMXHelper; +import javax.management.InstanceNotFoundException; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; +import java.lang.management.ManagementFactory; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -92,7 +96,14 @@ public void setUp() { } ContainerFactory.setIsKubernetes(this.currentIsKubernetesValue); - registry = ZaleniumRegistry.newInstance(); + try { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=Hub"); + ManagementFactory.getPlatformMBeanServer().getObjectInstance(objectName); + new JMXHelper().unregister(objectName); + } catch (MalformedObjectNameException | InstanceNotFoundException e) { + // Might be that the object does not exist, it is ok. Nothing to do, this is just a cleanup task. + } + registry = ZaleniumRegistry.newInstance(new Hub(new GridHubConfiguration())); // Creating the configuration and the registration request of the proxy (node) request = TestUtils.getRegistrationRequestForTesting(30000, diff --git a/src/test/java/de/zalando/ep/zalenium/proxy/SauceLabsRemoteProxyTest.java b/src/test/java/de/zalando/ep/zalenium/proxy/SauceLabsRemoteProxyTest.java index e7478e9533..d0df65478c 100644 --- a/src/test/java/de/zalando/ep/zalenium/proxy/SauceLabsRemoteProxyTest.java +++ b/src/test/java/de/zalando/ep/zalenium/proxy/SauceLabsRemoteProxyTest.java @@ -20,6 +20,8 @@ import org.openqa.grid.internal.GridRegistry; import org.openqa.grid.internal.RemoteProxy; import org.openqa.grid.internal.TestSession; +import org.openqa.grid.internal.utils.configuration.GridHubConfiguration; +import org.openqa.grid.web.Hub; import org.openqa.grid.web.servlet.handler.RequestType; import org.openqa.grid.web.servlet.handler.WebDriverRequest; import org.openqa.selenium.Platform; @@ -27,11 +29,13 @@ import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.server.jmx.JMXHelper; +import javax.management.InstanceNotFoundException; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -46,7 +50,14 @@ public class SauceLabsRemoteProxyTest { @Before public void setUp() { - registry = ZaleniumRegistry.newInstance(); + try { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=Hub"); + ManagementFactory.getPlatformMBeanServer().getObjectInstance(objectName); + new JMXHelper().unregister(objectName); + } catch (MalformedObjectNameException | InstanceNotFoundException e) { + // Might be that the object does not exist, it is ok. Nothing to do, this is just a cleanup task. + } + registry = ZaleniumRegistry.newInstance(new Hub(new GridHubConfiguration())); // Creating the configuration and the registration request of the proxy (node) RegistrationRequest request = TestUtils.getRegistrationRequestForTesting(30001, SauceLabsRemoteProxy.class.getCanonicalName()); diff --git a/src/test/java/de/zalando/ep/zalenium/proxy/TestingBotRemoteProxyTest.java b/src/test/java/de/zalando/ep/zalenium/proxy/TestingBotRemoteProxyTest.java index 1f66304691..28d4f584ff 100644 --- a/src/test/java/de/zalando/ep/zalenium/proxy/TestingBotRemoteProxyTest.java +++ b/src/test/java/de/zalando/ep/zalenium/proxy/TestingBotRemoteProxyTest.java @@ -21,6 +21,8 @@ import org.openqa.grid.internal.GridRegistry; import org.openqa.grid.internal.RemoteProxy; import org.openqa.grid.internal.TestSession; +import org.openqa.grid.internal.utils.configuration.GridHubConfiguration; +import org.openqa.grid.web.Hub; import org.openqa.grid.web.servlet.handler.RequestType; import org.openqa.grid.web.servlet.handler.WebDriverRequest; import org.openqa.selenium.Platform; @@ -28,12 +30,14 @@ import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.server.jmx.JMXHelper; +import javax.management.InstanceNotFoundException; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; @@ -57,7 +61,14 @@ public class TestingBotRemoteProxyTest { @SuppressWarnings("ConstantConditions") @Before public void setUp() throws IOException { - registry = ZaleniumRegistry.newInstance(); + try { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=Hub"); + ManagementFactory.getPlatformMBeanServer().getObjectInstance(objectName); + new JMXHelper().unregister(objectName); + } catch (MalformedObjectNameException | InstanceNotFoundException e) { + // Might be that the object does not exist, it is ok. Nothing to do, this is just a cleanup task. + } + registry = ZaleniumRegistry.newInstance(new Hub(new GridHubConfiguration())); // Creating the configuration and the registration request of the proxy (node) RegistrationRequest request = TestUtils.getRegistrationRequestForTesting(30002, TestingBotRemoteProxy.class.getCanonicalName()); diff --git a/src/test/java/de/zalando/ep/zalenium/servlet/LiveNodeServletTest.java b/src/test/java/de/zalando/ep/zalenium/servlet/LiveNodeServletTest.java index 6aa475c420..e85db3aa9b 100644 --- a/src/test/java/de/zalando/ep/zalenium/servlet/LiveNodeServletTest.java +++ b/src/test/java/de/zalando/ep/zalenium/servlet/LiveNodeServletTest.java @@ -16,11 +16,16 @@ import org.openqa.grid.internal.GridRegistry; import org.openqa.grid.internal.utils.configuration.GridHubConfiguration; import org.openqa.grid.web.Hub; +import org.openqa.selenium.remote.server.jmx.JMXHelper; +import javax.management.InstanceNotFoundException; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.util.function.Supplier; import static org.mockito.Mockito.mock; @@ -37,6 +42,13 @@ public class LiveNodeServletTest { @Before public void setUp() throws IOException { + try { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=Hub"); + ManagementFactory.getPlatformMBeanServer().getObjectInstance(objectName); + new JMXHelper().unregister(objectName); + } catch (MalformedObjectNameException | InstanceNotFoundException e) { + // Might be that the object does not exist, it is ok. Nothing to do, this is just a cleanup task. + } registry = ZaleniumRegistry.newInstance(new Hub(new GridHubConfiguration())); this.originalContainerClient = ContainerFactory.getContainerClientGenerator(); @@ -104,12 +116,15 @@ public void noRefreshInHtmlWhenParameterIsInvalid() throws IOException { livePreviewServletServlet.doPost(request, response); String postResponseContent = response.getOutputStream().toString(); - // content='-1' means that the page won't refresh assertThat(postResponseContent, containsString("")); } @After - public void tearDown() { + public void tearDown() throws MalformedObjectNameException { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://localhost:40000\""); + new JMXHelper().unregister(objectName); + objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://localhost:40001\""); + new JMXHelper().unregister(objectName); ContainerFactory.setContainerClientGenerator(originalContainerClient); } diff --git a/src/test/java/de/zalando/ep/zalenium/servlet/VncAuthenticationServletTest.java b/src/test/java/de/zalando/ep/zalenium/servlet/VncAuthenticationServletTest.java index 0569550033..2187f3b332 100644 --- a/src/test/java/de/zalando/ep/zalenium/servlet/VncAuthenticationServletTest.java +++ b/src/test/java/de/zalando/ep/zalenium/servlet/VncAuthenticationServletTest.java @@ -7,12 +7,17 @@ import static org.mockito.Mockito.when; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.util.function.Supplier; +import javax.management.InstanceNotFoundException; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import de.zalando.ep.zalenium.registry.ZaleniumRegistry; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -25,6 +30,9 @@ import de.zalando.ep.zalenium.proxy.DockerSeleniumStarterRemoteProxy; import de.zalando.ep.zalenium.util.DockerContainerMock; import de.zalando.ep.zalenium.util.TestUtils; +import org.openqa.grid.internal.utils.configuration.GridHubConfiguration; +import org.openqa.grid.web.Hub; +import org.openqa.selenium.remote.server.jmx.JMXHelper; public class VncAuthenticationServletTest { private GridRegistry registry; @@ -34,7 +42,14 @@ public class VncAuthenticationServletTest { @Before public void setUp() throws IOException { - registry = ZaleniumRegistry.newInstance(); + try { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=Hub"); + ManagementFactory.getPlatformMBeanServer().getObjectInstance(objectName); + new JMXHelper().unregister(objectName); + } catch (MalformedObjectNameException | InstanceNotFoundException e) { + // Might be that the object does not exist, it is ok. Nothing to do, this is just a cleanup task. + } + registry = ZaleniumRegistry.newInstance(new Hub(new GridHubConfiguration())); this.originalContainerClient = ContainerFactory.getContainerClientGenerator(); ContainerFactory.setContainerClientGenerator(DockerContainerMock::getMockedDockerContainerClient); @@ -62,7 +77,16 @@ public void setUp() throws IOException { when(request.getServerName()).thenReturn("localhost"); when(response.getOutputStream()).thenReturn(TestUtils.getMockedServletOutputStream()); } - + + @After + public void tearDown() throws MalformedObjectNameException { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://localhost:40001\""); + new JMXHelper().unregister(objectName); + objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://localhost:40000\""); + new JMXHelper().unregister(objectName); + ContainerFactory.setContainerClientGenerator(originalContainerClient); + } + @Test public void testAuthenticationSucceedsForNoVnc() throws Exception { VncAuthenticationServlet vncAuthenticationServlet = new VncAuthenticationServlet(registry); diff --git a/src/test/java/de/zalando/ep/zalenium/servlet/ZaleniumConsoleServletTest.java b/src/test/java/de/zalando/ep/zalenium/servlet/ZaleniumConsoleServletTest.java index 355f8f16f6..f24492dc6f 100644 --- a/src/test/java/de/zalando/ep/zalenium/servlet/ZaleniumConsoleServletTest.java +++ b/src/test/java/de/zalando/ep/zalenium/servlet/ZaleniumConsoleServletTest.java @@ -22,11 +22,16 @@ import org.openqa.selenium.MutableCapabilities; import org.openqa.selenium.Platform; import org.openqa.selenium.remote.CapabilityType; +import org.openqa.selenium.remote.server.jmx.JMXHelper; +import javax.management.InstanceNotFoundException; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.util.List; import java.util.function.Supplier; @@ -44,6 +49,13 @@ public class ZaleniumConsoleServletTest { @Before public void setUp() throws IOException { + try { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=Hub"); + ManagementFactory.getPlatformMBeanServer().getObjectInstance(objectName); + new JMXHelper().unregister(objectName); + } catch (MalformedObjectNameException | InstanceNotFoundException e) { + // Might be that the object does not exist, it is ok. Nothing to do, this is just a cleanup task. + } registry = ZaleniumRegistry.newInstance(new Hub(new GridHubConfiguration())); this.originalContainerClient = ContainerFactory.getContainerClientGenerator(); @@ -64,7 +76,7 @@ public void setUp() throws IOException { BrowserStackRemoteProxy.setCommonProxyUtilities(commonProxyUtilities); BrowserStackRemoteProxy browserStackRemoteProxy = BrowserStackRemoteProxy.getNewInstance(registrationRequest, registry); - registrationRequest = TestUtils.getRegistrationRequestForTesting(30002, TestingBotRemoteProxy.class.getCanonicalName()); + registrationRequest = TestUtils.getRegistrationRequestForTesting(30003, TestingBotRemoteProxy.class.getCanonicalName()); TestingBotRemoteProxy.setCommonProxyUtilities(commonProxyUtilities); TestingBotRemoteProxy testingBotRemoteProxy = TestingBotRemoteProxy.getNewInstance(registrationRequest, registry); @@ -105,7 +117,7 @@ public void setUp() throws IOException { @Test - public void addedNodesAreRenderedInServlet() throws ServletException, IOException { + public void addedNodesAreRenderedInServlet() throws IOException { ZaleniumConsoleServlet zaleniumConsoleServlet = new ZaleniumConsoleServlet(registry); @@ -120,7 +132,7 @@ public void addedNodesAreRenderedInServlet() throws ServletException, IOExceptio } @Test - public void postAndGetReturnSameContent() throws ServletException, IOException { + public void postAndGetReturnSameContent() throws IOException { ZaleniumConsoleServlet zaleniumConsoleServlet = new ZaleniumConsoleServlet(registry); @@ -151,7 +163,19 @@ public void checkResourcesInConsoleServlet() throws ServletException, IOExceptio } @After - public void tearDown() { + public void tearDown() throws MalformedObjectNameException { + ObjectName objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://localhost:30000\""); + new JMXHelper().unregister(objectName); + objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"https://ondemand.saucelabs.com:443\""); + new JMXHelper().unregister(objectName); + objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://hub-cloud.browserstack.com:80\""); + new JMXHelper().unregister(objectName); + objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://hub.testingbot.com:80\""); + new JMXHelper().unregister(objectName); + objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://localhost:40000\""); + new JMXHelper().unregister(objectName); + objectName = new ObjectName("org.seleniumhq.grid:type=RemoteProxy,node=\"http://localhost:40001\""); + new JMXHelper().unregister(objectName); ContainerFactory.setContainerClientGenerator(originalContainerClient); } } diff --git a/src/test/java/de/zalando/ep/zalenium/util/KubernetesContainerMock.java b/src/test/java/de/zalando/ep/zalenium/util/KubernetesContainerMock.java index 833106a3cc..daaf592aa3 100644 --- a/src/test/java/de/zalando/ep/zalenium/util/KubernetesContainerMock.java +++ b/src/test/java/de/zalando/ep/zalenium/util/KubernetesContainerMock.java @@ -152,7 +152,9 @@ public static KubernetesContainerClient getMockedKubernetesContainerClient() { execPaths.add(String.format("%stransfer-logs.sh%s", bashCommand, commandSuffix)); execPaths.add(String.format("%s/var/log/cont/&command=-c&command=.%s", tarCommand, commandSuffix)); execPaths.add(String.format("%s/videos/&command=-c&command=.%s", tarCommand, commandSuffix)); + execPaths.add(String.format("%s/videos/&command=-C&command=.%s", tarCommand, commandSuffix)); execPaths.add(String.format("%sstop-video%s", bashCommand, commandSuffix)); + execPaths.add(String.format("%sstart-video%s", bashCommand, commandSuffix)); execPaths.add(String.format("%stransfer-logs.sh%s", bashCommand, commandSuffix)); execPaths.add(String.format("%scleanup-container.sh%s", bashCommand, commandSuffix)); String notifyComplete = bashCommand diff --git a/src/test/java/de/zalando/ep/zalenium/util/TestUtils.java b/src/test/java/de/zalando/ep/zalenium/util/TestUtils.java index a95869d5b6..a6a2736f11 100644 --- a/src/test/java/de/zalando/ep/zalenium/util/TestUtils.java +++ b/src/test/java/de/zalando/ep/zalenium/util/TestUtils.java @@ -121,7 +121,7 @@ public String toString() { }; } - public static CommonProxyUtilities mockCommonProxyUtilitiesForDashboardTesting(TemporaryFolder temporaryFolder) { + public static CommonProxyUtilities mockCommonProxyUtilitiesForDashboardTesting(TemporaryFolder temporaryFolder) throws IOException { CommonProxyUtilities commonProxyUtilities = mock(CommonProxyUtilities.class); when(commonProxyUtilities.currentLocalPath()).thenReturn(temporaryFolder.getRoot().getAbsolutePath()); when(commonProxyUtilities.getShortDateAndTime()).thenCallRealMethod();