Skip to content

Commit 5c09976

Browse files
[Communication] - phone-numbers - Implement a phone number pool for live tests (Azure#27072)
* Implement a phone number pool for live tests * Fix pipeline script * Add MatrixConfigs param to communication template This allows for individual packages to customize their job matrix if needed. * Require test agent phone number in livetests Throw an exception when there is no phone number assigned to the current test agent. This only applies when not skipping update capabilities live tests. The main purpose of this is for the test run to fail when it isn't properly configured, instead of silently falling back on the default method for setting the test phone number.
1 parent 6a93edf commit 5c09976

File tree

6 files changed

+149
-53
lines changed

6 files changed

+149
-53
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"displayNames": {
3+
"true": "TestFromSource"
4+
},
5+
"matrix": {
6+
"Agent": {
7+
"ubuntu-20.04": {
8+
"OSVmImage": "MMSUbuntu20.04",
9+
"Pool": "azsdk-pool-mms-ubuntu-2004-general",
10+
"JavaTestVersion": "1.8",
11+
"AZURE_TEST_HTTP_CLIENTS": "okhttp",
12+
"AZURE_TEST_AGENT": "ubuntu_2004_java8"
13+
},
14+
"macOS-10.15": {
15+
"OSVmImage": "macOS-10.15",
16+
"Pool": "Azure Pipelines",
17+
"JavaTestVersion": "1.11",
18+
"AZURE_TEST_HTTP_CLIENTS": "netty",
19+
"AZURE_TEST_AGENT": "macos_1015_java11"
20+
}
21+
},
22+
"SKIP_UPDATE_CAPABILITIES_LIVE_TESTS": "false",
23+
"TestFromSource": false,
24+
"TestGoals": "surefire:test",
25+
"TestOptions": ""
26+
},
27+
"include": [
28+
{
29+
"Agent": {
30+
"ubuntu-20.04": {
31+
"OSVmImage": "MMSUbuntu20.04",
32+
"Pool": "azsdk-pool-mms-ubuntu-2004-general"
33+
}
34+
},
35+
"JavaTestVersion": "1.11",
36+
"AZURE_TEST_HTTP_CLIENTS": "netty",
37+
"TestFromSource": true,
38+
"TestGoals": "verify",
39+
"TestOptions": "-DskipCompile=true -DskipTestCompile=true -DcreateSourcesJar=false",
40+
"SKIP_UPDATE_CAPABILITIES_LIVE_TESTS": "true"
41+
},
42+
{
43+
"Agent": {
44+
"windows-2019": {
45+
"OSVmImage": "MMS2019",
46+
"Pool": "azsdk-pool-mms-win-2019-general"
47+
}
48+
},
49+
"JavaTestVersion": "1.11",
50+
"AZURE_TEST_HTTP_CLIENTS": "netty",
51+
"TestFromSource": false,
52+
"TestGoals": "verify",
53+
"TestOptions": "-DskipCompile=true -DskipTestCompile=true -DcreateSourcesJar=false",
54+
"SKIP_UPDATE_CAPABILITIES_LIVE_TESTS": "false",
55+
"AZURE_TEST_AGENT": "windows_2019_java11"
56+
}
57+
]
58+
}

sdk/communication/azure-communication-phonenumbers/src/test/java/com/azure/communication/phonenumbers/PhoneNumbersAsyncClientIntegrationTest.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class PhoneNumbersAsyncClientIntegrationTest extends PhoneNumbersIntegrat
3232
@ParameterizedTest
3333
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
3434
public void getPurchasedPhoneNumber(HttpClient httpClient) {
35-
String phoneNumber = getTestPhoneNumber(PHONE_NUMBER);
35+
String phoneNumber = redactIfPlaybackMode(getTestPhoneNumber());
3636
StepVerifier.create(
3737
this.getClientWithConnectionString(httpClient, "getPurchasedPhoneNumber").getPurchasedPhoneNumber(phoneNumber)
3838
)
@@ -46,7 +46,7 @@ public void getPurchasedPhoneNumber(HttpClient httpClient) {
4646
@ParameterizedTest
4747
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
4848
public void getPurchasedPhoneNumberWithAAD(HttpClient httpClient) {
49-
String phoneNumber = getTestPhoneNumber(PHONE_NUMBER);
49+
String phoneNumber = redactIfPlaybackMode(getTestPhoneNumber());
5050
StepVerifier.create(
5151
this.getClientWithManagedIdentity(httpClient, "getPurchasedPhoneNumberWithAAD").getPurchasedPhoneNumber(phoneNumber)
5252
)
@@ -60,7 +60,7 @@ public void getPurchasedPhoneNumberWithAAD(HttpClient httpClient) {
6060
@ParameterizedTest
6161
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
6262
public void getPurchasedPhoneNumberWithResponse(HttpClient httpClient) {
63-
String phoneNumber = getTestPhoneNumber(PHONE_NUMBER);
63+
String phoneNumber = redactIfPlaybackMode(getTestPhoneNumber());
6464
StepVerifier.create(
6565
this.getClientWithConnectionString(httpClient, "getPurchasedPhoneNumberWithResponse").getPurchasedPhoneNumberWithResponse(phoneNumber)
6666
)
@@ -132,7 +132,7 @@ public void beginPurchaseandReleasePhoneNumbers(HttpClient httpClient) {
132132
.flatMap((AsyncPollResponse<PhoneNumberOperation, PhoneNumberSearchResult> result) -> {
133133
return result.getFinalResult()
134134
.flatMap((PhoneNumberSearchResult searchResult) -> {
135-
String phoneNumber = getTestPhoneNumber(searchResult.getPhoneNumbers().get(0));
135+
String phoneNumber = redactIfPlaybackMode(searchResult.getPhoneNumbers().get(0));
136136
return beginPurchasePhoneNumbersHelper(httpClient, searchResult.getSearchId(), "beginPurchasePhoneNumbers").last()
137137
.flatMap((AsyncPollResponse<PhoneNumberOperation, PurchasePhoneNumbersResult> purchaseResult) -> {
138138
assertEquals(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, purchaseResult.getStatus());
@@ -153,10 +153,10 @@ public void beginPurchaseandReleasePhoneNumbers(HttpClient httpClient) {
153153
named = "COMMUNICATION_SKIP_INT_PHONENUMBERS_TEST",
154154
matches = "(?i)(true)")
155155
@DisabledIfEnvironmentVariable(
156-
named = "SKIP_LIVE_TEST",
156+
named = "SKIP_UPDATE_CAPABILITIES_LIVE_TESTS",
157157
matches = "(?i)(true)")
158158
public void beginUpdatePhoneNumberCapabilities(HttpClient httpClient) {
159-
String phoneNumber = getTestPhoneNumber(PHONE_NUMBER);
159+
String phoneNumber = redactIfPlaybackMode(getTestPhoneNumber());
160160

161161
StepVerifier.create(
162162
this.getClientWithConnectionString(httpClient, "getPurchasedPhoneNumberForCapabilities").getPurchasedPhoneNumberWithResponse(phoneNumber)
@@ -276,11 +276,4 @@ private PhoneNumbersAsyncClient getClientWithManagedIdentity(HttpClient httpClie
276276
PhoneNumbersClientBuilder builder = super.getClientBuilderUsingManagedIdentity(httpClient);
277277
return addLoggingPolicy(builder, testName).buildAsyncClient();
278278
}
279-
280-
private String getTestPhoneNumber(String phoneNumber) {
281-
if (getTestMode() == TestMode.PLAYBACK) {
282-
phoneNumber = "+REDACTED";
283-
}
284-
return phoneNumber;
285-
}
286279
}

sdk/communication/azure-communication-phonenumbers/src/test/java/com/azure/communication/phonenumbers/PhoneNumbersClientIntegrationTest.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class PhoneNumbersClientIntegrationTest extends PhoneNumbersIntegrationTe
3636
@ParameterizedTest
3737
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
3838
public void getPurchasedPhoneNumber(HttpClient httpClient) {
39-
String phoneNumber = getTestPhoneNumber(PHONE_NUMBER);
39+
String phoneNumber = redactIfPlaybackMode(getTestPhoneNumber());
4040
PurchasedPhoneNumber number = this.getClientWithConnectionString(httpClient, "getPurchasedPhoneNumberSync").getPurchasedPhoneNumber(phoneNumber);
4141
assertEquals(phoneNumber, number.getPhoneNumber());
4242
assertEquals(COUNTRY_CODE, number.getCountryCode());
@@ -45,7 +45,7 @@ public void getPurchasedPhoneNumber(HttpClient httpClient) {
4545
@ParameterizedTest
4646
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
4747
public void getPurchasedPhoneNumberWithAAD(HttpClient httpClient) {
48-
String phoneNumber = getTestPhoneNumber(PHONE_NUMBER);
48+
String phoneNumber = redactIfPlaybackMode(getTestPhoneNumber());
4949
PurchasedPhoneNumber number = this.getClientWithManagedIdentity(httpClient, "getPurchasedPhoneNumberWithAADSync").getPurchasedPhoneNumber(phoneNumber);
5050
assertEquals(phoneNumber, number.getPhoneNumber());
5151
assertEquals(COUNTRY_CODE, number.getCountryCode());
@@ -54,7 +54,7 @@ public void getPurchasedPhoneNumberWithAAD(HttpClient httpClient) {
5454
@ParameterizedTest
5555
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
5656
public void getPurchasedPhoneNumberWithResponse(HttpClient httpClient) {
57-
String phoneNumber = getTestPhoneNumber(PHONE_NUMBER);
57+
String phoneNumber = redactIfPlaybackMode(getTestPhoneNumber());
5858
Response<PurchasedPhoneNumber> response = this.getClientWithConnectionString(httpClient, "getPurchasedPhoneNumberWithResponseSync")
5959
.getPurchasedPhoneNumberWithResponse(phoneNumber, Context.NONE);
6060
PurchasedPhoneNumber number = response.getValue();
@@ -108,7 +108,7 @@ public void beginPurchaseandReleasePhoneNumbersWithoutContext(HttpClient httpCli
108108
PollResponse<PhoneNumberOperation> response = poller.waitForCompletion();
109109
if (LongRunningOperationStatus.SUCCESSFULLY_COMPLETED == response.getStatus()) {
110110
PhoneNumberSearchResult searchResult = poller.getFinalResult();
111-
String phoneNumber = getTestPhoneNumber(searchResult.getPhoneNumbers().get(0));
111+
String phoneNumber = redactIfPlaybackMode(searchResult.getPhoneNumbers().get(0));
112112
PollResponse<PhoneNumberOperation> purchaseOperationResponse = beginPurchasePhoneNumbersHelper(httpClient, searchResult.getSearchId(), "beginPurchasePhoneNumbersWithoutContextSync", false).waitForCompletion();
113113
assertEquals(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, purchaseOperationResponse.getStatus());
114114
PollResponse<PhoneNumberOperation> releaseOperationResponse = beginReleasePhoneNumberHelper(httpClient, phoneNumber, "beginReleasePhoneNumberWithoutContextSync", false).waitForCompletion();
@@ -128,7 +128,7 @@ public void beginPurchaseandReleasePhoneNumbers(HttpClient httpClient) {
128128
PollResponse<PhoneNumberOperation> response = poller.waitForCompletion();
129129
if (LongRunningOperationStatus.SUCCESSFULLY_COMPLETED == response.getStatus()) {
130130
PhoneNumberSearchResult searchResult = poller.getFinalResult();
131-
String phoneNumber = getTestPhoneNumber(searchResult.getPhoneNumbers().get(0));
131+
String phoneNumber = redactIfPlaybackMode(searchResult.getPhoneNumbers().get(0));
132132
PollResponse<PhoneNumberOperation> purchaseOperationResponse = beginPurchasePhoneNumbersHelper(httpClient, searchResult.getSearchId(), "beginPurchasePhoneNumbersSync", true).waitForCompletion();
133133
assertEquals(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, purchaseOperationResponse.getStatus());
134134
PollResponse<PhoneNumberOperation> releaseOperationResponse = beginReleasePhoneNumberHelper(httpClient, phoneNumber, "beginReleasePhoneNumberSync", true).waitForCompletion();
@@ -144,7 +144,7 @@ public void beginPurchaseandReleasePhoneNumbers(HttpClient httpClient) {
144144
named = "COMMUNICATION_SKIP_INT_PHONENUMBERS_TEST",
145145
matches = "(?i)(true)")
146146
public void beginUpdatePhoneNumberCapabilitiesWithoutContext(HttpClient httpClient) {
147-
String phoneNumber = getTestPhoneNumber(PHONE_NUMBER);
147+
String phoneNumber = redactIfPlaybackMode(getTestPhoneNumber());
148148
PollResponse<PhoneNumberOperation> result = beginUpdatePhoneNumberCapabilitiesHelper(httpClient, phoneNumber, "beginUpdatePhoneNumberCapabilitiesWithoutContextSync", false).waitForCompletion();
149149
assertEquals(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, result.getStatus());
150150
assertEquals(PhoneNumberOperationStatus.SUCCEEDED, result.getValue().getStatus());
@@ -156,10 +156,10 @@ public void beginUpdatePhoneNumberCapabilitiesWithoutContext(HttpClient httpClie
156156
named = "COMMUNICATION_SKIP_INT_PHONENUMBERS_TEST",
157157
matches = "(?i)(true)")
158158
@DisabledIfEnvironmentVariable(
159-
named = "SKIP_LIVE_TEST",
159+
named = "SKIP_UPDATE_CAPABILITIES_LIVE_TESTS",
160160
matches = "(?i)(true)")
161161
public void beginUpdatePhoneNumberCapabilities(HttpClient httpClient) {
162-
String phoneNumber = getTestPhoneNumber(PHONE_NUMBER);
162+
String phoneNumber = redactIfPlaybackMode(getTestPhoneNumber());
163163
PollResponse<PhoneNumberOperation> result = beginUpdatePhoneNumberCapabilitiesHelper(httpClient, phoneNumber, "beginUpdatePhoneNumberCapabilities", true).waitForCompletion();
164164
assertEquals(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, result.getStatus());
165165
assertEquals(PhoneNumberOperationStatus.SUCCEEDED, result.getValue().getStatus());
@@ -236,11 +236,4 @@ private PhoneNumbersClient getClientWithManagedIdentity(HttpClient httpClient, S
236236
PhoneNumbersClientBuilder builder = super.getClientBuilderUsingManagedIdentity(httpClient);
237237
return addLoggingPolicy(builder, testName).buildClient();
238238
}
239-
240-
private String getTestPhoneNumber(String phoneNumber) {
241-
if (getTestMode() == TestMode.PLAYBACK) {
242-
phoneNumber = "+REDACTED";
243-
}
244-
return phoneNumber;
245-
}
246239
}

sdk/communication/azure-communication-phonenumbers/src/test/java/com/azure/communication/phonenumbers/PhoneNumbersIntegrationTestBase.java

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,18 @@
2828

2929
public class PhoneNumbersIntegrationTestBase extends TestBase {
3030
private static final String CONNECTION_STRING = Configuration.getGlobalConfiguration()
31-
.get("COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING", "endpoint=https://REDACTED.communication.azure.com/;accesskey=QWNjZXNzS2V5");
32-
protected static final String COUNTRY_CODE =
31+
.get("COMMUNICATION_LIVETEST_STATIC_CONNECTION_STRING", "endpoint=https://REDACTED.communication.azure.com/;accesskey=QWNjZXNzS2V5");
32+
protected static final String COUNTRY_CODE =
3333
Configuration.getGlobalConfiguration().get("COUNTRY_CODE", "US");
34-
protected static final String AREA_CODE =
34+
protected static final String AREA_CODE =
3535
Configuration.getGlobalConfiguration().get("AREA_CODE", "833");
3636

37-
protected static final String PHONE_NUMBER =
38-
Configuration.getGlobalConfiguration().get("AZURE_PHONE_NUMBER", "+11234567891");
39-
40-
private static final StringJoiner JSON_PROPERTIES_TO_REDACT =
37+
private static final StringJoiner JSON_PROPERTIES_TO_REDACT =
4138
new StringJoiner("\":\"|\"", "\"", "\":\"")
4239
.add("id")
4340
.add("phoneNumber");
4441

45-
private static final Pattern JSON_PROPERTY_VALUE_REDACTION_PATTERN =
42+
private static final Pattern JSON_PROPERTY_VALUE_REDACTION_PATTERN =
4643
Pattern.compile(String.format("(?:%s)(.*?)(?:\",|\"})", JSON_PROPERTIES_TO_REDACT.toString()), Pattern.CASE_INSENSITIVE);
4744

4845
protected PhoneNumbersClientBuilder getClientBuilder(HttpClient httpClient) {
@@ -52,13 +49,13 @@ protected PhoneNumbersClientBuilder getClientBuilder(HttpClient httpClient) {
5249

5350
CommunicationConnectionString communicationConnectionString = new CommunicationConnectionString(CONNECTION_STRING);
5451
String communicationEndpoint = communicationConnectionString.getEndpoint();
55-
String communicationAccessKey = communicationConnectionString.getAccessKey();
52+
String communicationAccessKey = communicationConnectionString.getAccessKey();
5653

5754
PhoneNumbersClientBuilder builder = new PhoneNumbersClientBuilder();
5855
builder
59-
.httpClient(httpClient)
60-
.endpoint(communicationEndpoint)
61-
.credential(new AzureKeyCredential(communicationAccessKey));
56+
.httpClient(httpClient)
57+
.endpoint(communicationEndpoint)
58+
.credential(new AzureKeyCredential(communicationAccessKey));
6259

6360
if (getTestMode() == TestMode.RECORD) {
6461
List<Function<String, String>> redactors = new ArrayList<>();
@@ -76,8 +73,8 @@ protected PhoneNumbersClientBuilder getClientBuilderWithConnectionString(HttpCli
7673

7774
PhoneNumbersClientBuilder builder = new PhoneNumbersClientBuilder();
7875
builder
79-
.httpClient(httpClient)
80-
.connectionString(CONNECTION_STRING);
76+
.httpClient(httpClient)
77+
.connectionString(CONNECTION_STRING);
8178

8279
if (getTestMode() == TestMode.RECORD) {
8380
List<Function<String, String>> redactors = new ArrayList<>();
@@ -91,8 +88,8 @@ protected PhoneNumbersClientBuilder getClientBuilderWithConnectionString(HttpCli
9188
protected PhoneNumbersClientBuilder getClientBuilderUsingManagedIdentity(HttpClient httpClient) {
9289
PhoneNumbersClientBuilder builder = new PhoneNumbersClientBuilder();
9390
builder
94-
.endpoint(new CommunicationConnectionString(CONNECTION_STRING).getEndpoint())
95-
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
91+
.endpoint(new CommunicationConnectionString(CONNECTION_STRING).getEndpoint())
92+
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
9693

9794
if (getTestMode() == TestMode.PLAYBACK) {
9895
builder.credential(new FakeCredentials());
@@ -113,16 +110,34 @@ protected PhoneNumbersClientBuilder addLoggingPolicy(PhoneNumbersClientBuilder b
113110
return builder.addPolicy((context, next) -> logHeaders(testName, next));
114111
}
115112

113+
protected String getTestPhoneNumber() {
114+
boolean skipCapabilitiesTests = Configuration.getGlobalConfiguration()
115+
.get("SKIP_UPDATE_CAPABILITIES_LIVE_TESTS", "false").equals("true");
116+
117+
if (getTestMode() == TestMode.PLAYBACK || skipCapabilitiesTests) {
118+
return getDefaultPhoneNumber();
119+
}
120+
121+
return getPhoneNumberByTestAgent();
122+
}
123+
124+
protected String redactIfPlaybackMode(String phoneNumber) {
125+
if (getTestMode() == TestMode.PLAYBACK) {
126+
phoneNumber = "+REDACTED";
127+
}
128+
return phoneNumber;
129+
}
130+
116131
private Mono<HttpResponse> logHeaders(String testName, HttpPipelineNextPolicy next) {
117132
return next.process()
118-
.flatMap(httpResponse -> {
119-
final HttpResponse bufferedResponse = httpResponse.buffer();
120-
121-
// Should sanitize printed reponse url
122-
System.out.println("MS-CV header for " + testName + " request "
123-
+ bufferedResponse.getRequest().getUrl() + ": " + bufferedResponse.getHeaderValue("MS-CV"));
124-
return Mono.just(bufferedResponse);
125-
});
133+
.flatMap(httpResponse -> {
134+
final HttpResponse bufferedResponse = httpResponse.buffer();
135+
136+
// Should sanitize printed reponse url
137+
System.out.println("MS-CV header for " + testName + " request "
138+
+ bufferedResponse.getRequest().getUrl() + ": " + bufferedResponse.getHeaderValue("MS-CV"));
139+
return Mono.just(bufferedResponse);
140+
});
126141
}
127142

128143
static class FakeCredentials implements TokenCredential {
@@ -143,4 +158,23 @@ private String redact(String content, Matcher matcher, String replacement) {
143158
return content;
144159
}
145160

161+
private String getDefaultPhoneNumber() {
162+
return Configuration.getGlobalConfiguration().get("AZURE_PHONE_NUMBER", "+11234567891");
163+
}
164+
165+
private String getPhoneNumberByTestAgent() {
166+
String testAgent = Configuration.getGlobalConfiguration().get("AZURE_TEST_AGENT");
167+
if (testAgent == null) {
168+
throw new IllegalStateException(
169+
"AZURE_TEST_AGENT value is required to run update capabilities live tests.");
170+
}
171+
172+
String phoneNumber = Configuration.getGlobalConfiguration().get(String.format("AZURE_PHONE_NUMBER_%s", testAgent));
173+
if (phoneNumber == null) {
174+
throw new IllegalStateException(
175+
"A phone number specific to the current test agent is required to run update capabilities live tests.");
176+
}
177+
178+
return phoneNumber;
179+
}
146180
}

sdk/communication/azure-communication-phonenumbers/tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ stages:
55
parameters:
66
PackageName: azure-communication-phonenumbers
77
SafeName: azurecommunicationphonenumbers
8+
MatrixConfigs:
9+
- Name: PhoneNumbers_java_livetest_matrix
10+
Path: sdk/communication/azure-communication-phonenumbers/phone-numbers-livetest-matrix.json
11+
Selection: sparse
12+
GenerateVMJobs: true

0 commit comments

Comments
 (0)