Skip to content

Commit 25b4bd0

Browse files
committed
Add better unit testing for requests.
1 parent e6881db commit 25b4bd0

File tree

5 files changed

+142
-6
lines changed

5 files changed

+142
-6
lines changed

package-lock.json

Lines changed: 48 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@
6565
},
6666
"devDependencies": {
6767
"@types/chai": "^4.1.6",
68+
"@types/chai-as-promised": "^7.1.0",
6869
"@types/mocha": "^5.2.5",
6970
"@types/mock-fs": "^3.6.30",
7071
"@types/stream-buffers": "^3.0.3",
7172
"chai": "^4.2.0",
73+
"chai-as-promised": "^7.1.1",
7274
"jasmine": "^3.3.0",
7375
"mocha": "^5.2.0",
7476
"mock-fs": "^4.7.0",
77+
"nock": "^10.0.6",
7578
"nyc": "^13.3.0",
7679
"prettier": "~1.16.4",
7780
"source-map-support": "^0.5.9",

src/config_test.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as https from 'https';
33
import { dirname, join } from 'path';
44

55
import { expect } from 'chai';
6-
import mockfs = require('mock-fs');
6+
import mockfs from 'mock-fs';
77
import * as requestlib from 'request';
88

99
import { Core_v1Api } from './api';
@@ -30,20 +30,24 @@ function validateFileLoad(kc: KubeConfig) {
3030
expect(cluster2.skipTLSVerify).to.equal(true);
3131

3232
// check users
33-
expect(kc.users.length).to.equal(2, 'there are 2 users');
33+
expect(kc.users.length).to.equal(3, 'there are 3 users');
3434
const user1 = kc.users[0];
3535
const user2 = kc.users[1];
36+
const user3 = kc.users[2];
3637
expect(user1.name).to.equal('user1');
3738
expect(user1.certData).to.equal('VVNFUl9DQURBVEE=');
3839
expect(user1.keyData).to.equal('VVNFUl9DS0RBVEE=');
3940
expect(user2.name).to.equal('user2');
4041
expect(user2.certData).to.equal('VVNFUjJfQ0FEQVRB');
4142
expect(user2.keyData).to.equal('VVNFUjJfQ0tEQVRB');
42-
43+
expect(user3.name).to.equal('user3');
44+
expect(user3.username).to.equal('foo');
45+
expect(user3.password).to.equal('bar');
4346
// check contexts
44-
expect(kc.contexts.length).to.equal(2, 'there are two contexts');
47+
expect(kc.contexts.length).to.equal(3, 'there are three contexts');
4548
const context1 = kc.contexts[0];
4649
const context2 = kc.contexts[1];
50+
const context3 = kc.contexts[2];
4751
expect(context1.name).to.equal('context1');
4852
expect(context1.user).to.equal('user1');
4953
expect(context1.namespace).to.equal(undefined);
@@ -52,6 +56,9 @@ function validateFileLoad(kc: KubeConfig) {
5256
expect(context2.user).to.equal('user2');
5357
expect(context2.namespace).to.equal('namespace2');
5458
expect(context2.cluster).to.equal('cluster2');
59+
expect(context3.name).to.equal('passwd');
60+
expect(context3.user).to.equal('user3');
61+
expect(context3.cluster).to.equal('cluster2');
5562

5663
expect(kc.getCurrentContext()).to.equal('context2');
5764
}
@@ -199,6 +206,26 @@ describe('KubeConfig', () => {
199206
rejectUnauthorized: false,
200207
});
201208
});
209+
it('should apply password', () => {
210+
const kc = new KubeConfig();
211+
kc.loadFromFile(kcFileName);
212+
kc.setCurrentContext('passwd');
213+
214+
const opts: requestlib.Options = {
215+
url: 'https://company.com',
216+
};
217+
kc.applyToRequest(opts);
218+
expect(opts).to.deep.equal({
219+
ca: new Buffer('CADATA2', 'utf-8'),
220+
auth: {
221+
username: 'foo',
222+
password: 'bar',
223+
},
224+
url: 'https://company.com',
225+
strictSSL: false,
226+
rejectUnauthorized: false,
227+
});
228+
});
202229
});
203230

204231
describe('loadClusterConfigObjects', () => {

src/integration_test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { expect, use } from 'chai';
2+
import chaiAsPromised from 'chai-as-promised';
3+
import nock from 'nock';
4+
5+
import { Core_v1Api } from './api';
6+
import { KubeConfig } from './config';
7+
import { Cluster, User } from './config_types';
8+
9+
use(chaiAsPromised);
10+
11+
describe('FullRequest', () => {
12+
describe('getPods', () => {
13+
it('should get pods successfully', () => {
14+
const kc = new KubeConfig();
15+
const cluster = {
16+
name: 'foo',
17+
server: 'https://nowhere.foo',
18+
} as Cluster;
19+
const username = 'foo';
20+
const password = 'some-password';
21+
const user = {
22+
name: 'my-user',
23+
username,
24+
password,
25+
} as User;
26+
27+
kc.loadFromClusterAndUser(cluster, user);
28+
29+
const k8sApi = kc.makeApiClient(Core_v1Api);
30+
const result = {
31+
kind: 'PodList',
32+
apiVersion: 'v1',
33+
items: [],
34+
};
35+
const auth = Buffer.from(`${username}:${password}`).toString('base64');
36+
nock('https://nowhere.foo:443', {
37+
reqheaders: {
38+
authorization: `Basic ${auth}`,
39+
},
40+
})
41+
.get('/api/v1/namespaces/default/pods')
42+
.reply(200, result);
43+
44+
const promise = k8sApi.listNamespacedPod('default');
45+
46+
return expect(promise)
47+
.to.eventually.have.property('body')
48+
.that.deep.equals(result);
49+
});
50+
});
51+
});

testdata/kubeconfig.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ contexts:
2020
namespace: namespace2
2121
user: user2
2222
name: context2
23+
- context:
24+
cluster: cluster2
25+
user: user3
26+
name: passwd
2327

2428
current-context: context2
2529
kind: Config
@@ -32,4 +36,8 @@ users:
3236
- name: user2
3337
user:
3438
client-certificate-data: VVNFUjJfQ0FEQVRB
35-
client-key-data: VVNFUjJfQ0tEQVRB
39+
client-key-data: VVNFUjJfQ0tEQVRB
40+
- name: user3
41+
user:
42+
username: foo
43+
password: bar

0 commit comments

Comments
 (0)