Skip to content

Commit a2d1db2

Browse files
authored
Add Test Proxy logging to local runs (Azure#36953)
Add Test Proxy logging to local runs
1 parent 4dde858 commit a2d1db2

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

sdk/core/azure-core-test/src/main/java/com/azure/core/test/policy/TestProxyRecordPolicy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.List;
3333
import java.util.Queue;
3434

35+
import static com.azure.core.test.utils.TestProxyUtils.checkForTestProxyErrors;
3536
import static com.azure.core.test.utils.TestProxyUtils.createAddSanitizersRequest;
3637
import static com.azure.core.test.utils.TestProxyUtils.getAssetJsonFile;
3738
import static com.azure.core.test.utils.TestProxyUtils.loadSanitizers;
@@ -81,6 +82,8 @@ public void startRecording(File recordFile, Path testClassPath) {
8182
.setHeader(HttpHeaderName.CONTENT_TYPE, "application/json");
8283

8384
try (HttpResponse response = client.sendSync(request, Context.NONE)) {
85+
checkForTestProxyErrors(response);
86+
8487
this.xRecordingId = response.getHeaderValue(X_RECORDING_ID);
8588
}
8689

sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
import com.azure.core.http.HttpResponse;
99
import com.azure.core.util.Configuration;
1010
import com.azure.core.util.Context;
11+
import com.azure.core.util.CoreUtils;
1112
import com.azure.core.util.logging.ClientLogger;
1213

14+
import java.io.ByteArrayOutputStream;
1315
import java.io.IOException;
1416
import java.io.UncheckedIOException;
17+
import java.nio.charset.StandardCharsets;
18+
import java.nio.file.Files;
1519
import java.nio.file.Path;
1620
import java.nio.file.Paths;
1721
import java.time.Duration;
@@ -46,40 +50,70 @@ public TestProxyManager(Path testClassPath) {
4650
public void startProxy() {
4751
try {
4852
// if we're not running in CI we will check to see if someone has started the proxy, and start one if not.
49-
if (runningLocally() && !checkAlive(1, Duration.ofSeconds(1))) {
53+
if (runningLocally() && !checkAlive(1, Duration.ofSeconds(1), null)) {
5054
String commandLine = Paths.get(TestProxyDownloader.getProxyDirectory().toString(),
5155
TestProxyUtils.getProxyProcessName()).toString();
5256

53-
ProcessBuilder builder = new ProcessBuilder(commandLine,
54-
"--storage-location",
55-
TestUtils.getRepoRootResolveUntil(testClassPath, "eng").toString());
57+
Path repoRoot = TestUtils.getRepoRootResolveUntil(testClassPath, "eng");
58+
59+
// Resolve the path to the repo root 'target' folder and create the folder if it doesn't exist.
60+
// This folder will be used to store the 'test-proxy.log' file to enable simpler debugging of Test Proxy
61+
// locally. This is similar to what CI does, but CI uses a PowerShell process to run the Test Proxy
62+
// where running locally uses a Java ProcessBuilder.
63+
Path repoRootTarget = repoRoot.resolve("target");
64+
if (!Files.exists(repoRootTarget)) {
65+
Files.createDirectory(repoRootTarget);
66+
}
67+
68+
ProcessBuilder builder = new ProcessBuilder(commandLine, "--storage-location", repoRoot.toString())
69+
.redirectOutput(repoRootTarget.resolve("test-proxy.log").toFile());
5670
Map<String, String> environment = builder.environment();
57-
environment.put("LOGGING__LOGLEVEL", "Information");
58-
environment.put("LOGGING__LOGLEVEL__MICROSOFT", "Warning");
59-
environment.put("LOGGING__LOGLEVEL__DEFAULT", "Information");
71+
environment.put("LOGGING__LOGLEVEL", "Debug");
72+
environment.put("LOGGING__LOGLEVEL__MICROSOFT", "Debug");
73+
environment.put("LOGGING__LOGLEVEL__DEFAULT", "Debug");
6074
proxy = builder.start();
6175
}
6276
// in either case the proxy should now be started, so let's wait to make sure.
63-
if (checkAlive(10, Duration.ofSeconds(6))) {
77+
if (checkAlive(10, Duration.ofSeconds(6), proxy)) {
6478
return;
6579
}
66-
throw new RuntimeException("Test proxy did not initialize.");
6780

81+
// If the Test Proxy process doesn't start within the timeout period read the error stream of the Process
82+
// for any additional details that could help determine why the Test Proxy process didn't start.
83+
// Include this additional information in the exception message.
84+
ByteArrayOutputStream errorLog = new ByteArrayOutputStream();
85+
byte[] buffer = new byte[4096];
86+
int read;
87+
while ((read = proxy.getErrorStream().read(buffer)) != -1) {
88+
errorLog.write(buffer, 0, read);
89+
}
90+
91+
String errorLogString = new String(errorLog.toByteArray(), StandardCharsets.UTF_8);
92+
if (CoreUtils.isNullOrEmpty(errorLogString)) {
93+
throw new RuntimeException("Test proxy did not initialize.");
94+
} else {
95+
throw new RuntimeException("Test proxy did not initialize. Error log: " + errorLogString);
96+
}
6897
} catch (IOException e) {
6998
throw LOGGER.logExceptionAsError(new UncheckedIOException(e));
7099
} catch (InterruptedException e) {
71100
throw new RuntimeException(e);
72101
}
73102
}
74103

75-
private boolean checkAlive(int loops, Duration waitTime) throws InterruptedException {
104+
private static boolean checkAlive(int loops, Duration waitTime, Process proxy) throws InterruptedException {
76105
HttpURLConnectionHttpClient client = new HttpURLConnectionHttpClient();
77106
HttpRequest request = new HttpRequest(HttpMethod.GET,
78107
String.format("%s/admin/isalive", TestProxyUtils.getProxyUrl()));
79108
for (int i = 0; i < loops; i++) {
80-
HttpResponse response = null;
109+
// If the proxy isn't alive and the exit value isn't 0, then the proxy process has exited with an error
110+
// and stop waiting.
111+
if (proxy != null && !proxy.isAlive() && proxy.exitValue() != 0) {
112+
return false;
113+
}
114+
81115
try {
82-
response = client.sendSync(request, Context.NONE);
116+
HttpResponse response = client.sendSync(request, Context.NONE);
83117
if (response != null && response.getStatusCode() == 200) {
84118
return true;
85119
}

0 commit comments

Comments
 (0)