Skip to content

Commit

Permalink
[AppService] Fix - bug that prevented changing Container settings in …
Browse files Browse the repository at this point in the history
…Set-AzWebApp and Set-AzWebAppSlot (#12107)

* Fixed bug that prevented changing Container settings in `Set-AzWebApp` and `Set-AzWebAppSlot`

* Adding tests and fixingTest-CreateNewWebAppHyperV false positive

Co-authored-by: Vini Soto <[email protected]>
  • Loading branch information
vinisoto and Vini Soto authored Jun 11, 2020
1 parent 54327cc commit d17d1e7
Show file tree
Hide file tree
Showing 11 changed files with 6,862 additions and 315 deletions.
7 changes: 7 additions & 0 deletions src/Websites/Websites.Test/ScenarioTests/WebAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public void TestCreateNewWebAppHyperV()
WebsitesController.NewInstance.RunPsTest(_logger, "Test-CreateNewWebAppHyperV");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSetWebAppHyperVCredentials()
{
WebsitesController.NewInstance.RunPsTest(_logger, "Test-SetWebAppHyperVCredentials");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestEnableContainerContinuousDeploymentAndGetUrl()
Expand Down
130 changes: 127 additions & 3 deletions src/Websites/Websites.Test/ScenarioTests/WebAppTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,12 @@ function Test-CreateNewWebAppHyperV
"DOCKER_REGISTRY_SERVER_USERNAME" = $containerRegistryUser;
"DOCKER_REGISTRY_SERVER_PASSWORD" = $pass;}

foreach($nvp in $webApp.SiteConfig.AppSettings)
foreach($nvp in $result.SiteConfig.AppSettings)
{
Assert-True { $appSettings.Keys -contains $nvp.Name }
Assert-True { $appSettings[$nvp.Name] -match $nvp.Value }
Assert-AreEqual $appSettings[$nvp.Name] $nvp.Value
}


}
finally
{
Expand All @@ -523,6 +522,131 @@ function Test-CreateNewWebAppHyperV
}
}

