-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
396 additions
and
360 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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; | ||
|
||
beforeAll(() => { | ||
var loginConfig = new LoginConfig(); | ||
loginConfig.servicePrincipalId = "servicePrincipalID"; | ||
loginConfig.servicePrincipalKey = "servicePrinicipalkey"; | ||
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['${AzPSConstants.Success}'] = "true" | ||
$output['${AzPSConstants.Result}'] = $PSVersionTable.PSVersion.ToString() | ||
} | ||
catch { | ||
$output['${AzPSConstants.Error}'] = $_.exception.Message | ||
} | ||
return ConvertTo-Json $output`; | ||
|
||
let psVersion: string = await AzPSLogin.runPSScript(script); | ||
expect(psVersion === null).toBeFalsy(); | ||
}); | ||
|
||
}); |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import AzPSSCriptBuilder from "../../src/PowerShell/AzPSScriptBuilder"; | ||
import { LoginConfig } from "../../src/common/LoginConfig"; | ||
|
||
describe("Getting AzLogin PS script", () => { | ||
|
||
function setEnv(name: string, value: string) { | ||
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = value; | ||
} | ||
|
||
function cleanEnv() { | ||
for (const envKey in process.env) { | ||
if (envKey.startsWith('INPUT_')) { | ||
delete process.env[envKey] | ||
} | ||
} | ||
} | ||
|
||
beforeEach(() => { | ||
cleanEnv(); | ||
}); | ||
|
||
test('PS script to get latest module path should work', () => { | ||
expect(AzPSSCriptBuilder.getLatestModulePathScript("TestModule")).toContain("(Get-Module -Name 'TestModule' -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1).ModuleBase"); | ||
}); | ||
|
||
test('PS script should not set context while passing allowNoSubscriptionsLogin as true', () => { | ||
setEnv('environment', 'azurecloud'); | ||
setEnv('enable-AzPSSession', 'true'); | ||
setEnv('allow-no-subscriptions', 'true'); | ||
setEnv('auth-type', 'SERVICE_PRINCIPAL'); | ||
let creds = { | ||
'clientId': 'client-id', | ||
'clientSecret': 'client-secret', | ||
'tenantId': 'tenanat-id', | ||
'subscriptionId': 'subscription-id' | ||
} | ||
setEnv('creds', JSON.stringify(creds)); | ||
|
||
let loginConfig = new LoginConfig(); | ||
loginConfig.initialize(); | ||
return AzPSSCriptBuilder.getAzPSLoginScript(loginConfig).then(([loginMethod, loginScript]) => { | ||
expect(loginScript.includes("Connect-AzAccount -ServicePrincipal -Environment 'azurecloud' -Tenant 'tenanat-id' -Credential")).toBeTruthy(); | ||
expect(loginScript.includes("Set-AzContext -SubscriptionId")).toBeFalsy; | ||
}); | ||
}); | ||
|
||
test('PS script should set context while passing allowNoSubscriptionsLogin as false', () => { | ||
setEnv('environment', 'azurecloud'); | ||
setEnv('enable-AzPSSession', 'true'); | ||
setEnv('allow-no-subscriptions', 'false'); | ||
setEnv('auth-type', 'SERVICE_PRINCIPAL'); | ||
let creds = { | ||
'clientId': 'client-id', | ||
'clientSecret': 'client-secret', | ||
'tenantId': 'tenanat-id', | ||
'subscriptionId': 'subscription-id' | ||
} | ||
setEnv('creds', JSON.stringify(creds)); | ||
|
||
let loginConfig = new LoginConfig(); | ||
loginConfig.initialize(); | ||
return AzPSSCriptBuilder.getAzPSLoginScript(loginConfig).then(([loginMethod, loginScript]) => { | ||
expect(loginScript.includes("Connect-AzAccount -ServicePrincipal -Environment 'azurecloud' -Tenant 'tenanat-id' -Credential")).toBeTruthy(); | ||
expect(loginScript.includes("Set-AzContext -SubscriptionId")).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
test('PS script should use system managed identity to login when auth-type is IDENTITY', () => { | ||
setEnv('environment', 'azurecloud'); | ||
setEnv('enable-AzPSSession', 'true'); | ||
setEnv('allow-no-subscriptions', 'false'); | ||
setEnv('subscription-id', 'subscription-id'); | ||
setEnv('auth-type', 'IDENTITY'); | ||
|
||
let loginConfig = new LoginConfig(); | ||
loginConfig.initialize(); | ||
return AzPSSCriptBuilder.getAzPSLoginScript(loginConfig).then(([loginMethod, loginScript]) => { | ||
expect(loginScript.includes("Connect-AzAccount -Identity -Environment 'azurecloud'")).toBeTruthy(); | ||
expect(loginScript.includes("Connect-AzAccount -Identity -Environment 'azurecloud' -AccountId 'client-id'")).toBeFalsy(); | ||
expect(loginScript.includes("Set-AzContext -SubscriptionId")).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
test('PS script should use user managed identity to login when auth-type is IDENTITY', () => { | ||
setEnv('environment', 'azurecloud'); | ||
setEnv('enable-AzPSSession', 'true'); | ||
setEnv('allow-no-subscriptions', 'true'); | ||
setEnv('auth-type', 'IDENTITY'); | ||
setEnv('client-id', 'client-id'); | ||
|
||
let loginConfig = new LoginConfig(); | ||
loginConfig.initialize(); | ||
return AzPSSCriptBuilder.getAzPSLoginScript(loginConfig).then(([loginMethod, loginScript]) => { | ||
expect(loginScript.includes("Connect-AzAccount -Identity -Environment 'azurecloud' -AccountId 'client-id'")).toBeTruthy(); | ||
expect(loginScript.includes("Set-AzContext -SubscriptionId")).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default class AzPSConstants { | ||
static readonly DEFAULT_AZ_PATH_ON_LINUX: string = '/usr/share'; | ||
static readonly DEFAULT_AZ_PATH_ON_WINDOWS: string = 'C:\\Modules'; | ||
static readonly AzAccounts: string = "Az.Accounts"; | ||
|
||
static readonly PowerShell_CmdName = "pwsh"; | ||
|
||
static readonly Success: string = "Success"; | ||
static readonly Error: string = "Error"; | ||
static readonly Result: string = "Result"; | ||
} |
Oops, something went wrong.