-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
StartupHooks.cs
287 lines (243 loc) · 12 KB
/
StartupHooks.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Linq;
using Xunit;
using Microsoft.Extensions.DependencyModel;
namespace Microsoft.DotNet.CoreSetup.Test.HostActivation
{
public class StartupHooks : IClassFixture<StartupHooks.SharedTestState>
{
private SharedTestState sharedTestState;
private string startupHookVarName = "DOTNET_STARTUP_HOOKS";
private string startupHookRuntimeConfigName = "STARTUP_HOOKS";
private string startupHookSupport = "System.StartupHookProvider.IsSupported";
public StartupHooks(StartupHooks.SharedTestState fixture)
{
sharedTestState = fixture;
}
[Fact]
public void Muxer_activation_of_RuntimeConfig_StartupHook_Succeeds()
{
var fixture = sharedTestState.PortableAppFixture.Copy();
var dotnet = fixture.BuiltDotnet;
var appDll = fixture.TestProject.AppDll;
var startupHookFixture = sharedTestState.StartupHookFixture.Copy();
var startupHookDll = startupHookFixture.TestProject.AppDll;
RuntimeConfig.FromFile(fixture.TestProject.RuntimeConfigJson)
.WithProperty(startupHookRuntimeConfigName, startupHookDll)
.Save();
// RuntimeConfig defined startup hook
dotnet.Exec(appDll)
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdErrContaining($"Property STARTUP_HOOKS = {startupHookDll}")
.And.HaveStdOutContaining("Hello from startup hook!")
.And.HaveStdOutContaining("Hello World");
}
[Fact]
public void Muxer_activation_of_RuntimeConfig_And_Environment_StartupHooks_SucceedsInExpectedOrder()
{
var fixture = sharedTestState.PortableAppFixture.Copy();
var dotnet = fixture.BuiltDotnet;
var appDll = fixture.TestProject.AppDll;
var startupHookFixture = sharedTestState.StartupHookFixture.Copy();
var startupHookDll = startupHookFixture.TestProject.AppDll;
RuntimeConfig.FromFile(fixture.TestProject.RuntimeConfigJson)
.WithProperty(startupHookRuntimeConfigName, startupHookDll)
.Save();
var startupHook2Fixture = sharedTestState.StartupHookWithDependencyFixture.Copy();
var startupHook2Dll = startupHook2Fixture.TestProject.AppDll;
// include any char to counter output from other threads such as in #57243
const string wildcardPattern = @"[\r\n\s.]*";
// RuntimeConfig and Environment startup hooks in expected order
dotnet.Exec(appDll)
.EnvironmentVariable(startupHookVarName, startupHook2Dll)
.CaptureStdOut()
.CaptureStdErr()
.Execute()
.Should().Pass()
.And.HaveStdOutMatching("Hello from startup hook with dependency!" +
wildcardPattern +
"Hello from startup hook!" +
wildcardPattern +
"Hello World");
}
// Empty startup hook variable
[Fact]
public void Muxer_activation_of_Empty_StartupHook_Variable_Succeeds()
{
var fixture = sharedTestState.PortableAppFixture.Copy();
var dotnet = fixture.BuiltDotnet;
var appDll = fixture.TestProject.AppDll;
var startupHookVar = "";
dotnet.Exec(appDll)
.EnvironmentVariable(startupHookVarName, startupHookVar)
.CaptureStdOut()
.CaptureStdErr()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining("Hello World");
}
// Run the app with a startup hook assembly that depends on assemblies not on the TPA list
[Fact]
public void Muxer_activation_of_StartupHook_With_Missing_Dependencies_Fails()
{
var fixture = sharedTestState.PortableAppWithExceptionFixture.Copy();
var dotnet = fixture.BuiltDotnet;
var appDll = fixture.TestProject.AppDll;
var startupHookFixture = sharedTestState.StartupHookWithDependencyFixture.Copy();
var startupHookDll = startupHookFixture.TestProject.AppDll;
// Startup hook has a dependency not on the TPA list
dotnet.Exec(appDll)
.EnvironmentVariable(startupHookVarName, startupHookDll)
.CaptureStdOut()
.CaptureStdErr()
.Execute(expectedToFail: true)
.Should().Fail()
.And.HaveStdErrContaining("System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json");
}
private static void RemoveLibraryFromDepsJson(string depsJsonPath, string libraryName)
{
DependencyContext context;
using (FileStream fileStream = File.Open(depsJsonPath, FileMode.Open))
{
using (DependencyContextJsonReader reader = new DependencyContextJsonReader())
{
context = reader.Read(fileStream);
}
}
context = new DependencyContext(context.Target,
context.CompilationOptions,
context.CompileLibraries,
context.RuntimeLibraries.Select(lib => new RuntimeLibrary(
lib.Type,
lib.Name,
lib.Version,
lib.Hash,
lib.RuntimeAssemblyGroups.Select(assemblyGroup => new RuntimeAssetGroup(
assemblyGroup.Runtime,
assemblyGroup.RuntimeFiles.Where(f => !f.Path.EndsWith("SharedLibrary.dll")))).ToList().AsReadOnly(),
lib.NativeLibraryGroups,
lib.ResourceAssemblies,
lib.Dependencies,
lib.Serviceable,
lib.Path,
lib.HashPath,
lib.RuntimeStoreManifestName)),
context.RuntimeGraph);
using (FileStream fileStream = File.Open(depsJsonPath, FileMode.Truncate, FileAccess.Write))
{
DependencyContextWriter writer = new DependencyContextWriter();
writer.Write(context, fileStream);
}
}
// Run startup hook that adds an assembly resolver
[Fact]
public void Muxer_activation_of_StartupHook_With_Assembly_Resolver()
{
var fixture = sharedTestState.PortableAppWithMissingRefFixture.Copy();
var dotnet = fixture.BuiltDotnet;
var appDll = fixture.TestProject.AppDll;
var appDepsJson = Path.Combine(Path.GetDirectoryName(appDll), Path.GetFileNameWithoutExtension(appDll) + ".deps.json");
RemoveLibraryFromDepsJson(appDepsJson, "SharedLibrary.dll");
var startupHookFixture = sharedTestState.StartupHookWithAssemblyResolver.Copy();
var startupHookDll = startupHookFixture.TestProject.AppDll;
// No startup hook results in failure due to missing app dependency
dotnet.Exec(appDll)
.CaptureStdOut()
.CaptureStdErr()
.Execute(expectedToFail: true)
.Should().Fail()
.And.HaveStdErrContaining("FileNotFoundException: Could not load file or assembly 'SharedLibrary");
// Startup hook with assembly resolver results in use of injected dependency (which has value 2)
dotnet.Exec(appDll)
.EnvironmentVariable(startupHookVarName, startupHookDll)
.CaptureStdOut()
.CaptureStdErr()
.Execute(expectedToFail: true)
.Should().Fail()
.And.ExitWith(2);
}
[Fact]
public void Muxer_activation_of_StartupHook_With_IsSupported_False()
{
var fixture = sharedTestState.PortableAppFixture.Copy();
var dotnet = fixture.BuiltDotnet;
var appDll = fixture.TestProject.AppDll;
var startupHookFixture = sharedTestState.StartupHookFixture.Copy();
var startupHookDll = startupHookFixture.TestProject.AppDll;
RuntimeConfig.FromFile(fixture.TestProject.RuntimeConfigJson)
.WithProperty(startupHookSupport, "false")
.Save();
// Startup hooks are not executed when the StartupHookSupport
// feature switch is set to false.
dotnet.Exec(appDll)
.EnvironmentVariable(startupHookVarName, startupHookDll)
.CaptureStdOut()
.CaptureStdErr()
.Execute()
.Should().Pass()
.And.NotHaveStdOutContaining("Hello from startup hook!")
.And.HaveStdOutContaining("Hello World");
}
public class SharedTestState : IDisposable
{
// Entry point projects
public TestProjectFixture PortableAppFixture { get; }
public TestProjectFixture PortableAppWithExceptionFixture { get; }
// Entry point with missing reference assembly
public TestProjectFixture PortableAppWithMissingRefFixture { get; }
// Correct startup hooks
public TestProjectFixture StartupHookFixture { get; }
// Valid startup hooks with incorrect behavior
public TestProjectFixture StartupHookWithDependencyFixture { get; }
// Startup hook with an assembly resolver
public TestProjectFixture StartupHookWithAssemblyResolver { get; }
public RepoDirectoriesProvider RepoDirectories { get; }
public SharedTestState()
{
RepoDirectories = new RepoDirectoriesProvider();
// Entry point projects
PortableAppFixture = new TestProjectFixture("PortableApp", RepoDirectories)
.EnsureRestored()
.PublishProject();
PortableAppWithExceptionFixture = new TestProjectFixture("PortableAppWithException", RepoDirectories)
.EnsureRestored()
.PublishProject();
// Entry point with missing reference assembly
PortableAppWithMissingRefFixture = new TestProjectFixture("PortableAppWithMissingRef", RepoDirectories)
.EnsureRestored()
.PublishProject();
// Correct startup hooks
StartupHookFixture = new TestProjectFixture("StartupHook", RepoDirectories)
.EnsureRestored()
.PublishProject();
// Valid startup hooks with incorrect behavior
StartupHookWithDependencyFixture = new TestProjectFixture("StartupHookWithDependency", RepoDirectories)
.EnsureRestored()
.PublishProject();
// Startup hook with an assembly resolver
StartupHookWithAssemblyResolver = new TestProjectFixture("StartupHookWithAssemblyResolver", RepoDirectories)
.EnsureRestored()
.PublishProject();
}
public void Dispose()
{
// Entry point projects
PortableAppFixture.Dispose();
PortableAppWithExceptionFixture.Dispose();
// Entry point with missing reference assembly
PortableAppWithMissingRefFixture.Dispose();
// Correct startup hooks
StartupHookFixture.Dispose();
// Valid startup hooks with incorrect behavior
StartupHookWithDependencyFixture.Dispose();
// Startup hook with an assembly resolver
StartupHookWithAssemblyResolver.Dispose();
}
}
}
}