Skip to content

Commit 3ec3e73

Browse files
authored
Added Unit tests (Azure#15)
* Added unit tests for Azure PowerShell * Added unit tests * changes in utils * removed babel * changed variable name of enable-PSSession * refactor * added ci.yml * changes in utils test
1 parent 73efa77 commit 3ec3e73

14 files changed

Lines changed: 5093 additions & 101 deletions

File tree

.github/workflows/ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
on:
2+
pull_request:
3+
branches:
4+
- master
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
build_test_job:
11+
name: 'Build and test job'
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
matrix:
15+
os: [windows-latest, ubuntu-latest, macos-latest]
16+
steps:
17+
18+
- name: 'Checking out repo code'
19+
uses: actions/checkout@v2
20+
21+
- name: 'Validate build'
22+
run: |
23+
npm install
24+
npm run build
25+
26+
- name: 'Run L0 tests'
27+
run: |
28+
npm run test
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { ServicePrincipalLogin } from '../../src/PowerShell/ServicePrincipalLogin';
2+
3+
jest.mock('../../src/PowerShell/Utilities/Utils');
4+
jest.mock('../../src/PowerShell/Utilities/PowerShellToolRunner');
5+
let spnlogin: ServicePrincipalLogin;
6+
7+
beforeAll(() => {
8+
spnlogin = new ServicePrincipalLogin("servicePrincipalID", "servicePrinicipalkey", "tenantId", "subscriptionId");
9+
});
10+
11+
afterEach(() => {
12+
jest.restoreAllMocks();
13+
});
14+
15+
describe('Testing initialize', () => {
16+
let initializeSpy;
17+
18+
beforeEach(() => {
19+
initializeSpy = jest.spyOn(spnlogin, 'initialize');
20+
});
21+
test('ServicePrincipalLogin initialize should pass', async () => {
22+
await spnlogin.initialize();
23+
expect(initializeSpy).toHaveBeenCalled();
24+
});
25+
});
26+
27+
describe('Testing login', () => {
28+
let loginSpy;
29+
30+
beforeEach(() => {
31+
loginSpy = jest.spyOn(spnlogin, 'login');
32+
});
33+
test('ServicePrincipal login should pass', async () => {
34+
loginSpy.mockImplementationOnce(() => Promise.resolve(
35+
console.log('Azure PowerShell session successfully initialized')));
36+
await spnlogin.login();
37+
expect(loginSpy).toHaveBeenCalled();
38+
});
39+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Utils from '../../../src/PowerShell/Utilities/Utils';
2+
3+
const version: string = '9.0.0';
4+
const moduleName: string = 'az';
5+
6+
afterEach(() => {
7+
jest.restoreAllMocks();
8+
});
9+
10+
describe('Testing isValidVersion', () => {
11+
const validVersion: string = '1.2.4';
12+
const invalidVersion: string = 'a.bcd';
13+
14+
test('isValidVersion should be true', () => {
15+
expect(Utils.isValidVersion(validVersion)).toBeTruthy();
16+
});
17+
test('isValidVersion should be false', () => {
18+
expect(Utils.isValidVersion(invalidVersion)).toBeFalsy();
19+
});
20+
});
21+
22+
describe('Testing setPSModulePath', () => {
23+
test('PSModulepath with azPSVersion non-empty', () => {
24+
if(!process.env.PSModulePath) {
25+
process.env.PSModulePath = process.env.PSModulePath + "modulePath";
26+
}
27+
Utils.setPSModulePath(version);
28+
expect(process.env.PSModulepath).toContain(version);
29+
});
30+
test('PSModulePath with azPSVersion empty', () => {
31+
const currPSModulePath = process.env.PSModulepath;
32+
Utils.setPSModulePath();
33+
expect(process.env.PSModulePath).not.toEqual(currPSModulePath);
34+
});
35+
});
36+
37+
describe('Testing getLatestModule', () => {
38+
let getLatestModuleSpy;
39+
40+
beforeEach(() => {
41+
getLatestModuleSpy = jest.spyOn(Utils, 'getLatestModule');
42+
});
43+
test('getLatestModule should pass', async () => {
44+
getLatestModuleSpy.mockImplementationOnce((_moduleName: string) => Promise.resolve(version));
45+
await Utils.getLatestModule(moduleName);
46+
expect(getLatestModuleSpy).toHaveBeenCalled();
47+
});
48+
});

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inputs:
55
creds:
66
description: 'Paste output of `az ad sp create-for-rbac` as value of secret variable: AZURE_CREDENTIALS'
77
required: true
8-
enable-PSSession:
8+
enable-AzPSSession:
99
description: 'Set this value to true to enable Azure PowerShell Login in addition to Az CLI login'
1010
required: false
1111
default: false

jest.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
clearMocks: true,
6+
moduleFileExtensions: ['js', 'ts'],
7+
testEnvironment: 'node',
8+
testMatch: ['**/*.test.ts'],
9+
testRunner: 'jest-circus/runner',
10+
transform: {
11+
'^.+\\.ts$': 'ts-jest'
12+
},
13+
verbose: true
14+
};

lib/PowerShell/Utilities/PowerShellToolRunner.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,5 @@ class PowerShellToolRunner {
3131
yield exec.exec(`${PowerShellToolRunner.psPath} -Command`, [scriptBlock], options);
3232
});
3333
}
34-
static executePowerShellCommand(command, options = {}) {
35-
return __awaiter(this, void 0, void 0, function* () {
36-
yield exec.exec(`"${PowerShellToolRunner.psPath}" -Command "${command}"`, [], options);
37-
});
38-
}
3934
}
4035
exports.default = PowerShellToolRunner;

lib/PowerShell/Utilities/Utils.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ const PowerShellToolRunner_1 = __importDefault(require("./PowerShellToolRunner")
2626
class Utils {
2727
/**
2828
* Add the folder path where Az modules are present to PSModulePath based on runner
29-
*
3029
* @param azPSVersion
31-
*
3230
* If azPSVersion is empty, folder path in which all Az modules are present are set
3331
* If azPSVersion is not empty, folder path of exact Az module version is set
34-
*
3532
*/
3633
static setPSModulePath(azPSVersion = "") {
3734
let modulePath = "";

lib/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ function main() {
4444
let servicePrincipalKey = secrets.getSecret("$.clientSecret", true);
4545
let tenantId = secrets.getSecret("$.tenantId", false);
4646
let subscriptionId = secrets.getSecret("$.subscriptionId", false);
47-
const enablePSSession = core.getInput('enable-PSSession').toLowerCase() === "true";
47+
const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true";
4848
if (!servicePrincipalId || !servicePrincipalKey || !tenantId || !subscriptionId) {
4949
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied.");
5050
}
5151
// Attempting Az cli login
5252
yield executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`);
5353
yield executeAzCliCommand(`account set --subscription "${subscriptionId}"`);
5454
isAzCLISuccess = true;
55-
if (enablePSSession) {
55+
if (enableAzPSSession) {
5656
// Attempting Az PS login
5757
console.log(`Running Azure PS Login`);
5858
const spnlogin = new ServicePrincipalLogin_1.ServicePrincipalLogin(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId);

0 commit comments

Comments
 (0)