-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathStartupHooks.cs
171 lines (147 loc) · 7.05 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
// 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 Xunit;
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 app = sharedTestState.App.Copy();
var startupHookDll = sharedTestState.StartupHook.AppDll;
RuntimeConfig.FromFile(app.RuntimeConfigJson)
.WithProperty(startupHookRuntimeConfigName, startupHookDll)
.Save();
// RuntimeConfig defined startup hook
TestContext.BuiltDotNet.Exec(app.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 app = sharedTestState.App.Copy();
var startupHookDll = sharedTestState.StartupHook.AppDll;
RuntimeConfig.FromFile(app.RuntimeConfigJson)
.WithProperty(startupHookRuntimeConfigName, startupHookDll)
.Save();
var startupHook2 = sharedTestState.StartupHookWithAssemblyResolver;
var startupHook2Dll = startupHook2.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
TestContext.BuiltDotNet.Exec(app.AppDll)
.EnvironmentVariable(startupHookVarName, startupHook2Dll)
.CaptureStdOut()
.CaptureStdErr()
.Execute()
.Should().Pass()
.And.HaveStdOutMatching($"Hello from startup hook in {startupHook2.AssemblyName}!" +
wildcardPattern +
$"Hello from startup hook!" +
wildcardPattern +
"Hello World");
}
// Empty startup hook variable
[Fact]
public void Muxer_activation_of_Empty_StartupHook_Variable_Succeeds()
{
var startupHookVar = "";
TestContext.BuiltDotNet.Exec(sharedTestState.App.AppDll)
.EnvironmentVariable(startupHookVarName, startupHookVar)
.CaptureStdOut()
.CaptureStdErr()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining("Hello World")
.And.NotHaveStdErr();
}
// 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 startupHookDll = sharedTestState.StartupHookWithAssemblyResolver.AppDll;
// Startup hook has a dependency not on the TPA list
TestContext.BuiltDotNet.Exec(sharedTestState.App.AppDll)
.EnvironmentVariable(startupHookVarName, startupHookDll)
// Indicate that the startup hook should try to use a dependency
.EnvironmentVariable("TEST_STARTUPHOOK_USE_DEPENDENCY", true.ToString())
.CaptureStdOut()
.CaptureStdErr()
.Execute(expectedToFail: true)
.Should().Fail()
.And.HaveStdErrContaining("System.IO.FileNotFoundException: Could not load file or assembly 'SharedLibrary");
}
// Run startup hook that adds an assembly resolver
[Fact]
public void Muxer_activation_of_StartupHook_With_Assembly_Resolver()
{
var startupHookDll = sharedTestState.StartupHookWithAssemblyResolver.AppDll;
// Startup hook with assembly resolver results in use of injected dependency
TestContext.BuiltDotNet.Exec(sharedTestState.App.AppDll, "load_shared_library")
.EnvironmentVariable(startupHookVarName, startupHookDll)
// Indicate that the startup hook should add an assembly resolver
.EnvironmentVariable("TEST_STARTUPHOOK_ADD_RESOLVER", true.ToString())
.CaptureStdOut()
.CaptureStdErr()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining("Resolving SharedLibrary in startup hook")
.And.HaveStdOutContaining("SharedLibrary.SharedType.Value = SharedLibrary");
}
[Fact]
public void Muxer_activation_of_StartupHook_With_IsSupported_False()
{
var app = sharedTestState.App.Copy();
var startupHookDll = sharedTestState.StartupHook.AppDll;
RuntimeConfig.FromFile(app.RuntimeConfigJson)
.WithProperty(startupHookSupport, "false")
.Save();
// Startup hooks are not executed when the StartupHookSupport
// feature switch is set to false.
TestContext.BuiltDotNet.Exec(app.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 application
public TestApp App { get; }
// Correct startup hook
public TestApp StartupHook { get; }
// Startup hook that can be configured to add an assembly resolver or use a dependency
public TestApp StartupHookWithAssemblyResolver { get; }
public SharedTestState()
{
App = TestApp.CreateFromBuiltAssets("HelloWorld");
StartupHook = TestApp.CreateFromBuiltAssets("StartupHook");
StartupHookWithAssemblyResolver = TestApp.CreateFromBuiltAssets("StartupHookWithAssemblyResolver");
}
public void Dispose()
{
App?.Dispose();
StartupHook?.Dispose();
StartupHookWithAssemblyResolver?.Dispose();
}
}
}
}