added 5 more unit tests
This commit is contained in:
@ -8,6 +8,7 @@ import * as fs from 'fs';
|
|||||||
import * as jsyaml from 'js-yaml';
|
import * as jsyaml from 'js-yaml';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as child_process from 'child_process';
|
import * as child_process from 'child_process';
|
||||||
|
import * as exec from '@actions/exec';
|
||||||
var mockStatusCode;
|
var mockStatusCode;
|
||||||
const mockExecFn = jest.fn().mockImplementation(() => mockStatusCode);
|
const mockExecFn = jest.fn().mockImplementation(() => mockStatusCode);
|
||||||
jest.mock('@actions/exec/lib/toolrunner', () => {
|
jest.mock('@actions/exec/lib/toolrunner', () => {
|
||||||
@ -22,6 +23,97 @@ jest.mock('@actions/exec/lib/toolrunner', () => {
|
|||||||
|
|
||||||
|
|
||||||
describe('Testing all functions.', () => {
|
describe('Testing all functions.', () => {
|
||||||
|
|
||||||
|
test('getArcKubeconfig() - checking execution of service-account scenario',async () =>{
|
||||||
|
jest.spyOn(core, 'getInput').mockImplementation((inputName, options) => {
|
||||||
|
if (inputName == 'method') return 'service-account';
|
||||||
|
if (inputName == 'resource-group') return 'testrg';
|
||||||
|
if (inputName == 'cluster-name') return 'testcluster';
|
||||||
|
if (inputName == 'token') return 'token';
|
||||||
|
});
|
||||||
|
jest.spyOn(io, 'which').mockResolvedValue('az');
|
||||||
|
process.env['RUNNER_TEMP'] = 'tempDirPath';
|
||||||
|
jest.spyOn(Date, 'now').mockImplementation(() => 1234561234567);
|
||||||
|
const dummy_process=await child_process.spawn('ls',[]);
|
||||||
|
const dummy_sleep_promise = new Promise(resolve => setTimeout(resolve, 0));
|
||||||
|
jest.spyOn(child_process,'spawn').mockImplementation((commands, arg, option)=>{
|
||||||
|
return dummy_process;
|
||||||
|
});
|
||||||
|
jest.spyOn(arc,'sleep').mockImplementation((ms)=>{
|
||||||
|
return dummy_sleep_promise;
|
||||||
|
});
|
||||||
|
jest.spyOn(fs, 'chmodSync').mockImplementation();
|
||||||
|
jest.spyOn(core, 'exportVariable').mockImplementation();
|
||||||
|
jest.spyOn(exec,'exec').mockImplementation();
|
||||||
|
await arc.getArcKubeconfig();
|
||||||
|
expect(core.getInput).toBeCalledTimes(4);
|
||||||
|
expect(io.which).toHaveBeenCalledWith("az",true);
|
||||||
|
expect(child_process.spawn).toHaveBeenCalledWith('az',['connectedk8s','proxy','-n','testcluster','-g','testrg','-f',path.join('tempDirPath', 'kubeconfig_1234561234567'),'--token','token'], {
|
||||||
|
detached: true,
|
||||||
|
stdio: 'ignore'
|
||||||
|
});
|
||||||
|
expect(fs.chmodSync).toHaveBeenCalledWith(path.join('tempDirPath', 'kubeconfig_1234561234567'), '600');
|
||||||
|
expect(core.exportVariable).toHaveBeenCalledWith('KUBECONFIG', path.join('tempDirPath', 'kubeconfig_1234561234567'));
|
||||||
|
},180000);
|
||||||
|
|
||||||
|
test('getArcKubeconfig() - checking execution of service-principal scenario',async () =>{
|
||||||
|
jest.spyOn(core, 'getInput').mockImplementation((inputName, options) => {
|
||||||
|
if (inputName == 'method') return 'service-principal';
|
||||||
|
if (inputName == 'resource-group') return 'testrg';
|
||||||
|
if (inputName == 'cluster-name') return 'testcluster';
|
||||||
|
});
|
||||||
|
jest.spyOn(io, 'which').mockResolvedValue('az');
|
||||||
|
process.env['RUNNER_TEMP'] = 'tempDirPath';
|
||||||
|
jest.spyOn(Date, 'now').mockImplementation(() => 1234561234567);
|
||||||
|
const dummy_process=await child_process.spawn('ls',[]);
|
||||||
|
const dummy_sleep_promise = new Promise(resolve => setTimeout(resolve, 0));
|
||||||
|
jest.spyOn(child_process,'spawn').mockImplementation((commands, arg, option)=>{
|
||||||
|
return dummy_process;
|
||||||
|
});
|
||||||
|
jest.spyOn(arc,'sleep').mockImplementation((ms)=>{
|
||||||
|
return dummy_sleep_promise;
|
||||||
|
});
|
||||||
|
jest.spyOn(fs, 'chmodSync').mockImplementation();
|
||||||
|
jest.spyOn(core, 'exportVariable').mockImplementation();
|
||||||
|
jest.spyOn(exec,'exec').mockImplementation();
|
||||||
|
await arc.getArcKubeconfig();
|
||||||
|
expect(core.getInput).toBeCalledTimes(3);
|
||||||
|
expect(io.which).toHaveBeenCalledWith("az",true);
|
||||||
|
expect(child_process.spawn).toHaveBeenCalledWith('az',['connectedk8s','proxy','-n','testcluster','-g','testrg','-f',path.join('tempDirPath', 'kubeconfig_1234561234567')], {
|
||||||
|
detached: true,
|
||||||
|
stdio: 'ignore'
|
||||||
|
});
|
||||||
|
expect(fs.chmodSync).toHaveBeenCalledWith(path.join('tempDirPath', 'kubeconfig_1234561234567'), '600');
|
||||||
|
expect(core.exportVariable).toHaveBeenCalledWith('KUBECONFIG', path.join('tempDirPath', 'kubeconfig_1234561234567'));
|
||||||
|
},180000);
|
||||||
|
|
||||||
|
test('getArcKubeconfig() - wrong method passed',async ()=>{
|
||||||
|
jest.spyOn(core, 'getInput').mockImplementation((inputName, options) => {
|
||||||
|
if (inputName == 'method') return 'someMethod';
|
||||||
|
});
|
||||||
|
await expect(arc.getArcKubeconfig()).rejects.toThrow("Supported methods for arc cluster are 'service-account' and 'service-principal'.");
|
||||||
|
expect(core.getInput).toBeCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getArcKubeconfig() - resource group not passed',async ()=>{
|
||||||
|
jest.spyOn(core, 'getInput').mockImplementation((inputName, options) => {
|
||||||
|
if (inputName == 'method') return 'service-account';
|
||||||
|
if (inputName == 'resource-group') return '';
|
||||||
|
});
|
||||||
|
await expect(arc.getArcKubeconfig()).rejects.toThrow("'resourceGroupName' is not passed for arc cluster.");
|
||||||
|
expect(core.getInput).toBeCalledTimes(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getArcKubeconfig() - cluster name not passed',async ()=>{
|
||||||
|
jest.spyOn(core, 'getInput').mockImplementation((inputName, options) => {
|
||||||
|
if (inputName == 'method') return 'service-account';
|
||||||
|
if (inputName == 'resource-group') return 'testrg';
|
||||||
|
if (inputName == 'cluster-name') return '';
|
||||||
|
});
|
||||||
|
await expect(arc.getArcKubeconfig()).rejects.toThrow("'clusterName' is not passed for arc cluster.");
|
||||||
|
expect(core.getInput).toBeCalledTimes(3);
|
||||||
|
});
|
||||||
|
|
||||||
test('getExecutableExtension() - return .exe when os is Windows', () => {
|
test('getExecutableExtension() - return .exe when os is Windows', () => {
|
||||||
jest.spyOn(os, 'type').mockReturnValue('Windows_NT');
|
jest.spyOn(os, 'type').mockReturnValue('Windows_NT');
|
||||||
|
|
||||||
@ -208,35 +300,5 @@ describe('Testing all functions.', () => {
|
|||||||
expect(arc.getArcKubeconfig).toBeCalled();
|
expect(arc.getArcKubeconfig).toBeCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('getArcKubeconfig() - checking successful execution of service-account scenario',async () =>{
|
|
||||||
jest.spyOn(core, 'getInput').mockImplementation((inputName, options) => {
|
|
||||||
if (inputName == 'method') return 'service-account';
|
|
||||||
if (inputName == 'resource-group') return 'testrg';
|
|
||||||
if (inputName == 'cluster-name') return 'testcluster';
|
|
||||||
if (inputName == 'token') return 'token';
|
|
||||||
});
|
|
||||||
jest.spyOn(io, 'which').mockResolvedValue('az');
|
|
||||||
jest.spyOn(arc,'executeAzCliCommand').mockImplementation();
|
|
||||||
process.env['RUNNER_TEMP'] = 'tempDirPath'
|
|
||||||
jest.spyOn(Date, 'now').mockImplementation(() => 1234561234567);
|
|
||||||
jest.spyOn(child_process,'spawn').mockImplementation();
|
|
||||||
jest.spyOn(arc,'sleep').mockImplementation();
|
|
||||||
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {});
|
|
||||||
jest.spyOn(core, 'exportVariable').mockImplementation(() => {});
|
|
||||||
//await arc.getArcKubeconfig();
|
|
||||||
expect(()=>arc.getArcKubeconfig());
|
|
||||||
expect(core.getInput).toBeCalledTimes(4);
|
|
||||||
expect(io.which).toHaveBeenCalledWith("az",true);
|
|
||||||
expect(arc.executeAzCliCommand).toHaveBeenNthCalledWith(1,`account show`, false);
|
|
||||||
expect(arc.executeAzCliCommand).toHaveBeenNthCalledWith(2,`extension remove -n connectedk8s`, false);
|
|
||||||
expect(arc.executeAzCliCommand).toHaveBeenNthCalledWith(3,`extension add -n connectedk8s`, false);
|
|
||||||
expect(arc.executeAzCliCommand).toHaveBeenNthCalledWith(4,`extension list`, false);
|
|
||||||
expect(child_process.spawn).toHaveBeenCalledWith('az',['connectedk8s','proxy','-n','testcluster','-g','testrg','-f',path.join('tempDirPath', 'kubeconfig_1234561234567'),'--token','token'], {
|
|
||||||
detached: true,
|
|
||||||
stdio: 'ignore'
|
|
||||||
});
|
|
||||||
expect(arc.sleep).toBeCalled();
|
|
||||||
expect(fs.chmodSync).toHaveBeenCalledWith(path.join('tempDirPath', 'kubeconfig_1234561234567'), '600');
|
|
||||||
expect(core.exportVariable).toHaveBeenCalledWith('KUBECONFIG', path.join('tempDirPath', 'kubeconfig_1234561234567'));
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
@ -91,4 +91,4 @@ function executeAzCliCommand(command, silent, execOptions = {}, args = []) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.executeAzCliCommand = executeAzCliCommand;
|
exports.executeAzCliCommand = executeAzCliCommand;
|
2333
package-lock.json
generated
2333
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -22,10 +22,10 @@
|
|||||||
"js-yaml": "^3.13.1"
|
"js-yaml": "^3.13.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^12.0.4",
|
|
||||||
"typescript": "3.9.2",
|
|
||||||
"jest": "^26.0.1",
|
|
||||||
"@types/jest": "^25.2.2",
|
"@types/jest": "^25.2.2",
|
||||||
"ts-jest": "^25.5.1"
|
"@types/node": "^12.0.4",
|
||||||
|
"jest": "^26.6.3",
|
||||||
|
"ts-jest": "^25.5.1",
|
||||||
|
"typescript": "3.9.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ var azPath: string;
|
|||||||
|
|
||||||
const kubeconfig_timeout = 120;//timeout in seconds
|
const kubeconfig_timeout = 120;//timeout in seconds
|
||||||
|
|
||||||
export async function getArcKubeconfig() {
|
export async function getArcKubeconfig(): Promise<string> {
|
||||||
try {
|
try {
|
||||||
let method = core.getInput('method');
|
let method = core.getInput('method');
|
||||||
if (method != 'service-account' && method != 'service-principal'){
|
if (method != 'service-account' && method != 'service-principal'){
|
||||||
@ -59,8 +59,8 @@ export async function getArcKubeconfig() {
|
|||||||
fs.chmodSync(kubeconfigPath, '600');
|
fs.chmodSync(kubeconfigPath, '600');
|
||||||
core.exportVariable('KUBECONFIG', kubeconfigPath);
|
core.exportVariable('KUBECONFIG', kubeconfigPath);
|
||||||
console.log('KUBECONFIG environment variable is set');
|
console.log('KUBECONFIG environment variable is set');
|
||||||
} catch (error) {
|
} catch (ex) {
|
||||||
throw new Error(error);
|
return Promise.reject(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user