-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathDarcProcessManager.cs
200 lines (176 loc) · 6.91 KB
/
DarcProcessManager.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Microsoft.DotNet.DarcLib.Helpers;
using Microsoft.Extensions.Logging;
namespace ProductConstructionService.ReproTool;
internal class DarcProcessManager(
IProcessManager processManager,
ILogger<DarcProcessManager> logger)
{
private string? _darcExePath = null;
private const string DarcExeName = "darc";
public async Task InitializeAsync()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var cmd = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd";
_darcExePath = (await processManager.Execute(
cmd,
[
"/c",
"where",
DarcExeName
])).StandardOutput.Trim();
}
else
{
_darcExePath = (await processManager.Execute(
"/bin/sh",
[
"-c",
"which",
DarcExeName
])).StandardOutput.Trim();
}
}
internal async Task<ProcessExecutionResult> ExecuteAsync(IEnumerable<string> args)
{
if (string.IsNullOrEmpty(_darcExePath))
{
throw new InvalidOperationException($"Call {nameof(InitializeAsync)} before trying to execute a darc command");
}
return await processManager.Execute(
_darcExePath,
[
.. args,
"--bar-uri", Options.Options.PcsLocalUri
]);
}
public async Task<ProcessExecutionResult> DeleteSubscriptionsForChannelAsync(string channelName)
{
return await ExecuteAsync(["delete-subscriptions", "--channel", channelName, "--quiet"]);
}
public async Task<ProcessExecutionResult> DeleteChannelAsync(string channelName)
{
return await ExecuteAsync(["delete-channel", "--name", channelName]);
}
public async Task<IAsyncDisposable> CreateTestChannelAsync(string testChannelName, bool skipCleanup)
{
logger.LogInformation("Creating test channel {channelName}", testChannelName);
try
{
await DeleteChannelAsync(testChannelName);
}
catch (Exception)
{
// If there are subscriptions associated to the channel then a previous test clean up failed
// Run a subscription clean up and try again
try
{
await DeleteSubscriptionsForChannelAsync(testChannelName);
await DeleteChannelAsync(testChannelName);
}
catch (Exception)
{
// Otherwise ignore failures from delete-channel, its just a pre-cleanup that isn't really part of the test
// And if the test previously succeeded then it'll fail because the channel doesn't exist
}
}
var channel = await ExecuteAsync(["add-channel", "--name", testChannelName, "--classification", "test"]);
return AsyncDisposable.Create(async () =>
{
if (skipCleanup)
{
return;
}
logger.LogInformation("Cleaning up Test Channel {testChannelName}", testChannelName);
try
{
await DeleteChannelAsync(testChannelName);
}
catch (Exception)
{
// Ignore failures from delete-channel on cleanup, this delete is here to ensure that the channel is deleted
// even if the test does not do an explicit delete as part of the test. Other failures are typical that the channel has already been deleted.
}
});
}
public async Task<IAsyncDisposable> AddBuildToChannelAsync(int buildId, string channelName, bool skipCleanup)
{
logger.LogInformation("Adding build {build} to channel {channel}", buildId, channelName);
await ExecuteAsync(["add-build-to-channel", "--id", buildId.ToString(), "--channel", channelName, "--skip-assets-publishing"]);
return AsyncDisposable.Create(async () =>
{
if (skipCleanup)
{
return;
}
logger.LogInformation("Removing build {buildId} from channel {channelName}", buildId, channelName);
await ExecuteAsync(["delete-build-from-channel", "--id", buildId.ToString(), "--channel", channelName]);
});
}
public async Task<AsyncDisposableValue<string>> CreateSubscriptionAsync(
string sourceRepo,
string targetRepo,
string channel,
string targetBranch,
string? sourceDirectory,
string? targetDirectory,
bool skipCleanup,
List<string>? excludedAssets = null)
{
logger.LogInformation("Creating a test subscription");
string[] directoryArg = !string.IsNullOrEmpty(sourceDirectory) ?
["--source-directory", sourceDirectory] :
["--target-directory", targetDirectory!];
string[] excludedAssetsParameter = excludedAssets != null ?
[ "--excluded-assets", string.Join(';', excludedAssets) ] :
[];
var res = await ExecuteAsync([
"add-subscription",
"--channel", channel,
"--source-repo", sourceRepo,
"--target-repo", targetRepo,
"--target-branch", targetBranch,
"-q",
"--no-trigger",
"--source-enabled", "true",
"--update-frequency", "none",
.. directoryArg,
.. excludedAssetsParameter
]);
Match match = Regex.Match(res.StandardOutput, "Successfully created new subscription with id '([a-f0-9-]+)'");
if (match.Success)
{
var subscriptionId = match.Groups[1].Value;
return AsyncDisposableValue.Create(subscriptionId, async () =>
{
if (skipCleanup)
{
return;
}
logger.LogInformation("Cleaning up Test Subscription {subscriptionId}", subscriptionId);
try
{
await ExecuteAsync(["delete-subscriptions", "--id", subscriptionId, "--quiet"]);
}
catch (Exception)
{
// If this throws an exception the most likely cause is that the subscription was deleted as part of the test case
}
});
}
throw new Exception("Unable to create subscription.");
}
public async Task<ProcessExecutionResult> TriggerSubscriptionAsync(string subscriptionId)
{
return await ExecuteAsync(
[
"trigger-subscriptions",
"--ids", subscriptionId,
"-q"
]);
}
}