Skip to content

Commit f80d779

Browse files
authored
Azure Monitor Query: Migrate to test proxy (Azure#34542)
* Azure Monitor Query: Migrate to test proxy * add comment * fix recording * remove unused imports
1 parent 65e6ed3 commit f80d779

File tree

46 files changed

+56466
-1229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+56466
-1229
lines changed

sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/LogsQueryAsyncClientTest.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import com.azure.core.http.HttpClient;
1010
import com.azure.core.http.policy.RetryPolicy;
1111
import com.azure.core.http.policy.RetryStrategy;
12-
import com.azure.core.test.TestBase;
1312
import com.azure.core.test.TestMode;
13+
import com.azure.core.test.TestProxyTestBase;
1414
import com.azure.core.test.http.AssertingHttpClientBuilder;
1515
import com.azure.core.util.Configuration;
1616
import com.azure.core.util.Context;
@@ -23,9 +23,7 @@
2323
import com.azure.monitor.query.models.LogsQueryResult;
2424
import com.azure.monitor.query.models.LogsQueryResultStatus;
2525
import com.azure.monitor.query.models.QueryTimeInterval;
26-
import org.junit.jupiter.api.Assumptions;
2726
import org.junit.jupiter.api.BeforeEach;
28-
import org.junit.jupiter.api.Disabled;
2927
import org.junit.jupiter.api.Test;
3028
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
3129
import reactor.core.publisher.Mono;
@@ -38,7 +36,6 @@
3836
import java.util.Arrays;
3937
import java.util.LinkedHashMap;
4038
import java.util.List;
41-
import java.util.Random;
4239

4340
import static org.junit.jupiter.api.Assertions.assertEquals;
4441
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -48,7 +45,7 @@
4845
/**
4946
* Unit tests for {@link LogsQueryAsyncClient}.
5047
*/
51-
public class LogsQueryAsyncClientTest extends TestBase {
48+
public class LogsQueryAsyncClientTest extends TestProxyTestBase {
5249

5350
private static final String WORKSPACE_ID = Configuration.getGlobalConfiguration()
5451
.get("AZURE_MONITOR_LOGS_WORKSPACE_ID", "d2d0e126-fa1e-4b0a-b647-250cdd471e68");
@@ -130,18 +127,12 @@ public void testLogsResourceQuery() {
130127

131128
@Test
132129
public void testLogsQueryAllowPartialSuccess() {
133-
Assumptions.assumeTrue(getTestMode() == TestMode.PLAYBACK,
134-
"This test only executes in playback because the partial success condition requires pre-populated data.");
135-
136130
// Arrange
137-
final String query = "AppTraces \n"
138-
+ "| where Properties !has \"PartitionPumpManager\"\n"
139-
+ "| where Properties has \"LoggerName\" and Properties has_cs \"com.azure\"\n"
140-
+ "| project TimeGenerated, Message, Properties\n"
141-
+ "| extend m = parse_json(Message)\n"
142-
+ "| extend p = parse_json(Properties)\n"
143-
+ " | project TimeGenerated, Thread=p.ThreadName, Logger=p.LoggerName, ConnectionId=m.connectionId, Message\n"
144-
+ "\n";
131+
final String query = "let dt = datatable (DateTime: datetime, Bool:bool, Guid: guid, Int: "
132+
+ "int, Long:long, Double: double, String: string, Timespan: timespan, Decimal: decimal, Dynamic: dynamic)\n"
133+
+ "[datetime(2015-12-31 23:59:59.9), false, guid(74be27de-1e4e-49d9-b579-fe0b331d3642), 12345, 1, 12345.6789,"
134+
+ " 'string value', 10s, decimal(0.10101), dynamic({\"a\":123, \"b\":\"hello\", \"c\":[1,2,3], \"d\":{}})];"
135+
+ "range x from 1 to 400000 step 1 | extend y=1 | join kind=fullouter dt on $left.y == $right.Long";
145136

146137
final LogsQueryOptions options = new LogsQueryOptions().setAllowPartialErrors(true);
147138
final QueryTimeInterval interval = QueryTimeInterval.LAST_DAY;
@@ -314,13 +305,13 @@ public void testBatchStatistics() {
314305
+ " not readily reproducible and because the service caches query results, the queries that require extended time "
315306
+ "to complete if run the first time can return immediately if a cached result is available. So, this test can "
316307
+ " wait for a long time before succeeding. So, disabling this in LIVE test mode")
317-
@Disabled
318308
public void testServerTimeout() {
319309
// The server does not always stop processing the request and return a 504 before the client times out
320310
// so, retry until a 504 response is returned
321-
Random random = new Random();
322-
// add some random number to circumvent cached response from server
323-
long count = 1000000000000L + random.nextInt(10000);
311+
// With test proxy migration, the request body is also recorded and the request has to match exactly for the
312+
// recording to work. So, updating the exact count used to record the server timeout exception. When re-recording,
313+
// add a random number to this to bypass the server from returning cached results.
314+
long count = 1000000006959L;
324315
// this query should take more than 5 seconds usually, but the server may have cached the
325316
// response and may return before 5 seconds. So, retry with another query (different count value)
326317
StepVerifier.create(client.queryWorkspaceWithResponse(WORKSPACE_ID, "range x from 1 to " + count + " "

sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/LogsQueryClientTest.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import com.azure.core.http.policy.RetryPolicy;
1111
import com.azure.core.http.policy.RetryStrategy;
1212
import com.azure.core.http.rest.Response;
13-
import com.azure.core.test.TestBase;
1413
import com.azure.core.test.TestMode;
14+
import com.azure.core.test.TestProxyTestBase;
1515
import com.azure.core.test.http.AssertingHttpClientBuilder;
1616
import com.azure.core.util.Configuration;
1717
import com.azure.core.util.Context;
@@ -24,7 +24,6 @@
2424
import com.azure.monitor.query.models.LogsQueryResult;
2525
import com.azure.monitor.query.models.LogsQueryResultStatus;
2626
import com.azure.monitor.query.models.QueryTimeInterval;
27-
import org.junit.jupiter.api.Assumptions;
2827
import org.junit.jupiter.api.BeforeEach;
2928
import org.junit.jupiter.api.Test;
3029
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
@@ -37,7 +36,6 @@
3736
import java.util.Arrays;
3837
import java.util.LinkedHashMap;
3938
import java.util.List;
40-
import java.util.Random;
4139

4240
import static com.azure.monitor.query.LogsQueryAsyncClientTest.RESOURCE_ID;
4341
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -48,7 +46,7 @@
4846
/**
4947
* Unit tests for {@link LogsQueryClient}
5048
*/
51-
public class LogsQueryClientTest extends TestBase {
49+
public class LogsQueryClientTest extends TestProxyTestBase {
5250

5351
private static final String WORKSPACE_ID = Configuration.getGlobalConfiguration()
5452
.get("AZURE_MONITOR_LOGS_WORKSPACE_ID", "d2d0e126-fa1e-4b0a-b647-250cdd471e68");
@@ -121,18 +119,13 @@ public void testLogsQueryResource() {
121119

122120
@Test
123121
public void testLogsQueryAllowPartialSuccess() {
124-
Assumptions.assumeTrue(getTestMode() == TestMode.PLAYBACK,
125-
"This test only executes in playback because the partial success condition requires pre-populated data.");
126122

127123
// Arrange
128-
final String query = "AppTraces \n"
129-
+ "| where Properties !has \"PartitionPumpManager\"\n"
130-
+ "| where Properties has \"LoggerName\" and Properties has_cs \"com.azure\"\n"
131-
+ "| project TimeGenerated, Message, Properties\n"
132-
+ "| extend m = parse_json(Message)\n"
133-
+ "| extend p = parse_json(Properties)\n"
134-
+ " | project TimeGenerated, Thread=p.ThreadName, Logger=p.LoggerName, ConnectionId=m.connectionId, Message\n"
135-
+ "\n";
124+
final String query = "let dt = datatable (DateTime: datetime, Bool:bool, Guid: guid, Int: "
125+
+ "int, Long:long, Double: double, String: string, Timespan: timespan, Decimal: decimal, Dynamic: dynamic)\n"
126+
+ "[datetime(2015-12-31 23:59:59.9), false, guid(74be27de-1e4e-49d9-b579-fe0b331d3642), 12345, 1, 12345.6789,"
127+
+ " 'string value', 10s, decimal(0.10101), dynamic({\"a\":123, \"b\":\"hello\", \"c\":[1,2,3], \"d\":{}})];"
128+
+ "range x from 1 to 400000 step 1 | extend y=1 | join kind=fullouter dt on $left.y == $right.Long";
136129

137130
final LogsQueryOptions options = new LogsQueryOptions().setAllowPartialErrors(true);
138131
final QueryTimeInterval interval = QueryTimeInterval.LAST_DAY;
@@ -290,10 +283,11 @@ public void testBatchStatistics() {
290283
public void testServerTimeout() {
291284
// The server does not always stop processing the request and return a 504 before the client times out
292285
// so, retry until a 504 response is returned
293-
Random random = new Random();
294286
while (true) {
295-
// add some random number to circumvent cached response from server
296-
long count = 1000000000000L + random.nextInt(10000);
287+
// With test proxy migration, the request body is also recorded and the request has to match exactly for the
288+
// recording to work. So, updating the exact count used to record the server timeout exception. When re-recording,
289+
// add a random number to this to bypass the server from returning cached results.
290+
long count = 1000000007696L;
297291
try {
298292
// this query should take more than 5 seconds usually, but the server may have cached the
299293
// response and may return before 5 seconds. So, retry with another query (different count value)

sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MetricsQueryAsyncClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import com.azure.core.credential.AccessToken;
77
import com.azure.core.credential.TokenCredential;
88
import com.azure.core.http.HttpClient;
9-
import com.azure.core.test.TestBase;
109
import com.azure.core.test.TestMode;
10+
import com.azure.core.test.TestProxyTestBase;
1111
import com.azure.core.test.http.AssertingHttpClientBuilder;
1212
import com.azure.core.util.Configuration;
1313
import com.azure.core.util.Context;
@@ -42,7 +42,7 @@
4242
/**
4343
* Unit tests for {@link MetricsQueryAsyncClient}.
4444
*/
45-
public class MetricsQueryAsyncClientTest extends TestBase {
45+
public class MetricsQueryAsyncClientTest extends TestProxyTestBase {
4646
private static final String RESOURCE_URI = Configuration.getGlobalConfiguration()
4747
.get("AZURE_MONITOR_METRICS_RESOURCE_URI",
4848
"/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/srnagar-azuresdkgroup/providers/Microsoft.CognitiveServices/accounts/srnagara-textanalytics");

sdk/monitor/azure-monitor-query/src/test/java/com/azure/monitor/query/MetricsQueryClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import com.azure.core.http.HttpClient;
99
import com.azure.core.http.rest.PagedIterable;
1010
import com.azure.core.http.rest.Response;
11-
import com.azure.core.test.TestBase;
1211
import com.azure.core.test.TestMode;
12+
import com.azure.core.test.TestProxyTestBase;
1313
import com.azure.core.test.http.AssertingHttpClientBuilder;
1414
import com.azure.core.util.Configuration;
1515
import com.azure.core.util.Context;
@@ -44,7 +44,7 @@
4444
/**
4545
* Unit tests for {@link MetricsQueryClient}.
4646
*/
47-
public class MetricsQueryClientTest extends TestBase {
47+
public class MetricsQueryClientTest extends TestProxyTestBase {
4848
private static final String RESOURCE_URI = Configuration.getGlobalConfiguration()
4949
.get("AZURE_MONITOR_METRICS_RESOURCE_URI",
5050
"/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/srnagar-azuresdkgroup/providers/Microsoft.CognitiveServices/accounts/srnagara-textanalytics");
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,176 @@
11
{
2-
"networkCallRecords" : [ {
3-
"Method" : "POST",
4-
"Uri" : "https://REDACTED.loganalytics.io/v1/$batch",
5-
"Headers" : {
6-
"User-Agent" : "azsdk-java-azure-monitor-query/1.2.0-beta.1 (17.0.2; Windows 11; 10.0)",
7-
"x-ms-client-request-id" : "3d7f76cb-71e9-4837-930e-176eebc57741",
8-
"Content-Type" : "application/json"
9-
},
10-
"Response" : {
11-
"Transfer-Encoding" : "chunked",
12-
"Access-Control-Allow-Origin" : "*",
13-
"X-Content-Type-Options" : "nosniff",
14-
"Connection" : "keep-alive",
15-
"retry-after" : "0",
16-
"StatusCode" : "200",
17-
"Date" : "Tue, 14 Mar 2023 07:50:11 GMT",
18-
"via" : "1.1 draft-oms-5979b88dcd-kx47g",
19-
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
20-
"Access-Control-Expose-Headers" : "Retry-After,Age,WWW-Authenticate,x-resource-identities,x-ms-status-location",
21-
"Vary" : "Accept-Encoding",
22-
"Body" : "{\"responses\":[{\"id\":\"2\",\"status\":400,\"body\":{\"error\":{\"message\":\"The request had some invalid properties\",\"code\":\"BadArgumentError\",\"correlationId\":\"31269348-b3c4-4147-b79e-f1143ee64c0c\",\"innererror\":{\"code\":\"SyntaxError\",\"message\":\"A recognition error occurred in the query.\",\"innererror\":{\"code\":\"SYN0002\",\"message\":\"Query could not be parsed at '<EOF>' on line [2,299]\",\"line\":2,\"pos\":299,\"token\":\"<EOF>\"}}}}},{\"id\":\"1\",\"status\":200,\"body\":{\"tables\":[{\"name\":\"PrimaryResult\",\"columns\":[{\"name\":\"x\",\"type\":\"long\"},{\"name\":\"y\",\"type\":\"long\"},{\"name\":\"DateTime\",\"type\":\"datetime\"},{\"name\":\"Bool\",\"type\":\"bool\"},{\"name\":\"Guid\",\"type\":\"guid\"},{\"name\":\"Int\",\"type\":\"int\"},{\"name\":\"Long\",\"type\":\"long\"},{\"name\":\"Double\",\"type\":\"real\"},{\"name\":\"String\",\"type\":\"string\"},{\"name\":\"Timespan\",\"type\":\"timespan\"},{\"name\":\"Decimal\",\"type\":\"decimal\"},{\"name\":\"Dynamic\",\"type\":\"dynamic\"}],\"rows\":[[100,1,\"2015-12-31T23:59:59.9Z\",false,\"74be27de-1e4e-49d9-b579-fe0b331d3642\",12345,1,12345.6789,\"string value\",\"00:00:10\",\"0.10101\",\"{\\\"a\\\":123,\\\"b\\\":\\\"hello\\\",\\\"c\\\":[1,2,3],\\\"d\\\":{}}\"],[99,1,\"2015-12-31T23:59:59.9Z\",false,\"74be27de-1e4e-49d9-b579-fe0b331d3642\",12345,1,12345.6789,\"string value\",\"00:00:10\",\"0.10101\",\"{\\\"a\\\":123,\\\"b\\\":\\\"hello\\\",\\\"c\\\":[1,2,3],\\\"d\\\":{}}\"]]}]}}]}",
23-
"Content-Type" : "application/json; charset=utf-8"
24-
},
25-
"Exception" : null
26-
} ],
27-
"variables" : [ ]
28-
}
2+
"Entries": [
3+
{
4+
"RequestUri": "https://REDACTED/v1/$batch",
5+
"RequestMethod": "POST",
6+
"RequestHeaders": {
7+
"Accept": "application/json",
8+
"Authorization": "Sanitized",
9+
"Content-Length": "1234",
10+
"Content-Type": "application/json",
11+
"Date": "Tue, 18 Apr 2023 18:00:51 GMT",
12+
"User-Agent": "azsdk-java-azure-monitor-query/1.2.0-beta.1 (17.0.2; Windows 11; 10.0)",
13+
"x-ms-client-request-id": "e9551643-9abe-483b-af21-fb294d449a61"
14+
},
15+
"RequestBody": {
16+
"requests": [
17+
{
18+
"id": "1",
19+
"body": {
20+
"query": "let dt = datatable (DateTime: datetime, Bool:bool, Guid: guid, Int: int, Long:long, Double: double, String: string, Timespan: timespan, Decimal: decimal, Dynamic: dynamic)\n[datetime(2015-12-31 23:59:59.9), false, guid(74be27de-1e4e-49d9-b579-fe0b331d3642), 12345, 1, 12345.6789, \u0027string value\u0027, 10s, decimal(0.10101), dynamic({\u0022a\u0022:123, \u0022b\u0022:\u0022hello\u0022, \u0022c\u0022:[1,2,3], \u0022d\u0022:{}})];range x from 1 to 100 step 1 | extend y=1 | join kind=fullouter dt on $left.y == $right.Long | take 2"
21+
},
22+
"workspace": "d2d0e126-fa1e-4b0a-b647-250cdd471e68",
23+
"headers": {},
24+
"path": "/query",
25+
"method": "POST"
26+
},
27+
{
28+
"id": "2",
29+
"body": {
30+
"query": "let dt = datatable (DateTime: datetime, Bool:bool, Guid: guid, Int: int, Long:long, Double: double, String: string, Timespan: timespan, Decimal: decimal, Dynamic: dynamic)\n[datetime(2015-12-31 23:59:59.9), false, guid(74be27de-1e4e-49d9-b579-fe0b331d3642), 12345, 1, 12345.6789, \u0027string value\u0027, 10s, decimal(0.10101), dynamic({\u0022a\u0022:123, \u0022b\u0022:\u0022hello\u0022, \u0022c\u0022:[1,2,3], \u0022d\u0022:{}})];range x from 1 to 100 step 1 | extend y=1 | join kind=fullouter dt on $left.y == $right.Long | take"
31+
},
32+
"workspace": "d2d0e126-fa1e-4b0a-b647-250cdd471e68",
33+
"headers": {},
34+
"path": "/query",
35+
"method": "POST"
36+
}
37+
]
38+
},
39+
"StatusCode": 200,
40+
"ResponseHeaders": {
41+
"Access-Control-Allow-Origin": "*",
42+
"Access-Control-Expose-Headers": "Retry-After,Age,WWW-Authenticate,x-resource-identities,x-ms-status-location",
43+
"Connection": "keep-alive",
44+
"Content-Type": "application/json; charset=utf-8",
45+
"Date": "Tue, 18 Apr 2023 18:00:52 GMT",
46+
"Strict-Transport-Security": "max-age=15724800; includeSubDomains",
47+
"Transfer-Encoding": "chunked",
48+
"Vary": "Accept-Encoding",
49+
"Via": "1.1 draft-oms-666995cb46-22qw8",
50+
"X-Content-Type-Options": "nosniff"
51+
},
52+
"ResponseBody": {
53+
"responses": [
54+
{
55+
"id": "1",
56+
"status": 200,
57+
"headers": {
58+
"Age": "91",
59+
"request-context": "appId=cid-v1:70941e4f-7e8f-40b7-b730-183893db0297"
60+
},
61+
"body": {
62+
"tables": [
63+
{
64+
"name": "PrimaryResult",
65+
"columns": [
66+
{
67+
"name": "x",
68+
"type": "long"
69+
},
70+
{
71+
"name": "y",
72+
"type": "long"
73+
},
74+
{
75+
"name": "DateTime",
76+
"type": "datetime"
77+
},
78+
{
79+
"name": "Bool",
80+
"type": "bool"
81+
},
82+
{
83+
"name": "Guid",
84+
"type": "guid"
85+
},
86+
{
87+
"name": "Int",
88+
"type": "int"
89+
},
90+
{
91+
"name": "Long",
92+
"type": "long"
93+
},
94+
{
95+
"name": "Double",
96+
"type": "real"
97+
},
98+
{
99+
"name": "String",
100+
"type": "string"
101+
},
102+
{
103+
"name": "Timespan",
104+
"type": "timespan"
105+
},
106+
{
107+
"name": "Decimal",
108+
"type": "decimal"
109+
},
110+
{
111+
"name": "Dynamic",
112+
"type": "dynamic"
113+
}
114+
],
115+
"rows": [
116+
[
117+
100,
118+
1,
119+
"2015-12-31T23:59:59.9Z",
120+
false,
121+
"74be27de-1e4e-49d9-b579-fe0b331d3642",
122+
12345,
123+
1,
124+
12345.6789,
125+
"string value",
126+
"00:00:10",
127+
"0.10101",
128+
"{\u0022a\u0022:123,\u0022b\u0022:\u0022hello\u0022,\u0022c\u0022:[1,2,3],\u0022d\u0022:{}}"
129+
],
130+
[
131+
99,
132+
1,
133+
"2015-12-31T23:59:59.9Z",
134+
false,
135+
"74be27de-1e4e-49d9-b579-fe0b331d3642",
136+
12345,
137+
1,
138+
12345.6789,
139+
"string value",
140+
"00:00:10",
141+
"0.10101",
142+
"{\u0022a\u0022:123,\u0022b\u0022:\u0022hello\u0022,\u0022c\u0022:[1,2,3],\u0022d\u0022:{}}"
143+
]
144+
]
145+
}
146+
]
147+
}
148+
},
149+
{
150+
"id": "2",
151+
"status": 400,
152+
"body": {
153+
"error": {
154+
"message": "The request had some invalid properties",
155+
"code": "BadArgumentError",
156+
"correlationId": "c6c594e9-2703-4738-80c2-3b6ba90316e3",
157+
"innererror": {
158+
"code": "SyntaxError",
159+
"message": "A recognition error occurred in the query.",
160+
"innererror": {
161+
"code": "SYN0002",
162+
"message": "Query could not be parsed at \u0027\u003CEOF\u003E\u0027 on line [2,299]",
163+
"line": 2,
164+
"pos": 299,
165+
"token": "\u003CEOF\u003E"
166+
}
167+
}
168+
}
169+
}
170+
}
171+
]
172+
}
173+
}
174+
],
175+
"Variables": {}
176+
}

0 commit comments

Comments
 (0)