replace ad-hoc code with http_request (#31)
This commit is contained in:
Родитель
3a28918381
Коммит
e36795d361
16
README.md
16
README.md
|
@ -171,7 +171,20 @@ def sb = new org.mozilla.fxtest.ServiceBook()
|
|||
sb.testProject('kinto')
|
||||
```
|
||||
|
||||
## Version History
|
||||
## Requirements
|
||||
* [Pipeline Http Request Plugin] v1.8.20 or later.
|
||||
|
||||
# How to run tests
|
||||
|
||||
Make sure you have the latest Gradle install and run:
|
||||
|
||||
```sh
|
||||
$ gradle check
|
||||
BUILD SUCCESSFUL in 1s
|
||||
2 actionable tasks: 2 up-to-date
|
||||
```
|
||||
|
||||
# Version History
|
||||
|
||||
### 1.7 (2017-09-04)
|
||||
* Introduced ```ServiceBook``` class, with ```testProject``` method to execute tests for all pipeline associated with the specified project name.
|
||||
|
@ -206,3 +219,4 @@ sb.testProject('kinto')
|
|||
[Treeherder]: https://wiki.mozilla.org/Auto-tools/Projects/Treeherder
|
||||
[pytest-selenium]: http://pytest-selenium.readthedocs.io/en/latest/user_guide.html#capabilities-files
|
||||
[Pipeline Model Definition Plugin]: https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Model+Definition+Plugin
|
||||
[Pipeline Http Request Plugin]: https://wiki.jenkins.io/display/JENKINS/HTTP+Request+Plugin
|
||||
|
|
|
@ -1,85 +1,15 @@
|
|||
package org.mozilla.fxtest
|
||||
|
||||
import groovy.json.JsonSlurperClassic;
|
||||
|
||||
|
||||
HttpResponse doGetHttpRequest(String requestUrl){
|
||||
URL url = new URL(requestUrl);
|
||||
HttpURLConnection connection = url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.connect();
|
||||
HttpResponse resp = new HttpResponse(connection);
|
||||
|
||||
if(resp.isFailure()){
|
||||
error("\nGET from URL: $requestUrl\n HTTP Status: $resp.statusCode\n Message: $resp.message\n Response Body: $resp.body");
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
HttpResponse doPostHttpRequestWithJson(String json, String requestUrl){
|
||||
return doHttpRequestWithJson(json, requestUrl, "POST");
|
||||
}
|
||||
|
||||
HttpResponse doPutHttpRequestWithJson(String json, String requestUrl){
|
||||
return doHttpRequestWithJson(json, requestUrl, "PUT");
|
||||
}
|
||||
|
||||
HttpResponse doHttpRequestWithJson(String json, String requestUrl, String verb){
|
||||
URL url = new URL(requestUrl);
|
||||
HttpURLConnection connection = url.openConnection();
|
||||
|
||||
connection.setRequestMethod(verb);
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.doOutput = true;
|
||||
|
||||
def writer = new OutputStreamWriter(connection.outputStream);
|
||||
writer.write(json);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
|
||||
connection.connect();
|
||||
|
||||
//parse the response
|
||||
HttpResponse resp = new HttpResponse(connection);
|
||||
|
||||
if(resp.isFailure()){
|
||||
error("\n$verb to URL: $requestUrl\n JSON: $json\n HTTP Status: $resp.statusCode\n Message: $resp.message\n Response Body: $resp.body");
|
||||
}
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
class HttpResponse {
|
||||
String body;
|
||||
String message;
|
||||
Integer statusCode;
|
||||
boolean failure = false;
|
||||
|
||||
public HttpResponse(HttpURLConnection connection){
|
||||
this.statusCode = connection.responseCode;
|
||||
this.message = connection.responseMessage;
|
||||
|
||||
if(statusCode == 200 || statusCode == 201){
|
||||
this.body = connection.content.text;//this would fail the pipeline if there was a 400
|
||||
}else{
|
||||
this.failure = true;
|
||||
this.body = connection.getErrorStream().text;
|
||||
}
|
||||
|
||||
connection = null; //set connection to null for good measure, since we are done with it
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Servicebook iterator to get operational tests for a given project
|
||||
Object getProjectTests(String name) {
|
||||
resp = doGetHttpRequest("https://servicebook.stage.mozaws.net/api/project").body;
|
||||
def projectName = name.toLowerCase();
|
||||
def resp = httpRequest "https://servicebook.stage.mozaws.net/api/project";
|
||||
def jsonSlurper = new JsonSlurperClassic();
|
||||
def projects = jsonSlurper.parseText(resp);
|
||||
def projects = jsonSlurper.parseText(resp.content);
|
||||
|
||||
for (project in projects.data) {
|
||||
def projectName = name.toLowerCase();
|
||||
if (project.name == projectName) {
|
||||
if (project.name.toLowerCase() == projectName) {
|
||||
echo "The project " + name + " was found!"
|
||||
|
||||
def jenkin_tests = [];
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -12,6 +12,9 @@ class ServiceBookTests extends BasePipelineTest {
|
|||
@Before
|
||||
void setUp() throws Exception {
|
||||
super.setUp()
|
||||
String fileContents = new File('tests/projects.json').text
|
||||
def resp = [content: fileContents]
|
||||
helper.registerAllowedMethod('httpRequest', [String.class], {url -> resp})
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -30,12 +33,20 @@ class ServiceBookTests extends BasePipelineTest {
|
|||
}
|
||||
}
|
||||
|
||||
// @Test
|
||||
// void projectWithTests() {
|
||||
// def resp = [body:'{"data": [{"foo": {"tests": [{"name": "bar", "jenkins_pipeline": true}]}}]}']
|
||||
// def script = loadScript('src/org/mozilla/fxtest/ServiceBook.groovy')
|
||||
// script.doGetHttpRequest = { String url -> [resp] }
|
||||
// def tests = script.getProjectTests('foo')
|
||||
// assert tests != null
|
||||
// }
|
||||
@Test
|
||||
void projectWithTests() {
|
||||
def script = loadScript('src/org/mozilla/fxtest/ServiceBook.groovy')
|
||||
|
||||
// kinto has 1 jenkins enabled project
|
||||
def tests = script.getProjectTests('kinto')
|
||||
assert tests != null
|
||||
assert tests.size() == 1
|
||||
|
||||
// this project does not exists
|
||||
assert script.getProjectTests('IDONTEXIST') == null
|
||||
|
||||
// Balrog has zero jenkins tests
|
||||
def balrog = script.getProjectTests('Balrog')
|
||||
assert balrog.size() == 0
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче