Skip to content

Commit cef5395

Browse files
author
Hovsep
committed
Merge pull request Azure#1002 from jianghaolu/script
[fixes #103630362] Add version check scripts to Profile module
2 parents 4909ad0 + 710ff46 commit cef5395

File tree

9 files changed

+160
-2
lines changed

9 files changed

+160
-2
lines changed

src/ResourceManager/Profile/AzureRM.Profile.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ RequiredModules = @()
5151
RequiredAssemblies = @()
5252

5353
# Script files (.ps1) that are run in the caller's environment prior to importing this module
54-
ScriptsToProcess = @()
54+
ScriptsToProcess = @('CheckVersions.ps1')
5555

5656
# Type files (.ps1xml) to be loaded when importing this module
5757
TypesToProcess = @()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
$AzureRMModules = @(
2+
"AzureRM.ApiManagement";
3+
"AzureRM.Automation";
4+
"AzureRM.Backup";
5+
"AzureRM.Batch";
6+
"AzureRM.Compute";
7+
"AzureRM.DataFactories";
8+
"AzureRM.Dns";
9+
"AzureRM.HDInsight";
10+
"AzureRM.Insights";
11+
"AzureRM.KeyVault";
12+
"AzureRM.Network";
13+
"AzureRM.OperationalInsights";
14+
"AzureRM.RedisCache";
15+
"AzureRM.Resources";
16+
"AzureRM.SiteRecovery";
17+
"AzureRM.Sql";
18+
"AzureRM.Storage";
19+
"AzureRM.StreamAnalytics";
20+
"AzureRM.Tags";
21+
"AzureRM.TrafficManager";
22+
"AzureRM.UsageAggregates";
23+
"AzureRM.Websites"
24+
)
25+
26+
$global:AvailableModules = @()
27+
28+
function CheckVersions {
29+
$profile = GetModuleInfo("AzureRM.Profile")
30+
if (-not $profile)
31+
{
32+
exit 0
33+
}
34+
ForEach ($moduleName in $AzureRMModules) {
35+
$module = GetModuleInfo($moduleName)
36+
if ($module)
37+
{
38+
$module.RequiredModules | Where-Object {$_.Name -eq "AzureRM.Profile"} | ForEach {
39+
if ($profile.Version.Major -ne $_.Version.Major) {
40+
Write-Warning("$moduleName $($module.Version) is not compatible with $profile $($profile.Version)!")
41+
}
42+
}
43+
}
44+
}
45+
}
46+
47+
function GetModuleInfo {
48+
param(
49+
[Parameter(Position=0)]
50+
[string]
51+
$ModuleName)
52+
53+
if ($global:AvailableModules.Length -eq 0)
54+
{
55+
$global:AvailableModules = Get-Module -ListAvailable
56+
}
57+
58+
return $global:AvailableModules `
59+
| Where-Object { $_.Name -eq $ModuleName} `
60+
| Select-Object -first 1
61+
}
62+
63+
CheckVersions

src/ResourceManager/Profile/Commands.Profile.Test/Commands.Profile.Test.csproj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
<Compile Include="EnvironmentCmdletTests.cs" />
182182
<Compile Include="MockSubscriptionClientFactory.cs" />
183183
<Compile Include="ProfileController.cs" />
184+
<Compile Include="ProfileModuleTests.cs" />
184185
<Compile Include="RPRegistrationDelegatingHandlerTests.cs" />
185186
<Compile Include="SubscriptionCmdletTests.cs" />
186187
<Compile Include="TenantCmdletTests.cs" />
@@ -196,6 +197,15 @@
196197
</Content>
197198
<None Include="MSSharedLibKey.snk" />
198199
<None Include="packages.config" />
200+
<None Include="FakeModuleRepo\AzureRM.ApiManagement.998.9.8.nupkg">
201+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
202+
</None>
203+
<None Include="FakeModuleRepo\AzureRM.Profile.999.9.8.nupkg">
204+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
205+
</None>
206+
<None Include="SessionRecords\Microsoft.Azure.Commands.Profile.Test.ProfileModuleTests\WarningOnIncompatibleVersions.json">
207+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
208+
</None>
199209
<None Include="SessionRecords\Microsoft.Azure.Commands.Profile.Test.SubscriptionCmdletTests\AllParameterSetsSucceed.json">
200210
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
201211
</None>
@@ -205,6 +215,9 @@
205215
<None Include="SessionRecords\Microsoft.Azure.Commands.Profile.Test.SubscriptionCmdletTests\SetAzureRmContextWorks.json">
206216
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
207217
</None>
218+
<Content Include="ProfileModuleTests.ps1">
219+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
220+
</Content>
208221
</ItemGroup>
209222
<ItemGroup>
210223
<ProjectReference Include="..\..\..\Common\Commands.Common\Commands.Common.csproj">
@@ -227,4 +240,4 @@
227240
<ItemGroup />
228241
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
229242
<Import Project="..\..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
230-
</Project>
243+
</Project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Resources.Test.ScenarioTests;
16+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
17+
using Xunit;
18+
19+
namespace Microsoft.Azure.Commands.Profile.Test
20+
{
21+
public class ProfileModuleTests
22+
{
23+
[Fact]
24+
[Trait(Category.AcceptanceType, Category.CheckIn)]
25+
public void WarningOnIncompatibleVersions()
26+
{
27+
ProfileController.NewInstance.RunPsTest("db1ab6f0-4769-4b27-930e-01e2ef9c123c", "Test-LoadProfileModule");
28+
}
29+
}
30+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
<#
16+
.SYNOPSIS
17+
Tests warning gets printed on incompatible modules with profile
18+
#>
19+
function Test-LoadProfileModule
20+
{
21+
# Push current profile module
22+
Get-PackageProvider -Name NuGet -ForceBootstrap
23+
$global:pushedProfileModule = $(Get-Module AzureRM.Profile).Path
24+
Remove-Module AzureRM.Profile
25+
try {
26+
Register-PSRepository -Name "ProfileModuleTest" -SourceLocation (Resolve-Path .\FakeModuleRepo).Path -InstallationPolicy Trusted
27+
try {
28+
Install-Module AzureRM.ApiManagement -Scope CurrentUser -Repository ProfileModuleTest -RequiredVersion 998.9.8
29+
$global:buffer = Import-Module $global:pushedProfileModule 2>&1 3>&1 | Out-String
30+
Write-Warning $global:buffer
31+
Assert-True { $global:buffer -Like "*AzureRM.ApiManagement 998.9.8 is not compatible with AzureRM.Profile*" }
32+
} catch [system.exception] {
33+
Write-Error $_ -ErrorAction Continue
34+
} finally {
35+
Uninstall-Module AzureRM.ApiManagement -ErrorAction Ignore
36+
Uninstall-Module AzureRM.Profile -ErrorAction Ignore
37+
}
38+
} catch [system.exception] {
39+
Write-Error $_ -ErrorAction Continue
40+
} finally {
41+
Unregister-PSRepository -Name "ProfileModuleTest"
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Entries": [],
3+
"Names": {},
4+
"Variables": {}
5+
}

src/ResourceManager/Profile/Commands.Profile/Commands.Profile.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@
169169
<Link>AzureRM.Profile.psd1</Link>
170170
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
171171
</None>
172+
<None Include="..\CheckVersions.ps1">
173+
<Link>CheckVersions.ps1</Link>
174+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
175+
</None>
172176
<None Include="AzureRmProfileStartup.ps1">
173177
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
174178
</None>

0 commit comments

Comments
 (0)