Skip to content

Commit bf944ed

Browse files
authored
Merge pull request #3590 from viananth/bootstrap4
Module AzureRm.Bootstrapper updates
2 parents d556c42 + 9fabc26 commit bf944ed

File tree

5 files changed

+238
-83
lines changed

5 files changed

+238
-83
lines changed

tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,6 @@ Describe "Use-AzureRmProfile" {
927927
Mock Install-Module { "Installing module..."}
928928
Mock Import-Module -Verifiable { "Importing Module..."}
929929
Mock Find-PotentialConflict {}
930-
if (($PSVersionTable.PSVersion.Major -ge 5) -and ($PSVersionTable.PSVersion.Minor -ge 1)){
931930
Context "Modules not installed" {
932931
Mock Get-AzureRmModule -Verifiable {} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"}
933932
It "Should install modules" {
@@ -971,7 +970,6 @@ Describe "Use-AzureRmProfile" {
971970

972971
}
973972
}
974-
}
975973
Context "Invoke with invalid profile" {
976974
It "Should throw" {
977975
{ Use-AzureRmProfile -Profile 'WrongProfileName'} | Should Throw
@@ -983,7 +981,6 @@ Describe "Use-AzureRmProfile" {
983981
{ Use-AzureRmProfile -Profile $null} | Should Throw
984982
}
985983
}
986-
if (($PSVersionTable.PSVersion.Major -ge 5) -and ($PSVersionTable.PSVersion.Minor -ge 1)){
987984

988985
Context "Invoke with Scope as CurrentUser" {
989986
Mock Get-AzureRmModule -Verifiable {} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"}
@@ -1002,14 +999,12 @@ Describe "Use-AzureRmProfile" {
1002999
Assert-VerifiableMocks
10031000
}
10041001
}
1005-
}
10061002

10071003
Context "Invoke with invalide module name" {
10081004
It "Should throw" {
10091005
{ Use-AzureRmProfile -Profile 'Profile1' -Module 'MockModule'} | Should Throw
10101006
}
10111007
}
1012-
if (($PSVersionTable.PSVersion.Major -ge 5) -and ($PSVersionTable.PSVersion.Minor -ge 1)){
10131008

10141009
Context "Potential Conflict found" {
10151010
Mock Find-PotentialConflict -Verifiable { $true }
@@ -1029,10 +1024,9 @@ Describe "Use-AzureRmProfile" {
10291024
Mock Get-Module -Verifiable { $moduleObj }
10301025
It "Should skip importing module" {
10311026
$result = Use-AzureRmProfile -Profile 'Profile1' -ErrorVariable useError -ErrorAction SilentlyContinue
1032-
$useError -like "A different version of module Module1 is already imported in this session. Start a new PowerShell session and retry the operation." | Should Be $true
1027+
$useError.exception.message.contains("A different version of module") | Should Be $true
10331028
}
10341029
}
1035-
}
10361030
}
10371031
}
10381032

@@ -1041,16 +1035,15 @@ Describe "Install-AzureRmProfile" {
10411035
Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) }
10421036
Mock Get-AzureRmModule -Verifiable {} -ParameterFilter { $Profile -eq 'Profile1' -and $Module -eq 'Module1'}
10431037
Mock Get-AzureRmModule -Verifiable { "1.0"} -ParameterFilter { $Profile -eq 'Profile1' -and $Module -eq 'Module2'}
1044-
if (($PSVersionTable.PSVersion.Major -ge 5) -and ($PSVersionTable.PSVersion.Minor -ge 1)){
1045-
1038+
Mock Find-PotentialConflict -Verifiable { $false }
1039+
10461040
Context "Invoke with valid profile name" {
10471041
Mock Install-Module -Verifiable { "Installing module Module1... Version 1.0"}
10481042
It "Should install Module1" {
10491043
(Install-AzureRmProfile -Profile 'Profile1') | Should be "Installing module Module1... Version 1.0"
10501044
Assert-VerifiableMocks
10511045
}
10521046
}
1053-
}
10541047

10551048
Context "Invoke with invalid profile name" {
10561049
It "Should throw" {
@@ -1063,7 +1056,6 @@ Describe "Install-AzureRmProfile" {
10631056
{ Install-AzureRmProfile -Profile $null } | Should Throw
10641057
}
10651058
}
1066-
if (($PSVersionTable.PSVersion.Major -ge 5) -and ($PSVersionTable.PSVersion.Minor -ge 1)){
10671059

10681060
Context "Invoke with Scope as CurrentUser" {
10691061
Mock Get-AzureRmModule -Verifiable {} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"}
@@ -1082,7 +1074,6 @@ Describe "Install-AzureRmProfile" {
10821074
Assert-VerifiableMocks
10831075
}
10841076
}
1085-
}
10861077
}
10871078
}
10881079

