-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathAzureAppServiceDeployContainerBehaviourFixture.cs
320 lines (266 loc) · 20 KB
/
AzureAppServiceDeployContainerBehaviourFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Azure;
using Azure.ResourceManager.AppService;
using Azure.ResourceManager.AppService.Models;
using Azure.ResourceManager.Resources;
using Calamari.Azure;
using Calamari.Azure.AppServices;
using Calamari.AzureAppService.Azure;
using Calamari.AzureAppService.Behaviors;
using Calamari.Common.Commands;
using Calamari.Common.Plumbing.Variables;
using Calamari.Testing;
using Calamari.Testing.Helpers;
using FluentAssertions;
using NUnit.Framework;
using Octostache;
using Polly;
namespace Calamari.AzureAppService.Tests
{
/// <summary>
/// Tests that both windows and linux app services can have container deployments
/// </summary>
/// <remarks>
/// Both test fixtures have the same two tests, but they have different setups, so it's just easier to have separate test fixtures.
/// </remarks>
public class AzureAppServiceDeployContainerBehaviourFixture
{
[TestFixture]
public class WhenUsingAWindowsAppService : AppServiceIntegrationTest
{
CalamariVariables newVariables;
readonly HttpClient client = new HttpClient();
//We are having capacity issues in EastUS and WestUS2
protected override string DefaultResourceGroupLocation => RandomAzureRegion.GetRandomRegionWithExclusions("eastus", "westus2");
protected override async Task ConfigureTestResources(ResourceGroupResource resourceGroup)
{
var (_, webSite) = await CreateAppServicePlanAndWebApp(resourceGroup,
new AppServicePlanData(resourceGroup.Data.Location)
{
IsXenon = true,
IsHyperV = true,
Sku = new AppServiceSkuDescription
{
Name = "P1V3",
Tier = "PremiumV3"
}
},
webSiteData: new WebSiteData(resourceGroup.Data.Location)
{
SiteConfig = new SiteConfigProperties
{
WindowsFxVersion = "DOCKER|mcr.microsoft.com/dotnet/samples:aspnetapp",
IsAlwaysOn = true,
AppSettings = new List<AppServiceNameValuePair>
{
new AppServiceNameValuePair { Name = "DOCKER_REGISTRY_SERVER_URL", Value = "https://index.docker.io" },
new AppServiceNameValuePair { Name = "WEBSITES_ENABLE_APP_SERVICE_STORAGE", Value = "false" },
new AppServiceNameValuePair { Name = "WEBSITES_CONTAINER_START_TIME_LIMIT", Value = "460" }
}
}
});
WebSiteResource = webSite;
await AssertSetupSuccessAsync();
}
[Test]
public async Task AzureWindowsContainerDeploy()
{
newVariables = new CalamariVariables();
AddVariables(newVariables);
var runningContext = new RunningDeployment("", newVariables);
await new AzureAppServiceContainerDeployBehaviour(new InMemoryLog()).Execute(runningContext);
var targetSite = new AzureTargetSite(SubscriptionId,
ResourceGroupName,
WebSiteResource.Data.Name);
await AssertDeploySuccessAsync(targetSite);
}
[Test]
public async Task AzureWindowsContainerSlotDeploy()
{
var slotName = "stage";
newVariables = new CalamariVariables();
AddVariables(newVariables);
newVariables.Add("Octopus.Action.Azure.DeploymentSlot", slotName);
await WebSiteResource.GetWebSiteSlots()
.CreateOrUpdateAsync(WaitUntil.Completed,
slotName,
WebSiteResource.Data);
var runningContext = new RunningDeployment("", newVariables);
await new AzureAppServiceContainerDeployBehaviour(new InMemoryLog()).Execute(runningContext);
var targetSite = new AzureTargetSite(SubscriptionId,
ResourceGroupName,
WebSiteResource.Data.Name,
slotName);
await AssertDeploySuccessAsync(targetSite);
}
async Task AssertSetupSuccessAsync()
{
var timeout = Policy.TimeoutAsync(TimeSpan.FromMinutes(5));
var receivedContent = await timeout.ExecuteAsync(async () =>
{
string content;
do
{
var response = await RetryPolicies.TestsTransientHttpErrorsPolicy
.ExecuteAsync(async context =>
{
var r = await client.GetAsync($@"https://{WebSiteResource.Data.DefaultHostName}");
if (!r.IsSuccessStatusCode)
{
var messageContent = await r.Content.ReadAsStringAsync();
TestContext.WriteLine($"Unable to retrieve content from https://{WebSiteResource.Data.DefaultHostName}, failed with: {messageContent}");
}
r.EnsureSuccessStatusCode();
return r;
},
contextData: new Dictionary<string, object>());
content = await response.Content.ReadAsStringAsync();
} while (content is null || content.Contains("container is starting up"));
return content;
});
receivedContent.Should().Contain("<h1>Welcome to .NET</h1>");
}
async Task AssertDeploySuccessAsync(AzureTargetSite targetSite)
{
var imageName = newVariables.Get(SpecialVariables.Action.Package.PackageId);
var registryUrl = newVariables.Get(SpecialVariables.Action.Package.Registry);
var imageVersion = newVariables.Get(SpecialVariables.Action.Package.PackageVersion) ?? "latest";
var config = await WebSiteResource.GetWebSiteConfig().GetAsync();
Assert.AreEqual($@"DOCKER|{imageName}:{imageVersion}", config.Value.Data.WindowsFxVersion);
var appSettings = await ArmClient.GetAppSettingsListAsync(targetSite);
Assert.AreEqual("https://" + registryUrl, appSettings.FirstOrDefault(app => app.Name == "DOCKER_REGISTRY_SERVER_URL")?.Value);
}
void AddVariables(VariableDictionary vars)
{
AddAzureVariables(vars);
vars.Add(SpecialVariables.Action.Package.FeedId, "Feeds-42");
vars.Add(SpecialVariables.Action.Package.Registry, "index.docker.io");
vars.Add(SpecialVariables.Action.Package.PackageId, "e2eteam/sample-apiserver");
vars.Add(SpecialVariables.Action.Package.Image, "e2eteam/sample-apiserver:1.17");
vars.Add(SpecialVariables.Action.Package.PackageVersion, "1.17");
vars.Add(SpecialVariables.Action.Azure.DeploymentType, "Container");
}
}
[TestFixture]
public class WhenUsingALinuxAppService : AppServiceIntegrationTest
{
CalamariVariables newVariables;
readonly HttpClient client = new HttpClient();
// For some reason we are having issues creating these linux resources on Standard in EastUS
protected override string DefaultResourceGroupLocation => RandomAzureRegion.GetRandomRegionWithExclusions("eastus");
protected override async Task ConfigureTestResources(ResourceGroupResource resourceGroup)
{
var (_, webSite) = await CreateAppServicePlanAndWebApp(resourceGroup,
new AppServicePlanData(resourceGroup.Data.Location)
{
Kind = "linux",
IsReserved = true,
Sku = new AppServiceSkuDescription
{
Name = "P1V3",
Tier = "PremiumV3"
}
},
new WebSiteData(resourceGroup.Data.Location)
{
Kind = "app,linux,container",
SiteConfig = new SiteConfigProperties
{
LinuxFxVersion = "DOCKER|mcr.microsoft.com/dotnet/samples:aspnetapp",
IsAlwaysOn = true,
AppSettings = new List<AppServiceNameValuePair>
{
new AppServiceNameValuePair { Name = "DOCKER_REGISTRY_SERVER_URL", Value = "https://index.docker.io" },
new AppServiceNameValuePair { Name = "WEBSITES_ENABLE_APP_SERVICE_STORAGE", Value = "false" },
new AppServiceNameValuePair { Name = "WEBSITES_CONTAINER_START_TIME_LIMIT", Value = "460" }
}
}
});
WebSiteResource = webSite;
await AssertSetupSuccessAsync();
}
[Test]
public async Task AzureLinuxContainerDeploy()
{
newVariables = new CalamariVariables();
AddVariables(newVariables);
var runningContext = new RunningDeployment("", newVariables);
await new AzureAppServiceContainerDeployBehaviour(new InMemoryLog()).Execute(runningContext);
var targetSite = new AzureTargetSite(SubscriptionId,
ResourceGroupName,
WebSiteResource.Data.Name);
await AssertDeploySuccessAsync(targetSite);
}
[Test]
public async Task AzureLinuxContainerSlotDeploy()
{
var slotName = "stage";
newVariables = new CalamariVariables();
AddVariables(newVariables);
newVariables.Add("Octopus.Action.Azure.DeploymentSlot", slotName);
await WebSiteResource.GetWebSiteSlots()
.CreateOrUpdateAsync(WaitUntil.Completed,
slotName,
WebSiteResource.Data);
var runningContext = new RunningDeployment("", newVariables);
await new AzureAppServiceContainerDeployBehaviour(new InMemoryLog()).Execute(runningContext);
var targetSite = new AzureTargetSite(SubscriptionId,
ResourceGroupName,
WebSiteResource.Data.Name,
slotName);
await AssertDeploySuccessAsync(targetSite);
}
async Task AssertSetupSuccessAsync()
{
var timeout = Policy.TimeoutAsync(TimeSpan.FromMinutes(5));
var receivedContent = await timeout.ExecuteAsync(async () =>
{
string content;
do
{
var response = await RetryPolicies.TestsTransientHttpErrorsPolicy
.ExecuteAsync(async context =>
{
var r = await client.GetAsync($@"https://{WebSiteResource.Data.DefaultHostName}");
if (!r.IsSuccessStatusCode)
{
var messageContent = await r.Content.ReadAsStringAsync();
TestContext.WriteLine($"Unable to retrieve content from https://{WebSiteResource.Data.DefaultHostName}, failed with: {messageContent}");
}
r.EnsureSuccessStatusCode();
return r;
},
contextData: new Dictionary<string, object>());
content = await response.Content.ReadAsStringAsync();
} while (content is null || content.Contains("container is starting up"));
return content;
});
receivedContent.Should().Contain("<h1>Welcome to .NET</h1>");
}
async Task AssertDeploySuccessAsync(AzureTargetSite targetSite)
{
var imageName = newVariables.Get(SpecialVariables.Action.Package.PackageId);
var registryUrl = newVariables.Get(SpecialVariables.Action.Package.Registry);
var imageVersion = newVariables.Get(SpecialVariables.Action.Package.PackageVersion) ?? "latest";
var config = await WebSiteResource.GetWebSiteConfig().GetAsync();
Assert.AreEqual($@"DOCKER|{imageName}:{imageVersion}", config.Value.Data.LinuxFxVersion);
var appSettings = await ArmClient.GetAppSettingsListAsync(targetSite);
Assert.AreEqual("https://" + registryUrl, appSettings.FirstOrDefault(app => app.Name == "DOCKER_REGISTRY_SERVER_URL")?.Value);
}
void AddVariables(VariableDictionary vars)
{
AddAzureVariables(vars);
vars.Add(SpecialVariables.Action.Package.FeedId, "Feeds-42");
vars.Add(SpecialVariables.Action.Package.Registry, "index.docker.io");
vars.Add(SpecialVariables.Action.Package.PackageId, "nginx");
vars.Add(SpecialVariables.Action.Package.Image, "nginx:latest");
vars.Add(SpecialVariables.Action.Package.PackageVersion, "latest");
vars.Add(SpecialVariables.Action.Azure.DeploymentType, "Container");
}
}
}
}