<#
.SYNOPSIS
Tests changing registry credentials for a Windows Container app
.DESCRIPTION
SmokeTest
#>
function Test-SetWebAppHyperVCredentials
{
# Setup
$rgname = Get-ResourceGroupName
$wname = Get-WebsiteName
$location = Get-WebLocation
$whpName = Get-WebHostPlanName
$tier = "PremiumContainer"
$apiversion = "2015-08-01"
$resourceType = "Microsoft.Web/sites"
$containerImageName = "pstestacr.azurecr.io/tests/iis:latest"
$containerRegistryUrl = "https://pstestacr.azurecr.io"
$containerRegistryUser = "pstestacr"
$pass = "cYK4qnENExflnnOkBN7P+gkmBG0sqgIv"
$containerRegistryPassword = ConvertTo-SecureString -String $pass -AsPlainText -Force
$dockerPrefix = "DOCKER|"


try
{
#Setup
New-AzResourceGroup -Name $rgname -Location $location
$serverFarm = New-AzAppServicePlan -ResourceGroupName $rgname -Name $whpName -Location $location -Tier $tier -WorkerSize Small -HyperV

# Create new web app
$job = New-AzWebApp -ResourceGroupName $rgname -Name $wname -Location $location -AppServicePlan $whpName -ContainerImageName $containerImageName -ContainerRegistryUrl $containerRegistryUrl -ContainerRegistryUser $containerRegistryUser -ContainerRegistryPassword $containerRegistryPassword -AsJob
$job | Wait-Job
$actual = $job | Receive-Job

# Assert
Assert-AreEqual $wname $actual.Name
Assert-AreEqual $serverFarm.Id $actual.ServerFarmId

# Get new web app
$result = Get-AzWebApp -ResourceGroupName $rgname -Name $wname

# Assert
Assert-AreEqual $wname $result.Name
Assert-AreEqual $serverFarm.Id $result.ServerFarmId
Assert-AreEqual $true $result.IsXenon
Assert-AreEqual ($dockerPrefix + $containerImageName) $result.SiteConfig.WindowsFxVersion

$appSettings = @{
"DOCKER_REGISTRY_SERVER_URL" = $containerRegistryUrl;
"DOCKER_REGISTRY_SERVER_USERNAME" = $containerRegistryUser;
"DOCKER_REGISTRY_SERVER_PASSWORD" = $pass;}

foreach($nvp in $result.SiteConfig.AppSettings)
{
Assert-True { $appSettings.Keys -contains $nvp.Name }
Assert-AreEqual $appSettings[$nvp.Name] $nvp.Value
}

$updatedContainerImageName = "microsoft/iis:latest"

# Change the webapp's container image to a public image and remove the credentials
$updateJob = Set-AzWebApp -ResourceGroupName $rgname -Name $wname -ContainerImageName $updatedContainerImageName -ContainerRegistryUrl '' -ContainerRegistryUser '' -ContainerRegistryPassword $null -AsJob
$updateJob | Wait-Job
$updated = $updateJob | Receive-Job

# Get updated web app
$updatedWebApp = Get-AzWebApp -ResourceGroupName $rgname -Name $wname

# Assert that container image has been updated
Assert-AreEqual ($dockerPrefix + $updatedContainerImageName) $updatedWebApp.SiteConfig.WindowsFxVersion

# Assert that registry credentials have been removed
foreach($nvp in $updatedWebApp.SiteConfig.AppSettings)
{
Assert-False { $appSettings.Keys -contains $nvp.Name}
}

# Create a slot
$slotName = "stagingslot"
$slotJob = New-AzWebAppSlot -ResourceGroupName $rgname -AppServicePlan $whpName -Name $wname -slot $slotName -ContainerImageName $containerImageName -ContainerRegistryUrl $containerRegistryUrl -ContainerRegistryUser $containerRegistryUser -ContainerRegistryPassword $containerRegistryPassword -AsJob
$slotJob | Wait-Job
$actualSlot = $slotJob | Receive-Job

# Assert
$appWithSlotName = "$wname/$slotName"
Assert-AreEqual $appWithSlotName $actualSlot.Name

# Get deployment slot
$slot = Get-AzWebAppSlot -ResourceGroupName $rgname -Name $wname -Slot $slotName

# Assert app settings in slot
foreach($nvp in $slot.SiteConfig.AppSettings)
{
Assert-True { $appSettings.Keys -contains $nvp.Name }
Assert-AreEqual $appSettings[$nvp.Name] $nvp.Value
}

# Change the slot's container image to a public image and remove the credentials
$updateSlotJob = Set-AzWebAppSlot -ResourceGroupName $rgname -Name $wname -Slot $slotName -ContainerImageName $updatedContainerImageName -ContainerRegistryUrl '' -ContainerRegistryUser '' -ContainerRegistryPassword $null -AsJob
$updateSlotJob | Wait-Job
$updatedSlot = $updateSlotJob | Receive-Job

# Get updated slot
$updatedWebAppSlot = Get-AzWebAppSlot -ResourceGroupName $rgname -Name $wname -Slot $slotName

# Assert that container image has been updated
Assert-AreEqual ($dockerPrefix + $updatedContainerImageName) $updatedWebAppSlot.SiteConfig.WindowsFxVersion

# Assert that registry credentials have been removed from the slot
foreach($nvp in $updatedWebAppSlot.SiteConfig.AppSettings)
{
Assert-False { $appSettings.Keys -contains $nvp.Name}
}

}
finally
{
# Cleanup
Remove-AzWebApp -ResourceGroupName $rgname -Name $wname -Force
Remove-AzAppServicePlan -ResourceGroupName $rgname -Name $whpName -Force
Remove-AzResourceGroup -Name $rgname -Force
}
}

<#
.SYNOPSIS
Tests enagbling continuous deployment for container and getting continuous deployment url.
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Websites/Websites/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
## Upcoming Release
* Added safeguard to delete created webapp if restore failed in `Restore-AzDeletedWebApp`
* Added "SourceWebApp.Location" for `New-AzWebApp` and `New-AzWebAppSlot`
* Fixed bug that prevented changing Container settings in `Set-AzWebApp` and `Set-AzWebAppSlot`

