/
InstanceManagerTests.cs
229 lines (196 loc) · 9.24 KB
/
InstanceManagerTests.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Script.WebHost;
using Microsoft.Azure.WebJobs.Script.WebHost.Management;
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
using Microsoft.Extensions.Logging;
using Microsoft.WebJobs.Script.Tests;
using Xunit;
namespace Microsoft.Azure.WebJobs.Script.Tests.Managment
{
[Trait(TestTraits.Category, TestTraits.EndToEnd)]
[Trait(TestTraits.Group, TestTraits.ContainerInstanceTests)]
public class InstanceManagerTests : IDisposable
{
private readonly TestLoggerProvider _loggerProvider;
private readonly TestEnvironmentEx _environment;
private readonly ScriptWebHostEnvironment _scriptWebEnvironment;
private readonly InstanceManager _instanceManager;
private readonly HttpClient _httpClient;
public InstanceManagerTests()
{
_httpClient = new HttpClient();
_loggerProvider = new TestLoggerProvider();
var loggerFactory = new LoggerFactory();
loggerFactory.AddProvider(_loggerProvider);
_environment = new TestEnvironmentEx();
_scriptWebEnvironment = new ScriptWebHostEnvironment(_environment);
var optionsFactory = new TestOptionsFactory<ScriptApplicationHostOptions>(new ScriptApplicationHostOptions());
_instanceManager = new InstanceManager(optionsFactory, _httpClient, _scriptWebEnvironment, _environment, loggerFactory.CreateLogger<InstanceManager>(), new TestMetricsLogger());
InstanceManager.Reset();
}
[Fact]
public async Task StartAssignment_AppliesAssignmentContext()
{
var envValue = new
{
Name = Path.GetTempFileName().Replace(".", string.Empty),
Value = Guid.NewGuid().ToString()
};
_environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1");
var context = new HostAssignmentContext
{
Environment = new Dictionary<string, string>
{
{ envValue.Name, envValue.Value }
}
};
bool result = _instanceManager.StartAssignment(context);
Assert.True(result);
Assert.True(_scriptWebEnvironment.InStandbyMode);
// specialization is done in the background
await Task.Delay(500);
var value = _environment.GetEnvironmentVariable(envValue.Name);
Assert.Equal(value, envValue.Value);
// verify logs
var logs = _loggerProvider.GetAllLogMessages().Select(p => p.FormattedMessage).ToArray();
Assert.Collection(logs,
p => Assert.StartsWith("Starting Assignment", p),
p => Assert.StartsWith("Applying 1 app setting(s)", p),
p => Assert.StartsWith("Triggering specialization", p));
// calling again should return false, since we're no longer
// in placeholder mode
_loggerProvider.ClearAllLogMessages();
result = _instanceManager.StartAssignment(context);
Assert.False(result);
logs = _loggerProvider.GetAllLogMessages().Select(p => p.FormattedMessage).ToArray();
Assert.Collection(logs,
p => Assert.StartsWith("Assign called while host is not in placeholder mode", p));
}
[Fact]
public async Task StartAssignment_Failure_ExitsPlaceholderMode()
{
_environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1");
var context = new HostAssignmentContext
{
Environment = new Dictionary<string, string>
{
// force the assignment to fail
{ "throw", "test" }
}
};
bool result = _instanceManager.StartAssignment(context);
Assert.True(result);
Assert.True(_scriptWebEnvironment.InStandbyMode);
await TestHelpers.Await(() => !_scriptWebEnvironment.InStandbyMode, timeout: 5000);
var error = _loggerProvider.GetAllLogMessages().Where(p => p.Level == LogLevel.Error).First();
Assert.Equal("Assign failed", error.FormattedMessage);
Assert.Equal("Kaboom!", error.Exception.Message);
}
[Fact]
public async Task StartAssignment_Succeeds_With_No_RunFromPackage_AppSetting()
{
_environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1");
var context = new HostAssignmentContext
{
Environment = new Dictionary<string, string>()
};
bool result = _instanceManager.StartAssignment(context);
Assert.True(result);
Assert.True(_scriptWebEnvironment.InStandbyMode);
await TestHelpers.Await(() => !_scriptWebEnvironment.InStandbyMode, timeout: 5000);
var logs = _loggerProvider.GetAllLogMessages().Select(p => p.FormattedMessage).ToArray();
Assert.Collection(logs,
p => Assert.StartsWith("Starting Assignment", p),
p => Assert.StartsWith("Applying 0 app setting(s)", p),
p => Assert.StartsWith("Triggering specialization", p));
}
[Fact]
public void StartAssignment_ReturnsFalse_WhenNotInStandbyMode()
{
Assert.False(SystemEnvironment.Instance.IsPlaceholderModeEnabled());
var context = new HostAssignmentContext();
bool result = _instanceManager.StartAssignment(context);
Assert.False(result);
}
[Fact]
public async Task ValidateContext_InvalidZipUrl_ReturnsError()
{
var environment = new Dictionary<string, string>()
{
{ EnvironmentSettingNames.AzureWebsiteZipDeployment, "http://invalid.com/invalid/dne" }
};
var assignmentContext = new HostAssignmentContext
{
SiteId = 1234,
SiteName = "TestSite",
Environment = environment
};
string error = await _instanceManager.ValidateContext(assignmentContext);
Assert.Equal("Invalid zip url specified (StatusCode: NotFound)", error);
var logs = _loggerProvider.GetAllLogMessages().Select(p => p.FormattedMessage).ToArray();
Assert.Collection(logs,
p => Assert.StartsWith("Validating host assignment context (SiteId: 1234, SiteName: 'TestSite')", p),
p => Assert.StartsWith("linux.container.specialization.zip.head failed", p),
p => Assert.StartsWith("linux.container.specialization.zip.head failed", p),
p => Assert.StartsWith("linux.container.specialization.zip.head failed", p),
p => Assert.StartsWith("ValidateContext failed", p));
}
[Fact]
public async Task ValidateContext_EmptyZipUrl_ReturnsSuccess()
{
var environment = new Dictionary<string, string>();
var assignmentContext = new HostAssignmentContext
{
SiteId = 1234,
SiteName = "TestSite",
Environment = environment
};
string error = await _instanceManager.ValidateContext(assignmentContext);
Assert.Null(error);
var logs = _loggerProvider.GetAllLogMessages().Select(p => p.FormattedMessage).ToArray();
Assert.Collection(logs,
p => Assert.StartsWith("Validating host assignment context (SiteId: 1234, SiteName: 'TestSite')", p));
}
[Fact]
public async Task ValidateContext_Succeeds()
{
var environment = new Dictionary<string, string>()
{
{ EnvironmentSettingNames.AzureWebsiteZipDeployment, "http://microsoft.com" }
};
var assignmentContext = new HostAssignmentContext
{
SiteId = 1234,
SiteName = "TestSite",
Environment = environment
};
string error = await _instanceManager.ValidateContext(assignmentContext);
Assert.Null(error);
var logs = _loggerProvider.GetAllLogMessages().Select(p => p.FormattedMessage).ToArray();
Assert.Collection(logs,
p => Assert.StartsWith("Validating host assignment context (SiteId: 1234, SiteName: 'TestSite')", p));
}
public void Dispose()
{
Environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, null);
}
private class TestEnvironmentEx : TestEnvironment
{
public override void SetEnvironmentVariable(string name, string value)
{
if (name == "throw")
{
throw new InvalidOperationException("Kaboom!");
}
base.SetEnvironmentVariable(name, value);
}
}
}
}