|
| 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