-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Identity] Test improvements #15999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Identity] Test improvements #15999
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,6 @@ interface AuthRequestDetails { | |
| describe("ManagedIdentityCredential", function() { | ||
| let envCopy: string = ""; | ||
| let sandbox: Sinon.SinonSandbox; | ||
| let clock: Sinon.SinonFakeTimers; | ||
|
|
||
| beforeEach(() => { | ||
| envCopy = JSON.stringify(process.env); | ||
|
|
@@ -39,10 +38,6 @@ describe("ManagedIdentityCredential", function() { | |
| delete process.env.IDENTITY_SERVER_THUMBPRINT; | ||
| delete process.env.IMDS_ENDPOINT; | ||
| sandbox = Sinon.createSandbox(); | ||
| clock = sandbox.useFakeTimers({ | ||
| now: Date.now(), | ||
| shouldAdvanceTime: true | ||
| }); | ||
| }); | ||
| afterEach(() => { | ||
| const env = JSON.parse(envCopy); | ||
|
|
@@ -53,7 +48,6 @@ describe("ManagedIdentityCredential", function() { | |
| process.env.IDENTITY_SERVER_THUMBPRINT = env.IDENTITY_SERVER_THUMBPRINT; | ||
| process.env.IMDS_ENDPOINT = env.IMDS_ENDPOINT; | ||
| sandbox.restore(); | ||
| clock.restore(); | ||
| }); | ||
|
|
||
| it("sends an authorization request with a modified resource name", async function() { | ||
|
|
@@ -232,12 +226,16 @@ describe("ManagedIdentityCredential", function() { | |
| ...mockHttpClient.tokenCredentialOptions | ||
| }); | ||
|
|
||
| const promise = credential.getToken("scopes"); | ||
| // Sinon's clock has some issues with Node 16. | ||
| // If we remove this conditional on Node 16, we get the following error: | ||
| // PromiseRejection HandledWarning: Promise rejection was handled asynchronously | ||
| // It will point to the `await promise` line below. | ||
| const clock = process.version.startsWith("v16") ? undefined : sandbox.useFakeTimers(); | ||
|
|
||
| imdsMsiRetryConfig.startDelayInMs = 80; // 800ms / 10 | ||
| const promise = credential.getToken("scopes"); | ||
|
|
||
| // 800ms -> 1600ms -> 3200ms, results in 6400ms, / 10 = 640ms | ||
| clock.tick(640); | ||
| // 800ms -> 1600ms -> 3200ms, results in 6400ms | ||
| await clock?.tickAsync(6400); | ||
|
||
|
|
||
| await assertRejects( | ||
| promise, | ||
|
|
@@ -246,6 +244,8 @@ describe("ManagedIdentityCredential", function() { | |
| `Failed to retrieve IMDS token after ${imdsMsiRetryConfig.maxRetries} retries.` | ||
| ) > -1 | ||
| ); | ||
|
|
||
| clock?.restore(); | ||
| }); | ||
|
|
||
| // Unavailable exception throws while IMDS endpoint is unavailable. This test not valid. | ||
|
|
@@ -397,15 +397,7 @@ describe("ManagedIdentityCredential", function() { | |
| ); | ||
|
|
||
| assert.equal(authRequest.headers.get("Authorization"), `Basic ${key}`); | ||
| if (authDetails.token) { | ||
| // We use Date.now underneath. | ||
| assert.equal( | ||
| Math.floor(authDetails.token.expiresOnTimestamp / 1000000), | ||
| Math.floor(Date.now() / 1000000) | ||
| ); | ||
| } else { | ||
| assert.fail("No token was returned!"); | ||
| } | ||
| assert.ok(authDetails.token?.expiresOnTimestamp); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There’s really no reason to test the contents of expiresOnTimestamp. We define this value in our test utilities. The processing of the request will take a variable time depending on the environment (Node, VM, etc) |
||
| } finally { | ||
| unlinkSync(tempFile); | ||
| rmdirSync(tempDir); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be a nice follow up item to submit a bug to sinon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the point of using the clock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am trying to narrow this down to an actual bug outside of our tests. It’s a bit challenging. Once I have it narrowed down, I’ll make an issue on Sinon (or perhaps do changes in our code to fix it? I don’t know yet).