From 45296dc9a580baae739a0e9f5a3c6eb7df469443 Mon Sep 17 00:00:00 2001 From: Mike Place Date: Fri, 2 Apr 2021 13:51:07 +0200 Subject: [PATCH 1/6] Add logging --- vars/buildStatus.groovy | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vars/buildStatus.groovy b/vars/buildStatus.groovy index b8b101339..856b00d46 100644 --- a/vars/buildStatus.groovy +++ b/vars/buildStatus.groovy @@ -59,6 +59,7 @@ private static URL constructURL(String host, ArrayList job, boolean ssl) throws String uri if (ssl){ uri = "https://${host}/buildStatus/text?job=${job_path}" + log(level: 'INFO', text: "Querying job status at: ${uri}") } else { uri = "http://${host}/buildStatus/text?job=${job_path}" } @@ -72,6 +73,7 @@ def call(Map args = [:]) { def return_boolean = args.get('return_boolean', false) def ssl = args.get('ssl', true) def result = makeRequest(constructURL(host, job, ssl)) + log(level: 'INFO', text: "Request result: ${result}") if (return_boolean){ if (result == "Success") { return true From 97569f84f20bf6e3992e630b9d1fcc88d701c0dd Mon Sep 17 00:00:00 2001 From: Mike Place Date: Fri, 2 Apr 2021 14:40:09 +0200 Subject: [PATCH 2/6] Modify logging --- vars/buildStatus.groovy | 1 - 1 file changed, 1 deletion(-) diff --git a/vars/buildStatus.groovy b/vars/buildStatus.groovy index 856b00d46..e85d60c26 100644 --- a/vars/buildStatus.groovy +++ b/vars/buildStatus.groovy @@ -59,7 +59,6 @@ private static URL constructURL(String host, ArrayList job, boolean ssl) throws String uri if (ssl){ uri = "https://${host}/buildStatus/text?job=${job_path}" - log(level: 'INFO', text: "Querying job status at: ${uri}") } else { uri = "http://${host}/buildStatus/text?job=${job_path}" } From 1250c850bae757687342519e591760319332bdc4 Mon Sep 17 00:00:00 2001 From: Mike Place Date: Fri, 2 Apr 2021 14:50:00 +0200 Subject: [PATCH 3/6] Modify logging --- vars/buildStatus.groovy | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vars/buildStatus.groovy b/vars/buildStatus.groovy index e85d60c26..59cde69ef 100644 --- a/vars/buildStatus.groovy +++ b/vars/buildStatus.groovy @@ -41,12 +41,16 @@ private static String makeRequest(URL url) throws IOException { InputStreamReader isr = new InputStreamReader(con.getInputStream()) BufferedReader brd = new BufferedReader(isr) if (responseCode == HttpURLConnection.HTTP_OK) { + log(level: 'INFO', text: "Received HTTP OK") String inputLine StringBuffer response = new StringBuffer() while ((inputLine = brd.readLine()) != null) { + log(level: 'INFO', text: "Adding text") response.append(inputLine) } brd.close() + def decoded_response = response.toString() + log(level: 'INFO', text: "Text of response: ${decoded_response}") return response.toString() } else { raise IOException("Failure to connect to Jenkins instance") From 723727b0e57b3dc21dfd8df4a3d103a72f50119a Mon Sep 17 00:00:00 2001 From: Mike Place Date: Fri, 2 Apr 2021 14:58:06 +0200 Subject: [PATCH 4/6] Modify logging --- vars/buildStatus.groovy | 2 -- 1 file changed, 2 deletions(-) diff --git a/vars/buildStatus.groovy b/vars/buildStatus.groovy index 59cde69ef..c3358d5e8 100644 --- a/vars/buildStatus.groovy +++ b/vars/buildStatus.groovy @@ -41,11 +41,9 @@ private static String makeRequest(URL url) throws IOException { InputStreamReader isr = new InputStreamReader(con.getInputStream()) BufferedReader brd = new BufferedReader(isr) if (responseCode == HttpURLConnection.HTTP_OK) { - log(level: 'INFO', text: "Received HTTP OK") String inputLine StringBuffer response = new StringBuffer() while ((inputLine = brd.readLine()) != null) { - log(level: 'INFO', text: "Adding text") response.append(inputLine) } brd.close() From 01adebe74368035a6c0a80a22e62d5a7f82de5d3 Mon Sep 17 00:00:00 2001 From: Mike Place Date: Fri, 2 Apr 2021 15:13:29 +0200 Subject: [PATCH 5/6] Use standard HTTP request step --- vars/buildStatus.groovy | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/vars/buildStatus.groovy b/vars/buildStatus.groovy index c3358d5e8..78f3b8542 100644 --- a/vars/buildStatus.groovy +++ b/vars/buildStatus.groovy @@ -25,35 +25,7 @@ buildStatus( **/ import java.net.URL -import java.net.HttpURLConnection -import java.io.BufferedReader -import java.io.InputStreamReader - - -private static String makeRequest(URL url) throws IOException { - // URL url = new URL("${baseUrl}/${path}") - HttpURLConnection con = (HttpURLConnection)url.openConnection() - con.setRequestMethod("GET") - con.setRequestProperty("User-Agent", "Jenkins Build Status/1.0") - int responseCode = con.getResponseCode() - - InputStreamReader isr = new InputStreamReader(con.getInputStream()) - BufferedReader brd = new BufferedReader(isr) - if (responseCode == HttpURLConnection.HTTP_OK) { - String inputLine - StringBuffer response = new StringBuffer() - while ((inputLine = brd.readLine()) != null) { - response.append(inputLine) - } - brd.close() - def decoded_response = response.toString() - log(level: 'INFO', text: "Text of response: ${decoded_response}") - return response.toString() - } else { - raise IOException("Failure to connect to Jenkins instance") - } -} private static URL constructURL(String host, ArrayList job, boolean ssl) throws Exception { String delim = "%2F" @@ -73,7 +45,7 @@ def call(Map args = [:]) { def job = args.get('job', []) def return_boolean = args.get('return_boolean', false) def ssl = args.get('ssl', true) - def result = makeRequest(constructURL(host, job, ssl)) + def result = httpRequest(url: constructURL(host, job, ssl)) log(level: 'INFO', text: "Request result: ${result}") if (return_boolean){ if (result == "Success") { From 8134ac8c3fb00b92cd3be3aa3ef179edf6d6f712 Mon Sep 17 00:00:00 2001 From: Mike Place Date: Fri, 2 Apr 2021 18:39:05 +0200 Subject: [PATCH 6/6] Migrate to httpRequest --- src/test/groovy/BuildStatusTests.groovy | 27 ++----------------------- vars/buildStatus.groovy | 4 ++-- 2 files changed, 4 insertions(+), 27 deletions(-) diff --git a/src/test/groovy/BuildStatusTests.groovy b/src/test/groovy/BuildStatusTests.groovy index 437ded11e..fac410ec8 100644 --- a/src/test/groovy/BuildStatusTests.groovy +++ b/src/test/groovy/BuildStatusTests.groovy @@ -19,44 +19,20 @@ import org.junit.Before import org.junit.After import org.junit.Test import static org.junit.Assert.assertTrue -import com.sun.net.httpserver.HttpServer -import com.sun.net.httpserver.HttpContext -import com.sun.net.httpserver.HttpExchange -import com.sun.net.httpserver.HttpHandler class BuildStatusTests extends ApmBasePipelineTest { - // Build a small test server - def i = new InetSocketAddress('localhost', 9999) - def HttpServer ws = HttpServer.create(i, 100) - HttpContext job_status_context = ws.createContext("/buildStatus/text") - - @Override @Before void setUp() throws Exception { super.setUp() script = loadScript('vars/buildStatus.groovy') - job_status_context.setHandler({ exchange -> - String response = "Success" - exchange.responseHeaders.set("Content-Type", "text/plain;charset=utf-8") - exchange.sendResponseHeaders(200, response.getBytes().length); - OutputStream os = exchange.getResponseBody(); - os.write(response.getBytes()); - os.close(); - exchange.Send - }); - ws.start() - } - - @After - void tearDown() throws Exception { - ws.stop(0) } @Test void test() throws Exception { + helper.registerAllowedMethod("httpRequest", [Map.class], { "Success" }) def result = script.call(host: 'localhost:9999', job: ['apm-agent-java', 'apm-agent-java-mbp', 'master'], ssl: false) assertTrue("Success" == result) assertJobStatusSuccess() @@ -64,6 +40,7 @@ class BuildStatusTests extends ApmBasePipelineTest { @Test void testBoolSuccess() throws Exception { + helper.registerAllowedMethod("httpRequest", [Map.class], { "Success" }) def result = script.call(host: 'localhost:9999', job: ['apm-agent-java', 'apm-agent-java-mbp', 'master'], return_boolean: true, ssl: false) assertTrue(result) } diff --git a/vars/buildStatus.groovy b/vars/buildStatus.groovy index 78f3b8542..a9b42d4b9 100644 --- a/vars/buildStatus.groovy +++ b/vars/buildStatus.groovy @@ -45,8 +45,8 @@ def call(Map args = [:]) { def job = args.get('job', []) def return_boolean = args.get('return_boolean', false) def ssl = args.get('ssl', true) - def result = httpRequest(url: constructURL(host, job, ssl)) - log(level: 'INFO', text: "Request result: ${result}") + def to_url = constructURL(host, job, ssl).toString() + def result = httpRequest(url: to_url) if (return_boolean){ if (result == "Success") { return true