-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathPzip2ExecutorTests.cs
More file actions
319 lines (276 loc) · 12.4 KB
/
Pzip2ExecutorTests.cs
File metadata and controls
319 lines (276 loc) · 12.4 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace VirtualClient.Actions
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using VirtualClient.Common;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using VirtualClient.Contracts;
using VirtualClient.Common.Contracts;
using VirtualClient.Common.Telemetry;
[TestFixture]
[Category("Unit")]
public class Pbzip2ExecutorTests
{
private MockFixture mockFixture;
private DependencyPath mockPackage;
private ConcurrentBuffer defaultOutput = new ConcurrentBuffer();
[SetUp]
public void SetupDefaultBehavior()
{
this.mockFixture = new MockFixture();
this.mockFixture.Setup(PlatformID.Unix);
this.mockPackage = new DependencyPath("pbzip2", this.mockFixture.PlatformSpecifics.GetPackagePath("pbzip2"));
this.mockFixture.PackageManager.OnGetPackage().ReturnsAsync(this.mockPackage);
this.mockFixture.File.Reset();
this.mockFixture.File.Setup(f => f.Exists(It.IsAny<string>()))
.Returns(true);
this.mockFixture.Directory.Setup(f => f.Exists(It.IsAny<string>()))
.Returns(true);
this.mockFixture.FileSystem.SetupGet(fs => fs.File).Returns(this.mockFixture.File.Object);
this.mockFixture.Parameters = new Dictionary<string, IConvertible>()
{
{ nameof(Pbzip2Executor.Options), "testOption1 testOption2" },
{ nameof(Pbzip2Executor.InputFiles), "Test1.zip Test2.txt" },
{ nameof(Pbzip2Executor.PackageName), "Pbzip2" },
{ nameof(Pbzip2Executor.Scenario), "mockScenario"}
};
string currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string resultsPath = Path.Combine(currentDirectory, "Examples", "Pbzip2", "Pbzip2ResultsExample.txt");
string results = File.ReadAllText(resultsPath);
this.defaultOutput.Clear();
this.defaultOutput.Append(results);
}
[Test]
public void Pbzip2StateIsSerializable()
{
State state = new State(new Dictionary<string, IConvertible>
{
["Pbzip2StateInitialized"] = true
});
string serializedState = state.ToJson();
JObject deserializedState = JObject.Parse(serializedState);
Pbzip2Executor.Pbzip2State result = deserializedState?.ToObject<Pbzip2Executor.Pbzip2State>();
Assert.AreEqual(true, result.Pbzip2StateInitialized);
}
[Test]
public async Task Pbzip2ExecutorGetsDefaultFileIfInputFileOrDirNotProvided()
{
ProcessStartInfo expectedInfo = new ProcessStartInfo();
this.mockFixture.Parameters = new Dictionary<string, IConvertible>()
{
{ nameof(Pbzip2Executor.Options), "testOption1 testOption2" },
{ nameof(Pbzip2Executor.InputFiles), "" },
{ nameof(Pbzip2Executor.PackageName), "pbzip2" },
{ nameof(Pbzip2Executor.Scenario), "mockScenario" }
};
string expectedCommand = $"sudo wget --no-check-certificate https://sun.aei.polsl.pl//~sdeor/corpus/silesia.zip";
bool commandExecuted = false;
this.mockFixture.ProcessManager.OnCreateProcess = (exe, arguments, workingDir) =>
{
if (expectedCommand == $"{exe} {arguments}")
{
commandExecuted = true;
}
return new InMemoryProcess
{
StartInfo = new ProcessStartInfo
{
FileName = exe,
Arguments = arguments
},
ExitCode = 0,
OnStart = () => true,
OnHasExited = () => true,
StandardError = this.defaultOutput
};
};
using (TestPbzip2Executor Pbzip2Executor = new TestPbzip2Executor(this.mockFixture.Dependencies, this.mockFixture.Parameters))
{
await Pbzip2Executor.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);
}
Assert.IsTrue(commandExecuted);
}
[Test]
public async Task Pbzip2ExecutorRunsTheExpectedWorkloadCommand()
{
ProcessStartInfo expectedInfo = new ProcessStartInfo();
this.mockFixture.Parameters = new Dictionary<string, IConvertible>()
{
{ nameof(Pbzip2Executor.Options), "testOption1 testOption2" },
{ nameof(Pbzip2Executor.InputFiles), "" },
{ nameof(Pbzip2Executor.PackageName), "pbzip2" },
{ nameof(Pbzip2Executor.Scenario), "mockScenario" }
};
string mockPackagePath = this.mockPackage.Path;
string expectedCommand = $"sudo bash -c \"pbzip2 testOption1 testOption2 {this.mockFixture.PlatformSpecifics.Combine(mockPackagePath, "silesia/*")}\"";
bool commandExecuted = false;
this.mockFixture.ProcessManager.OnCreateProcess = (exe, arguments, workingDir) =>
{
if (expectedCommand == $"{exe} {arguments}")
{
commandExecuted = true;
}
return new InMemoryProcess
{
StartInfo = new ProcessStartInfo
{
FileName = exe,
Arguments = arguments
},
ExitCode = 0,
OnStart = () => true,
OnHasExited = () => true,
StandardError = this.defaultOutput
};
};
using (TestPbzip2Executor Pbzip2Executor = new TestPbzip2Executor(this.mockFixture.Dependencies, this.mockFixture.Parameters))
{
await Pbzip2Executor.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);
}
Assert.IsTrue(commandExecuted);
}
[Test]
public async Task Pbzip2ExecutorExecutesTheCorrectCommandsWithInstallationIfInputFileOrDirNotProvided()
{
ProcessStartInfo expectedInfo = new ProcessStartInfo();
this.mockFixture.Parameters = new Dictionary<string, IConvertible>()
{
{ nameof(Pbzip2Executor.Options), "testOption1 testOption2" },
{ nameof(Pbzip2Executor.InputFiles), "" },
{ nameof(Pbzip2Executor.PackageName), "pbzip2" },
{ nameof(Pbzip2Executor.Scenario), "mockScenario"}
};
string mockPackagePath = this.mockPackage.Path;
List<string> expectedCommands = new List<string>
{
$"sudo wget --no-check-certificate https://sun.aei.polsl.pl//~sdeor/corpus/silesia.zip",
$"sudo unzip silesia.zip -d silesia",
$"sudo bash -c \"pbzip2 testOption1 testOption2 {this.mockFixture.PlatformSpecifics.Combine(mockPackagePath, "silesia/*")}\""
};
int processCount = 0;
this.mockFixture.ProcessManager.OnCreateProcess = (exe, arguments, workingDir) =>
{
Assert.AreEqual(expectedCommands.ElementAt(processCount), $"{exe} {arguments}");
processCount++;
return new InMemoryProcess
{
StartInfo = new ProcessStartInfo
{
FileName = exe,
Arguments = arguments
},
ExitCode = 0,
OnStart = () => true,
OnHasExited = () => true,
StandardError = this.defaultOutput
};
};
this.mockFixture.StateManager.OnGetState().ReturnsAsync(JObject.FromObject(new Pbzip2Executor.Pbzip2State()
{
Pbzip2StateInitialized = false
}));
using (TestPbzip2Executor Pbzip2Executor = new TestPbzip2Executor(this.mockFixture.Dependencies, this.mockFixture.Parameters))
{
await Pbzip2Executor.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);
}
Assert.IsTrue(processCount == 3);
}
[Test]
public async Task Pbzip2ExecutorExecutesTheCorrectCommandsWithInstallationIfInputFilesOrDirsAreProvided()
{
ProcessStartInfo expectedInfo = new ProcessStartInfo();
string mockPackagePath = this.mockPackage.Path;
List<string> expectedCommands = new List<string>
{
$"sudo bash -c \"pbzip2 testOption1 testOption2 Test1.zip Test2.txt\"",
};
int processCount = 0;
this.mockFixture.ProcessManager.OnCreateProcess = (exe, arguments, workingDir) =>
{
Assert.AreEqual(expectedCommands.ElementAt(processCount), $"{exe} {arguments}");
processCount++;
return new InMemoryProcess
{
StartInfo = new ProcessStartInfo
{
FileName = exe,
Arguments = arguments
},
ExitCode = 0,
OnStart = () => true,
OnHasExited = () => true,
StandardError = this.defaultOutput
};
};
this.mockFixture.StateManager.OnGetState().ReturnsAsync(JObject.FromObject(new Pbzip2Executor.Pbzip2State()
{
Pbzip2StateInitialized = false
}));
using (TestPbzip2Executor Pbzip2Executor = new TestPbzip2Executor(this.mockFixture.Dependencies, this.mockFixture.Parameters))
{
await Pbzip2Executor.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);
}
Assert.IsTrue(processCount == 1);
}
[Test]
public async Task Pbzip2ExecutorSkipsInitializationOfTheWorkloadForExecutionAfterTheFirstRun()
{
ProcessStartInfo expectedInfo = new ProcessStartInfo();
string mockPackagePath = this.mockPackage.Path;
List<string> expectedCommands = new List<string>
{
$"sudo bash -c \"pbzip2 testOption1 testOption2 Test1.zip Test2.txt\""
};
int processCount = 0;
this.mockFixture.ProcessManager.OnCreateProcess = (exe, arguments, workingDir) =>
{
Assert.AreEqual(expectedCommands.ElementAt(processCount), $"{exe} {arguments}");
processCount++;
return new InMemoryProcess
{
StartInfo = new ProcessStartInfo
{
FileName = exe,
Arguments = arguments
},
ExitCode = 0,
OnStart = () => true,
OnHasExited = () => true,
StandardError = this.defaultOutput
};
};
this.mockFixture.StateManager.OnGetState().ReturnsAsync(JObject.FromObject(new Pbzip2Executor.Pbzip2State()
{
Pbzip2StateInitialized = true
}));
using (TestPbzip2Executor Pbzip2Executor = new TestPbzip2Executor(this.mockFixture.Dependencies, this.mockFixture.Parameters))
{
await Pbzip2Executor.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);
}
Assert.IsTrue(processCount == 1);
}
private class TestPbzip2Executor : Pbzip2Executor
{
public TestPbzip2Executor(IServiceCollection dependencies, IDictionary<string, IConvertible> parameters)
: base(dependencies, parameters)
{
}
public new Task ExecuteAsync(EventContext context, CancellationToken cancellationToken)
{
return base.ExecuteAsync(context, cancellationToken);
}
}
}
}