## Version 1.9.0
* Fixed typo on help of `Update-AzWebAppAccessRestrictionConfig`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ private Hashtable GetAppSettingsToUpdate()
Hashtable appSettings = new Hashtable();
if (ContainerRegistryUrl != null)
{
appSettings[CmdletHelpers.DocerRegistryServerUrl] = ContainerRegistryUrl;
appSettings[CmdletHelpers.DockerRegistryServerUrl] = ContainerRegistryUrl;
}
if (ContainerRegistryUser != null)
{
appSettings[CmdletHelpers.DocerRegistryServerUserName] = ContainerRegistryUser;
appSettings[CmdletHelpers.DockerRegistryServerUserName] = ContainerRegistryUser;
}
if (ContainerRegistryPassword != null)
{
appSettings[CmdletHelpers.DocerRegistryServerPassword] = ContainerRegistryPassword.ConvertToString();
appSettings[CmdletHelpers.DockerRegistryServerPassword] = ContainerRegistryPassword.ConvertToString();
}
if (EnableContainerContinuousDeployment.IsPresent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ public class SetAzureWebAppSlotCmdlet : WebAppSlotBaseCmdlet
public string ContainerImageName { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Private Container Registry Server Url", ParameterSetName = ParameterSet1Name)]
[ValidateNotNullOrEmpty]
[ValidateNotNull]
public string ContainerRegistryUrl { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Private Container Registry Username", ParameterSetName = ParameterSet1Name)]
[ValidateNotNullOrEmpty]
[ValidateNotNull]
public string ContainerRegistryUser { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Private Container Registry Password", ParameterSetName = ParameterSet1Name)]
[ValidateNotNullOrEmpty]
public SecureString ContainerRegistryPassword { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Enables/Disables container continuous deployment webhook", ParameterSetName = ParameterSet1Name)]
Expand Down Expand Up @@ -218,15 +217,28 @@ public override void ExecuteCmdlet()

if (ContainerRegistryUrl != null)
{
appSettings[CmdletHelpers.DocerRegistryServerUrl] = ContainerRegistryUrl;
appSettings.Remove(CmdletHelpers.DockerRegistryServerUrl);
if (ContainerRegistryUrl != string.Empty)
{
appSettings[CmdletHelpers.DockerRegistryServerUrl] = ContainerRegistryUrl;
}
}

if (ContainerRegistryUser != null)
{
appSettings[CmdletHelpers.DocerRegistryServerUserName] = ContainerRegistryUser;
appSettings.Remove(CmdletHelpers.DockerRegistryServerUserName);

if (ContainerRegistryUser != string.Empty)
{
appSettings[CmdletHelpers.DockerRegistryServerUserName] = ContainerRegistryUser;
}
}

appSettings.Remove(CmdletHelpers.DockerRegistryServerPassword);

if (ContainerRegistryPassword != null)
{
appSettings[CmdletHelpers.DocerRegistryServerPassword] = ContainerRegistryPassword.ConvertToString();
appSettings[CmdletHelpers.DockerRegistryServerPassword] = ContainerRegistryPassword.ConvertToString();
}

if (parameters.Contains("EnableContainerContinuousDeployment"))
Expand Down
6 changes: 3 additions & 3 deletions src/Websites/Websites/Cmdlets/WebApps/NewAzureWebApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,17 @@ private SiteConfig GetNewConfig(AppServicePlan appServiceplan)
}
if (_cmdlet.ContainerRegistryUrl != null)
{
siteConfig.AppSettings.Add(new NameValuePair(CmdletHelpers.DocerRegistryServerUrl, _cmdlet.ContainerRegistryUrl));
siteConfig.AppSettings.Add(new NameValuePair(CmdletHelpers.DockerRegistryServerUrl, _cmdlet.ContainerRegistryUrl));
newConfigAdded = true;
}
if (_cmdlet.ContainerRegistryUser != null)
{
siteConfig.AppSettings.Add(new NameValuePair(CmdletHelpers.DocerRegistryServerUserName, _cmdlet.ContainerRegistryUser));
siteConfig.AppSettings.Add(new NameValuePair(CmdletHelpers.DockerRegistryServerUserName, _cmdlet.ContainerRegistryUser));
newConfigAdded = true;
}
if (_cmdlet.ContainerRegistryPassword != null)
{
siteConfig.AppSettings.Add(new NameValuePair(CmdletHelpers.DocerRegistryServerPassword, _cmdlet.ContainerRegistryPassword.ConvertToString()));
siteConfig.AppSettings.Add(new NameValuePair(CmdletHelpers.DockerRegistryServerPassword, _cmdlet.ContainerRegistryPassword.ConvertToString()));
newConfigAdded = true;
}
if (_cmdlet.EnableContainerContinuousDeployment.IsPresent)
Expand Down
26 changes: 19 additions & 7 deletions src/Websites/Websites/Cmdlets/WebApps/SetAzureWebApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ public class SetAzureWebAppCmdlet : WebAppBaseCmdlet
public string ContainerImageName { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Private Container Registry Server Url", ParameterSetName = ParameterSet1Name)]
[ValidateNotNullOrEmpty]
[ValidateNotNull]
public string ContainerRegistryUrl { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Private Container Registry Username", ParameterSetName = ParameterSet1Name)]
[ValidateNotNullOrEmpty]
[ValidateNotNull]
public string ContainerRegistryUser { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Private Container Registry Password", ParameterSetName = ParameterSet1Name)]
[ValidateNotNullOrEmpty]
public SecureString ContainerRegistryPassword { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Enables/Disables container continuous deployment webhook", ParameterSetName = ParameterSet1Name)]
Expand Down Expand Up @@ -220,18 +219,31 @@ public override void ExecuteCmdlet()
}
}


if (ContainerRegistryUrl != null)
{
appSettings[CmdletHelpers.DocerRegistryServerUrl] = ContainerRegistryUrl;
appSettings.Remove(CmdletHelpers.DockerRegistryServerUrl);

if (ContainerRegistryUrl != string.Empty)
{
appSettings[CmdletHelpers.DockerRegistryServerUrl] = ContainerRegistryUrl;
}
}

if (ContainerRegistryUser != null)
{
appSettings[CmdletHelpers.DocerRegistryServerUserName] = ContainerRegistryUser;
appSettings.Remove(CmdletHelpers.DockerRegistryServerUserName);

if (ContainerRegistryUser != string.Empty)
{
appSettings[CmdletHelpers.DockerRegistryServerUserName] = ContainerRegistryUser;
}
}

appSettings.Remove(CmdletHelpers.DockerRegistryServerPassword);

if (ContainerRegistryPassword != null)
{
appSettings[CmdletHelpers.DocerRegistryServerPassword] = ContainerRegistryPassword.ConvertToString();
appSettings[CmdletHelpers.DockerRegistryServerPassword] = ContainerRegistryPassword.ConvertToString();
}

if (parameters.Contains("EnableContainerContinuousDeployment"))
Expand Down
6 changes: 3 additions & 3 deletions src/Websites/Websites/Utilities/CmdletHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public static NetworkManagementClient networkClient
private const string ApplicationServiceEnvironmentResourceIdFormat =
"/subscriptions/{0}/resourcegroups/{1}/providers/Microsoft.Web/{2}/{3}";

public const string DocerRegistryServerUrl = "DOCKER_REGISTRY_SERVER_URL";
public const string DocerRegistryServerUserName = "DOCKER_REGISTRY_SERVER_USERNAME";
public const string DocerRegistryServerPassword = "DOCKER_REGISTRY_SERVER_PASSWORD";
public const string DockerRegistryServerUrl = "DOCKER_REGISTRY_SERVER_URL";
public const string DockerRegistryServerUserName = "DOCKER_REGISTRY_SERVER_USERNAME";
public const string DockerRegistryServerPassword = "DOCKER_REGISTRY_SERVER_PASSWORD";
public const string DockerEnableCI = "DOCKER_ENABLE_CI";
public const string DockerImagePrefix = "DOCKER|";

Expand Down
26 changes: 14 additions & 12 deletions src/Websites/Websites/Utilities/WebsitesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,6 @@ public void UpdateWebAppConfiguration(string resourceGroupName, string location,

if (useSlot)
{
if (siteConfig != null)
{
WrappedWebsitesClient.WebApps().UpdateConfigurationSlot(
resourceGroupName,
webSiteName,
siteConfig.ConvertToSiteConfigResource(),
slotName);
}

if (appSettings != null)
{
Expand All @@ -557,6 +549,15 @@ public void UpdateWebAppConfiguration(string resourceGroupName, string location,
slotName);
}

if (siteConfig != null)
{
WrappedWebsitesClient.WebApps().UpdateConfigurationSlot(
resourceGroupName,
webSiteName,
siteConfig.ConvertToSiteConfigResource(),
slotName);
}

if (connectionStrings != null)
{
WrappedWebsitesClient.WebApps().UpdateConnectionStringsSlot(
Expand All @@ -577,10 +578,6 @@ public void UpdateWebAppConfiguration(string resourceGroupName, string location,
}
else
{
if (siteConfig != null)
{
WrappedWebsitesClient.WebApps().UpdateConfiguration(resourceGroupName, webSiteName, siteConfig.ConvertToSiteConfigResource());
}

if (appSettings != null)
{
Expand All @@ -590,6 +587,11 @@ public void UpdateWebAppConfiguration(string resourceGroupName, string location,
new StringDictionary { Properties = appSettings });
}

if (siteConfig != null)
{
WrappedWebsitesClient.WebApps().UpdateConfiguration(resourceGroupName, webSiteName, siteConfig.ConvertToSiteConfigResource());
}

if (connectionStrings != null)
{
WrappedWebsitesClient.WebApps().UpdateConnectionStrings(
Expand Down

0 comments on commit d17d1e7

Please sign in to comment.