Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit e0a9f07

Browse files
authored
Test timer repro (#1934)
* TimerRepro tests * fmt
1 parent babe7c2 commit e0a9f07

File tree

3 files changed

+124
-10
lines changed

3 files changed

+124
-10
lines changed

src/ApiService/ApiService/TimerRepro.cs

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,30 @@ namespace Microsoft.OneFuzz.Service;
55
public class TimerRepro {
66
private readonly ILogTracer _log;
77

8-
private readonly IStorage _storage;
8+
private readonly IOnefuzzContext _onefuzzContext;
99

10-
private readonly IReproOperations _reproOperations;
11-
12-
public TimerRepro(ILogTracer log, IStorage storage, IReproOperations reproOperations) {
10+
public TimerRepro(ILogTracer log, IOnefuzzContext onefuzzContext) {
1311
_log = log;
14-
_storage = storage;
15-
_reproOperations = reproOperations;
12+
_onefuzzContext = onefuzzContext;
1613
}
1714

1815
// [Function("TimerRepro")]
1916
public async Async.Task Run([TimerTrigger("00:00:30")] TimerInfo myTimer) {
20-
var expired = _reproOperations.SearchExpired();
17+
var expired = _onefuzzContext.ReproOperations.SearchExpired();
2118
await foreach (var repro in expired) {
2219
_log.Info($"stopping repro: {repro.VmId}");
23-
await _reproOperations.Stopping(repro);
20+
await _onefuzzContext.ReproOperations.Stopping(repro);
2421
}
2522

2623
var expiredVmIds = expired.Select(repro => repro?.VmId);
2724

28-
await foreach (var repro in _reproOperations.SearchStates(VmStateHelper.NeedsWork)) {
25+
await foreach (var repro in _onefuzzContext.ReproOperations.SearchStates(VmStateHelper.NeedsWork)) {
2926
if (await expiredVmIds.ContainsAsync(repro.VmId)) {
3027
// this VM already got processed during the expired phase
3128
continue;
3229
}
3330
_log.Info($"update repro: {repro.VmId}");
34-
await _reproOperations.ProcessStateUpdates(repro);
31+
await _onefuzzContext.ReproOperations.ProcessStateUpdates(repro);
3532
}
3633
}
3734

src/ApiService/Tests/Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="FsCheck" Version="2.16.4" />
1111
<PackageReference Include="FsCheck.Xunit" Version="2.16.4" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
13+
<PackageReference Include="Moq" Version="4.17.2" />
1314
<PackageReference Include="xunit" Version="2.4.1" />
1415
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1516
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.Azure.Functions.Worker;
5+
using Microsoft.OneFuzz.Service;
6+
using Moq;
7+
using Xunit;
8+
9+
namespace Tests;
10+
11+
public class TimerReproTests {
12+
private ILogTracer log;
13+
private Mock<IOnefuzzContext> mockCtx;
14+
private Mock<IReproOperations> mockReproOperations;
15+
16+
public TimerReproTests() {
17+
mockCtx = new Mock<IOnefuzzContext>();
18+
19+
mockReproOperations = new Mock<IReproOperations>();
20+
21+
mockReproOperations.Setup(x => x.SearchExpired())
22+
.Returns(AsyncEnumerable.Empty<Repro>());
23+
mockReproOperations.Setup(x => x.SearchStates(VmStateHelper.NeedsWork))
24+
.Returns(AsyncEnumerable.Empty<Repro>());
25+
26+
log = new Mock<ILogTracer>().Object;
27+
}
28+
29+
[Fact]
30+
public async System.Threading.Tasks.Task NoExpiredRepros() {
31+
mockReproOperations.Setup(x => x.SearchExpired())
32+
.Returns(AsyncEnumerable.Empty<Repro>());
33+
34+
mockCtx.Setup(x => x.ReproOperations)
35+
.Returns(mockReproOperations.Object);
36+
37+
var timerRepro = new TimerRepro(log, mockCtx.Object);
38+
await timerRepro.Run(new TimerInfo());
39+
40+
mockReproOperations.Verify(x => x.Stopping(It.IsAny<Repro>()), Times.Never());
41+
}
42+
43+
[Fact]
44+
public async System.Threading.Tasks.Task ExpiredRepro() {
45+
mockReproOperations.Setup(x => x.SearchExpired())
46+
.Returns(new List<Repro> {
47+
GenerateRepro()
48+
}.ToAsyncEnumerable());
49+
50+
mockCtx.Setup(x => x.ReproOperations)
51+
.Returns(mockReproOperations.Object);
52+
53+
var timerRepro = new TimerRepro(log, mockCtx.Object);
54+
await timerRepro.Run(new TimerInfo());
55+
56+
mockReproOperations.Verify(x => x.Stopping(It.IsAny<Repro>()), Times.Once());
57+
}
58+
59+
[Fact]
60+
public async System.Threading.Tasks.Task NoNeedsWorkRepros() {
61+
mockReproOperations.Setup(x => x.SearchStates(VmStateHelper.NeedsWork))
62+
.Returns(AsyncEnumerable.Empty<Repro>());
63+
64+
mockCtx.Setup(x => x.ReproOperations)
65+
.Returns(mockReproOperations.Object);
66+
67+
var timerRepro = new TimerRepro(log, mockCtx.Object);
68+
await timerRepro.Run(new TimerInfo());
69+
70+
mockReproOperations.Verify(x => x.ProcessStateUpdates(It.IsAny<Repro>(), It.IsAny<int>()), Times.Never());
71+
}
72+
73+
[Fact]
74+
public async System.Threading.Tasks.Task DontProcessExpiredVms() {
75+
var expiredVm = GenerateRepro();
76+
var notExpiredVm = GenerateRepro();
77+
78+
mockReproOperations.Setup(x => x.SearchExpired())
79+
.Returns(new List<Repro> {
80+
expiredVm
81+
}.ToAsyncEnumerable());
82+
83+
mockReproOperations.Setup(x => x.SearchStates(VmStateHelper.NeedsWork))
84+
.Returns(new List<Repro> {
85+
expiredVm,
86+
notExpiredVm
87+
}.ToAsyncEnumerable());
88+
89+
mockCtx.Setup(x => x.ReproOperations)
90+
.Returns(mockReproOperations.Object);
91+
92+
var timerRepro = new TimerRepro(log, mockCtx.Object);
93+
await timerRepro.Run(new TimerInfo());
94+
95+
mockReproOperations.Verify(x => x.ProcessStateUpdates(It.IsAny<Repro>(), It.IsAny<int>()), Times.Once());
96+
}
97+
98+
private static Repro GenerateRepro() {
99+
return new Repro(
100+
Guid.NewGuid(),
101+
Guid.Empty,
102+
Guid.Empty,
103+
new ReproConfig(
104+
new Container(String.Empty),
105+
String.Empty,
106+
0
107+
),
108+
VmState.Init,
109+
null,
110+
Os.Linux,
111+
null,
112+
null,
113+
null,
114+
null);
115+
}
116+
}

0 commit comments

Comments
 (0)