Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
286 changes: 149 additions & 137 deletions src/health_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
});