Skip to content
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
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
on:
pull_request:
branches:
- master
push:
branches:
- master

jobs:
build_test_job:
name: 'Build and test job'
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
steps:

- name: 'Checking out repo code'
uses: actions/checkout@v2

- name: 'Validate build'
run: |
npm install
npm run build

- name: 'Run L0 tests'
run: |
npm run test
39 changes: 39 additions & 0 deletions __tests__/PowerShell/ServicePrinicipalLogin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ServicePrincipalLogin } from '../../src/PowerShell/ServicePrincipalLogin';

jest.mock('../../src/PowerShell/Utilities/Utils');
jest.mock('../../src/PowerShell/Utilities/PowerShellToolRunner');
let spnlogin: ServicePrincipalLogin;

beforeAll(() => {
spnlogin = new ServicePrincipalLogin("servicePrincipalID", "servicePrinicipalkey", "tenantId", "subscriptionId");
});

afterEach(() => {
jest.restoreAllMocks();
});

describe('Testing initialize', () => {
let initializeSpy;

beforeEach(() => {
initializeSpy = jest.spyOn(spnlogin, 'initialize');
});
test('ServicePrincipalLogin initialize should pass', async () => {
await spnlogin.initialize();
expect(initializeSpy).toHaveBeenCalled();
});
});

describe('Testing login', () => {
let loginSpy;

beforeEach(() => {
loginSpy = jest.spyOn(spnlogin, 'login');
});
test('ServicePrincipal login should pass', async () => {
loginSpy.mockImplementationOnce(() => Promise.resolve(
console.log('Azure PowerShell session successfully initialized')));
await spnlogin.login();
expect(loginSpy).toHaveBeenCalled();
});
});
48 changes: 48 additions & 0 deletions __tests__/PowerShell/Utilities/Utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Utils from '../../../src/PowerShell/Utilities/Utils';

const version: string = '9.0.0';
const moduleName: string = 'az';

afterEach(() => {
jest.restoreAllMocks();
});

describe('Testing isValidVersion', () => {
const validVersion: string = '1.2.4';
const invalidVersion: string = 'a.bcd';

test('isValidVersion should be true', () => {
expect(Utils.isValidVersion(validVersion)).toBeTruthy();
});
test('isValidVersion should be false', () => {
expect(Utils.isValidVersion(invalidVersion)).toBeFalsy();
});
});

describe('Testing setPSModulePath', () => {
test('PSModulepath with azPSVersion non-empty', () => {
if(!process.env.PSModulePath) {
process.env.PSModulePath = process.env.PSModulePath + "modulePath";
}
Utils.setPSModulePath(version);
expect(process.env.PSModulepath).toContain(version);
});
test('PSModulePath with azPSVersion empty', () => {
const currPSModulePath = process.env.PSModulepath;
Utils.setPSModulePath();
expect(process.env.PSModulePath).not.toEqual(currPSModulePath);
});
});

describe('Testing getLatestModule', () => {
let getLatestModuleSpy;

beforeEach(() => {
getLatestModuleSpy = jest.spyOn(Utils, 'getLatestModule');
});
test('getLatestModule should pass', async () => {
getLatestModuleSpy.mockImplementationOnce((_moduleName: string) => Promise.resolve(version));
await Utils.getLatestModule(moduleName);
expect(getLatestModuleSpy).toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inputs:
creds:
description: 'Paste output of `az ad sp create-for-rbac` as value of secret variable: AZURE_CREDENTIALS'
required: true
enable-PSSession:
enable-AzPSSession:
description: 'Set this value to true to enable Azure PowerShell Login in addition to Az CLI login'
required: false
default: false
Expand Down
14 changes: 14 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html

module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
};
5 changes: 0 additions & 5 deletions lib/PowerShell/Utilities/PowerShellToolRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,5 @@ class PowerShellToolRunner {
yield exec.exec(`${PowerShellToolRunner.psPath} -Command`, [scriptBlock], options);
});
}
static executePowerShellCommand(command, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
yield exec.exec(`"${PowerShellToolRunner.psPath}" -Command "${command}"`, [], options);
});
}
}
exports.default = PowerShellToolRunner;
3 changes: 0 additions & 3 deletions lib/PowerShell/Utilities/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ const PowerShellToolRunner_1 = __importDefault(require("./PowerShellToolRunner")
class Utils {
/**
* Add the folder path where Az modules are present to PSModulePath based on runner
*
* @param azPSVersion
*
* If azPSVersion is empty, folder path in which all Az modules are present are set
* If azPSVersion is not empty, folder path of exact Az module version is set
*
*/
static setPSModulePath(azPSVersion = "") {
let modulePath = "";
Expand Down
4 changes: 2 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ function main() {
let servicePrincipalKey = secrets.getSecret("$.clientSecret", true);
let tenantId = secrets.getSecret("$.tenantId", false);
let subscriptionId = secrets.getSecret("$.subscriptionId", false);
const enablePSSession = core.getInput('enable-PSSession').toLowerCase() === "true";
const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true";
if (!servicePrincipalId || !servicePrincipalKey || !tenantId || !subscriptionId) {
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied.");
}
// Attempting Az cli login
yield executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`);
yield executeAzCliCommand(`account set --subscription "${subscriptionId}"`);
isAzCLISuccess = true;
if (enablePSSession) {
if (enableAzPSSession) {
// Attempting Az PS login
console.log(`Running Azure PS Login`);
const spnlogin = new ServicePrincipalLogin_1.ServicePrincipalLogin(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId);
Expand Down
Loading