PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ResourceManager/Network/Commands.Network.Test/ScenarioTests/ApplicationGatewayTests.ps1

https://gitlab.com/jslee1/azure-powershell
Powershell | 207 lines | 119 code | 35 blank | 53 comment | 0 complexity | 7ba5e1c1dd0e348404da760ace8dbf44 MD5 | raw file
  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. .SYNOPSIS
  16. Application gateway tests
  17. #>
  18. function Test-ApplicationGatewayCRUD
  19. {
  20. # Setup
  21. $rglocation = Get-ProviderLocation ResourceManagement
  22. $resourceTypeParent = "Microsoft.Network/applicationgateways"
  23. $location = Get-ProviderLocation $resourceTypeParent
  24. $rgname = Get-ResourceGroupName
  25. $appgwName = Get-ResourceName
  26. $vnetName = Get-ResourceName
  27. $gwSubnetName = Get-ResourceName
  28. $nicSubnetName = Get-ResourceName
  29. $publicIpName = Get-ResourceName
  30. $gipconfigname = Get-ResourceName
  31. $fipconfig01Name = Get-ResourceName
  32. $fipconfig02Name = Get-ResourceName
  33. $poolName = Get-ResourceName
  34. $nicPoolName = Get-ResourceName
  35. $frontendPort01Name = Get-ResourceName
  36. $frontendPort02Name = Get-ResourceName
  37. $poolSetting01Name = Get-ResourceName
  38. $poolSetting02Name = Get-ResourceName
  39. $listener01Name = Get-ResourceName
  40. $listener02Name = Get-ResourceName
  41. $rule01Name = Get-ResourceName
  42. $rule02Name = Get-ResourceName
  43. $nic01Name = Get-ResourceName
  44. $nic02Name = Get-ResourceName
  45. try
  46. {
  47. # Create the resource group
  48. $resourceGroup = New-AzureRmResourceGroup -Name $rgname -Location $location -Tags @{Name = "testtag"; Value = "APPGw tag"}
  49.       
  50. # Create the Virtual Network
  51. $gwSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $gwSubnetName -AddressPrefix 10.0.0.0/24
  52. $nicSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $nicSubnetName -AddressPrefix 10.0.2.0/24
  53. $vnet = New-AzureRmvirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $gwSubnet, $nicSubnet
  54. $vnet = Get-AzureRmvirtualNetwork -Name $vnetName -ResourceGroupName $rgname
  55. $gwSubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $gwSubnetName -VirtualNetwork $vnet
  56.   $nicSubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $nicSubnetName -VirtualNetwork $vnet
  57. # Create public ip
  58. $publicip = New-AzureRmPublicIpAddress -ResourceGroupName $rgname -name $publicIpName -location $location -AllocationMethod Dynamic
  59. #$publicip = Get-AzureRmPublicIpAddress -ResourceGroupName $rgname -name $publicIpName
  60. # create 2 nics to add to backend
  61. $nic01 = New-AzureRmNetworkInterface -Name $nic01Name -ResourceGroupName $rgname -Location $location -Subnet $nicSubnet
  62. $nic02 = New-AzureRmNetworkInterface -Name $nic02Name -ResourceGroupName $rgname -Location $location -Subnet $nicSubnet
  63. # Create application gateway configuration
  64. $gipconfig = New-AzureRmApplicationGatewayIPConfiguration -Name $gipconfigname -Subnet $gwSubnet
  65. $fipconfig01 = New-AzureRmApplicationGatewayFrontendIPConfig -Name $fipconfig01Name -PublicIPAddress $publicip
  66. $fipconfig02 = New-AzureRmApplicationGatewayFrontendIPConfig -Name $fipconfig02Name -Subnet $gwSubnet
  67. $pool = New-AzureRmApplicationGatewayBackendAddressPool -Name $poolName -BackendIPAddresses 1.1.1.1, 2.2.2.2, 3.3.3.3
  68. # Add an empty backend address pool
  69. $nicPool = New-AzureRmApplicationGatewayBackendAddressPool -Name $nicPoolName
  70. $fp01 = New-AzureRmApplicationGatewayFrontendPort -Name $frontendPort01Name  -Port 80
  71. $fp02 = New-AzureRmApplicationGatewayFrontendPort -Name $frontendPort02Name  -Port 8080
  72. $poolSetting01 = New-AzureRmApplicationGatewayBackendHttpSettings -Name $poolSetting01Name -Port 80 -Protocol Http -CookieBasedAffinity Disabled
  73. $poolSetting02 = New-AzureRmApplicationGatewayBackendHttpSettings -Name $poolSetting02Name -Port 80 -Protocol Http -CookieBasedAffinity Enabled
  74. $listener01 = New-AzureRmApplicationGatewayHttpListener -Name $listener01Name -Protocol Http -FrontendIPConfiguration $fipconfig01 -FrontendPort $fp01
  75. $listener02 = New-AzureRmApplicationGatewayHttpListener -Name $listener02Name -Protocol Http -FrontendIPConfiguration $fipconfig02 -FrontendPort $fp02
  76. $rule01 = New-AzureRmApplicationGatewayRequestRoutingRule -Name $rule01Name -RuleType basic -BackendHttpSettings $poolSetting01 -HttpListener $listener01 -BackendAddressPool $pool
  77. $rule02 = New-AzureRmApplicationGatewayRequestRoutingRule -Name $rule02Name -RuleType basic -BackendHttpSettings $poolSetting02 -HttpListener $listener02 -BackendAddressPool $pool
  78. $sku = New-AzureRmApplicationGatewaySku -Name Standard_Small -Tier Standard -Capacity 2
  79. # Create Application Gateway
  80. $appgw = New-AzureRmApplicationGateway -Name $appgwName -ResourceGroupName $rgname -Location $location -BackendAddressPools $pool, $nicPool -BackendHttpSettingsCollection $poolSetting01, $poolSetting02 -FrontendIpConfigurations $fipconfig01, $fipconfig02  -GatewayIpConfigurations $gipconfig -FrontendPorts $fp01, $fp02 -HttpListeners $listener01, $listener02 -RequestRoutingRules $rule01, $rule02 -Sku $sku
  81. # Get Application Gateway
  82. $getgw =  Get-AzureRmApplicationGateway -Name $appgwName -ResourceGroupName $rgname
  83. # add nics to application gateway backend address pool
  84. $nicPool = Get-AzureRmApplicationGatewayBackendAddressPool -ApplicationGateway $getgw -Name $nicPoolName
  85. $nic01.IpConfigurations[0].ApplicationGatewayBackendAddressPools.Add($nicPool);
  86. $nic02.IpConfigurations[0].ApplicationGatewayBackendAddressPools.Add($nicPool);
  87. # set the nics
  88. $nic01 = $nic01 | Set-AzureRmNetworkInterface
  89. $nic02 = $nic02 | Set-AzureRmNetworkInterface
  90. # Add probe, request timeout, multi-hosting, URL routing to an exisitng gateway
  91. # Probe, request timeout, multi-site and URL routing are optional.
  92. $probeName = Get-ResourceName
  93. $frontendPort03Name = Get-ResourceName
  94. $poolSetting03Name = Get-ResourceName
  95. $listener03Name = Get-ResourceName
  96. $rule03Name = Get-ResourceName
  97. $PathRule01Name = Get-ResourceName
  98. $PathRule02Name = Get-ResourceName
  99. $urlPathMapName = Get-ResourceName
  100. # Adding new frontend port
  101. $getgw = Add-AzureRmApplicationGatewayFrontendPort -ApplicationGateway $getgw -Name $frontendPort03Name  -Port 8888
  102. $fp = Get-AzureRmApplicationGatewayFrontendPort -ApplicationGateway $getgw -Name $frontendPort03Name 
  103. # Add new probe
  104. $getgw = Add-AzureRmApplicationGatewayProbeConfig -ApplicationGateway $getgw -Name $probeName -Protocol Http -HostName "probe.com" -Path "/path/path.htm" -Interval 89 -Timeout 88 -UnhealthyThreshold 8
  105. $probe = Get-AzureRmApplicationGatewayProbeConfig -ApplicationGateway $getgw -Name $probeName
  106. # Add listener that has hostname. Hostname is used to have multi-site
  107. $fipconfig = Get-AzureRmApplicationGatewayFrontendIPConfig -ApplicationGateway $getgw -Name $fipconfig02Name
  108. $getgw = Add-AzureRmApplicationGatewayHttpListener -ApplicationGateway $getgw -Name $listener03Name -Protocol Http -FrontendIPConfiguration $fipconfig -FrontendPort $fp -HostName TestHostName
  109. $listener = Get-AzureRmApplicationGatewayHttpListener -ApplicationGateway $getgw -Name $listener03Name
  110. $pool = Get-AzureRmApplicationGatewayBackendAddressPool -ApplicationGateway $getgw -Name $poolName
  111. # Add new BackendHttpSettings that has probe and request timeout
  112. $getgw = Add-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $getgw -Name $poolSetting03Name -Port 80 -Protocol Http -CookieBasedAffinity Disabled -Probe $probe -RequestTimeout 66
  113. $poolSetting = Get-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $getgw -Name $poolSetting03Name
  114. # Add new URL routing
  115. $imagePathRule = New-AzureRmApplicationGatewayPathRuleConfig -Name $PathRule01Name -Paths "/image" -BackendAddressPool $pool -BackendHttpSettings $poolSetting
  116. $videoPathRule = New-AzureRmApplicationGatewayPathRuleConfig -Name $PathRule02Name -Paths "/video" -BackendAddressPool $pool -BackendHttpSettings $poolSetting
  117. $getgw = Add-AzureRmApplicationGatewayUrlPathMapConfig -ApplicationGateway $getgw -Name $urlPathMapName -PathRules $videoPathRule, $imagePathRule -DefaultBackendAddressPool $pool -DefaultBackendHttpSettings $poolSetting
  118. $urlPathMap = Get-AzureRmApplicationGatewayUrlPathMapConfig -ApplicationGateway $getgw -Name $urlPathMapName
  119. # Add new rule with URL routing
  120. $getgw = Add-AzureRmApplicationGatewayRequestRoutingRule -ApplicationGateway $getgw -Name $rule03Name -RuleType PathBasedRouting -HttpListener $listener -UrlPathMap $urlPathMap
  121. # Modify existing application gateway with new configuration
  122. Set-AzureRmApplicationGateway -ApplicationGateway $getgw
  123. # Remove probe, request timeout, multi-site and URL routing from exiting gateway
  124. # Probe, request timeout, multi-site, URL routing are optional
  125. $getgw = Get-AzureRmApplicationGateway -Name $appgwName -ResourceGroupName $rgname
  126. # Remove probe
  127. $getgw = Remove-AzureRmApplicationGatewayProbeConfig -ApplicationGateway $getgw -Name $probeName
  128. # Remove URL path map
  129. $getgw = Remove-AzureRmApplicationGatewayUrlPathMapConfig -ApplicationGateway $getgw -Name $urlPathMapName
  130. # Modify BackendHttpSettings to remove probe and request timeout
  131. $getgw = Set-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $getgw -Name $poolSetting03Name -Port 80 -Protocol Http -CookieBasedAffinity Disabled
  132. $poolSetting = Get-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $getgw -Name $poolSetting03Name
  133. # Modify listener to remove hostname. Hostname is used to have multi-site.
  134. $fp = Get-AzureRmApplicationGatewayFrontendPort -ApplicationGateway $getgw -Name $frontendPort03Name 
  135. $fipconfig = Get-AzureRmApplicationGatewayFrontendIPConfig -ApplicationGateway $getgw -Name $fipconfig02Name
  136. $getgw = Set-AzureRmApplicationGatewayHttpListener -ApplicationGateway $getgw -Name $listener03Name -Protocol Http -FrontendIPConfiguration $fipconfig -FrontendPort $fp
  137. $listener = Get-AzureRmApplicationGatewayHttpListener -ApplicationGateway $getgw -Name $listener03Name
  138. # Modify rule to remove URL rotuing
  139. $pool = Get-AzureRmApplicationGatewayBackendAddressPool -ApplicationGateway $getgw -Name $poolName
  140. $getgw = Set-AzureRmApplicationGatewayRequestRoutingRule -ApplicationGateway $getgw -Name $rule03Name -RuleType basic -HttpListener $listener -BackendHttpSettings $poolSetting -BackendAddressPool $pool
  141. # Modify existing application gateway with new configuration
  142. Set-AzureRmApplicationGateway -ApplicationGateway $getgw
  143. # Stop Application Gateway
  144. Stop-AzureRmApplicationGateway -ApplicationGateway $getgw
  145.  
  146. # Delete Application Gateway
  147. Remove-AzureRmApplicationGateway -Name $appgwName -ResourceGroupName $rgname -Force
  148. }
  149. finally
  150. {
  151. # Cleanup
  152. Clean-ResourceGroup $rgname
  153. }
  154. }
  155. <#
  156. .SYNOPSIS
  157. Compare application gateways
  158. #>
  159. function Compare-AzureRmApplicationGateway($actual, $expected)
  160. {
  161. Assert-AreEqual $expected.Name $actual.Name
  162. Assert-AreEqual $expected.Name $actual.Name
  163. Assert-AreEqual $expected.Sku.Name $actual.Sku.Name
  164. Assert-AreEqual $expected.Sku.Tier $actual.Sku.Tier
  165. Assert-AreEqual $expected.Sku.Capacity $actual.Sku.Capacity
  166. Assert-AreEqual $expected.FrontendPorts.Count $actual.FrontendPorts.Count
  167. Assert-AreEqual $expected.SslCertificates.Count $actual.SslCertificates.Count
  168. Assert-AreEqual $expected.BackendAddressPools.Count $actual.BackendAddressPools.Count
  169. Assert-AreEqual $expected.BackendHttpSettingsCollection.Count $actual.BackendHttpSettingsCollection.Count
  170. Assert-AreEqual $expected.HttpListeners.Count $actual.HttpListeners.Count
  171. Assert-AreEqual $expected.RequestRoutingRules.Count $actual.RequestRoutingRules.Count
  172. }