-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
A Hub API that can query all running sessions. #6158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
java/server/src/org/openqa/grid/web/servlet/NodeSessionsServlet.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
java/server/test/org/openqa/grid/e2e/misc/GridListActiveSessionsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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