diff --git a/src/health.ts b/src/health.ts index 569418f7e7..59e29aafe5 100644 --- a/src/health.ts +++ b/src/health.ts @@ -48,8 +48,8 @@ export class Health { return this.healthz(opts); } return false; - } catch (err: unknown) { - if (err instanceof Error && err.name === 'AbortError') { + } catch (err: any) { + if (err.name === 'AbortError') { throw err; } throw new Error('Error occurred in health request'); diff --git a/src/health_test.ts b/src/health_test.ts index 8684f9dfeb..84a172b9ca 100644 --- a/src/health_test.ts +++ b/src/health_test.ts @@ -4,145 +4,157 @@ import nock from 'nock'; import { KubeConfig } from './config.js'; import { Health } from './health.js'; import { Cluster, User } from './config_types.js'; +import { RequestOptions } from 'node:https'; describe('Health', () => { - describe('livez', () => { - it('should throw an error if no current active cluster', async () => { - const kc = new KubeConfig(); - const health = new Health(kc); - await rejects(health.livez({}), { message: 'No currently active cluster' }); - }); - - it('should return true if /livez returns with status 200', async () => { - const kc = new KubeConfig(); - const cluster = { - name: 'foo', - server: 'https://server.com', - } as Cluster; - - const user = { - name: 'my-user', - password: 'some-password', - } as User; - kc.loadFromClusterAndUser(cluster, user); - - const scope = nock('https://server.com').get('/livez').reply(200); - const health = new Health(kc); - - const r = await health.livez({}); - strictEqual(r, true); - scope.done(); - }); - - it('should return false if /livez returns with status 500', async () => { - const kc = new KubeConfig(); - const cluster = { - name: 'foo', - server: 'https://server.com', - } as Cluster; - - const user = { - name: 'my-user', - password: 'some-password', - } as User; - kc.loadFromClusterAndUser(cluster, user); - - const scope = nock('https://server.com').get('/livez').reply(500); - const health = new Health(kc); - - const r = await health.livez({}); - strictEqual(r, false); - scope.done(); - }); - - it('should return true if /livez returns status 404 and /healthz returns status 200', async () => { - const kc = new KubeConfig(); - const cluster = { - name: 'foo', - server: 'https://server.com', - } as Cluster; - - const user = { - name: 'my-user', - password: 'some-password', - } as User; - kc.loadFromClusterAndUser(cluster, user); - - const scope = nock('https://server.com'); - scope.get('/livez').reply(404); - scope.get('/healthz').reply(200); - const health = new Health(kc); - - const r = await health.livez({}); - strictEqual(r, true); - scope.done(); - }); - - it('should return false if /livez returns status 404 and /healthz returns status 500', async () => { - const kc = new KubeConfig(); - const cluster = { - name: 'foo', - server: 'https://server.com', - } as Cluster; - - const user = { - name: 'my-user', - password: 'some-password', - } as User; - kc.loadFromClusterAndUser(cluster, user); - - const scope = nock('https://server.com'); - scope.get('/livez').reply(404); - scope.get('/healthz').reply(500); - const health = new Health(kc); - - const r = await health.livez({}); - strictEqual(r, false); - scope.done(); - }); - - it('should return true if both /livez and /healthz return status 404', async () => { - const kc = new KubeConfig(); - const cluster = { - name: 'foo', - server: 'https://server.com', - } as Cluster; - - const user = { - name: 'my-user', - password: 'some-password', - } as User; - kc.loadFromClusterAndUser(cluster, user); - - const scope = nock('https://server.com'); - scope.get('/livez').reply(404); - scope.get('/healthz').reply(200); - const health = new Health(kc); - - const r = await health.livez({}); - strictEqual(r, true); - scope.done(); - }); - - it('should throw an error when fetch throws an error', async () => { - const kc = new KubeConfig(); - const cluster = { - name: 'foo', - server: 'https://server.com', - } as Cluster; - - const user = { - name: 'my-user', - password: 'some-password', - } as User; - kc.loadFromClusterAndUser(cluster, user); - - const scope = nock('https://server.com'); - scope.get('/livez').replyWithError(new Error('an error')); - const health = new Health(kc); - - await rejects(health.livez({}), { message: 'Error occurred in health request' }); - scope.done(); + [ + { + path: '/livez', + method: async (health: Health, opts: RequestOptions) => health.livez(opts), + }, + { + path: '/readyz', + method: async (health: Health, opts: RequestOptions) => health.readyz(opts), + }, + ].forEach((test) => { + describe(test.path, () => { + it('should throw an error if no current active cluster', async () => { + const kc = new KubeConfig(); + const health = new Health(kc); + await rejects(test.method(health, {}), { message: 'No currently active cluster' }); + }); + + it(`should return true if ${test.path} returns with status 200`, async () => { + const kc = new KubeConfig(); + const cluster = { + name: 'foo', + server: 'https://server.com', + } as Cluster; + + const user = { + name: 'my-user', + password: 'some-password', + } as User; + kc.loadFromClusterAndUser(cluster, user); + + const scope = nock('https://server.com').get(test.path).reply(200); + const health = new Health(kc); + + const r = await test.method(health, {}); + strictEqual(r, true); + scope.done(); + }); + + it(`should return false if ${test.path} returns with status 500`, async () => { + const kc = new KubeConfig(); + const cluster = { + name: 'foo', + server: 'https://server.com', + } as Cluster; + + const user = { + name: 'my-user', + password: 'some-password', + } as User; + kc.loadFromClusterAndUser(cluster, user); + + const scope = nock('https://server.com').get(test.path).reply(500); + const health = new Health(kc); + + const r = await test.method(health, {}); + strictEqual(r, false); + scope.done(); + }); + + it(`should return true if ${test.path} returns status 404 and /healthz returns status 200`, async () => { + const kc = new KubeConfig(); + const cluster = { + name: 'foo', + server: 'https://server.com', + } as Cluster; + + const user = { + name: 'my-user', + password: 'some-password', + } as User; + kc.loadFromClusterAndUser(cluster, user); + + const scope = nock('https://server.com'); + scope.get(test.path).reply(404); + scope.get('/healthz').reply(200); + const health = new Health(kc); + + const r = await test.method(health, {}); + strictEqual(r, true); + scope.done(); + }); + + it(`should return false if ${test.path} returns status 404 and /healthz returns status 500`, async () => { + const kc = new KubeConfig(); + const cluster = { + name: 'foo', + server: 'https://server.com', + } as Cluster; + + const user = { + name: 'my-user', + password: 'some-password', + } as User; + kc.loadFromClusterAndUser(cluster, user); + + const scope = nock('https://server.com'); + scope.get(test.path).reply(404); + scope.get('/healthz').reply(500); + const health = new Health(kc); + + const r = await test.method(health, {}); + strictEqual(r, false); + scope.done(); + }); + + it(`should return true if both ${test.path} and /healthz return status 404`, async () => { + const kc = new KubeConfig(); + const cluster = { + name: 'foo', + server: 'https://server.com', + } as Cluster; + + const user = { + name: 'my-user', + password: 'some-password', + } as User; + kc.loadFromClusterAndUser(cluster, user); + + const scope = nock('https://server.com'); + scope.get(test.path).reply(404); + scope.get('/healthz').reply(404); + const health = new Health(kc); + + const r = await test.method(health, {}); + strictEqual(r, true); + scope.done(); + }); + + it('should throw an error when fetch throws an error', async () => { + const kc = new KubeConfig(); + const cluster = { + name: 'foo', + server: 'https://server.com', + } as Cluster; + + const user = { + name: 'my-user', + password: 'some-password', + } as User; + kc.loadFromClusterAndUser(cluster, user); + + const scope = nock('https://server.com'); + scope.get(test.path).replyWithError(new Error('an error')); + const health = new Health(kc); + + await rejects(test.method(health, {}), { message: 'Error occurred in health request' }); + scope.done(); + }); }); }); });