This repository was archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathTestContext.cs
More file actions
171 lines (131 loc) · 8.2 KB
/
TestContext.cs
File metadata and controls
171 lines (131 loc) · 8.2 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
using Microsoft.Extensions.Options;
using Microsoft.FeatureManagement;
using Microsoft.OneFuzz.Service;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using Tests;
using Async = System.Threading.Tasks;
namespace IntegrationTests.Fakes;
// TestContext provides a minimal IOnefuzzContext implementation to allow running
// of functions as unit or integration tests.
public sealed class TestContext : IOnefuzzContext {
public TestContext(IHttpClientFactory httpClientFactory, OneFuzzLoggerProvider provider, IStorage storage, ICreds creds, string storagePrefix) {
Cache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
ServiceConfiguration = new TestServiceConfiguration(storagePrefix);
Storage = storage;
Creds = creds;
SecretsOperations = new TestSecretOperations();
EntityConverter = new EntityConverter(SecretsOperations);
// this one is faked entirely; we can’t perform these operations at test time
VmssOperations = new TestVmssOperations();
Containers = new Containers(provider.CreateLogger<Containers>(), Storage, ServiceConfiguration, this, Cache);
Queue = new Queue(Storage, provider.CreateLogger<Queue>());
RequestHandling = new RequestHandling(provider.CreateLogger<RequestHandling>());
TaskOperations = new TaskOperations(provider.CreateLogger<TaskOperations>(), Cache, this);
NodeOperations = new NodeOperations(provider.CreateLogger<NodeOperations>(), this);
JobOperations = new JobOperations(provider.CreateLogger<JobOperations>(), this);
JobResultOperations = new JobResultOperations(provider.CreateLogger<JobResultOperations>(), this);
NodeTasksOperations = new NodeTasksOperations(provider.CreateLogger<NodeTasksOperations>(), this);
TaskEventOperations = new TaskEventOperations(provider.CreateLogger<TaskEventOperations>(), this);
NodeMessageOperations = new NodeMessageOperations(provider.CreateLogger<NodeMessageOperations>(), this);
ConfigOperations = new ConfigOperations(provider.CreateLogger<ConfigOperations>(), this, Cache);
PoolOperations = new PoolOperations(provider.CreateLogger<PoolOperations>(), this);
ScalesetOperations = new ScalesetOperations(provider.CreateLogger<ScalesetOperations>(), Cache, this);
ReproOperations = new ReproOperations(provider.CreateLogger<ReproOperations>(), this);
Reports = new Reports(provider.CreateLogger<Reports>(), Containers);
NotificationOperations = new NotificationOperations(provider.CreateLogger<NotificationOperations>(), this);
JobCrashReportedOperations = new JobCrashReportedOperations(provider.CreateLogger<JobCrashReportedOperations>(), this);
FeatureManagerSnapshot = new TestFeatureManagerSnapshot();
WebhookOperations = new TestWebhookOperations(httpClientFactory, provider.CreateLogger<WebhookOperations>(), this);
Events = new TestEvents(provider.CreateLogger<Events>(), this);
Metrics = new TestMetrics(provider.CreateLogger<Metrics>(), this);
WebhookMessageLogOperations = new TestWebhookMessageLogOperations(provider.CreateLogger<WebhookMessageLogOperations>(), this);
}
// convenience method for test setup
public Async.Task InsertAll(params EntityBase[] objs)
=> Async.Task.WhenAll(
objs.Select(x => x switch {
Task t => TaskOperations.Insert(t),
Node n => NodeOperations.Insert(n),
Pool p => PoolOperations.Insert(p),
Job j => JobOperations.Insert(j),
JobResult jr => JobResultOperations.Insert(jr),
Repro r => ReproOperations.Insert(r),
Scaleset ss => ScalesetOperations.Insert(ss),
NodeTasks nt => NodeTasksOperations.Insert(nt),
InstanceConfig ic => ConfigOperations.Insert(ic),
Notification n => NotificationOperations.Insert(n),
Webhook w => WebhookOperations.Insert(w),
JobCrashReported crashReported => JobCrashReportedOperations.Insert(crashReported),
_ => throw new NotSupportedException($"You will need to add an TestContext.InsertAll case for {x.GetType()} entities"),
}));
public Async.Task InsertAll(IEnumerable<EntityBase> objs)
=> Async.Task.WhenAll(
objs.Select(x => x switch {
Task t => TaskOperations.Insert(t),
Node n => NodeOperations.Insert(n),
Pool p => PoolOperations.Insert(p),
Job j => JobOperations.Insert(j),
JobResult jr => JobResultOperations.Insert(jr),
Repro r => ReproOperations.Insert(r),
Scaleset ss => ScalesetOperations.Insert(ss),
NodeTasks nt => NodeTasksOperations.Insert(nt),
InstanceConfig ic => ConfigOperations.Insert(ic),
Notification n => NotificationOperations.Insert(n),
Webhook w => WebhookOperations.Insert(w),
JobCrashReported crashReported => JobCrashReportedOperations.Insert(crashReported),
_ => throw new NotSupportedException($"You will need to add an TestContext.InsertAll case for {x.GetType()} entities"),
}));
// Implementations:
public IMemoryCache Cache { get; }
public IEvents Events { get; }
public IMetrics Metrics { get; }
public IServiceConfig ServiceConfiguration { get; }
public IStorage Storage { get; }
public ICreds Creds { get; }
public IContainers Containers { get; set; }
public IQueue Queue { get; }
public IRequestHandling RequestHandling { get; }
public ITaskOperations TaskOperations { get; }
public IJobOperations JobOperations { get; }
public IJobResultOperations JobResultOperations { get; }
public INodeOperations NodeOperations { get; }
public INodeTasksOperations NodeTasksOperations { get; }
public ITaskEventOperations TaskEventOperations { get; }
public INodeMessageOperations NodeMessageOperations { get; }
public IConfigOperations ConfigOperations { get; }
public IPoolOperations PoolOperations { get; }
public IScalesetOperations ScalesetOperations { get; }
public IVmssOperations VmssOperations { get; }
public IReproOperations ReproOperations { get; }
public IReports Reports { get; }
public EntityConverter EntityConverter { get; }
public INotificationOperations NotificationOperations { get; }
public ISecretsOperations SecretsOperations { get; }
public IFeatureManagerSnapshot FeatureManagerSnapshot { get; }
public IWebhookOperations WebhookOperations { get; }
public IWebhookMessageLogOperations WebhookMessageLogOperations { get; }
public IJobCrashReportedOperations JobCrashReportedOperations { get; }
// -- Remainder not implemented --
public IConfig Config => throw new System.NotImplementedException();
public IAutoScaleOperations AutoScaleOperations => throw new NotImplementedException();
public IDiskOperations DiskOperations => throw new System.NotImplementedException();
public IExtensions Extensions => throw new System.NotImplementedException();
public IIpOperations IpOperations => throw new System.NotImplementedException();
public ILogAnalytics LogAnalytics => throw new System.NotImplementedException();
public IProxyForwardOperations ProxyForwardOperations => throw new System.NotImplementedException();
public IProxyOperations ProxyOperations => throw new System.NotImplementedException();
public IScheduler Scheduler => throw new System.NotImplementedException();
public IVmOperations VmOperations => throw new System.NotImplementedException();
public INsgOperations NsgOperations => throw new NotImplementedException();
public ISubnet Subnet => throw new NotImplementedException();
public ITeams Teams => throw new NotImplementedException();
public IGithubIssues GithubIssues => throw new NotImplementedException();
public IAdo Ado => throw new NotImplementedException();
public IConfigurationRefresher ConfigurationRefresher => throw new NotImplementedException();
}