Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Feb 24, 2020
1 parent 77bd5e6 commit c4cb511
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ export interface Named {

// Only really public for testing...
export function findObject<T extends Named>(list: T[], name: string, key: string): T | null {
if (!list) {
return null;
}
for (const obj of list) {
if (obj.name === name) {
if (obj[key]) {
Expand Down
58 changes: 47 additions & 11 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as os from 'os';
import { CoreV1Api } from './api';
import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolutePath } from './config';
import { Cluster, newClusters, newContexts, newUsers, User } from './config_types';
import { isUndefined } from 'util';

const kcFileName = 'testdata/kubeconfig.yaml';
const kc2FileName = 'testdata/kubeconfig-2.yaml';
Expand All @@ -27,9 +28,10 @@ describe('Config', () => {});

function validateFileLoad(kc: KubeConfig) {
// check clusters
expect(kc.clusters.length).to.equal(2, 'there are 2 clusters');
const cluster1 = kc.clusters[0];
const cluster2 = kc.clusters[1];
const clusters = kc.getClusters();
expect(clusters.length).to.equal(2, 'there are 2 clusters');
const cluster1 = clusters[0];
const cluster2 = clusters[1];
expect(cluster1.name).to.equal('cluster1');
expect(cluster1.caData).to.equal('Q0FEQVRB');
expect(cluster1.server).to.equal('http://example.com');
Expand All @@ -39,10 +41,11 @@ function validateFileLoad(kc: KubeConfig) {
expect(cluster2.skipTLSVerify).to.equal(true);

// check users
expect(kc.users.length).to.equal(3, 'there are 3 users');
const user1 = kc.users[0];
const user2 = kc.users[1];
const user3 = kc.users[2];
const users = kc.getUsers();
expect(users.length).to.equal(3, 'there are 3 users');
const user1 = users[0];
const user2 = users[1];
const user3 = users[2];
expect(user1.name).to.equal('user1');
expect(user1.certData).to.equal('VVNFUl9DQURBVEE=');
expect(user1.keyData).to.equal('VVNFUl9DS0RBVEE=');
Expand All @@ -52,11 +55,13 @@ function validateFileLoad(kc: KubeConfig) {
expect(user3.name).to.equal('user3');
expect(user3.username).to.equal('foo');
expect(user3.password).to.equal('bar');

// check contexts
expect(kc.contexts.length).to.equal(3, 'there are three contexts');
const context1 = kc.contexts[0];
const context2 = kc.contexts[1];
const context3 = kc.contexts[2];
const contexts = kc.getContexts();
expect(contexts.length).to.equal(3, 'there are three contexts');
const context1 = contexts[0];
const context2 = contexts[1];
const context3 = contexts[2];
expect(context1.name).to.equal('context1');
expect(context1.user).to.equal('user1');
expect(context1.namespace).to.equal(undefined);
Expand All @@ -73,7 +78,15 @@ function validateFileLoad(kc: KubeConfig) {
}

describe('KubeConfig', () => {
it('should return null on no contexts', () => {
const kc = new KubeConfig() as any;
kc.contexts = undefined;
expect(kc.getContextObject('non-existent')).to.be.null;
});
describe('findObject', () => {
it('should return null on undefined', () => {
expect(findObject(undefined as any, 'foo', 'bar')).to.equal(null);
});
it('should find objects', () => {
interface MyNamed {
name: string;
Expand Down Expand Up @@ -967,6 +980,26 @@ describe('KubeConfig', () => {
});

describe('MakeAbsolutePaths', () => {
it('make paths absolute', () => {
const kc = new KubeConfig();
kc.addCluster({
name: 'testCluster',
server: `https://localhost:9889`,
skipTLSVerify: true,
caFile: 'foo/bar.crt',
});
kc.addUser({
token: 'token',
username: 'username',
name: 'testUser',
certFile: 'user/user.crt',
keyFile: 'user/user.key',
});
kc.makePathsAbsolute('/tmp');
expect(kc.clusters[0].caFile).to.equal('/tmp/foo/bar.crt');
expect(kc.users[0].certFile).to.equal('/tmp/user/user.crt');
expect(kc.users[0].keyFile).to.equal('/tmp/user/user.key');
});
it('should correctly make absolute paths', () => {
const relative = 'foo/bar';
const absolute = '/tmp/foo/bar';
Expand Down Expand Up @@ -1156,16 +1189,19 @@ describe('KubeConfig', () => {
describe('Programmatic', () => {
it('should be able to generate a valid config from code', () => {
const kc = new KubeConfig();
(kc as any).clusters = undefined;
kc.addCluster({
name: 'testCluster',
server: `https://localhost:9889`,
skipTLSVerify: true,
});
(kc as any).users = undefined;
kc.addUser({
token: 'token',
username: 'username',
name: 'testUser',
});
(kc as any).contexts = undefined;
kc.addContext({
cluster: 'testCluster',
name: 'test',
Expand Down
24 changes: 24 additions & 0 deletions src/file_auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ import { User } from './config_types';
import { FileAuth } from './file_auth';

describe('FileAuth', () => {
it('should refresh when null', async () => {
const auth = new FileAuth();
(auth as any).token = null;
const token = 'test';
mockfs({
'/path/to/fake/dir': {
'token.txt': token,
},
});
const user = {
authProvider: {
config: {
tokenFile: '/path/to/fake/dir/token.txt',
},
},
} as User;

const opts = {} as request.Options;
opts.headers = [];

await auth.applyAuthentication(user, opts);
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
mockfs.restore();
});
it('should refresh when expired', async () => {
const auth = new FileAuth();
(auth as any).token = 'other';
Expand Down
56 changes: 56 additions & 0 deletions src/watch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,54 @@ describe('Watch', () => {
const watch = new Watch(kc);
});

it('should handle non-200 error codes', async () => {
const kc = new KubeConfig();
Object.assign(kc, fakeConfig);
const fakeRequestor = mock(DefaultRequest);
const watch = new Watch(kc, instance(fakeRequestor));

const fakeRequest = {
pipe: (stream) => {},
};

when(fakeRequestor.webRequest(anything(), anyFunction())).thenReturn(fakeRequest);

const path = '/some/path/to/object';

let doneCalled = false;
let doneErr: any;

await watch.watch(
path,
{},
(phase: string, obj: string) => {},
(err: any) => {
doneCalled = true;
doneErr = err;
},
);

verify(fakeRequestor.webRequest(anything(), anyFunction()));

const [opts, doneCallback] = capture(fakeRequestor.webRequest).last();
const reqOpts: request.OptionsWithUri = opts as request.OptionsWithUri;

expect(reqOpts.uri).to.equal(`${server}${path}`);
expect(reqOpts.method).to.equal('GET');
expect(reqOpts.json).to.equal(true);

expect(doneCalled).to.equal(false);

const resp = {
statusCode: 409,
statusMessage: 'Conflict',
};
doneCallback(null, resp, {});

expect(doneCalled).to.equal(true);
expect(doneErr.toString()).to.equal('Error: Conflict');
});

it('should watch correctly', async () => {
const kc = new KubeConfig();
Object.assign(kc, fakeConfig);
Expand Down Expand Up @@ -279,4 +327,12 @@ describe('Watch', () => {
expect(receivedTypes).to.deep.equal([obj.type]);
expect(receivedObjects).to.deep.equal([obj.object]);
});

it('should throw on empty config', () => {
const kc = new KubeConfig();
const watch = new Watch(kc);

const promise = watch.watch('/some/path', {}, () => {}, () => {});
expect(promise).to.be.rejected;
});
});

0 comments on commit c4cb511

Please sign in to comment.