|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using FluentAssertions; |
| 6 | +using Halibut.ServiceModel; |
| 7 | +using NSubstitute; |
| 8 | +using NUnit.Framework; |
| 9 | +using Octopus.Tentacle.Client.EventDriven; |
| 10 | +using Octopus.Tentacle.Client.Execution; |
| 11 | +using Octopus.Tentacle.Client.Observability; |
| 12 | +using Octopus.Tentacle.Client.Scripts; |
| 13 | +using Octopus.Tentacle.Contracts; |
| 14 | +using Octopus.Tentacle.Contracts.ClientServices; |
| 15 | +using Octopus.Tentacle.Contracts.Logging; |
| 16 | +using Octopus.Tentacle.Contracts.Observability; |
| 17 | + |
| 18 | +namespace Octopus.Tentacle.Client.Tests |
| 19 | +{ |
| 20 | + [TestFixture] |
| 21 | + public class ScriptServiceV1ExecutorTests |
| 22 | + { |
| 23 | + /// <summary> |
| 24 | + /// In ScriptServiceV1, it is possible that additional logs may be returned after the |
| 25 | + /// first call to GetStatus responds with a completed status. The script executor |
| 26 | + /// mitigates against this by checking a second time if the script is completed. |
| 27 | + /// </summary> |
| 28 | + [Test] |
| 29 | + public async Task GetStatusWillDoubleCheckIfProcessIsCompleted() |
| 30 | + { |
| 31 | + // Arrange |
| 32 | + var scriptService = Substitute.For<IAsyncClientScriptService>(); |
| 33 | + scriptService.GetStatusAsync(Arg.Any<ScriptStatusRequest>(), Arg.Any<HalibutProxyRequestOptions>()) |
| 34 | + .Returns( |
| 35 | + x => Task.FromResult(new ScriptStatusResponse( |
| 36 | + x.Arg<ScriptStatusRequest>().Ticket, |
| 37 | + ProcessState.Complete, |
| 38 | + nextLogSequence: 1, |
| 39 | + exitCode: 0, |
| 40 | + logs: new List<ProcessOutput> |
| 41 | + { |
| 42 | + new(ProcessOutputSource.StdOut, "First log line"), |
| 43 | + })), |
| 44 | + x => Task.FromResult(new ScriptStatusResponse( |
| 45 | + x.Arg<ScriptStatusRequest>().Ticket, |
| 46 | + ProcessState.Complete, |
| 47 | + nextLogSequence: 2, |
| 48 | + exitCode: 0, |
| 49 | + logs: new List<ProcessOutput> |
| 50 | + { |
| 51 | + new(ProcessOutputSource.StdOut, "Second log line"), |
| 52 | + })) |
| 53 | + ); |
| 54 | + |
| 55 | + var scriptExecutor = new ScriptServiceV1Executor( |
| 56 | + scriptService, |
| 57 | + RpcCallExecutorFactory.Create(TimeSpan.Zero, Substitute.For<ITentacleClientObserver>()), |
| 58 | + ClientOperationMetricsBuilder.Start(), |
| 59 | + Substitute.For<ITentacleClientTaskLog>() |
| 60 | + ); |
| 61 | + |
| 62 | + |
| 63 | + var context = new CommandContext( |
| 64 | + scriptTicket: new ScriptTicket("TestTicket"), |
| 65 | + nextLogSequence: 0, |
| 66 | + ScriptServiceVersion.ScriptServiceVersion1 |
| 67 | + ); |
| 68 | + |
| 69 | + // Act |
| 70 | + var result = await scriptExecutor.GetStatus(context, CancellationToken.None); |
| 71 | + |
| 72 | + // Assert |
| 73 | + result.ScriptStatus.Should().NotBeNull(); |
| 74 | + result.ScriptStatus.ExitCode.Should().Be(0); |
| 75 | + result.ScriptStatus.Logs.Should().HaveCount(2); |
| 76 | + result.ScriptStatus.Logs[0].Source.Should().Be(ProcessOutputSource.StdOut); |
| 77 | + result.ScriptStatus.Logs[0].Text.Should().Be("First log line"); |
| 78 | + result.ScriptStatus.Logs[1].Source.Should().Be(ProcessOutputSource.StdOut); |
| 79 | + result.ScriptStatus.Logs[1].Text.Should().Be("Second log line"); |
| 80 | + result.ScriptStatus.State.Should().Be(ProcessState.Complete); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public async Task GetStatusWillOnlyCheckOnceIfProcessIsNotComplete() |
| 85 | + { |
| 86 | + // Arrange |
| 87 | + var scriptService = Substitute.For<IAsyncClientScriptService>(); |
| 88 | + scriptService.GetStatusAsync(Arg.Any<ScriptStatusRequest>(), Arg.Any<HalibutProxyRequestOptions>()) |
| 89 | + .Returns( |
| 90 | + x => Task.FromResult( |
| 91 | + new ScriptStatusResponse( |
| 92 | + x.Arg<ScriptStatusRequest>().Ticket, |
| 93 | + ProcessState.Running, |
| 94 | + nextLogSequence: 1, |
| 95 | + exitCode: 0, |
| 96 | + logs: new List<ProcessOutput> |
| 97 | + { |
| 98 | + new(ProcessOutputSource.StdOut, "First log line"), |
| 99 | + } |
| 100 | + ) |
| 101 | + ), |
| 102 | + x => Task.FromResult( |
| 103 | + new ScriptStatusResponse( |
| 104 | + x.Arg<ScriptStatusRequest>().Ticket, |
| 105 | + ProcessState.Complete, |
| 106 | + nextLogSequence: 2, |
| 107 | + exitCode: 0, |
| 108 | + logs: new List<ProcessOutput> |
| 109 | + { |
| 110 | + new(ProcessOutputSource.StdOut, "This line should not be returned"), |
| 111 | + } |
| 112 | + ) |
| 113 | + ) |
| 114 | + ); |
| 115 | + var scriptExecutor = new ScriptServiceV1Executor( |
| 116 | + scriptService, |
| 117 | + RpcCallExecutorFactory.Create(TimeSpan.Zero, Substitute.For<ITentacleClientObserver>()), |
| 118 | + ClientOperationMetricsBuilder.Start(), |
| 119 | + Substitute.For<ITentacleClientTaskLog>() |
| 120 | + ); |
| 121 | + var context = new CommandContext( |
| 122 | + scriptTicket: new ScriptTicket("TestTicket"), |
| 123 | + nextLogSequence: 0, |
| 124 | + scripServiceVersionUsed: ScriptServiceVersion.ScriptServiceVersion1 |
| 125 | + ); |
| 126 | + |
| 127 | + // Act |
| 128 | + var result = await scriptExecutor.GetStatus(context, CancellationToken.None); |
| 129 | + |
| 130 | + // Assert |
| 131 | + result.ScriptStatus.Should().NotBeNull(); |
| 132 | + result.ScriptStatus.ExitCode.Should().Be(0); |
| 133 | + result.ScriptStatus.Logs.Should().HaveCount(1); |
| 134 | + result.ScriptStatus.Logs[0].Source.Should().Be(ProcessOutputSource.StdOut); |
| 135 | + result.ScriptStatus.Logs[0].Text.Should().Be("First log line"); |
| 136 | + result.ScriptStatus.State.Should().Be(ProcessState.Running); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments