|
14 | 14 |
|
15 | 15 | import com.google.auth.oauth2.AccessToken; |
16 | 16 | import com.google.auth.oauth2.GoogleCredentials; |
| 17 | +import com.google.auto.value.AutoValue; |
17 | 18 | import com.google.common.collect.ImmutableMap; |
18 | 19 | import io.opentelemetry.api.common.AttributeKey; |
19 | 20 | import io.opentelemetry.api.trace.Span; |
|
46 | 47 | import java.util.Set; |
47 | 48 | import java.util.concurrent.TimeUnit; |
48 | 49 | import java.util.function.Supplier; |
| 50 | +import java.util.stream.Stream; |
| 51 | +import javax.annotation.Nullable; |
49 | 52 | import org.junit.jupiter.api.BeforeEach; |
50 | 53 | import org.junit.jupiter.api.Test; |
51 | 54 | import org.junit.jupiter.api.extension.ExtendWith; |
| 55 | +import org.junit.jupiter.params.ParameterizedTest; |
| 56 | +import org.junit.jupiter.params.provider.Arguments; |
| 57 | +import org.junit.jupiter.params.provider.MethodSource; |
52 | 58 | import org.mockito.ArgumentCaptor; |
53 | 59 | import org.mockito.Captor; |
54 | 60 | import org.mockito.Mock; |
@@ -149,19 +155,11 @@ public void testCustomizerOtlpGrpc() { |
149 | 155 | // Prepare mocks |
150 | 156 | prepareMockBehaviorForGoogleCredentials(); |
151 | 157 | OtlpGrpcSpanExporter mockOtlpGrpcSpanExporter = Mockito.mock(OtlpGrpcSpanExporter.class); |
152 | | - OtlpGrpcSpanExporterBuilder otlpSpanExporterBuilder = OtlpGrpcSpanExporter.builder(); |
153 | 158 | OtlpGrpcSpanExporterBuilder spyOtlpGrpcSpanExporterBuilder = |
154 | | - Mockito.spy(otlpSpanExporterBuilder); |
155 | | - Mockito.when(spyOtlpGrpcSpanExporterBuilder.build()).thenReturn(mockOtlpGrpcSpanExporter); |
156 | | - Mockito.when(mockOtlpGrpcSpanExporter.shutdown()).thenReturn(CompletableResultCode.ofSuccess()); |
| 159 | + Mockito.spy(OtlpGrpcSpanExporter.builder()); |
157 | 160 | List<SpanData> exportedSpans = new ArrayList<>(); |
158 | | - Mockito.when(mockOtlpGrpcSpanExporter.export(Mockito.anyCollection())) |
159 | | - .thenAnswer( |
160 | | - invocationOnMock -> { |
161 | | - exportedSpans.addAll(invocationOnMock.getArgument(0)); |
162 | | - return CompletableResultCode.ofSuccess(); |
163 | | - }); |
164 | | - Mockito.when(mockOtlpGrpcSpanExporter.toBuilder()).thenReturn(spyOtlpGrpcSpanExporterBuilder); |
| 161 | + configureGrpcMockExporters( |
| 162 | + mockOtlpGrpcSpanExporter, spyOtlpGrpcSpanExporterBuilder, exportedSpans); |
165 | 163 |
|
166 | 164 | try (MockedStatic<GoogleCredentials> googleCredentialsMockedStatic = |
167 | 165 | Mockito.mockStatic(GoogleCredentials.class)) { |
@@ -213,6 +211,186 @@ public void testCustomizerFailWithMissingResourceProject() { |
213 | 211 | } |
214 | 212 | } |
215 | 213 |
|
| 214 | + @ParameterizedTest |
| 215 | + @MethodSource("provideQuotaBehaviorTestCases") |
| 216 | + @SuppressWarnings("CannotMockMethod") |
| 217 | + public void testQuotaProjectBehavior(QuotaProjectIdTestBehavior testCase) { |
| 218 | + // Set resource project system property |
| 219 | + System.setProperty( |
| 220 | + ConfigurableOption.GOOGLE_CLOUD_PROJECT.getSystemProperty(), DUMMY_GCP_RESOURCE_PROJECT_ID); |
| 221 | + // Configure mock credentials to return fake access token |
| 222 | + Mockito.when(mockedGoogleCredentials.getAccessToken()) |
| 223 | + .thenReturn(new AccessToken("fake", Date.from(Instant.now()))); |
| 224 | + |
| 225 | + // To prevent unncecessary stubbings, mock getQuotaProjectId only when necessary |
| 226 | + if (testCase.getUserSpecifiedQuotaProjectId() == null |
| 227 | + || testCase.getUserSpecifiedQuotaProjectId().isEmpty()) { |
| 228 | + String quotaProjectFromCredential = |
| 229 | + testCase.getIsQuotaProjectPresentInCredentials() ? DUMMY_GCP_QUOTA_PROJECT_ID : null; |
| 230 | + Mockito.when(mockedGoogleCredentials.getQuotaProjectId()) |
| 231 | + .thenReturn(quotaProjectFromCredential); |
| 232 | + } |
| 233 | + |
| 234 | + // configure environment according to test case |
| 235 | + String quotaProjectId = testCase.getUserSpecifiedQuotaProjectId(); // maybe empty string |
| 236 | + if (testCase.getUserSpecifiedQuotaProjectId() != null) { |
| 237 | + // user specified a quota project id |
| 238 | + System.setProperty( |
| 239 | + ConfigurableOption.GOOGLE_CLOUD_QUOTA_PROJECT.getSystemProperty(), quotaProjectId); |
| 240 | + } |
| 241 | + |
| 242 | + // prepare mock exporter |
| 243 | + OtlpGrpcSpanExporter mockOtlpGrpcSpanExporter = Mockito.mock(OtlpGrpcSpanExporter.class); |
| 244 | + OtlpGrpcSpanExporterBuilder spyOtlpGrpcSpanExporterBuilder = |
| 245 | + Mockito.spy(OtlpGrpcSpanExporter.builder()); |
| 246 | + List<SpanData> exportedSpans = new ArrayList<>(); |
| 247 | + configureGrpcMockExporters( |
| 248 | + mockOtlpGrpcSpanExporter, spyOtlpGrpcSpanExporterBuilder, exportedSpans); |
| 249 | + |
| 250 | + try (MockedStatic<GoogleCredentials> googleCredentialsMockedStatic = |
| 251 | + Mockito.mockStatic(GoogleCredentials.class)) { |
| 252 | + googleCredentialsMockedStatic |
| 253 | + .when(GoogleCredentials::getApplicationDefault) |
| 254 | + .thenReturn(mockedGoogleCredentials); |
| 255 | + |
| 256 | + // Export telemetry to capture headers in the export calls |
| 257 | + OpenTelemetrySdk sdk = buildOpenTelemetrySdkWithExporter(mockOtlpGrpcSpanExporter); |
| 258 | + generateTestSpan(sdk); |
| 259 | + CompletableResultCode code = sdk.shutdown(); |
| 260 | + CompletableResultCode joinResult = code.join(10, TimeUnit.SECONDS); |
| 261 | + assertTrue(joinResult.isSuccess()); |
| 262 | + Mockito.verify(spyOtlpGrpcSpanExporterBuilder, Mockito.times(1)) |
| 263 | + .setHeaders(headerSupplierCaptor.capture()); |
| 264 | + |
| 265 | + // assert that the Authorization bearer token header is present |
| 266 | + Map<String, String> exportHeaders = headerSupplierCaptor.getValue().get(); |
| 267 | + assertThat(exportHeaders).containsEntry("Authorization", "Bearer fake"); |
| 268 | + |
| 269 | + if (testCase.getExpectedQuotaProjectInHeader() == null) { |
| 270 | + // there should be no user quota project header |
| 271 | + assertThat(exportHeaders).doesNotContainKey(QUOTA_USER_PROJECT_HEADER); |
| 272 | + } else { |
| 273 | + // there should be user quota project header with expected value |
| 274 | + assertThat(exportHeaders) |
| 275 | + .containsEntry(QUOTA_USER_PROJECT_HEADER, testCase.getExpectedQuotaProjectInHeader()); |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * Test cases specifying expected value for the user quota project header given the user input and |
| 282 | + * the current credentials state. |
| 283 | + * |
| 284 | + * <p>{@code null} for {@link QuotaProjectIdTestBehavior#getUserSpecifiedQuotaProjectId()} |
| 285 | + * indicates the case of user not specifying the quota project ID. |
| 286 | + * |
| 287 | + * <p>{@code null} value for {@link QuotaProjectIdTestBehavior#getExpectedQuotaProjectInHeader()} |
| 288 | + * indicates the expectation that the QUOTA_USER_PROJECT_HEADER should not be present in the |
| 289 | + * export headers. |
| 290 | + * |
| 291 | + * <p>{@code true} for {@link QuotaProjectIdTestBehavior#getIsQuotaProjectPresentInCredentials()} |
| 292 | + * indicates that the mocked credentials are configured to provide DUMMY_GCP_QUOTA_PROJECT_ID as |
| 293 | + * the quota project ID. |
| 294 | + */ |
| 295 | + private static Stream<Arguments> provideQuotaBehaviorTestCases() { |
| 296 | + return Stream.of( |
| 297 | + Arguments.of( |
| 298 | + QuotaProjectIdTestBehavior.builder() |
| 299 | + .setUserSpecifiedQuotaProjectId(DUMMY_GCP_QUOTA_PROJECT_ID) |
| 300 | + .setIsQuotaProjectPresentInCredentials(true) |
| 301 | + .setExpectedQuotaProjectInHeader(DUMMY_GCP_QUOTA_PROJECT_ID) |
| 302 | + .build()), |
| 303 | + Arguments.of( |
| 304 | + QuotaProjectIdTestBehavior.builder() |
| 305 | + .setUserSpecifiedQuotaProjectId(DUMMY_GCP_QUOTA_PROJECT_ID) |
| 306 | + .setIsQuotaProjectPresentInCredentials(false) |
| 307 | + .setExpectedQuotaProjectInHeader(DUMMY_GCP_QUOTA_PROJECT_ID) |
| 308 | + .build()), |
| 309 | + Arguments.of( |
| 310 | + QuotaProjectIdTestBehavior.builder() |
| 311 | + .setUserSpecifiedQuotaProjectId("my-custom-quota-project-id") |
| 312 | + .setIsQuotaProjectPresentInCredentials(true) |
| 313 | + .setExpectedQuotaProjectInHeader("my-custom-quota-project-id") |
| 314 | + .build()), |
| 315 | + Arguments.of( |
| 316 | + QuotaProjectIdTestBehavior.builder() |
| 317 | + .setUserSpecifiedQuotaProjectId("my-custom-quota-project-id") |
| 318 | + .setIsQuotaProjectPresentInCredentials(false) |
| 319 | + .setExpectedQuotaProjectInHeader("my-custom-quota-project-id") |
| 320 | + .build()), |
| 321 | + Arguments.of( |
| 322 | + QuotaProjectIdTestBehavior.builder() |
| 323 | + .setUserSpecifiedQuotaProjectId("") // user explicitly specifies empty |
| 324 | + .setIsQuotaProjectPresentInCredentials(true) |
| 325 | + .setExpectedQuotaProjectInHeader(DUMMY_GCP_QUOTA_PROJECT_ID) |
| 326 | + .build()), |
| 327 | + Arguments.of( |
| 328 | + QuotaProjectIdTestBehavior.builder() |
| 329 | + .setUserSpecifiedQuotaProjectId(null) // user omits specifying quota project |
| 330 | + .setIsQuotaProjectPresentInCredentials(true) |
| 331 | + .setExpectedQuotaProjectInHeader(DUMMY_GCP_QUOTA_PROJECT_ID) |
| 332 | + .build()), |
| 333 | + Arguments.of( |
| 334 | + QuotaProjectIdTestBehavior.builder() |
| 335 | + .setUserSpecifiedQuotaProjectId("") |
| 336 | + .setIsQuotaProjectPresentInCredentials(false) |
| 337 | + .setExpectedQuotaProjectInHeader(null) |
| 338 | + .build()), |
| 339 | + Arguments.of( |
| 340 | + QuotaProjectIdTestBehavior.builder() |
| 341 | + .setUserSpecifiedQuotaProjectId(null) |
| 342 | + .setIsQuotaProjectPresentInCredentials(false) |
| 343 | + .setExpectedQuotaProjectInHeader(null) |
| 344 | + .build())); |
| 345 | + } |
| 346 | + |
| 347 | + // Configure necessary behavior on the Grpc mock exporters to work |
| 348 | + // TODO: Potential improvement - make this work for Http exporter as well. |
| 349 | + private static void configureGrpcMockExporters( |
| 350 | + OtlpGrpcSpanExporter mockGrpcExporter, |
| 351 | + OtlpGrpcSpanExporterBuilder spyGrpcExporterBuilder, |
| 352 | + List<SpanData> exportedSpanContainer) { |
| 353 | + Mockito.when(spyGrpcExporterBuilder.build()).thenReturn(mockGrpcExporter); |
| 354 | + Mockito.when(mockGrpcExporter.shutdown()).thenReturn(CompletableResultCode.ofSuccess()); |
| 355 | + Mockito.when(mockGrpcExporter.toBuilder()).thenReturn(spyGrpcExporterBuilder); |
| 356 | + Mockito.when(mockGrpcExporter.export(Mockito.anyCollection())) |
| 357 | + .thenAnswer( |
| 358 | + invocationOnMock -> { |
| 359 | + exportedSpanContainer.addAll(invocationOnMock.getArgument(0)); |
| 360 | + return CompletableResultCode.ofSuccess(); |
| 361 | + }); |
| 362 | + } |
| 363 | + |
| 364 | + @AutoValue |
| 365 | + abstract static class QuotaProjectIdTestBehavior { |
| 366 | + // A null user specified quota represents the use case where user omits specifying quota |
| 367 | + @Nullable |
| 368 | + abstract String getUserSpecifiedQuotaProjectId(); |
| 369 | + |
| 370 | + abstract boolean getIsQuotaProjectPresentInCredentials(); |
| 371 | + |
| 372 | + // If expected quota project in header is null, the header entry should not be present in export |
| 373 | + @Nullable |
| 374 | + abstract String getExpectedQuotaProjectInHeader(); |
| 375 | + |
| 376 | + static Builder builder() { |
| 377 | + return new AutoValue_GcpAuthAutoConfigurationCustomizerProviderTest_QuotaProjectIdTestBehavior |
| 378 | + .Builder(); |
| 379 | + } |
| 380 | + |
| 381 | + @AutoValue.Builder |
| 382 | + abstract static class Builder { |
| 383 | + abstract Builder setUserSpecifiedQuotaProjectId(String quotaProjectId); |
| 384 | + |
| 385 | + abstract Builder setIsQuotaProjectPresentInCredentials( |
| 386 | + boolean quotaProjectPresentInCredentials); |
| 387 | + |
| 388 | + abstract Builder setExpectedQuotaProjectInHeader(String expectedQuotaProjectInHeader); |
| 389 | + |
| 390 | + abstract QuotaProjectIdTestBehavior build(); |
| 391 | + } |
| 392 | + } |
| 393 | + |
216 | 394 | @SuppressWarnings("CannotMockMethod") |
217 | 395 | private void prepareMockBehaviorForGoogleCredentials() { |
218 | 396 | Mockito.when(mockedGoogleCredentials.getQuotaProjectId()) |
@@ -256,7 +434,9 @@ public String getName() { |
256 | 434 | private static boolean authHeadersQuotaProjectIsPresent(Map<String, String> headers) { |
257 | 435 | Set<Entry<String, String>> headerEntrySet = headers.entrySet(); |
258 | 436 | return headerEntrySet.contains( |
259 | | - new SimpleEntry<>(QUOTA_USER_PROJECT_HEADER, DUMMY_GCP_QUOTA_PROJECT_ID)) |
| 437 | + new SimpleEntry<>( |
| 438 | + QUOTA_USER_PROJECT_HEADER, |
| 439 | + GcpAuthAutoConfigurationCustomizerProviderTest.DUMMY_GCP_QUOTA_PROJECT_ID)) |
260 | 440 | && headerEntrySet.contains(new SimpleEntry<>("Authorization", "Bearer fake")); |
261 | 441 | } |
262 | 442 |
|
|
0 commit comments