Skip to content

Commit 70fdd93

Browse files
authored
Add StandbyManager E2E test for Java (#4158)
1 parent a18ec63 commit 70fdd93

File tree

3 files changed

+294
-215
lines changed

3 files changed

+294
-215
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Net;
8+
using System.Net.Http;
9+
using System.Reflection;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
using System.Web.Http;
13+
using Microsoft.AspNetCore.Hosting;
14+
using Microsoft.AspNetCore.TestHost;
15+
using Microsoft.Azure.WebJobs.Host.Executors;
16+
using Microsoft.Azure.WebJobs.Script.WebHost;
17+
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
18+
using Microsoft.Extensions.DependencyInjection;
19+
using Microsoft.Extensions.Logging;
20+
using Microsoft.WebJobs.Script.Tests;
21+
using Newtonsoft.Json.Linq;
22+
using Xunit;
23+
24+
namespace Microsoft.Azure.WebJobs.Script.Tests
25+
{
26+
public class StandbyManagerE2ETestBase : IDisposable
27+
{
28+
protected readonly string _testRootPath;
29+
protected string _expectedHostId;
30+
protected TestLoggerProvider _loggerProvider;
31+
protected string _expectedScriptPath;
32+
protected HttpClient _httpClient;
33+
protected TestServer _httpServer;
34+
protected readonly object _originalTimeZoneInfoCache = GetCachedTimeZoneInfo();
35+
36+
public StandbyManagerE2ETestBase()
37+
{
38+
_testRootPath = Path.Combine(Path.GetTempPath(), "StandbyManagerTests");
39+
CleanupTestDirectory();
40+
41+
StandbyManager.ResetChangeToken();
42+
}
43+
44+
protected async Task InitializeTestHostAsync(string testDirName, IEnvironment environment)
45+
{
46+
var httpConfig = new HttpConfiguration();
47+
var uniqueTestRootPath = Path.Combine(_testRootPath, testDirName, Guid.NewGuid().ToString());
48+
var scriptRootPath = Path.Combine(uniqueTestRootPath, "wwwroot");
49+
50+
FileUtility.EnsureDirectoryExists(scriptRootPath);
51+
string proxyConfigPath = Path.Combine(scriptRootPath, "proxies.json");
52+
File.WriteAllText(proxyConfigPath, "{}");
53+
await TestHelpers.Await(() => File.Exists(proxyConfigPath));
54+
55+
_loggerProvider = new TestLoggerProvider();
56+
57+
if (environment.IsAppServiceEnvironment())
58+
{
59+
// if the test is mocking App Service environment, we need
60+
// to also set the HOME and WEBSITE_SITE_NAME variables
61+
environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHomePath, uniqueTestRootPath);
62+
environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteName, "test-host-name");
63+
}
64+
65+
var webHostBuilder = Program.CreateWebHostBuilder()
66+
.ConfigureAppConfiguration(c =>
67+
{
68+
// This source reads from AzureWebJobsScriptRoot, which does not work
69+
// with the custom paths that these tests are using.
70+
var source = c.Sources.OfType<WebScriptHostConfigurationSource>().SingleOrDefault();
71+
if (source != null)
72+
{
73+
c.Sources.Remove(source);
74+
}
75+
c.AddTestSettings();
76+
})
77+
.ConfigureLogging(c =>
78+
{
79+
c.AddProvider(_loggerProvider);
80+
})
81+
.ConfigureServices(c =>
82+
{
83+
c.ConfigureAll<ScriptApplicationHostOptions>(o =>
84+
{
85+
o.IsSelfHost = true;
86+
o.LogPath = Path.Combine(uniqueTestRootPath, "logs");
87+
o.SecretsPath = Path.Combine(uniqueTestRootPath, "secrets");
88+
o.ScriptPath = _expectedScriptPath = scriptRootPath;
89+
});
90+
91+
c.AddSingleton<IEnvironment>(_ => environment);
92+
c.AddSingleton<IConfigureBuilder<ILoggingBuilder>>(new DelegatedConfigureBuilder<ILoggingBuilder>(b => b.AddProvider(_loggerProvider)));
93+
});
94+
95+
_httpServer = new TestServer(webHostBuilder);
96+
_httpClient = _httpServer.CreateClient();
97+
_httpClient.BaseAddress = new Uri("https://localhost/");
98+
99+
TestHelpers.WaitForWebHost(_httpClient);
100+
101+
var traces = _loggerProvider.GetAllLogMessages().ToArray();
102+
103+
Assert.NotNull(traces.Single(p => p.FormattedMessage.StartsWith("Host is in standby mode")));
104+
105+
_expectedHostId = await _httpServer.Host.Services.GetService<IHostIdProvider>().GetHostIdAsync(CancellationToken.None);
106+
}
107+
108+
109+
protected async Task VerifyWarmupSucceeds(bool restart = false)
110+
{
111+
string uri = "api/warmup";
112+
if (restart)
113+
{
114+
uri += "?restart=1";
115+
}
116+
117+
// issue warmup request and verify
118+
var request = new HttpRequestMessage(HttpMethod.Get, uri);
119+
var response = await _httpClient.SendAsync(request);
120+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
121+
string responseBody = await response.Content.ReadAsStringAsync();
122+
Assert.Equal("WarmUp complete.", responseBody);
123+
}
124+
125+
protected async Task<string[]> ListFunctions()
126+
{
127+
var secretManager = _httpServer.Host.Services.GetService<ISecretManagerProvider>().Current;
128+
var keys = await secretManager.GetHostSecretsAsync();
129+
string key = keys.MasterKey;
130+
131+
string uri = $"admin/functions?code={key}";
132+
var request = new HttpRequestMessage(HttpMethod.Get, uri);
133+
134+
var response = await _httpClient.SendAsync(request);
135+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
136+
string responseBody = await response.Content.ReadAsStringAsync();
137+
var functions = JArray.Parse(responseBody);
138+
return functions.Select(p => (string)p.SelectToken("name")).ToArray();
139+
}
140+
141+
private void CleanupTestDirectory()
142+
{
143+
try
144+
{
145+
FileUtility.DeleteDirectoryAsync(_testRootPath, true).GetAwaiter().GetResult();
146+
}
147+
catch
148+
{
149+
// best effort cleanup
150+
}
151+
}
152+
153+
protected static object GetCachedTimeZoneInfo()
154+
{
155+
var cachedDataField = typeof(TimeZoneInfo).GetField("s_cachedData", BindingFlags.NonPublic | BindingFlags.Static);
156+
return cachedDataField.GetValue(null);
157+
}
158+
159+
public virtual void Dispose()
160+
{
161+
_loggerProvider.Dispose();
162+
_httpServer.Dispose();
163+
_httpClient.Dispose();
164+
CleanupTestDirectory();
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)