-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pre and post cleanup-cli * exec azpath * enable azure powershell part * set user agent * extract utils * divide cleanup * extract azpsconfig class * fix test * move runpsscript * change to AzPSUtils * fix typo
- Loading branch information
Showing
11 changed files
with
523 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,92 @@ | ||
import * as os from 'os'; | ||
|
||
import { AzPSLogin } from '../../src/PowerShell/AzPSLogin'; | ||
import { LoginConfig } from '../../src/common/LoginConfig'; | ||
import AzPSConstants from '../../src/PowerShell/AzPSConstants'; | ||
|
||
let azpsLogin: AzPSLogin; | ||
jest.setTimeout(30000); | ||
|
||
beforeAll(() => { | ||
var loginConfig = new LoginConfig(); | ||
loginConfig.servicePrincipalId = "servicePrincipalID"; | ||
loginConfig.servicePrincipalSecret = "servicePrincipalSecret"; | ||
loginConfig.tenantId = "tenantId"; | ||
loginConfig.subscriptionId = "subscriptionId"; | ||
azpsLogin = new AzPSLogin(loginConfig); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('Testing login', () => { | ||
let loginSpy; | ||
|
||
beforeEach(() => { | ||
loginSpy = jest.spyOn(azpsLogin, 'login'); | ||
}); | ||
|
||
test('ServicePrincipal login should pass', async () => { | ||
loginSpy.mockImplementationOnce(() => Promise.resolve()); | ||
await azpsLogin.login(); | ||
expect(loginSpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('Testing set module path', () => { | ||
test('setDefaultPSModulePath should work', () => { | ||
azpsLogin.setPSModulePathForGitHubRunner(); | ||
const runner: string = process.env.RUNNER_OS || os.type(); | ||
if(runner.toLowerCase() === "linux"){ | ||
expect(process.env.PSModulePath).toContain(AzPSConstants.DEFAULT_AZ_PATH_ON_LINUX); | ||
} | ||
if(runner.toLowerCase().startsWith("windows")){ | ||
expect(process.env.PSModulePath).toContain(AzPSConstants.DEFAULT_AZ_PATH_ON_WINDOWS); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
describe('Testing runPSScript', () => { | ||
test('Get PowerShell Version', async () => { | ||
let script = `try { | ||
$ErrorActionPreference = "Stop" | ||
$WarningPreference = "SilentlyContinue" | ||
$output = @{} | ||
$output['Success'] = $true | ||
$output['Result'] = $PSVersionTable.PSVersion.ToString() | ||
} | ||
catch { | ||
$output['Success'] = $false | ||
$output['Error'] = $_.exception.Message | ||
} | ||
return ConvertTo-Json $output`; | ||
|
||
let psVersion: string = await AzPSLogin.runPSScript(script); | ||
expect(psVersion === null).toBeFalsy(); | ||
}); | ||
|
||
test('Get PowerShell Version with Wrong Name', async () => { | ||
let script = `try { | ||
$ErrorActionPreference = "Stop" | ||
$WarningPreference = "SilentlyContinue" | ||
$output = @{} | ||
$output['Success'] = $true | ||
$output['Result'] = $PSVersionTableWrongName.PSVersion.ToString() | ||
} | ||
catch { | ||
$output['Success'] = $false | ||
$output['Error'] = $_.exception.Message | ||
} | ||
return ConvertTo-Json $output`; | ||
|
||
try{ | ||
await AzPSLogin.runPSScript(script); | ||
throw new Error("The last step should fail."); | ||
}catch(error){ | ||
expect(error.message.includes("Azure PowerShell login failed with error: You cannot call a method on a null-valued expression.")).toBeTruthy(); | ||
} | ||
}); | ||
|
||
import * as os from 'os'; | ||
|
||
import { AzPSLogin } from '../../src/PowerShell/AzPSLogin'; | ||
import { LoginConfig } from '../../src/common/LoginConfig'; | ||
import { AzPSConstants, AzPSUtils } from '../../src/PowerShell/AzPSUtils'; | ||
|
||
let azpsLogin: AzPSLogin; | ||
jest.setTimeout(30000); | ||
|
||
beforeAll(() => { | ||
var loginConfig = new LoginConfig(); | ||
loginConfig.servicePrincipalId = "servicePrincipalID"; | ||
loginConfig.servicePrincipalSecret = "servicePrincipalSecret"; | ||
loginConfig.tenantId = "tenantId"; | ||
loginConfig.subscriptionId = "subscriptionId"; | ||
azpsLogin = new AzPSLogin(loginConfig); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('Testing login', () => { | ||
let loginSpy; | ||
|
||
beforeEach(() => { | ||
loginSpy = jest.spyOn(azpsLogin, 'login'); | ||
}); | ||
|
||
test('ServicePrincipal login should pass', async () => { | ||
loginSpy.mockImplementationOnce(() => Promise.resolve()); | ||
await azpsLogin.login(); | ||
expect(loginSpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('Testing set module path', () => { | ||
test('setDefaultPSModulePath should work', () => { | ||
AzPSUtils.setPSModulePathForGitHubRunner(); | ||
const runner: string = process.env.RUNNER_OS || os.type(); | ||
if(runner.toLowerCase() === "linux"){ | ||
expect(process.env.PSModulePath).toContain(AzPSConstants.DEFAULT_AZ_PATH_ON_LINUX); | ||
} | ||
if(runner.toLowerCase().startsWith("windows")){ | ||
expect(process.env.PSModulePath).toContain(AzPSConstants.DEFAULT_AZ_PATH_ON_WINDOWS); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
describe('Testing runPSScript', () => { | ||
test('Get PowerShell Version', async () => { | ||
let script = `try { | ||
$ErrorActionPreference = "Stop" | ||
$WarningPreference = "SilentlyContinue" | ||
$output = @{} | ||
$output['Success'] = $true | ||
$output['Result'] = $PSVersionTable.PSVersion.ToString() | ||
} | ||
catch { | ||
$output['Success'] = $false | ||
$output['Error'] = $_.exception.Message | ||
} | ||
return ConvertTo-Json $output`; | ||
|
||
let psVersion: string = await AzPSUtils.runPSScript(script); | ||
expect(psVersion === null).toBeFalsy(); | ||
}); | ||
|
||
test('Get PowerShell Version with Wrong Name', async () => { | ||
let script = `try { | ||
$ErrorActionPreference = "Stop" | ||
$WarningPreference = "SilentlyContinue" | ||
$output = @{} | ||
$output['Success'] = $true | ||
$output['Result'] = $PSVersionTableWrongName.PSVersion.ToString() | ||
} | ||
catch { | ||
$output['Success'] = $false | ||
$output['Error'] = $_.exception.Message | ||
} | ||
return ConvertTo-Json $output`; | ||
|
||
try{ | ||
await AzPSUtils.runPSScript(script); | ||
throw new Error("The last step should fail."); | ||
}catch(error){ | ||
expect(error.message.includes("Azure PowerShell login failed with error: You cannot call a method on a null-valued expression.")).toBeTruthy(); | ||
} | ||
}); | ||
|
||
}); |
Oops, something went wrong.