Skip to content

Commit c4cb511

Browse files
committed
Improve test coverage.
1 parent 77bd5e6 commit c4cb511

File tree

4 files changed

+130
-11
lines changed

4 files changed

+130
-11
lines changed

src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,9 @@ export interface Named {
524524

525525
// Only really public for testing...
526526
export function findObject<T extends Named>(list: T[], name: string, key: string): T | null {
527+
if (!list) {
528+
return null;
529+
}
527530
for (const obj of list) {
528531
if (obj.name === name) {
529532
if (obj[key]) {

src/config_test.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as os from 'os';
1313
import { CoreV1Api } from './api';
1414
import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolutePath } from './config';
1515
import { Cluster, newClusters, newContexts, newUsers, User } from './config_types';
16+
import { isUndefined } from 'util';
1617

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

2829
function validateFileLoad(kc: KubeConfig) {
2930
// check clusters
30-
expect(kc.clusters.length).to.equal(2, 'there are 2 clusters');
31-
const cluster1 = kc.clusters[0];
32-
const cluster2 = kc.clusters[1];
31+
const clusters = kc.getClusters();
32+
expect(clusters.length).to.equal(2, 'there are 2 clusters');
33+
const cluster1 = clusters[0];
34+
const cluster2 = clusters[1];
3335
expect(cluster1.name).to.equal('cluster1');
3436
expect(cluster1.caData).to.equal('Q0FEQVRB');
3537
expect(cluster1.server).to.equal('http://example.com');
@@ -39,10 +41,11 @@ function validateFileLoad(kc: KubeConfig) {
3941
expect(cluster2.skipTLSVerify).to.equal(true);
4042

4143
// check users
42-
expect(kc.users.length).to.equal(3, 'there are 3 users');
43-
const user1 = kc.users[0];
44-
const user2 = kc.users[1];
45-
const user3 = kc.users[2];
44+
const users = kc.getUsers();
45+
expect(users.length).to.equal(3, 'there are 3 users');
46+
const user1 = users[0];
47+
const user2 = users[1];
48+
const user3 = users[2];
4649
expect(user1.name).to.equal('user1');
4750
expect(user1.certData).to.equal('VVNFUl9DQURBVEE=');
4851
expect(user1.keyData).to.equal('VVNFUl9DS0RBVEE=');
@@ -52,11 +55,13 @@ function validateFileLoad(kc: KubeConfig) {
5255
expect(user3.name).to.equal('user3');
5356
expect(user3.username).to.equal('foo');
5457
expect(user3.password).to.equal('bar');
58+
5559
// check contexts
56-
expect(kc.contexts.length).to.equal(3, 'there are three contexts');
57-
const context1 = kc.contexts[0];
58-
const context2 = kc.contexts[1];
59-
const context3 = kc.contexts[2];
60+
const contexts = kc.getContexts();
61+
expect(contexts.length).to.equal(3, 'there are three contexts');
62+
const context1 = contexts[0];
63+
const context2 = contexts[1];
64+
const context3 = contexts[2];
6065
expect(context1.name).to.equal('context1');
6166
expect(context1.user).to.equal('user1');
6267
expect(context1.namespace).to.equal(undefined);
@@ -73,7 +78,15 @@ function validateFileLoad(kc: KubeConfig) {
7378
}
7479

7580
describe('KubeConfig', () => {
81+
it('should return null on no contexts', () => {
82+
const kc = new KubeConfig() as any;
83+
kc.contexts = undefined;
84+
expect(kc.getContextObject('non-existent')).to.be.null;
85+
});
7686
describe('findObject', () => {
87+
it('should return null on undefined', () => {
88+
expect(findObject(undefined as any, 'foo', 'bar')).to.equal(null);
89+
});
7790
it('should find objects', () => {
7891
interface MyNamed {
7992
name: string;
@@ -967,6 +980,26 @@ describe('KubeConfig', () => {
967980
});
968981

969982
describe('MakeAbsolutePaths', () => {
983+
it('make paths absolute', () => {
984+
const kc = new KubeConfig();
985+
kc.addCluster({
986+
name: 'testCluster',
987+
server: `https://localhost:9889`,
988+
skipTLSVerify: true,
989+
caFile: 'foo/bar.crt',
990+
});
991+
kc.addUser({
992+
token: 'token',
993+
username: 'username',
994+
name: 'testUser',
995+
certFile: 'user/user.crt',
996+
keyFile: 'user/user.key',
997+
});
998+
kc.makePathsAbsolute('/tmp');
999+
expect(kc.clusters[0].caFile).to.equal('/tmp/foo/bar.crt');
1000+
expect(kc.users[0].certFile).to.equal('/tmp/user/user.crt');
1001+
expect(kc.users[0].keyFile).to.equal('/tmp/user/user.key');
1002+
});
9701003
it('should correctly make absolute paths', () => {
9711004
const relative = 'foo/bar';
9721005
const absolute = '/tmp/foo/bar';
@@ -1156,16 +1189,19 @@ describe('KubeConfig', () => {
11561189
describe('Programmatic', () => {
11571190
it('should be able to generate a valid config from code', () => {
11581191
const kc = new KubeConfig();
1192+
(kc as any).clusters = undefined;
11591193
kc.addCluster({
11601194
name: 'testCluster',
11611195
server: `https://localhost:9889`,
11621196
skipTLSVerify: true,
11631197
});
1198+
(kc as any).users = undefined;
11641199
kc.addUser({
11651200
token: 'token',
11661201
username: 'username',
11671202
name: 'testUser',
11681203
});
1204+
(kc as any).contexts = undefined;
11691205
kc.addContext({
11701206
cluster: 'testCluster',
11711207
name: 'test',

src/file_auth_test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ import { User } from './config_types';
77
import { FileAuth } from './file_auth';
88

99
describe('FileAuth', () => {
10+
it('should refresh when null', async () => {
11+
const auth = new FileAuth();
12+
(auth as any).token = null;
13+
const token = 'test';
14+
mockfs({
15+
'/path/to/fake/dir': {
16+
'token.txt': token,
17+
},
18+
});
19+
const user = {
20+
authProvider: {
21+
config: {
22+
tokenFile: '/path/to/fake/dir/token.txt',
23+
},
24+
},
25+
} as User;
26+
27+
const opts = {} as request.Options;
28+
opts.headers = [];
29+
30+
await auth.applyAuthentication(user, opts);
31+
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
32+
mockfs.restore();
33+
});
1034
it('should refresh when expired', async () => {
1135
const auth = new FileAuth();
1236
(auth as any).token = 'other';

src/watch_test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,54 @@ describe('Watch', () => {
3939
const watch = new Watch(kc);
4040
});
4141

42+
it('should handle non-200 error codes', async () => {
43+
const kc = new KubeConfig();
44+
Object.assign(kc, fakeConfig);
45+
const fakeRequestor = mock(DefaultRequest);
46+
const watch = new Watch(kc, instance(fakeRequestor));
47+
48+
const fakeRequest = {
49+
pipe: (stream) => {},
50+
};
51+
52+
when(fakeRequestor.webRequest(anything(), anyFunction())).thenReturn(fakeRequest);
53+
54+
const path = '/some/path/to/object';
55+
56+
let doneCalled = false;
57+
let doneErr: any;
58+
59+
await watch.watch(
60+
path,
61+
{},
62+
(phase: string, obj: string) => {},
63+
(err: any) => {
64+
doneCalled = true;
65+
doneErr = err;
66+
},
67+
);
68+
69+
verify(fakeRequestor.webRequest(anything(), anyFunction()));
70+
71+
const [opts, doneCallback] = capture(fakeRequestor.webRequest).last();
72+
const reqOpts: request.OptionsWithUri = opts as request.OptionsWithUri;
73+
74+
expect(reqOpts.uri).to.equal(`${server}${path}`);
75+
expect(reqOpts.method).to.equal('GET');
76+
expect(reqOpts.json).to.equal(true);
77+
78+
expect(doneCalled).to.equal(false);
79+
80+
const resp = {
81+
statusCode: 409,
82+
statusMessage: 'Conflict',
83+
};
84+
doneCallback(null, resp, {});
85+
86+
expect(doneCalled).to.equal(true);
87+
expect(doneErr.toString()).to.equal('Error: Conflict');
88+
});
89+
4290
it('should watch correctly', async () => {
4391
const kc = new KubeConfig();
4492
Object.assign(kc, fakeConfig);
@@ -279,4 +327,12 @@ describe('Watch', () => {
279327
expect(receivedTypes).to.deep.equal([obj.type]);
280328
expect(receivedObjects).to.deep.equal([obj.object]);
281329
});
330+
331+
it('should throw on empty config', () => {
332+
const kc = new KubeConfig();
333+
const watch = new Watch(kc);
334+
335+
const promise = watch.watch('/some/path', {}, () => {}, () => {});
336+
expect(promise).to.be.rejected;
337+
});
282338
});

0 commit comments

Comments
 (0)