tools/AzureRM.BootStrapper/AzureRM.Bootstrapper.psm1

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,20 @@ function Find-PotentialConflict
601601
$availableModules = Get-Module $Module -ListAvailable
602602
$IsPotentialConflict = $false
603603

604+
Write-Information "Modules installed: $availableModules"
605+
606+
if ($null -eq $availableModules)
607+
{
608+
return $false
609+
}
610+
604611
# If Admin, check CurrentUser Module folder path and vice versa
605612
if ($script:IsAdmin)
606613
{
607-
$availableModules | ForEach-Object { if ($_.Path.Contains($env:HOMEPATH)) { $IsPotentialConflict = $true } }
614+
$availableModules | ForEach-Object { if (($null -ne $_.Path) -and $_.Path.Contains($env:HOMEPATH)) { $IsPotentialConflict = $true } }
608615
}
609616
else {
610-
$availableModules | ForEach-Object { if ($_.Path.Contains($env:ProgramFiles)) { $IsPotentialConflict = $true } }
617+
$availableModules | ForEach-Object { if (($null -ne $_.Path) -and $_.Path.Contains($env:ProgramFiles)) { $IsPotentialConflict = $true } }
611618
}
612619

613620
if ($IsPotentialConflict)
@@ -625,6 +632,32 @@ function Find-PotentialConflict
625632
return $false
626633
}
627634

635+
# Helper function to invoke install-module
636+
function Invoke-InstallModule
637+
{
638+
param($module, $version, $scope)
639+
$installCmd = Get-Command Install-Module
640+
if($installCmd.Parameters.ContainsKey('AllowClobber'))
641+
{
642+
if (-not $scope)
643+
{
644+
Install-Module $Module -RequiredVersion $version -AllowClobber -Repository $script:BootStrapRepo
645+
}
646+
else {
647+
Install-Module $Module -RequiredVersion $version -Scope $scope -AllowClobber -Repository $script:BootStrapRepo
648+
}
649+
}
650+
else {
651+
if (-not $scope)
652+
{
653+
Install-Module $Module -RequiredVersion $version -Force -Repository $script:BootStrapRepo
654+
}
655+
else {
656+
Install-Module $Module -RequiredVersion $version -Scope $scope -Force -Repository $script:BootStrapRepo
657+
}
658+
}
659+
}
660+
628661
# Add Scope parameter to the cmdlet
629662
function Add-ScopeParam
630663
{
@@ -894,14 +927,7 @@ function Use-AzureRmProfile
894927
$version = $versionEnum.Current
895928
Write-Progress -Activity "Installing Module $Module version: $version" -Status "Progress:" -PercentComplete ($ModuleCount/($Modules.Length)*100)
896929
Write-Verbose "Installing module $module"
897-
if (-not $Scope)
898-
{
899-
Install-Module $Module -RequiredVersion $version -AllowClobber
900-
}
901-
else
902-
{
903-
Install-Module $Module -RequiredVersion $version -scope $Scope -AllowClobber
904-
}
930+
Invoke-InstallModule -module $Module -version $version -scope $scope
905931
}
906932
}
907933

@@ -970,15 +996,8 @@ function Install-AzureRmProfile
970996
$toss = $versionEnum.MoveNext()
971997
$version = $versionEnum.Current
972998
Write-Progress -Activity "Installing Module $Module version: $version" -Status "Progress:" -PercentComplete ($ModuleCount/($Modules.Length)*100)
973-
Write-Verbose "Installing module $module"
974-
if (-not $Scope)
975-
{
976-
Install-Module $Module -RequiredVersion $version -AllowClobber
977-
}
978-
else
979-
{
980-
Install-Module $Module -RequiredVersion $version -scope $Scope -AllowClobber
981-
}
999+
Write-Verbose "Installing module $module"
1000+
Invoke-InstallModule -module $Module -version $version -scope $scope
9821001
}
9831002
}
9841003
}
@@ -1009,7 +1028,7 @@ function Uninstall-AzureRmProfile
10091028
{
10101029
if (($Force.IsPresent -or $PSCmdlet.ShouldContinue("Uninstall Profile $Profile", "Removing Modules for profile $Profile")))
10111030
{
1012-
Write-Verbose "Trying to uninstall module $module"
1031+
Write-Verbose "Trying to uninstall profile $profile"
10131032
Uninstall-ProfileHelper -PMap $ProfileMap @PSBoundParameters
10141033
}
10151034
}

tools/AzureRM.BootStrapper/ProfileMap.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"AzureRM.UsageAggregates": ["2.6.0"],
4646
"AzureRM.Websites": ["2.6.0"]
4747
},
48-
"2017-01-consistent": {
48+
"2017-03-09-profile": {
4949
"AzureRM": ["1.2.8"],
5050
"AzureRM.Profile": ["1.0.4.2"],
5151
"Azure.Storage": ["1.0.4.2"],

0 commit comments

Comments
 (0)