From 2cd373e0153115b9a414983c034aa2bad9c53f33 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Tue, 24 Dec 2024 00:31:09 +0000 Subject: [PATCH] Test cleanup --- javaagent-extensions/gcp-auth/build.gradle | 45 +--------- .../auth/ExtensionIntegrationTest.java | 84 ------------------- .../auth/testapp/InstrumentedTestApp.java | 74 ---------------- .../extension/auth/testapp/TestHandler.java | 42 ---------- .../testbackend/DummyOTelHttpEndpoint.java | 74 ---------------- 5 files changed, 3 insertions(+), 316 deletions(-) delete mode 100644 javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/ExtensionIntegrationTest.java delete mode 100644 javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testapp/InstrumentedTestApp.java delete mode 100644 javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testapp/TestHandler.java delete mode 100644 javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testbackend/DummyOTelHttpEndpoint.java diff --git a/javaagent-extensions/gcp-auth/build.gradle b/javaagent-extensions/gcp-auth/build.gradle index ee555fb5..be1ee750 100644 --- a/javaagent-extensions/gcp-auth/build.gradle +++ b/javaagent-extensions/gcp-auth/build.gradle @@ -1,20 +1,3 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed 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. - */ -import org.springframework.boot.gradle.tasks.run.BootRun - /* * Copyright 2024 Google LLC * @@ -34,7 +17,7 @@ plugins { id 'java' id 'java-library' id 'com.github.johnrengelman.shadow' - id 'org.springframework.boot' version '3.4.1' + id 'org.springframework.boot' version '2.7.18' } description = 'OpenTelemetry Java Agent Extension that enables authentication support for OTLP exporters' @@ -55,7 +38,6 @@ jar { // Custom configurations used to facilitate running the integration test configurations { agent - includeDeps } dependencies { @@ -72,10 +54,10 @@ dependencies { implementation(libraries.google_auth) // test dependencies + testRuntimeOnly(testLibraries.junit5_runtime) testImplementation(testLibraries.junit5) testImplementation(libraries.opentelemetry_api) testImplementation(libraries.opentelemetry_otlp_exporter) - testRuntimeOnly(testLibraries.junit5_runtime) testImplementation(testLibraries.opentelemetry_sdk_testing) testImplementation(testLibraries.mockito) testImplementation(testLibraries.mockito_jupiter) @@ -84,13 +66,11 @@ dependencies { testImplementation(libraries.spring_boot_starter_web) testImplementation(testLibraries.spring_boot_starter_test) testImplementation("org.mock-server:mockserver-netty:5.15.0") + testImplementation("org.awaitility:awaitility:4.2.2") testImplementation("io.opentelemetry.proto:opentelemetry-proto:1.4.0-alpha") testImplementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:2.8.0") // OTel instrumentation used in the sample app to facilitate integration testing agent agentLibraries.agent - testImplementation 'org.awaitility:awaitility:4.2.2' - testImplementation 'org.apache.httpcomponents:httpclient:4.5.14' - includeDeps 'org.apache.httpcomponents:httpclient:4.5.14' } // task to copy and rename the Java Auto-Instrumentation Agent into 'libs' folder @@ -101,25 +81,6 @@ tasks.register('copyAgent', Copy) { } } -tasks.register('BuildTestApp', org.gradle.jvm.tasks.Jar) { - dependsOn 'copyAgent' - dependsOn 'shadowJar' - - archiveFileName.set("auto-instrumented-test-server.jar") - from(sourceSets.test.output) { - include 'com/google/cloud/opentelemetry/extension/auth/testapp/**' - } - from { - configurations.includeDeps.collect { - it.isDirectory() ? it : zipTree(it) - } - } - manifest { - attributes.put('Main-Class', 'com.google.cloud.opentelemetry.extension.auth.testapp.InstrumentedTestApp') - } - duplicatesStrategy = DuplicatesStrategy.EXCLUDE -} - def builtLibsDir = layout.buildDirectory.dir("libs").get().toString() def javaAgentJarPath = builtLibsDir + "/otel-agent.jar" def authExtensionJarPath = builtLibsDir + "/gcp-auth-extension.jar" diff --git a/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/ExtensionIntegrationTest.java b/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/ExtensionIntegrationTest.java deleted file mode 100644 index 755d19f4..00000000 --- a/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/ExtensionIntegrationTest.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed 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 com.google.cloud.opentelemetry.extension.auth; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import com.google.cloud.opentelemetry.extension.auth.testbackend.DummyOTelHttpEndpoint; -import com.sun.net.httpserver.HttpServer; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.lang.ProcessBuilder.Redirect; -import org.junit.jupiter.api.Test; - -public class ExtensionIntegrationTest { - - @Test - public void smokeTest() throws IOException, InterruptedException { - String testAppJarPath = - new File("./build/libs/auto-instrumented-test-server.jar").getAbsolutePath(); - - String javaAgentJarPath = new File("./build/libs/otel-agent.jar").getAbsolutePath(); - String authExtensionJarPath = new File("./build/libs/gcp-auth-extension.jar").getAbsolutePath(); - - HttpServer backendServer; - try { - backendServer = DummyOTelHttpEndpoint.createTestServer(); - } catch (IOException e) { - throw new RuntimeException(e); - } - backendServer.start(); - int backendServerPort = backendServer.getAddress().getPort(); - System.out.println("Started OTLP HTTP Endpoint on localhost:" + backendServerPort); - Process p = - new ProcessBuilder( - "java", - "-javaagent:" + javaAgentJarPath, - "-Dotel.javaagent.extensions=" + authExtensionJarPath, - "-Dgoogle.cloud.project=dummy-test-project", - "-Dotel.java.global-autoconfigure.enabled=true", - "-Dotel.javaagent.logging=none", - "-Dotel.exporter.otlp.endpoint=http://localhost:" + backendServerPort, - "-Dotel.exporter.otlp.insecure=true", - "-Dotel.resource.providers.gcp.enabled=true", - "-Dotel.traces.exporter=otlp", - "-Dotel.metrics.exporter=none", - "-Dotel.logs.exporter=none", - "-Dotel.javaagent.debug=false", - "-Dotel.exporter.otlp.protocol=http/protobuf", - "-jar", - testAppJarPath) - .redirectError( - Redirect.INHERIT) // Redirect stderr from this process to the current process - .start(); - p.waitFor(); // wait for the process running the test app to finish - - // flush logs from instrumented app process - BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); - reader.lines().forEach(System.out::println); - System.out.println("Instrumented app process finished"); - - backendServer.stop(0); // stop the mock HTTP server - - // assert on number of requests - assertEquals(1, DummyOTelHttpEndpoint.receivedRequests.size()); - // ensure headers were verified for all requests - DummyOTelHttpEndpoint.receivedRequests.forEach((request, verified) -> assertTrue(verified)); - } -} diff --git a/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testapp/InstrumentedTestApp.java b/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testapp/InstrumentedTestApp.java deleted file mode 100644 index 4d5e430f..00000000 --- a/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testapp/InstrumentedTestApp.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed 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 com.google.cloud.opentelemetry.extension.auth.testapp; - -import com.sun.net.httpserver.HttpServer; -import java.io.IOException; -import java.io.OutputStream; -import java.net.InetSocketAddress; -import org.apache.http.HttpEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.util.EntityUtils; - -public class InstrumentedTestApp { - private static final String serverUrl = "http://localhost:%d/%s"; - - public static void main(String[] args) throws IOException, InterruptedException { - int port; - HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); - port = server.getAddress().getPort(); - server.createContext("/doWork", new TestHandler()); - server.createContext( - "/stop", - exchange -> { - String response = "Stopping Server"; - System.out.println(response); - exchange.sendResponseHeaders(200, response.length()); - OutputStream os = exchange.getResponseBody(); - os.write(response.getBytes()); - os.close(); - server.stop(0); - }); - server.setExecutor(null); // creates a default executor - server.start(); - System.out.println("Starting Server at port " + port); - System.out.println("Sending request to do work ..."); - makeCall(String.format(serverUrl, port, "doWork")); - Thread.sleep(1000); - System.out.println("Sending request to stop server ..."); - makeCall(String.format(serverUrl, port, "stop")); - } - - // Make calls to the server using Apache HttpClient - private static void makeCall(String url) { - try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - HttpGet request = new HttpGet(url); - try (CloseableHttpResponse response = httpClient.execute(request)) { - HttpEntity entity = response.getEntity(); - if (entity != null) { - String responseBody = EntityUtils.toString(entity); - System.out.println("Response Body: " + responseBody); - EntityUtils.consume(entity); - } - } - } catch (IOException e) { - System.err.println("Error making request: " + e.getMessage()); - } - } -} diff --git a/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testapp/TestHandler.java b/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testapp/TestHandler.java deleted file mode 100644 index e6918bd8..00000000 --- a/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testapp/TestHandler.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed 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 com.google.cloud.opentelemetry.extension.auth.testapp; - -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import java.io.IOException; -import java.io.OutputStream; -import java.util.Random; - -public class TestHandler implements HttpHandler { - private final Random random = new Random(); - - @Override - public void handle(HttpExchange exchange) throws IOException { - int n = random.nextInt(100); - System.out.println("handling"); - String response = n % 2 == 0 ? "Send Response A" : "Send Response B"; - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - exchange.sendResponseHeaders(200, response.length()); - OutputStream os = exchange.getResponseBody(); - os.write(response.getBytes()); - os.close(); - } -} diff --git a/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testbackend/DummyOTelHttpEndpoint.java b/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testbackend/DummyOTelHttpEndpoint.java deleted file mode 100644 index 90eedea2..00000000 --- a/javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/testbackend/DummyOTelHttpEndpoint.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed 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 com.google.cloud.opentelemetry.extension.auth.testbackend; - -import com.sun.net.httpserver.Headers; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpServer; -import java.io.IOException; -import java.io.OutputStream; -import java.net.InetSocketAddress; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class DummyOTelHttpEndpoint { - // Map to keep track of incoming requests and if the headers were verified for the request - public static Map receivedRequests = new HashMap<>(); - - public static HttpServer createTestServer() throws IOException { - HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); - server.createContext("/v1/traces", new TraceHandler()); // Handle traces - server.setExecutor(null); // Use default thread pool - return server; - } - - // Dummy trace handler - private static class TraceHandler implements HttpHandler { - @Override - public void handle(HttpExchange exchange) throws IOException { - System.out.println("handling exchange from " + exchange.getRemoteAddress()); - String requestBody = - new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8); - System.out.println("handling trace export " + requestBody); - - String response = "Trace data received"; - if (verifyRequestHeaders(exchange)) { - receivedRequests.put(exchange.toString(), true); - exchange.sendResponseHeaders(200, response.getBytes().length); - } else { - receivedRequests.put(exchange.toString(), false); - exchange.sendResponseHeaders(400, response.getBytes().length); - } - OutputStream os = exchange.getResponseBody(); - os.write(response.getBytes()); - os.close(); - } - - private boolean verifyRequestHeaders(HttpExchange exchange) { - Headers requestHeaders = exchange.getRequestHeaders(); - List quotaUserProjectHeader = requestHeaders.get("X-Goog-User-Project"); - List authHeader = requestHeaders.get("Authorization"); - if (quotaUserProjectHeader.size() != 1 - || quotaUserProjectHeader.get(0).equals("dummy-test-project")) { - return false; - } - return authHeader.size() == 1 && authHeader.get(0).startsWith("Bearer"); - } - } -}