From 50014f98acb24dfe571062fe94e629b82bc2d7ab Mon Sep 17 00:00:00 2001 From: msJinLei Date: Fri, 9 Jul 2021 14:12:03 +0800 Subject: [PATCH 1/2] Fix access error when subscripiton has no Tags property - Fix the issue https://github.com/Azure/azure-powershell/issues/15425 - Add related test cases --- .../UnitTest/PSAzureSubscriptionTest.cs | 65 +++++++++++++++++++ .../Models/PSAzureSubscription.cs | 6 +- tools/Common.Netcore.Dependencies.targets | 34 +++++----- tools/Test/SmokeTest/RmCoreSmokeTests.ps1 | 1 + 4 files changed, 84 insertions(+), 22 deletions(-) diff --git a/src/Accounts/Accounts.Test/UnitTest/PSAzureSubscriptionTest.cs b/src/Accounts/Accounts.Test/UnitTest/PSAzureSubscriptionTest.cs index fd381ddae40f..08dafac41504 100644 --- a/src/Accounts/Accounts.Test/UnitTest/PSAzureSubscriptionTest.cs +++ b/src/Accounts/Accounts.Test/UnitTest/PSAzureSubscriptionTest.cs @@ -49,5 +49,70 @@ public void NewPSAzureSubscription() Assert.Equal(psAzureSubscription.SubscriptionPolicies.QuotaId, quotaId); Assert.Equal(psAzureSubscription.SubscriptionPolicies.SpendingLimit, spendingLimit.ToString()); } + + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void ConvertAzureSubscripitonToPSAzureSubscriptionWithTags() + { + string tenants = new Guid().ToString(), homeTenant = new Guid().ToString(); + var subscription = new AzureSubscription() + { + Id = new Guid().ToString(), + Name = "Playground-01", + State = "Enabled", + ExtendedProperties = + { + { "SubscriptionPolices", @"{""locationPlacementId"":""Internal_2014-09-01"",""quotaId"":""Internal_2014-09-01"",""spendingLimit"":""Off""}" }, + { "Environment", "AzureCloud" }, + { "AuthorizationSource", "RoleBased" }, + { "Account", "fakeuser@microsoft.com" }, + { "ManagedByTenants", tenants }, + { "Tenants", tenants }, + { "Tags", @"{""CigdemTestTag"":""Enabled""}" }, + { "HomeTenant", homeTenant } + } + }; + + var psSubscription = new PSAzureSubscription(subscription); + Assert.Equal(subscription.Id, psSubscription.Id); + Assert.Equal(subscription.Name, psSubscription.Name); + Assert.Equal(subscription.State, psSubscription.State); + Assert.Equal(homeTenant, psSubscription.TenantId); + Assert.Equal(homeTenant, psSubscription.HomeTenantId); + Assert.Equal("Internal_2014-09-01", psSubscription.SubscriptionPolicies.LocationPlacementId); + Assert.Single(psSubscription.Tags); + } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void ConvertAzureSubscripitonToPSAzureSubscriptionWithoutTags() + { + string tenants = new Guid().ToString(), homeTenant = new Guid().ToString(); + var subscription = new AzureSubscription() + { + Id = new Guid().ToString(), + Name = "Playground-01", + State = "Enabled", + ExtendedProperties = + { + { "SubscriptionPolices", @"{""locationPlacementId"":""Internal_2014-09-01"",""quotaId"":""Internal_2014-09-01"",""spendingLimit"":""Off""}" }, + { "AuthorizationSource", "RoleBased" }, + { "Account", "fakeuser@microsoft.com" }, + { "ManagedByTenants", tenants }, + { "Tenants", tenants }, + { "HomeTenant", homeTenant } + } + }; + + var psSubscription = new PSAzureSubscription(subscription); + Assert.Equal(subscription.Id, psSubscription.Id); + Assert.Equal(subscription.Name, psSubscription.Name); + Assert.Equal(subscription.State, psSubscription.State); + Assert.Equal(homeTenant, psSubscription.TenantId); + Assert.Equal(homeTenant, psSubscription.HomeTenantId); + Assert.Equal("Internal_2014-09-01", psSubscription.SubscriptionPolicies.LocationPlacementId); + Assert.Null(psSubscription.Tags); + } } } diff --git a/src/Accounts/Authentication.ResourceManager/Models/PSAzureSubscription.cs b/src/Accounts/Authentication.ResourceManager/Models/PSAzureSubscription.cs index e2a0cbac082b..d998c1470d18 100644 --- a/src/Accounts/Authentication.ResourceManager/Models/PSAzureSubscription.cs +++ b/src/Accounts/Authentication.ResourceManager/Models/PSAzureSubscription.cs @@ -223,11 +223,7 @@ public Dictionary Tags { get { - if (!string.IsNullOrEmpty(this.GetProperty(AzureSubscription.Property.Tags))) - { - return this.GetTags(); - } - return null; + return this.GetTags(); } } } diff --git a/tools/Common.Netcore.Dependencies.targets b/tools/Common.Netcore.Dependencies.targets index da68959213a7..adefe9955b22 100644 --- a/tools/Common.Netcore.Dependencies.targets +++ b/tools/Common.Netcore.Dependencies.targets @@ -3,22 +3,22 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + @@ -36,7 +36,7 @@ - $(NugetPackageRoot)\microsoft.azure.powershell.storage\1.3.40-preview\tools\ + $(NugetPackageRoot)\microsoft.azure.powershell.storage\1.3.41-preview\tools\ diff --git a/tools/Test/SmokeTest/RmCoreSmokeTests.ps1 b/tools/Test/SmokeTest/RmCoreSmokeTests.ps1 index 429dfe79c645..1838006633c5 100644 --- a/tools/Test/SmokeTest/RmCoreSmokeTests.ps1 +++ b/tools/Test/SmokeTest/RmCoreSmokeTests.ps1 @@ -83,6 +83,7 @@ $resourceTestCommands = @( @{Name = "Az.Storage [Management]"; Command = {New-AzStorageAccount -Name $storageAccountName -SkuName Standard_LRS -Location westus -ResourceGroupName $resourceGroupName -ErrorAction Stop}}, @{Name = "Az.Storage [Data]"; Command = {New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey 12345678 -ErrorAction Stop}}, @{Name = "Az.Accounts"; Command = {Get-AzDomain -ErrorAction Stop}}, + @{Name = "Az.Accounts"; Command = {Get-AzSubscription -DefaultProfile (Get-AzContext)}}, @{Name = "Az.Advisor"; Command = {Get-AzAdvisorConfiguration -ErrorAction Stop}}, @{Name = "Az.Aks"; Command = {Get-AzAksCluster -ErrorAction Stop}}, @{Name = "Az.AnalysisServices"; Command = {Get-AzAnalysisServicesServer -ErrorAction Stop}}, From 23b039c0eca48a7e24022125525ca8680d0825b7 Mon Sep 17 00:00:00 2001 From: Lei Jin Date: Wed, 14 Jul 2021 13:13:02 +0800 Subject: [PATCH 2/2] Polish test name for new Az.Accounts smoke test case --- tools/Test/SmokeTest/RmCoreSmokeTests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/Test/SmokeTest/RmCoreSmokeTests.ps1 b/tools/Test/SmokeTest/RmCoreSmokeTests.ps1 index 1838006633c5..c0aadfb63919 100644 --- a/tools/Test/SmokeTest/RmCoreSmokeTests.ps1 +++ b/tools/Test/SmokeTest/RmCoreSmokeTests.ps1 @@ -83,7 +83,7 @@ $resourceTestCommands = @( @{Name = "Az.Storage [Management]"; Command = {New-AzStorageAccount -Name $storageAccountName -SkuName Standard_LRS -Location westus -ResourceGroupName $resourceGroupName -ErrorAction Stop}}, @{Name = "Az.Storage [Data]"; Command = {New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey 12345678 -ErrorAction Stop}}, @{Name = "Az.Accounts"; Command = {Get-AzDomain -ErrorAction Stop}}, - @{Name = "Az.Accounts"; Command = {Get-AzSubscription -DefaultProfile (Get-AzContext)}}, + @{Name = "Az.Accounts [DefaultProfile]"; Command = {Get-AzSubscription -DefaultProfile (Get-AzContext)}}, @{Name = "Az.Advisor"; Command = {Get-AzAdvisorConfiguration -ErrorAction Stop}}, @{Name = "Az.Aks"; Command = {Get-AzAksCluster -ErrorAction Stop}}, @{Name = "Az.AnalysisServices"; Command = {Get-AzAnalysisServicesServer -ErrorAction Stop}},