|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
5 | 6 | import static org.mockito.ArgumentMatchers.any; |
6 | 7 | import static org.mockito.Mockito.mock; |
7 | 8 | import static org.mockito.Mockito.mockConstruction; |
|
27 | 28 | import java.util.List; |
28 | 29 | import java.util.Map; |
29 | 30 | import java.util.TimeZone; |
| 31 | +import java.util.concurrent.atomic.AtomicInteger; |
30 | 32 | import java.util.stream.Collectors; |
31 | 33 | import java.util.stream.IntStream; |
32 | 34 | import java.util.stream.Stream; |
| 35 | +import org.junit.jupiter.api.Test; |
33 | 36 | import org.junit.jupiter.params.ParameterizedTest; |
34 | 37 | import org.junit.jupiter.params.provider.Arguments; |
35 | 38 | import org.junit.jupiter.params.provider.MethodSource; |
@@ -213,4 +216,178 @@ public void testParseExpiry(String input, Instant expectedInstant, String descri |
213 | 216 | assertEquals(expectedInstant, parsedInstant); |
214 | 217 | } |
215 | 218 | } |
| 219 | + |
| 220 | + // ---- Fallback tests for --profile flag handling ---- |
| 221 | + |
| 222 | + private CliTokenSource makeTokenSource( |
| 223 | + Environment env, List<String> primaryCmd, List<String> fallbackCmd) { |
| 224 | + OSUtilities osUtils = mock(OSUtilities.class); |
| 225 | + when(osUtils.getCliExecutableCommand(any())).thenAnswer(inv -> inv.getArgument(0)); |
| 226 | + try (MockedStatic<OSUtils> mockedOSUtils = mockStatic(OSUtils.class)) { |
| 227 | + mockedOSUtils.when(() -> OSUtils.get(any())).thenReturn(osUtils); |
| 228 | + return new CliTokenSource( |
| 229 | + primaryCmd, "token_type", "access_token", "expiry", env, fallbackCmd); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + private String validTokenJson(String accessToken) { |
| 234 | + String expiry = |
| 235 | + ZonedDateTime.now() |
| 236 | + .plusHours(1) |
| 237 | + .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")); |
| 238 | + return String.format( |
| 239 | + "{\"token_type\":\"Bearer\",\"access_token\":\"%s\",\"expiry\":\"%s\"}", |
| 240 | + accessToken, expiry); |
| 241 | + } |
| 242 | + |
| 243 | + @Test |
| 244 | + public void testFallbackOnUnknownProfileFlagInStderr() { |
| 245 | + Environment env = mock(Environment.class); |
| 246 | + when(env.getEnv()).thenReturn(new HashMap<>()); |
| 247 | + |
| 248 | + List<String> primaryCmd = |
| 249 | + Arrays.asList("databricks", "auth", "token", "--profile", "my-profile"); |
| 250 | + List<String> fallbackCmdList = |
| 251 | + Arrays.asList("databricks", "auth", "token", "--host", "https://workspace.databricks.com"); |
| 252 | + |
| 253 | + CliTokenSource tokenSource = makeTokenSource(env, primaryCmd, fallbackCmdList); |
| 254 | + |
| 255 | + AtomicInteger callCount = new AtomicInteger(0); |
| 256 | + try (MockedConstruction<ProcessBuilder> mocked = |
| 257 | + mockConstruction( |
| 258 | + ProcessBuilder.class, |
| 259 | + (pb, context) -> { |
| 260 | + if (callCount.getAndIncrement() == 0) { |
| 261 | + Process failProcess = mock(Process.class); |
| 262 | + when(failProcess.getInputStream()) |
| 263 | + .thenReturn(new ByteArrayInputStream(new byte[0])); |
| 264 | + when(failProcess.getErrorStream()) |
| 265 | + .thenReturn( |
| 266 | + new ByteArrayInputStream("Error: unknown flag: --profile".getBytes())); |
| 267 | + when(failProcess.waitFor()).thenReturn(1); |
| 268 | + when(pb.start()).thenReturn(failProcess); |
| 269 | + } else { |
| 270 | + Process successProcess = mock(Process.class); |
| 271 | + when(successProcess.getInputStream()) |
| 272 | + .thenReturn(new ByteArrayInputStream(validTokenJson("fallback-token").getBytes())); |
| 273 | + when(successProcess.getErrorStream()) |
| 274 | + .thenReturn(new ByteArrayInputStream(new byte[0])); |
| 275 | + when(successProcess.waitFor()).thenReturn(0); |
| 276 | + when(pb.start()).thenReturn(successProcess); |
| 277 | + } |
| 278 | + })) { |
| 279 | + Token token = tokenSource.getToken(); |
| 280 | + assertEquals("fallback-token", token.getAccessToken()); |
| 281 | + assertEquals(2, mocked.constructed().size()); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + @Test |
| 286 | + public void testFallbackTriggeredWhenUnknownFlagInStdout() { |
| 287 | + // Fallback triggers even when "unknown flag" appears in stdout rather than stderr. |
| 288 | + Environment env = mock(Environment.class); |
| 289 | + when(env.getEnv()).thenReturn(new HashMap<>()); |
| 290 | + |
| 291 | + List<String> primaryCmd = |
| 292 | + Arrays.asList("databricks", "auth", "token", "--profile", "my-profile"); |
| 293 | + List<String> fallbackCmdList = |
| 294 | + Arrays.asList("databricks", "auth", "token", "--host", "https://workspace.databricks.com"); |
| 295 | + |
| 296 | + CliTokenSource tokenSource = makeTokenSource(env, primaryCmd, fallbackCmdList); |
| 297 | + |
| 298 | + AtomicInteger callCount = new AtomicInteger(0); |
| 299 | + try (MockedConstruction<ProcessBuilder> mocked = |
| 300 | + mockConstruction( |
| 301 | + ProcessBuilder.class, |
| 302 | + (pb, context) -> { |
| 303 | + if (callCount.getAndIncrement() == 0) { |
| 304 | + Process failProcess = mock(Process.class); |
| 305 | + when(failProcess.getInputStream()) |
| 306 | + .thenReturn( |
| 307 | + new ByteArrayInputStream( |
| 308 | + "Error: unknown flag: --profile".getBytes())); |
| 309 | + when(failProcess.getErrorStream()) |
| 310 | + .thenReturn(new ByteArrayInputStream(new byte[0])); |
| 311 | + when(failProcess.waitFor()).thenReturn(1); |
| 312 | + when(pb.start()).thenReturn(failProcess); |
| 313 | + } else { |
| 314 | + Process successProcess = mock(Process.class); |
| 315 | + when(successProcess.getInputStream()) |
| 316 | + .thenReturn(new ByteArrayInputStream(validTokenJson("fallback-token").getBytes())); |
| 317 | + when(successProcess.getErrorStream()) |
| 318 | + .thenReturn(new ByteArrayInputStream(new byte[0])); |
| 319 | + when(successProcess.waitFor()).thenReturn(0); |
| 320 | + when(pb.start()).thenReturn(successProcess); |
| 321 | + } |
| 322 | + })) { |
| 323 | + Token token = tokenSource.getToken(); |
| 324 | + assertEquals("fallback-token", token.getAccessToken()); |
| 325 | + assertEquals(2, mocked.constructed().size()); |
| 326 | + } |
| 327 | + } |
| 328 | + |
| 329 | + @Test |
| 330 | + public void testNoFallbackOnRealAuthError() { |
| 331 | + // When the primary fails with a real error (not unknown flag), no fallback is attempted. |
| 332 | + Environment env = mock(Environment.class); |
| 333 | + when(env.getEnv()).thenReturn(new HashMap<>()); |
| 334 | + |
| 335 | + List<String> primaryCmd = |
| 336 | + Arrays.asList("databricks", "auth", "token", "--profile", "my-profile"); |
| 337 | + List<String> fallbackCmdList = |
| 338 | + Arrays.asList("databricks", "auth", "token", "--host", "https://workspace.databricks.com"); |
| 339 | + |
| 340 | + CliTokenSource tokenSource = makeTokenSource(env, primaryCmd, fallbackCmdList); |
| 341 | + |
| 342 | + try (MockedConstruction<ProcessBuilder> mocked = |
| 343 | + mockConstruction( |
| 344 | + ProcessBuilder.class, |
| 345 | + (pb, context) -> { |
| 346 | + Process failProcess = mock(Process.class); |
| 347 | + when(failProcess.getInputStream()) |
| 348 | + .thenReturn(new ByteArrayInputStream(new byte[0])); |
| 349 | + when(failProcess.getErrorStream()) |
| 350 | + .thenReturn( |
| 351 | + new ByteArrayInputStream( |
| 352 | + "databricks OAuth is not configured for this host".getBytes())); |
| 353 | + when(failProcess.waitFor()).thenReturn(1); |
| 354 | + when(pb.start()).thenReturn(failProcess); |
| 355 | + })) { |
| 356 | + DatabricksException ex = |
| 357 | + assertThrows(DatabricksException.class, tokenSource::getToken); |
| 358 | + assertTrue(ex.getMessage().contains("databricks OAuth is not configured")); |
| 359 | + assertEquals(1, mocked.constructed().size()); |
| 360 | + } |
| 361 | + } |
| 362 | + |
| 363 | + @Test |
| 364 | + public void testNoFallbackWhenFallbackCmdNotSet() { |
| 365 | + // When fallbackCmd is null and the primary fails with unknown flag, original error propagates. |
| 366 | + Environment env = mock(Environment.class); |
| 367 | + when(env.getEnv()).thenReturn(new HashMap<>()); |
| 368 | + |
| 369 | + List<String> primaryCmd = |
| 370 | + Arrays.asList("databricks", "auth", "token", "--profile", "my-profile"); |
| 371 | + |
| 372 | + CliTokenSource tokenSource = makeTokenSource(env, primaryCmd, null); |
| 373 | + |
| 374 | + try (MockedConstruction<ProcessBuilder> mocked = |
| 375 | + mockConstruction( |
| 376 | + ProcessBuilder.class, |
| 377 | + (pb, context) -> { |
| 378 | + Process failProcess = mock(Process.class); |
| 379 | + when(failProcess.getInputStream()) |
| 380 | + .thenReturn(new ByteArrayInputStream(new byte[0])); |
| 381 | + when(failProcess.getErrorStream()) |
| 382 | + .thenReturn( |
| 383 | + new ByteArrayInputStream("Error: unknown flag: --profile".getBytes())); |
| 384 | + when(failProcess.waitFor()).thenReturn(1); |
| 385 | + when(pb.start()).thenReturn(failProcess); |
| 386 | + })) { |
| 387 | + DatabricksException ex = |
| 388 | + assertThrows(DatabricksException.class, tokenSource::getToken); |
| 389 | + assertTrue(ex.getMessage().contains("unknown flag: --profile")); |
| 390 | + assertEquals(1, mocked.constructed().size()); |
| 391 | + } |
| 392 | + } |
216 | 393 | } |
0 commit comments