Skip to content

Commit aba2e56

Browse files
committed
update config tests
1 parent 45b2682 commit aba2e56

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

src/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
configureAuthMethods,
3636
createConfiguration,
3737
RequestContext,
38+
SecurityAuthentication,
3839
ServerConfiguration
3940
} from './gen';
4041

@@ -53,7 +54,7 @@ function fileExists(filepath: string): boolean {
5354
}
5455
}
5556

56-
export class KubeConfig {
57+
export class KubeConfig implements SecurityAuthentication{
5758
private static authenticators: Authenticator[] = [
5859
new AzureAuth(),
5960
new GoogleCloudPlatformAuth(),
@@ -191,7 +192,7 @@ export class KubeConfig {
191192
// Copy headers from httpsOptions to RequestContext
192193
const headers = httpsOptions.headers || {};
193194
Object.entries(headers).forEach(([key, value]) => {
194-
context.setHeaderParam(key, '${value}');
195+
context.setHeaderParam(key, `${value}`);
195196
})
196197

197198
// Copy AgentOptions from RequestOptions

src/config_test.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import * as https from 'https';
33
import { join } from 'path';
44
import { RequestOptions } from 'https';
55

6-
import { expect } from 'chai';
6+
import { expect,use } from 'chai';
7+
import chaiAsPromised from 'chai-as-promised'
78
import mockfs = require('mock-fs');
89
import * as path from 'path';
910

@@ -12,6 +13,7 @@ import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolu
1213
import { Cluster, newClusters, newContexts, newUsers, User, ActionOnInvalid } from './config_types';
1314
import { ExecAuth } from './exec_auth';
1415
import { HttpMethod } from '.';
16+
import { assertRequestOptionsEqual } from '../test/match-buffer';
1517

1618
const kcFileName = 'testdata/kubeconfig.yaml';
1719
const kc2FileName = 'testdata/kubeconfig-2.yaml';
@@ -23,6 +25,8 @@ const kcNoUserFileName = 'testdata/empty-user-kubeconfig.yaml';
2325
const kcInvalidContextFileName = 'testdata/empty-context-kubeconfig.yaml';
2426
const kcInvalidClusterFileName = 'testdata/empty-cluster-kubeconfig.yaml';
2527

28+
use(chaiAsPromised)
29+
2630
/* tslint:disable: no-empty */
2731
describe('Config', () => { });
2832

@@ -243,9 +247,9 @@ describe('KubeConfig', () => {
243247

244248
expect(opts).to.deep.equal({
245249
headers: {},
246-
ca: new Buffer('CADATA2', 'utf-8'),
247-
cert: new Buffer('USER2_CADATA', 'utf-8'),
248-
key: new Buffer('USER2_CKDATA', 'utf-8'),
250+
ca: Buffer.from('CADATA2', 'utf-8'),
251+
cert: Buffer.from('USER2_CADATA', 'utf-8'),
252+
key: Buffer.from('USER2_CKDATA', 'utf-8'),
249253
rejectUnauthorized: false,
250254
});
251255
});
@@ -261,17 +265,24 @@ describe('KubeConfig', () => {
261265
const rc = new RequestContext(testServerName1, HttpMethod.GET);
262266
await kc.applySecurityAuthentication(rc);
263267
await kc.applytoHTTPSOptions(opts);
264-
expect(opts).to.deep.equal({
268+
const expectedCA = Buffer.from('CADATA2', 'utf-8')
269+
const expectedAgent = new https.Agent({
270+
ca: expectedCA,
271+
cert: undefined,
272+
key: undefined,
273+
passphrase: undefined,
274+
pfx: undefined,
275+
rejectUnauthorized: false,
276+
})
277+
let expectedOptions: https.RequestOptions = {
278+
auth: 'foo:bar',
265279
headers: {},
266-
ca: new Buffer('CADATA2', 'utf-8'),
267-
auth: {
268-
username: 'foo',
269-
password: 'bar',
270-
},
271-
url: 'https://company.com',
272-
strictSSL: false,
273280
rejectUnauthorized: false,
274-
});
281+
servername: 'https://company.com',
282+
agent: expectedAgent,
283+
}
284+
285+
assertRequestOptionsEqual(opts,expectedOptions)
275286
});
276287
});
277288

test/match-buffer.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { deepStrictEqual } from 'assert';
2+
import { expect } from 'chai';
3+
import { RequestOptions, Agent } from 'https';
14
import { Matcher } from 'ts-mockito/lib/matcher/type/Matcher';
25

36
export function matchBuffer(channel: number, contents: string): Matcher {
@@ -24,3 +27,41 @@ class StringBufferMatcher extends Matcher {
2427
return `buffer did not contain "${this.contents}"`;
2528
}
2629
}
30+
31+
export function assertRequestOptionsEqual(options1: RequestOptions, options2: RequestOptions): void {
32+
//@ts-ignore agent has type Agent | Boolean which we expect to be populated with Agent here
33+
const agent1: Agent = options1.agent
34+
//@ts-ignore
35+
const agent2: Agent = options2.agent
36+
37+
const BUFFER_EQUAL_TRUE = 0
38+
const ca1 = agent1.options.ca
39+
const ca2 = agent2.options.ca
40+
//@ts-ignore
41+
if(ca1 !== ca2 && Buffer.compare(ca1,ca2) !== BUFFER_EQUAL_TRUE){
42+
throw("unequal agent ca buffer")
43+
}
44+
const cert1 = agent1.options.cert
45+
const cert2 = agent2.options.cert
46+
//@ts-ignore
47+
if(cert1 !== cert2 && Buffer.compare(cert1,cert2) !== BUFFER_EQUAL_TRUE){
48+
throw("unequal agent cert buffer")
49+
50+
}
51+
52+
const key1 = agent1.options.key
53+
const key2 = agent2.options.key
54+
//@ts-ignore
55+
if(key1 !== key2 && Buffer.compare(key1,key2) !== BUFFER_EQUAL_TRUE){
56+
throw("unequal agent key buffer")
57+
}
58+
59+
expect(agent1.options.passphrase).to.equal(agent2.options.passphrase);
60+
expect(agent1.options.pfx).to.equal(agent2.options.pfx);
61+
expect(agent1.options.rejectUnauthorized).to.equal(agent2.options.rejectUnauthorized);
62+
63+
expect(options1.auth).to.equal(options2.auth)
64+
expect(options1.headers).to.deep.equal(options2.headers)
65+
expect(options1.rejectUnauthorized).to.equal(options2.rejectUnauthorized)
66+
expect(options1.servername).to.equal(options2.servername)
67+
}

0 commit comments

Comments
 (0)