Skip to content
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/server/src/org/openqa/grid/web/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openqa.grid.web.servlet.HubStatusServlet;
import org.openqa.grid.web.servlet.HubW3CStatusServlet;
import org.openqa.grid.web.servlet.LifecycleServlet;
import org.openqa.grid.web.servlet.NodeSessionsServlet;
import org.openqa.grid.web.servlet.ProxyStatusServlet;
import org.openqa.grid.web.servlet.RegistrationServlet;
import org.openqa.grid.web.servlet.ResourceServlet;
Expand Down Expand Up @@ -130,6 +131,7 @@ private void addDefaultServlets(ServletContextHandler handler) {
handler.addServlet(DriverServlet.class.getName(), "/selenium-server/driver/*");

handler.addServlet(ProxyStatusServlet.class.getName(), "/grid/api/proxy/*");
handler.addServlet(NodeSessionsServlet.class.getName(), "/grid/api/sessions/*");

handler.addServlet(HubStatusServlet.class.getName(), "/grid/api/hub/*");

Expand Down
119 changes: 119 additions & 0 deletions java/server/src/org/openqa/grid/web/servlet/NodeSessionsServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// 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.grid.web.servlet;

import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonException;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.json.JsonOutput;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* API to query all the sessions that are currently running in the hub.
*/
public class NodeSessionsServlet extends RegistryBasedServlet {

private final Json json = new Json();

public NodeSessionsServlet() {
this(null);
}

public NodeSessionsServlet(GridRegistry registry) {
super(registry);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
process(rsp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
process(resp);
}

protected void process(HttpServletResponse response) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setStatus(200);
Map<String, Object> proxies = new TreeMap<>();
try (Writer writer = response.getWriter();
JsonOutput out = json.newOutput(writer)) {
proxies.put("success", true);
proxies.put("proxies", extractSessionsFromAllProxies());
out.write(proxies);
}
}

private List<Map<String, Object>> extractSessionsFromAllProxies() {
List<Map<String, Object>> results = new LinkedList<>();
List<RemoteProxy> proxies = getRegistry().getAllProxies().getBusyProxies();
for (RemoteProxy proxy : proxies) {
Map<String, Object> res = new TreeMap<>();
res.put("id", proxy.getId());
res.put("remoteHost", proxy.getRemoteHost().toString());
Map<String, Object> sessionsInProxy = new TreeMap<>(extractSessionInfo(proxy));
if (sessionsInProxy.isEmpty()) {
sessionsInProxy.put("success", false);
}
res.put("sessions", sessionsInProxy);
results.add(res);
}
return results;
}

private Map<String, Object> extractSessionInfo(RemoteProxy proxy) {
try {
URL url = proxy.getRemoteHost();
HttpRequest req = new HttpRequest(HttpMethod.GET, "/wd/hub/sessions");
HttpResponse rsp = proxy.getHttpClient(url).execute(req);

try (InputStream in = new ByteArrayInputStream(rsp.getContent());
Reader reader = new InputStreamReader(in, rsp.getContentEncoding());
JsonInput jsonReader = json.newInput(reader)){
return jsonReader.read(Json.MAP_TYPE);
} catch (JsonException e) {
// Nothing to do --- poorly formed payload.
}
} catch (IOException e) {
throw new GridException(e.getMessage());
}
return new TreeMap<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to indicate some form of error has occurred?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it. The status gets flipped to false if there was any difficulty in extracting sessions info

}
}
2 changes: 2 additions & 0 deletions java/server/test/org/openqa/grid/e2e/GridE2ETests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openqa.grid.e2e.misc.ConfigInheritanceTest;
import org.openqa.grid.e2e.misc.Grid1HeartbeatTest;
import org.openqa.grid.e2e.misc.GridDistributionTest;
import org.openqa.grid.e2e.misc.GridListActiveSessionsTest;
import org.openqa.grid.e2e.misc.GridSerializeExceptionTest;
import org.openqa.grid.e2e.misc.GridViaCommandLineTest;
import org.openqa.grid.e2e.misc.HubRestart;
Expand Down Expand Up @@ -58,6 +59,7 @@
NodeTimeOutTest.class,
SmokeTest.class, // slow
WebDriverPriorityDemo.class,
GridListActiveSessionsTest.class
})
public class GridE2ETests {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// 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.grid.e2e.misc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.openqa.grid.e2e.node.SmokeTest;
import org.openqa.grid.web.Hub;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@SuppressWarnings("unchecked")
public class GridListActiveSessionsTest {

@Test
public void testNoSessions() throws Exception {
runTest(1, 0, map -> {
List<Map<String, Object>> proxies = extractProxies(map);
assertTrue("No sessions data should be found", proxies.isEmpty());
});
}

@Test
public void testOneSessionWithSingleProxy() throws Exception {
runTestForMultipleSessions(1, 1, 1);
}

@Test
public void testMultipleSessionsWithSingleProxy() throws Exception {
runTestForMultipleSessions(1, 2, 2);
}

@Test
public void testMultipleSessionsWithMultipleProxies() throws Exception {
runTestForMultipleSessions(2, 2, 1);
}

private void runTestForMultipleSessions(int howManyNodes, int howManySessions,
int expectedSessionsPerProxy) throws Exception {
runTest(howManyNodes,
howManySessions,
map -> {
List<Map<String, Object>> proxies = extractProxies(map);
assertEquals("Number of proxies", howManyNodes, proxies.size());
for (Map<String, Object> proxy : proxies) {
Map<String, Object> sessions = (Map<String, Object>) proxy.get("sessions");
List<?> values = (List<?>) sessions.get("value");
assertEquals("Sessions per proxy", expectedSessionsPerProxy,
values.size());
}
});
}

private void runTest(int nodesCount, int howMany,
Consumer<Map<String, Object>> assertions) throws Exception {
Hub hub = null;
List<RemoteWebDriver> drivers = new ArrayList<>();
try {
hub = SmokeTest.prepareTestGrid(DesiredCapabilities.chrome(), nodesCount);
drivers = createSession(howMany, hub);
Map<String, Object> sessions = getSessions(hub);
assertions.accept(sessions);
} finally {
drivers.forEach(RemoteWebDriver::quit);
if (hub != null) {
hub.stop();
}
}
}

private List<RemoteWebDriver> createSession(int howMany, Hub hub) {
List<RemoteWebDriver> drivers = new ArrayList<>();
if (howMany == 0) {
return drivers;
}
URL url;
try {
url = new URL("http://" + hub.getUrl().getHost() + ":" +
hub.getUrl().getPort() + "/wd/hub");
} catch (MalformedURLException e) {
return new ArrayList<>();
}
for (int i = 0; i < howMany; i++) {
drivers.add(new RemoteWebDriver(url, new ChromeOptions()));
}
return drivers;

}

private Map<String, Object> getSessions(Hub hub) throws IOException {
String url = String.format("http://%s:%d/grid/api/sessions", hub.getUrl().getHost(),
hub.getUrl().getPort());
URL grid = new URL(url);
URLConnection connection = grid.openConnection();
try (InputStream in = connection.getInputStream();
JsonInput input = new Json().newInput(new BufferedReader(new InputStreamReader(in)))) {
return input.read(Json.MAP_TYPE);

}
}

private List<Map<String, Object>> extractProxies(Map<String, Object> map) {
boolean success = Boolean.parseBoolean(map.get("success").toString());
assertTrue("Status should be true", success);
return (List<Map<String, Object>>) map.get("proxies");
}

}
46 changes: 28 additions & 18 deletions java/server/test/org/openqa/grid/e2e/node/SmokeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,7 @@ public class SmokeTest {

@Before
public void prepare() throws Exception {

hub = GridTestHelper.getHub();

SelfRegisteringRemote remote =
GridTestHelper.getRemoteWithoutCapabilities(hub.getUrl(), GridRole.NODE);
remote.addBrowser(GridTestHelper.getDefaultBrowserCapability(), 1);

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setCapability(RegistrationRequest.SELENIUM_PROTOCOL,SeleniumProtocol.WebDriver);

remote.addBrowser(capabilities, 1);

remote.setRemoteServer(new SeleniumServer(remote.getConfiguration()));
remote.startRemoteServer();

remote.getConfiguration().timeout = -1;
remote.sendRegistrationRequest();
RegistryTestHelper.waitForNode(hub.getRegistry(), 1);
hub = prepareTestGrid();
}

@Test
Expand All @@ -79,4 +62,31 @@ public void browserOnWebDriver() {
public void stop() {
hub.stop();
}

public static Hub prepareTestGrid() throws Exception {
return prepareTestGrid(DesiredCapabilities.htmlUnit(),1);
}

public static Hub prepareTestGrid(DesiredCapabilities caps, int nodesCount) throws Exception {
Hub hub = GridTestHelper.getHub();
for (int i = 1; i <= nodesCount; i++) {

SelfRegisteringRemote remote =
GridTestHelper.getRemoteWithoutCapabilities(hub.getUrl(), GridRole.NODE);
remote.addBrowser(caps, 1);

DesiredCapabilities capabilities = new DesiredCapabilities(caps);
caps.setCapability(RegistrationRequest.SELENIUM_PROTOCOL, SeleniumProtocol.WebDriver);

remote.addBrowser(capabilities, 1);

remote.setRemoteServer(new SeleniumServer(remote.getConfiguration()));
remote.startRemoteServer();

remote.getConfiguration().timeout = -1;
remote.sendRegistrationRequest();
RegistryTestHelper.waitForNode(hub.getRegistry(), i);
}
return hub;
}
}