-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v15] readGrpcCert: Remove Promise.any usage (#50683)
* readGrpcCert: Remove Promise.any usage * Handle errors from the initial fileExistsAndHasSize call
- Loading branch information
Showing
2 changed files
with
128 additions
and
27 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
web/packages/teleterm/src/services/grpcCredentials/files.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import os from 'node:os'; | ||
import timers from 'node:timers/promises'; | ||
|
||
import { readGrpcCert } from './files'; | ||
|
||
let tempDir: string; | ||
|
||
beforeAll(async () => { | ||
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'grpc-files-test')); | ||
}); | ||
|
||
afterAll(async () => { | ||
await fs.rm(tempDir, { recursive: true, force: true }); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('readGrpcCert', () => { | ||
it('reads the file if the file already exists', async () => { | ||
await fs.writeFile(path.join(tempDir, 'already-exists'), 'foobar'); | ||
|
||
await expect( | ||
readGrpcCert(tempDir, 'already-exists').then(buffer => buffer.toString()) | ||
).resolves.toEqual('foobar'); | ||
}); | ||
|
||
it('reads the file when the file is created after starting a watcher', async () => { | ||
const readGrpcCertPromise = readGrpcCert( | ||
tempDir, | ||
'created-after-start' | ||
).then(buffer => buffer.toString()); | ||
await timers.setTimeout(10); | ||
|
||
await fs.writeFile(path.join(tempDir, 'created-after-start'), 'foobar'); | ||
|
||
await expect(readGrpcCertPromise).resolves.toEqual('foobar'); | ||
}); | ||
|
||
it('returns an error if the file is not created within the timeout', async () => { | ||
await expect( | ||
readGrpcCert(tempDir, 'non-existent', { timeoutMs: 1 }) | ||
).rejects.toMatchObject({ | ||
message: expect.stringContaining('within the timeout'), | ||
}); | ||
}); | ||
|
||
it('returns an error if stat fails', async () => { | ||
const expectedError = new Error('Something went wrong'); | ||
jest.spyOn(fs, 'stat').mockRejectedValue(expectedError); | ||
|
||
await expect( | ||
readGrpcCert( | ||
tempDir, | ||
'non-existent', | ||
{ timeoutMs: 100 } // Make sure that the test doesn't hang for 10s on failure. | ||
) | ||
).rejects.toEqual(expectedError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters