Skip to content

update config tests #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
5 changes: 3 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
configureAuthMethods,
createConfiguration,
RequestContext,
SecurityAuthentication,
ServerConfiguration
} from './gen';

Expand All @@ -53,7 +54,7 @@ function fileExists(filepath: string): boolean {
}
}

export class KubeConfig {
export class KubeConfig implements SecurityAuthentication{
private static authenticators: Authenticator[] = [
new AzureAuth(),
new GoogleCloudPlatformAuth(),
Expand Down Expand Up @@ -191,7 +192,7 @@ export class KubeConfig {
// Copy headers from httpsOptions to RequestContext
const headers = httpsOptions.headers || {};
Object.entries(headers).forEach(([key, value]) => {
context.setHeaderParam(key, '${value}');
context.setHeaderParam(key, `${value}`);
})

// Copy AgentOptions from RequestOptions
Expand Down
37 changes: 24 additions & 13 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as https from 'https';
import { join } from 'path';
import { RequestOptions } from 'https';

import { expect } from 'chai';
import { expect,use } from 'chai';
import chaiAsPromised from 'chai-as-promised'
import mockfs = require('mock-fs');
import * as path from 'path';

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

const kcFileName = 'testdata/kubeconfig.yaml';
const kc2FileName = 'testdata/kubeconfig-2.yaml';
Expand All @@ -23,6 +25,8 @@ const kcNoUserFileName = 'testdata/empty-user-kubeconfig.yaml';
const kcInvalidContextFileName = 'testdata/empty-context-kubeconfig.yaml';
const kcInvalidClusterFileName = 'testdata/empty-cluster-kubeconfig.yaml';

use(chaiAsPromised)

/* tslint:disable: no-empty */
describe('Config', () => { });

Expand Down Expand Up @@ -243,9 +247,9 @@ describe('KubeConfig', () => {

expect(opts).to.deep.equal({
headers: {},
ca: new Buffer('CADATA2', 'utf-8'),
cert: new Buffer('USER2_CADATA', 'utf-8'),
key: new Buffer('USER2_CKDATA', 'utf-8'),
ca: Buffer.from('CADATA2', 'utf-8'),
cert: Buffer.from('USER2_CADATA', 'utf-8'),
key: Buffer.from('USER2_CKDATA', 'utf-8'),
rejectUnauthorized: false,
});
});
Expand All @@ -261,17 +265,24 @@ describe('KubeConfig', () => {
const rc = new RequestContext(testServerName1, HttpMethod.GET);
await kc.applySecurityAuthentication(rc);
await kc.applytoHTTPSOptions(opts);
expect(opts).to.deep.equal({
const expectedCA = Buffer.from('CADATA2', 'utf-8')
const expectedAgent = new https.Agent({
ca: expectedCA,
cert: undefined,
key: undefined,
passphrase: undefined,
pfx: undefined,
rejectUnauthorized: false,
})
let expectedOptions: https.RequestOptions = {
auth: 'foo:bar',
headers: {},
ca: new Buffer('CADATA2', 'utf-8'),
auth: {
username: 'foo',
password: 'bar',
},
url: 'https://company.com',
strictSSL: false,
rejectUnauthorized: false,
});
servername: 'https://company.com',
agent: expectedAgent,
}

assertRequestOptionsEqual(opts,expectedOptions)
});
});

Expand Down
41 changes: 41 additions & 0 deletions test/match-buffer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { deepStrictEqual } from 'assert';
import { expect } from 'chai';
import { RequestOptions, Agent } from 'https';
import { Matcher } from 'ts-mockito/lib/matcher/type/Matcher';

export function matchBuffer(channel: number, contents: string): Matcher {
Expand All @@ -24,3 +27,41 @@ class StringBufferMatcher extends Matcher {
return `buffer did not contain "${this.contents}"`;
}
}

export function assertRequestOptionsEqual(options1: RequestOptions, options2: RequestOptions): void {
//@ts-ignore agent has type Agent | Boolean which we expect to be populated with Agent here
const agent1: Agent = options1.agent
//@ts-ignore
const agent2: Agent = options2.agent

const BUFFER_EQUAL_TRUE = 0
const ca1 = agent1.options.ca
const ca2 = agent2.options.ca
//@ts-ignore
if(ca1 !== ca2 && Buffer.compare(ca1,ca2) !== BUFFER_EQUAL_TRUE){
throw("unequal agent ca buffer")
}
const cert1 = agent1.options.cert
const cert2 = agent2.options.cert
//@ts-ignore
if(cert1 !== cert2 && Buffer.compare(cert1,cert2) !== BUFFER_EQUAL_TRUE){
throw("unequal agent cert buffer")

}

const key1 = agent1.options.key
const key2 = agent2.options.key
//@ts-ignore
if(key1 !== key2 && Buffer.compare(key1,key2) !== BUFFER_EQUAL_TRUE){
throw("unequal agent key buffer")
}

expect(agent1.options.passphrase).to.equal(agent2.options.passphrase);
expect(agent1.options.pfx).to.equal(agent2.options.pfx);
expect(agent1.options.rejectUnauthorized).to.equal(agent2.options.rejectUnauthorized);

expect(options1.auth).to.equal(options2.auth)
expect(options1.headers).to.deep.equal(options2.headers)
expect(options1.rejectUnauthorized).to.equal(options2.rejectUnauthorized)
expect(options1.servername).to.equal(options2.servername)
}