From 3d3a0273e604e9139da47acd9d27201d909efdfc Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 17 Jun 2024 09:44:28 +0000 Subject: [PATCH 1/6] build(deps-dev): bump braces from 3.0.2 to 3.0.3 Bumps [braces](https://github.com/micromatch/braces) from 3.0.2 to 3.0.3. - [Changelog](https://github.com/micromatch/braces/blob/master/CHANGELOG.md) - [Commits](https://github.com/micromatch/braces/compare/3.0.2...3.0.3) --- updated-dependencies: - dependency-name: braces dependency-type: indirect ... Signed-off-by: dependabot[bot] --- package-lock.json | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1620b5a..e487f96 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1018,12 +1018,12 @@ } }, "node_modules/braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dev": true, "dependencies": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" }, "engines": { "node": ">=8" @@ -1617,9 +1617,9 @@ } }, "node_modules/fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dev": true, "dependencies": { "to-regex-range": "^5.0.1" @@ -7604,12 +7604,12 @@ } }, "braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dev": true, "requires": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" } }, "callsites": { @@ -8048,9 +8048,9 @@ } }, "fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dev": true, "requires": { "to-regex-range": "^5.0.1" From 9b215af86c1b77c587efb0dc59b8c460d7fd46aa Mon Sep 17 00:00:00 2001 From: Dmitry Ostrikov Date: Mon, 17 Jun 2024 18:16:34 +0700 Subject: [PATCH 2/6] feat(repeater): adopt self-deploying repeaters closes #174 --- .../Api/CreateRepeaterRequest.cs | 22 ------- .../Api/DefaultRepeaters.cs | 34 ----------- .../Api/DeleteRepeaterRequest.cs | 13 ----- src/SecTester.Repeater/Api/IRepeaters.cs | 10 ---- .../Api/RepeaterIdentity.cs | 8 --- .../Bus/DefaultRepeaterBusFactory.cs | 4 +- src/SecTester.Repeater/Bus/IRepeaterBus.cs | 2 +- .../Bus/IRepeaterBusFactory.cs | 2 +- src/SecTester.Repeater/Bus/RepeaterInfo.cs | 5 +- .../Bus/SocketIoRepeaterBus.cs | 15 ++++- .../DefaultRepeaterFactory.cs | 13 ++--- .../Extensions/ServiceCollectionExtensions.cs | 2 - src/SecTester.Repeater/README.md | 30 ---------- src/SecTester.Repeater/Repeater.cs | 10 ++-- src/SecTester.Repeater/RepeaterOptions.cs | 4 +- .../SecTester.Repeater.csproj | 1 - src/SecTester.Runner/SecRunner.cs | 6 +- .../Api/DefaultRepeatersTests.cs | 58 ------------------- .../Bus/DefaultRepeaterBusFactoryTests.cs | 5 +- .../Bus/SocketIoRepeaterBusTests.cs | 32 +++++++++- .../DefaultRepeaterFactoryTests.cs | 25 ++------ .../ServiceCollectionExtensionsTests.cs | 10 ---- .../RepeaterOptionsTests.cs | 2 +- .../SecTester.Repeater.Tests/RepeaterTests.cs | 7 ++- .../SecTester.Repeater.Tests.csproj | 1 - test/SecTester.Repeater.Tests/Usings.cs | 1 - test/SecTester.Runner.Tests/SecRunnerTests.cs | 30 +--------- test/SecTester.Scan.Tests/Usings.cs | 4 +- 28 files changed, 76 insertions(+), 280 deletions(-) delete mode 100644 src/SecTester.Repeater/Api/CreateRepeaterRequest.cs delete mode 100644 src/SecTester.Repeater/Api/DefaultRepeaters.cs delete mode 100644 src/SecTester.Repeater/Api/DeleteRepeaterRequest.cs delete mode 100644 src/SecTester.Repeater/Api/IRepeaters.cs delete mode 100644 src/SecTester.Repeater/Api/RepeaterIdentity.cs delete mode 100644 test/SecTester.Repeater.Tests/Api/DefaultRepeatersTests.cs diff --git a/src/SecTester.Repeater/Api/CreateRepeaterRequest.cs b/src/SecTester.Repeater/Api/CreateRepeaterRequest.cs deleted file mode 100644 index 32134b7..0000000 --- a/src/SecTester.Repeater/Api/CreateRepeaterRequest.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Net.Http; -using System.Text; -using SecTester.Core.Commands; -using SecTester.Core.Dispatchers; - -namespace SecTester.Repeater.Api; - -internal record CreateRepeaterRequest : HttpRequest -{ - public CreateRepeaterRequest(string name, string? description) : - base("/api/v1/repeaters", HttpMethod.Post) - { - var data = new - { - Name = name, - Description = description, - ClientRole = "dev-centric" - }; - var content = MessageSerializer.Serialize(data); - Body = new StringContent(content, Encoding.UTF8, "application/json"); - } -} diff --git a/src/SecTester.Repeater/Api/DefaultRepeaters.cs b/src/SecTester.Repeater/Api/DefaultRepeaters.cs deleted file mode 100644 index d3cf91f..0000000 --- a/src/SecTester.Repeater/Api/DefaultRepeaters.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Threading.Tasks; -using SecTester.Core.Bus; -using SecTester.Core.Exceptions; - -namespace SecTester.Repeater.Api; - -public class DefaultRepeaters : IRepeaters -{ - private readonly ICommandDispatcher _commandDispatcher; - - public DefaultRepeaters(ICommandDispatcher commandDispatcher) - { - _commandDispatcher = commandDispatcher; - } - - public async Task CreateRepeater(string name, string? description = default) - { - var repeaterId = (await _commandDispatcher.Execute(new CreateRepeaterRequest(name, description)).ConfigureAwait(false))?.Id; - - if (string.IsNullOrEmpty(repeaterId)) - { - throw new SecTesterException("Cannot create repeater"); - } - - return repeaterId!; - } - - public async Task DeleteRepeater(string repeaterId) - { - await _commandDispatcher.Execute( - new DeleteRepeaterRequest(repeaterId) - ).ConfigureAwait(false); - } -} diff --git a/src/SecTester.Repeater/Api/DeleteRepeaterRequest.cs b/src/SecTester.Repeater/Api/DeleteRepeaterRequest.cs deleted file mode 100644 index 2fb23d2..0000000 --- a/src/SecTester.Repeater/Api/DeleteRepeaterRequest.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Net.Http; -using SecTester.Core; -using SecTester.Core.Commands; - -namespace SecTester.Repeater.Api; - -internal record DeleteRepeaterRequest : HttpRequest -{ - public DeleteRepeaterRequest(string repeaterId) : - base($"/api/v1/repeaters/{repeaterId}", HttpMethod.Delete, expectReply: false) - { - } -} diff --git a/src/SecTester.Repeater/Api/IRepeaters.cs b/src/SecTester.Repeater/Api/IRepeaters.cs deleted file mode 100644 index 059cc95..0000000 --- a/src/SecTester.Repeater/Api/IRepeaters.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Threading.Tasks; - -namespace SecTester.Repeater.Api; - -public interface IRepeaters -{ - Task CreateRepeater(string name, string? description = default); - - Task DeleteRepeater(string repeaterId); -} diff --git a/src/SecTester.Repeater/Api/RepeaterIdentity.cs b/src/SecTester.Repeater/Api/RepeaterIdentity.cs deleted file mode 100644 index 59399c1..0000000 --- a/src/SecTester.Repeater/Api/RepeaterIdentity.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace SecTester.Repeater.Api; - -internal record RepeaterIdentity(string Id) -{ - public string Id { get; } = Id ?? throw new ArgumentNullException(nameof(Id)); -} diff --git a/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs b/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs index db3ae74..9001e8a 100644 --- a/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs +++ b/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs @@ -22,7 +22,7 @@ public DefaultRepeaterBusFactory(Configuration config, ILoggerFactory loggerFact _scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory)); } - public IRepeaterBus Create(string repeaterId) + public IRepeaterBus Create(string? namePrefix = default) { if (_config.Credentials is null) { @@ -39,7 +39,7 @@ public IRepeaterBus Create(string repeaterId) ReconnectionDelayMax = options.ReconnectionDelayMax, ConnectionTimeout = options.ConnectionTimeout, Transport = TransportProtocol.WebSocket, - Auth = new { token = _config.Credentials.Token, domain = repeaterId } + Auth = new { token = _config.Credentials.Token, domain = namePrefix ?? System.Net.Dns.GetHostName() } }) { Serializer = new SocketIOMessagePackSerializer() diff --git a/src/SecTester.Repeater/Bus/IRepeaterBus.cs b/src/SecTester.Repeater/Bus/IRepeaterBus.cs index af02b33..cb8113b 100644 --- a/src/SecTester.Repeater/Bus/IRepeaterBus.cs +++ b/src/SecTester.Repeater/Bus/IRepeaterBus.cs @@ -11,5 +11,5 @@ public interface IRepeaterBus : IAsyncDisposable event Action UpgradeAvailable; Task Connect(); - Task Deploy(string repeaterId, CancellationToken? cancellationToken = null); + Task Deploy(string? repeaterId, CancellationToken? cancellationToken = null); } diff --git a/src/SecTester.Repeater/Bus/IRepeaterBusFactory.cs b/src/SecTester.Repeater/Bus/IRepeaterBusFactory.cs index 505fa63..13157d5 100644 --- a/src/SecTester.Repeater/Bus/IRepeaterBusFactory.cs +++ b/src/SecTester.Repeater/Bus/IRepeaterBusFactory.cs @@ -2,5 +2,5 @@ namespace SecTester.Repeater.Bus; public interface IRepeaterBusFactory { - IRepeaterBus Create(string repeaterId); + IRepeaterBus Create(string? namePrefix = default); } diff --git a/src/SecTester.Repeater/Bus/RepeaterInfo.cs b/src/SecTester.Repeater/Bus/RepeaterInfo.cs index 4671e90..51006b9 100644 --- a/src/SecTester.Repeater/Bus/RepeaterInfo.cs +++ b/src/SecTester.Repeater/Bus/RepeaterInfo.cs @@ -1,10 +1,11 @@ +using System; using MessagePack; namespace SecTester.Repeater.Bus; [MessagePackObject] -public sealed record RepeaterInfo +public sealed record RepeaterInfo(string RepeaterId) { [Key("repeaterId")] - public string RepeaterId { get; set; } = null!; + public string RepeaterId { get; init; } = RepeaterId ?? throw new ArgumentNullException(nameof(RepeaterId)); } diff --git a/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs b/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs index db5e7fd..7a4d167 100644 --- a/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs +++ b/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs @@ -90,7 +90,7 @@ public async ValueTask DisposeAsync() GC.SuppressFinalize(this); } - public async Task Deploy(string repeaterId, CancellationToken? cancellationToken = null) + public async Task Deploy(string? repeaterId, CancellationToken? cancellationToken = null) { try { @@ -98,13 +98,24 @@ public async Task Deploy(string repeaterId, CancellationToken? cancellationToken _connection.On("deployed", response => tcs.TrySetResult(response.GetValue())); - await _connection.EmitAsync("deploy", new RepeaterInfo { RepeaterId = repeaterId }).ConfigureAwait(false); + var args = string.IsNullOrWhiteSpace(repeaterId) ? Array.Empty() : new object[] { new RepeaterInfo(repeaterId!) }; + + await _connection + .EmitAsync("deploy", args) + .ConfigureAwait(false); using var _ = cancellationToken?.Register(() => tcs.TrySetCanceled()); var result = await tcs.Task.ConfigureAwait(false); _logger.LogDebug("Repeater ({RepeaterId}) deployed", result?.RepeaterId); + + if (null == result || string.IsNullOrWhiteSpace(result.RepeaterId)) + { + throw new InvalidOperationException("An error occured while repeater is being deployed"); + } + + return result.RepeaterId; } finally { diff --git a/src/SecTester.Repeater/DefaultRepeaterFactory.cs b/src/SecTester.Repeater/DefaultRepeaterFactory.cs index 0a1a4a5..aa6babc 100644 --- a/src/SecTester.Repeater/DefaultRepeaterFactory.cs +++ b/src/SecTester.Repeater/DefaultRepeaterFactory.cs @@ -3,7 +3,6 @@ using Microsoft.Extensions.Logging; using SecTester.Core; using SecTester.Core.Logger; -using SecTester.Repeater.Api; using SecTester.Repeater.Bus; namespace SecTester.Repeater; @@ -12,14 +11,12 @@ public class DefaultRepeaterFactory : IRepeaterFactory { private readonly Configuration _configuration; private readonly IRepeaterBusFactory _busFactory; - private readonly IRepeaters _repeaters; private readonly ILoggerFactory _loggerFactory; private readonly IAnsiCodeColorizer _ansiCodeColorizer; private readonly RequestRunnerResolver _requestRunnerResolver; - public DefaultRepeaterFactory(IRepeaters repeaters, IRepeaterBusFactory busFactory, Configuration configuration, ILoggerFactory loggerFactory, IAnsiCodeColorizer ansiCodeColorizer, RequestRunnerResolver requestRunnerResolver) + public DefaultRepeaterFactory(IRepeaterBusFactory busFactory, Configuration configuration, ILoggerFactory loggerFactory, IAnsiCodeColorizer ansiCodeColorizer, RequestRunnerResolver requestRunnerResolver) { - _repeaters = repeaters ?? throw new ArgumentNullException(nameof(repeaters)); _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); _busFactory = busFactory ?? throw new ArgumentNullException(nameof(busFactory)); _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); @@ -27,15 +24,13 @@ public DefaultRepeaterFactory(IRepeaters repeaters, IRepeaterBusFactory busFacto _requestRunnerResolver = requestRunnerResolver ?? throw new ArgumentNullException(nameof(requestRunnerResolver)); } - public async Task CreateRepeater(RepeaterOptions? options = default) + public Task CreateRepeater(RepeaterOptions? options = default) { options ??= new RepeaterOptions(); - var repeaterId = await _repeaters.CreateRepeater($"{options.NamePrefix}-{Guid.NewGuid()}", options.Description).ConfigureAwait(false); - - var bus = _busFactory.Create(repeaterId); + var bus = _busFactory.Create(options.NamePrefix); var version = new Version(_configuration.RepeaterVersion); - return new Repeater(repeaterId, bus, version, _loggerFactory.CreateLogger(), _ansiCodeColorizer, _requestRunnerResolver); + return Task.FromResult(new Repeater(bus, version, _loggerFactory.CreateLogger(), _ansiCodeColorizer, _requestRunnerResolver)); } } diff --git a/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs b/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs index 92dd443..a61ba30 100644 --- a/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs +++ b/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.DependencyInjection; using SecTester.Core.Extensions; using SecTester.Core.Utils; -using SecTester.Repeater.Api; using SecTester.Repeater.Bus; using SecTester.Repeater.Runners; @@ -22,7 +21,6 @@ public static IServiceCollection AddSecTesterRepeater(this IServiceCollection co .AddHttpCommandDispatcher() .AddSingleton() .AddScoped() - .AddScoped() .AddScoped() .AddScoped() .AddScoped(sp => diff --git a/src/SecTester.Repeater/README.md b/src/SecTester.Repeater/README.md index e024f73..79f77de 100644 --- a/src/SecTester.Repeater/README.md +++ b/src/SecTester.Repeater/README.md @@ -44,36 +44,6 @@ The `CreateRepeater` method accepts the options described below: | Option | Description | | :--------------------- | ----------------------------------------------------------------------------------------------------- | | `namePrefix` | Enter a name prefix that will be used as a constant part of the unique name. By default, `sectester`. | -| `description` | Set a short description of the Repeater. | -| `requestRunnerOptions` | Custom the request runner settings that will be used to execute requests to your application. | - -The default `requestRunnerOptions` is as follows: - -```json -{ - "timeout": 30000, - "maxContentLength": 100, - "reuseConnection": false, - "allowedMimes": [ - "text/html", - "text/plain", - "text/css", - "text/javascript", - "text/markdown", - "text/xml", - "application/javascript", - "application/x-javascript", - "application/json", - "application/xml", - "application/x-www-form-urlencoded", - "application/msgpack", - "application/ld+json", - "application/graphql" - ] -} -``` - -The `RequestRunnerOptions` exposes the following options that can used to customize the request runner's behavior: [RequestRunnerOptions.cs](https://github.com/NeuraLegion/sectester-net/blob/master/src/SecTester.Repeater/Runners/RequestRunnerOptions.cs) The `Repeater` instance provides the `Start` method. This method is required to establish a connection with the Bright cloud engine and interact with other services. diff --git a/src/SecTester.Repeater/Repeater.cs b/src/SecTester.Repeater/Repeater.cs index 75e2305..8bcbd9e 100644 --- a/src/SecTester.Repeater/Repeater.cs +++ b/src/SecTester.Repeater/Repeater.cs @@ -11,7 +11,6 @@ namespace SecTester.Repeater; public delegate IRequestRunner? RequestRunnerResolver(Protocol key); - public class Repeater : IRepeater { private readonly IRepeaterBus _bus; @@ -21,10 +20,9 @@ public class Repeater : IRepeater private readonly IAnsiCodeColorizer _ansiCodeColorizer; private readonly RequestRunnerResolver _requestRunnersAccessor; - public Repeater(string repeaterId, IRepeaterBus bus, Version version, ILogger logger, + public Repeater(IRepeaterBus bus, Version version, ILogger logger, IAnsiCodeColorizer ansiCodeColorizer, RequestRunnerResolver requestRunnersAccessor) { - RepeaterId = repeaterId ?? throw new ArgumentNullException(nameof(repeaterId)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _version = version ?? throw new ArgumentNullException(nameof(version)); _bus = bus ?? throw new ArgumentNullException(nameof(bus)); @@ -33,7 +31,7 @@ public Repeater(string repeaterId, IRepeaterBus bus, Version version, ILogger HandleIncomingRequest(IncomingRequest message) + private async Task HandleIncomingRequest(IncomingRequest message) { var runner = _requestRunnersAccessor(message.Protocol); diff --git a/src/SecTester.Repeater/RepeaterOptions.cs b/src/SecTester.Repeater/RepeaterOptions.cs index cd3f709..8c0fc67 100644 --- a/src/SecTester.Repeater/RepeaterOptions.cs +++ b/src/SecTester.Repeater/RepeaterOptions.cs @@ -5,7 +5,7 @@ namespace SecTester.Repeater; public record RepeaterOptions { private const int MaxPrefixLength = 44; - private readonly string _namePrefix = "sectester"; + private readonly string _namePrefix = System.Net.Dns.GetHostName(); public string NamePrefix { @@ -19,6 +19,4 @@ public string NamePrefix _namePrefix = value; } } - - public string? Description { get; init; } } diff --git a/src/SecTester.Repeater/SecTester.Repeater.csproj b/src/SecTester.Repeater/SecTester.Repeater.csproj index 4a83f72..2dbb762 100644 --- a/src/SecTester.Repeater/SecTester.Repeater.csproj +++ b/src/SecTester.Repeater/SecTester.Repeater.csproj @@ -14,7 +14,6 @@ - diff --git a/src/SecTester.Runner/SecRunner.cs b/src/SecTester.Runner/SecRunner.cs index deb9878..0c151e0 100644 --- a/src/SecTester.Runner/SecRunner.cs +++ b/src/SecTester.Runner/SecRunner.cs @@ -5,7 +5,6 @@ using SecTester.Core; using SecTester.Core.Extensions; using SecTester.Repeater; -using SecTester.Repeater.Api; using SecTester.Repeater.Extensions; using SecTester.Reporter; using SecTester.Scan; @@ -18,17 +17,15 @@ public class SecRunner : IAsyncDisposable private readonly Configuration _configuration; private readonly IFormatter _formatter; private readonly IRepeaterFactory _repeaterFactory; - private readonly IRepeaters _repeatersManager; private readonly IScanFactory _scanFactory; private IRepeater? _repeater; - public SecRunner(Configuration configuration, IRepeaterFactory repeaterFactory, IScanFactory scanFactory, IRepeaters repeatersManager, + public SecRunner(Configuration configuration, IRepeaterFactory repeaterFactory, IScanFactory scanFactory, IFormatter formatter) { _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); _repeaterFactory = repeaterFactory ?? throw new ArgumentNullException(nameof(repeaterFactory)); _scanFactory = scanFactory ?? throw new ArgumentNullException(nameof(scanFactory)); - _repeatersManager = repeatersManager ?? throw new ArgumentNullException(nameof(repeatersManager)); _formatter = formatter ?? throw new ArgumentNullException(nameof(formatter)); } @@ -80,7 +77,6 @@ public async Task Clear(CancellationToken cancellationToken = default) if (_repeater is not null) { await _repeater.Stop(cancellationToken).ConfigureAwait(false); - await _repeatersManager.DeleteRepeater(_repeater.RepeaterId).ConfigureAwait(false); await _repeater.DisposeAsync().ConfigureAwait(false); } } diff --git a/test/SecTester.Repeater.Tests/Api/DefaultRepeatersTests.cs b/test/SecTester.Repeater.Tests/Api/DefaultRepeatersTests.cs deleted file mode 100644 index 3921232..0000000 --- a/test/SecTester.Repeater.Tests/Api/DefaultRepeatersTests.cs +++ /dev/null @@ -1,58 +0,0 @@ -namespace SecTester.Repeater.Tests.Api; - -public class DefaultRepeatersTests : IDisposable -{ - private const string Id = "99138d92-69db-44cb-952a-1cd9ec031e20"; - - private readonly ICommandDispatcher _commandDispatcher; - private readonly DefaultRepeaters _sut; - - public DefaultRepeatersTests() - { - _commandDispatcher = Substitute.For(); - _sut = new DefaultRepeaters(_commandDispatcher); - } - - public void Dispose() - { - _commandDispatcher.ClearSubstitute(); - GC.SuppressFinalize(this); - } - - [Fact] - public async Task CreateRepeater_CreatesRepeater() - { - // arrange - _commandDispatcher.Execute(Arg.Any()).Returns(new RepeaterIdentity(Id)); - - // act - var result = await _sut.CreateRepeater("foo"); - - // assert - result.Should().Be(Id); - } - - [Fact] - public async Task CreateRepeater_ThrowsError() - { - // arrange - _commandDispatcher.Execute(Arg.Any()).Returns(new RepeaterIdentity("")); - - // act - var act = () => _sut.CreateRepeater("foo"); - - // assert - await act.Should().ThrowAsync() - .WithMessage("Cannot create repeater"); - } - - [Fact] - public async Task DeleteRepeater_RemovesRepeater() - { - // act - await _sut.DeleteRepeater("foo"); - - // assert - await _commandDispatcher.Received(1).Execute(Arg.Any()); - } -} diff --git a/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs b/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs index 842c0c7..d85d186 100644 --- a/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs +++ b/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs @@ -2,7 +2,6 @@ namespace SecTester.Repeater.Tests.Bus; public class DefaultRepeaterBusFactoryTests : IDisposable { - private const string Id = "g5MvgM74sweGcK1U6hvs76"; private const string Hostname = "app.brightsec.com"; private const string Token = "0zmcwpe.nexr.0vlon8mp7lvxzjuvgjy88olrhadhiukk"; @@ -32,7 +31,7 @@ public async Task Create_CreatesBus() DefaultRepeaterBusFactory sut = new(config, _loggerFactory, _serviceScopeFactory); // act - await using var bus = sut.Create(Id); + await using var bus = sut.Create(); // assert bus.Should().BeAssignableTo(); @@ -48,7 +47,7 @@ public async Task Create_CredentialsNotDefined_ThrowsError() // act var act = async () => { - await using var _ = sut.Create(Id); + await using var _ = sut.Create(); }; // assert diff --git a/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs b/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs index 17351e8..a757883 100644 --- a/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs +++ b/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs @@ -151,13 +151,41 @@ public async Task Connect_ShouldSendPingMessage() public async Task Deploy_Success() { // arrange + _socketIoMessage.GetValue().Returns(new RepeaterInfo(RepeaterId)); + _connection.On("deployed", Arg.Invoke(_socketIoMessage)); + + // act + await _sut.Deploy(null); + + // assert + await _connection.Received().EmitAsync("deploy", Arg.Is(x => x.Length == 0)); + } + + [Fact] + public async Task Deploy_GivenRepeaterId_Success() + { + // arrange + _socketIoMessage.GetValue().Returns(new RepeaterInfo(RepeaterId)); _connection.On("deployed", Arg.Invoke(_socketIoMessage)); // act await _sut.Deploy(RepeaterId); // assert - await _connection.Received().EmitAsync("deploy", Arg.Is(x => x.RepeaterId.Equals(RepeaterId, StringComparison.OrdinalIgnoreCase))); + await _connection.Received().EmitAsync("deploy", Arg.Is(x => x.Length == 1 && ((x[0] as RepeaterInfo)!).RepeaterId == RepeaterId)); + } + + [Fact] + public async Task Deploy_NotReceivingRepeaterId_ThrowsError() + { + // arrange + _connection.On("deployed", Arg.Invoke(_socketIoMessage)); + + // act + var act = () => _sut.Deploy(null); + + // assert + await act.Should().ThrowAsync().WithMessage("An error occured while repeater is being deployed"); } [Fact] @@ -168,7 +196,7 @@ public async Task Deploy_GivenCancellationToken_ThrowsError() cancellationTokenSource.Cancel(); // act - var act = () => _sut.Deploy(RepeaterId, cancellationTokenSource.Token); + var act = () => _sut.Deploy(null, cancellationTokenSource.Token); // assert await act.Should().ThrowAsync(); diff --git a/test/SecTester.Repeater.Tests/DefaultRepeaterFactoryTests.cs b/test/SecTester.Repeater.Tests/DefaultRepeaterFactoryTests.cs index 6be5247..f5331a5 100644 --- a/test/SecTester.Repeater.Tests/DefaultRepeaterFactoryTests.cs +++ b/test/SecTester.Repeater.Tests/DefaultRepeaterFactoryTests.cs @@ -3,13 +3,11 @@ namespace SecTester.Repeater.Tests; public class DefaultRepeaterFactoryTests : IDisposable { private const string Id = "99138d92-69db-44cb-952a-1cd9ec031e20"; - private const string DefaultNamePrefix = "sectester"; private const string Hostname = "app.brightsec.com"; private readonly IRepeaterBusFactory _busFactory = Substitute.For(); private readonly Configuration _configuration = new(Hostname); - private readonly IRepeaters _repeaters = Substitute.For(); private readonly ILoggerFactory _loggerFactory = Substitute.For(); private readonly IAnsiCodeColorizer _ansiCodeColorizer = Substitute.For(); private readonly RequestRunnerResolver _resolver = Substitute.For(); @@ -18,14 +16,13 @@ public class DefaultRepeaterFactoryTests : IDisposable public DefaultRepeaterFactoryTests() { - _sut = new DefaultRepeaterFactory(_repeaters, _busFactory, _configuration, _loggerFactory, _ansiCodeColorizer, _resolver); + _sut = new DefaultRepeaterFactory(_busFactory, _configuration, _loggerFactory, _ansiCodeColorizer, _resolver); } public void Dispose() { _ansiCodeColorizer.ClearSubstitute(); _busFactory.ClearSubstitute(); - _repeaters.ClearSubstitute(); _loggerFactory.ClearSubstitute(); _resolver.ClearSubstitute(); GC.SuppressFinalize(this); @@ -35,19 +32,14 @@ public void Dispose() public async Task CreateRepeater_CreatesRepeater() { // arrange - _repeaters.CreateRepeater(Arg.Is(s => s.Contains(DefaultNamePrefix))) - .Returns(Id); + var defaultNamePrefix = Dns.GetHostName(); // act await using var repeater = await _sut.CreateRepeater(); // assert repeater.Should().BeOfType(); - repeater.Should().BeEquivalentTo( - new - { - RepeaterId = Id - }); + _busFactory.Received().Create(Arg.Is(s => s.Contains(defaultNamePrefix))); } [Fact] @@ -56,21 +48,14 @@ public async Task CreateRepeater_GivenOptions_CreatesRepeater() // arrange var options = new RepeaterOptions { - NamePrefix = "foo", - Description = "bar" + NamePrefix = "foo" }; - _repeaters.CreateRepeater(Arg.Is(s => s.Contains(options.NamePrefix)), options.Description) - .Returns(Id); // act await using var repeater = await _sut.CreateRepeater(options); // assert repeater.Should().BeOfType(); - repeater.Should().BeEquivalentTo( - new - { - RepeaterId = Id - }); + _busFactory.Received().Create(Arg.Is(s => s.Contains("foo"))); } } diff --git a/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs b/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs index 91e4b39..1baa2de 100644 --- a/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs +++ b/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs @@ -20,16 +20,6 @@ public void AddSecTesterRepeater_RegistersRepeaterFactory() _sut.Received().AddScoped(); } - [Fact] - public void AddSecTesterRepeater_RegistersRepeaters() - { - // act - _sut.AddSecTesterRepeater(); - - // assert - _sut.Received().AddScoped(); - } - [Fact] public void AddSecTesterRepeater_RegistersTimerProvider() { diff --git a/test/SecTester.Repeater.Tests/RepeaterOptionsTests.cs b/test/SecTester.Repeater.Tests/RepeaterOptionsTests.cs index 2f3aed7..6a2ddf8 100644 --- a/test/SecTester.Repeater.Tests/RepeaterOptionsTests.cs +++ b/test/SecTester.Repeater.Tests/RepeaterOptionsTests.cs @@ -48,7 +48,7 @@ public void RepeaterOptions_NamePrefixIsNotPassed_KeepsDefaultValue() result.Should().BeEquivalentTo( new { - NamePrefix = "sectester" + NamePrefix = System.Net.Dns.GetHostName() }); } } diff --git a/test/SecTester.Repeater.Tests/RepeaterTests.cs b/test/SecTester.Repeater.Tests/RepeaterTests.cs index 8b72118..9f55bbf 100644 --- a/test/SecTester.Repeater.Tests/RepeaterTests.cs +++ b/test/SecTester.Repeater.Tests/RepeaterTests.cs @@ -15,7 +15,7 @@ public class RepeaterTests : IDisposable, IAsyncDisposable public RepeaterTests() { var version = new Version(Version); - _sut = new Repeater(Id, _bus, version, _logger, _ansiCodeColorizer, _resolver); + _sut = new Repeater(_bus, version, _logger, _ansiCodeColorizer, _resolver); } public async ValueTask DisposeAsync() @@ -39,11 +39,14 @@ public void Dispose() [Fact] public async Task Start_DeploysItself() { + // arrange + _bus.Deploy(null, Arg.Any()).Returns(Task.FromResult(Id)); + // act await _sut.Start(); // assert - await _bus.Received().Deploy(Id, Arg.Any()); + _sut.RepeaterId.Should().Be(Id); } [Fact] diff --git a/test/SecTester.Repeater.Tests/SecTester.Repeater.Tests.csproj b/test/SecTester.Repeater.Tests/SecTester.Repeater.Tests.csproj index ad694f3..a525a41 100644 --- a/test/SecTester.Repeater.Tests/SecTester.Repeater.Tests.csproj +++ b/test/SecTester.Repeater.Tests/SecTester.Repeater.Tests.csproj @@ -9,7 +9,6 @@ - diff --git a/test/SecTester.Repeater.Tests/Usings.cs b/test/SecTester.Repeater.Tests/Usings.cs index 6b72f15..5b7390a 100644 --- a/test/SecTester.Repeater.Tests/Usings.cs +++ b/test/SecTester.Repeater.Tests/Usings.cs @@ -23,7 +23,6 @@ global using SecTester.Core.RetryStrategies; global using SecTester.Core.Logger; global using SecTester.Core.Utils; -global using SecTester.Repeater.Api; global using SecTester.Repeater.Bus; global using SecTester.Repeater.Extensions; global using SecTester.Repeater.Runners; diff --git a/test/SecTester.Runner.Tests/SecRunnerTests.cs b/test/SecTester.Runner.Tests/SecRunnerTests.cs index e5d504e..61b0e0a 100644 --- a/test/SecTester.Runner.Tests/SecRunnerTests.cs +++ b/test/SecTester.Runner.Tests/SecRunnerTests.cs @@ -1,7 +1,6 @@ using NSubstitute.ExceptionExtensions; using SecTester.Core; using SecTester.Repeater; -using SecTester.Repeater.Api; namespace SecTester.Runner.Tests; @@ -16,7 +15,6 @@ public class SecRunnerTests private readonly RepeaterOptions _options = new(); private readonly IRepeater _repeater = Substitute.For(); private readonly IRepeaterFactory _repeaterFactory = Substitute.For(); - private readonly IRepeaters _repeatersManager = Substitute.For(); private readonly IScanFactory _scanFactory = Substitute.For(); private readonly ICredentialProvider _credentialProvider = Substitute.For(); @@ -26,7 +24,7 @@ public SecRunnerTests() { _repeater.RepeaterId.Returns(RepeaterId); _repeaterFactory.CreateRepeater(_options).Returns(_repeater); - _sut = new SecRunner(_configuration, _repeaterFactory, _scanFactory, _repeatersManager, _formatter); + _sut = new SecRunner(_configuration, _repeaterFactory, _scanFactory, _formatter); } [Fact] @@ -119,19 +117,6 @@ public async Task Clear_StopRepeater() await _repeater.Received(1).Stop(); } - [Fact] - public async Task Clear_DeletesRepeater() - { - // arrange - await _sut.Init(_options); - - // act - await _sut.Clear(); - - // assert - await _repeatersManager.Received(1).DeleteRepeater(RepeaterId); - } - [Fact] public async Task Clear_RepeaterRemoved_RepeaterIsNotDefined() { @@ -216,19 +201,6 @@ public void CreateScan_NotInitializedYet_ThrowsException() act.Should().Throw().WithMessage("Must be initialized first."); } - [Fact] - public async Task DisposeAsync_RemoveRepeaters() - { - // arrange - await _sut.Init(_options); - - // act - await _sut.DisposeAsync(); - - // assert - await _repeatersManager.Received(1).DeleteRepeater(RepeaterId); - } - [Fact] public async Task DisposeAsync_StopsRepeater() { diff --git a/test/SecTester.Scan.Tests/Usings.cs b/test/SecTester.Scan.Tests/Usings.cs index 94bcba8..1cdced7 100644 --- a/test/SecTester.Scan.Tests/Usings.cs +++ b/test/SecTester.Scan.Tests/Usings.cs @@ -12,10 +12,10 @@ global using NSubstitute.ExceptionExtensions; global using SecTester.Core; global using SecTester.Core.Bus; -global using SecTester.Core.Exceptions; -global using SecTester.Core.Utils; global using SecTester.Core.Dispatchers; +global using SecTester.Core.Exceptions; global using SecTester.Core.Extensions; +global using SecTester.Core.Utils; global using SecTester.Scan.CI; global using SecTester.Scan.Commands; global using SecTester.Scan.Exceptions; From 48b671fdddb8cd27870fecca5f0ab7e9f026e38e Mon Sep 17 00:00:00 2001 From: Dmitry Ostrikov Date: Mon, 17 Jun 2024 18:27:49 +0700 Subject: [PATCH 3/6] feat(repeater): adopt self-deploying repeaters closes #174 --- src/SecTester.Core/Dispatchers/HttpCommandDispatcher.cs | 2 +- src/SecTester.Repeater/README.md | 6 +++--- .../Extensions/HttpResponseMessageExtensionsTests.cs | 1 - test/SecTester.Core.Tests/Usings.cs | 3 +-- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/SecTester.Core/Dispatchers/HttpCommandDispatcher.cs b/src/SecTester.Core/Dispatchers/HttpCommandDispatcher.cs index 14dd140..fba7fb8 100644 --- a/src/SecTester.Core/Dispatchers/HttpCommandDispatcher.cs +++ b/src/SecTester.Core/Dispatchers/HttpCommandDispatcher.cs @@ -6,9 +6,9 @@ using System.Threading; using System.Threading.Tasks; using SecTester.Core.Bus; -using SecTester.Core.Utils; using SecTester.Core.Commands; using SecTester.Core.Extensions; +using SecTester.Core.Utils; namespace SecTester.Core.Dispatchers; diff --git a/src/SecTester.Repeater/README.md b/src/SecTester.Repeater/README.md index 79f77de..d726ff5 100644 --- a/src/SecTester.Repeater/README.md +++ b/src/SecTester.Repeater/README.md @@ -41,9 +41,9 @@ await using var repeater = await repeaterFactory.CreateRepeater(new RepeaterOpti The `CreateRepeater` method accepts the options described below: -| Option | Description | -| :--------------------- | ----------------------------------------------------------------------------------------------------- | -| `namePrefix` | Enter a name prefix that will be used as a constant part of the unique name. By default, `sectester`. | +| Option | Description | +| :--------------------- |--------------------------------------------------------------------------------------------------------------------| +| `namePrefix` | Enter a name prefix that will be used as a constant part of the unique name. By default, the host name value used. | The `Repeater` instance provides the `Start` method. This method is required to establish a connection with the Bright cloud engine and interact with other services. diff --git a/test/SecTester.Core.Tests/Extensions/HttpResponseMessageExtensionsTests.cs b/test/SecTester.Core.Tests/Extensions/HttpResponseMessageExtensionsTests.cs index d91a720..0564d3d 100644 --- a/test/SecTester.Core.Tests/Extensions/HttpResponseMessageExtensionsTests.cs +++ b/test/SecTester.Core.Tests/Extensions/HttpResponseMessageExtensionsTests.cs @@ -1,5 +1,4 @@ using System.Net; -using System.Text; namespace SecTester.Core.Tests.Extensions; diff --git a/test/SecTester.Core.Tests/Usings.cs b/test/SecTester.Core.Tests/Usings.cs index bc6ca1d..9855ea5 100644 --- a/test/SecTester.Core.Tests/Usings.cs +++ b/test/SecTester.Core.Tests/Usings.cs @@ -21,8 +21,7 @@ global using SecTester.Core.Dispatchers; global using SecTester.Core.Exceptions; global using SecTester.Core.Extensions; -global using SecTester.Core.RetryStrategies; -global using SecTester.Core.Exceptions; global using SecTester.Core.Logger; +global using SecTester.Core.RetryStrategies; global using SecTester.Core.Utils; global using Xunit; From 587225457b804d74748b0de09c6e15cc5bcaa93f Mon Sep 17 00:00:00 2001 From: Dmitry Ostrikov Date: Mon, 17 Jun 2024 18:31:59 +0700 Subject: [PATCH 4/6] feat(repeater): fix test closes #174 --- test/SecTester.Repeater.Tests/RepeaterTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/SecTester.Repeater.Tests/RepeaterTests.cs b/test/SecTester.Repeater.Tests/RepeaterTests.cs index 9f55bbf..cce68f4 100644 --- a/test/SecTester.Repeater.Tests/RepeaterTests.cs +++ b/test/SecTester.Repeater.Tests/RepeaterTests.cs @@ -40,7 +40,7 @@ public void Dispose() public async Task Start_DeploysItself() { // arrange - _bus.Deploy(null, Arg.Any()).Returns(Task.FromResult(Id)); + _bus.Deploy(Arg.Any(), Arg.Any()).Returns(Task.FromResult(Id)); // act await _sut.Start(); From 0db132139e41fc89c538665946d4d721326a40a1 Mon Sep 17 00:00:00 2001 From: Dmitry Ostrikov Date: Tue, 18 Jun 2024 16:26:06 +0700 Subject: [PATCH 5/6] feat(repeater): get rid if same repeater re-deployment closes #174 --- src/SecTester.Repeater/Bus/IRepeaterBus.cs | 2 +- .../Bus/SocketIoRepeaterBus.cs | 6 ++---- src/SecTester.Repeater/Repeater.cs | 2 +- .../Bus/SocketIoRepeaterBusTests.cs | 16 +--------------- test/SecTester.Repeater.Tests/RepeaterTests.cs | 2 +- 5 files changed, 6 insertions(+), 22 deletions(-) diff --git a/src/SecTester.Repeater/Bus/IRepeaterBus.cs b/src/SecTester.Repeater/Bus/IRepeaterBus.cs index cb8113b..53ddda1 100644 --- a/src/SecTester.Repeater/Bus/IRepeaterBus.cs +++ b/src/SecTester.Repeater/Bus/IRepeaterBus.cs @@ -11,5 +11,5 @@ public interface IRepeaterBus : IAsyncDisposable event Action UpgradeAvailable; Task Connect(); - Task Deploy(string? repeaterId, CancellationToken? cancellationToken = null); + Task Deploy(CancellationToken? cancellationToken = null); } diff --git a/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs b/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs index 7a4d167..d91dd59 100644 --- a/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs +++ b/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs @@ -90,7 +90,7 @@ public async ValueTask DisposeAsync() GC.SuppressFinalize(this); } - public async Task Deploy(string? repeaterId, CancellationToken? cancellationToken = null) + public async Task Deploy(CancellationToken? cancellationToken = null) { try { @@ -98,10 +98,8 @@ public async Task Deploy(string? repeaterId, CancellationToken? cancella _connection.On("deployed", response => tcs.TrySetResult(response.GetValue())); - var args = string.IsNullOrWhiteSpace(repeaterId) ? Array.Empty() : new object[] { new RepeaterInfo(repeaterId!) }; - await _connection - .EmitAsync("deploy", args) + .EmitAsync("deploy", Array.Empty()) .ConfigureAwait(false); using var _ = cancellationToken?.Register(() => tcs.TrySetCanceled()); diff --git a/src/SecTester.Repeater/Repeater.cs b/src/SecTester.Repeater/Repeater.cs index 8bcbd9e..f3fa84f 100644 --- a/src/SecTester.Repeater/Repeater.cs +++ b/src/SecTester.Repeater/Repeater.cs @@ -58,7 +58,7 @@ public async Task Start(CancellationToken cancellationToken = default) SubscribeToEvents(); await _bus.Connect().ConfigureAwait(false); - RepeaterId = await _bus.Deploy(RepeaterId, cancellationToken).ConfigureAwait(false); + RepeaterId = await _bus.Deploy(cancellationToken).ConfigureAwait(false); Status = RunningStatus.Running; } diff --git a/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs b/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs index a757883..7825366 100644 --- a/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs +++ b/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs @@ -161,20 +161,6 @@ public async Task Deploy_Success() await _connection.Received().EmitAsync("deploy", Arg.Is(x => x.Length == 0)); } - [Fact] - public async Task Deploy_GivenRepeaterId_Success() - { - // arrange - _socketIoMessage.GetValue().Returns(new RepeaterInfo(RepeaterId)); - _connection.On("deployed", Arg.Invoke(_socketIoMessage)); - - // act - await _sut.Deploy(RepeaterId); - - // assert - await _connection.Received().EmitAsync("deploy", Arg.Is(x => x.Length == 1 && ((x[0] as RepeaterInfo)!).RepeaterId == RepeaterId)); - } - [Fact] public async Task Deploy_NotReceivingRepeaterId_ThrowsError() { @@ -196,7 +182,7 @@ public async Task Deploy_GivenCancellationToken_ThrowsError() cancellationTokenSource.Cancel(); // act - var act = () => _sut.Deploy(null, cancellationTokenSource.Token); + var act = () => _sut.Deploy(cancellationTokenSource.Token); // assert await act.Should().ThrowAsync(); diff --git a/test/SecTester.Repeater.Tests/RepeaterTests.cs b/test/SecTester.Repeater.Tests/RepeaterTests.cs index cce68f4..6a2708e 100644 --- a/test/SecTester.Repeater.Tests/RepeaterTests.cs +++ b/test/SecTester.Repeater.Tests/RepeaterTests.cs @@ -40,7 +40,7 @@ public void Dispose() public async Task Start_DeploysItself() { // arrange - _bus.Deploy(Arg.Any(), Arg.Any()).Returns(Task.FromResult(Id)); + _bus.Deploy(Arg.Any()).Returns(Task.FromResult(Id)); // act await _sut.Start(); From 9d2577c09855937a1156094c78610adcc57fcabe Mon Sep 17 00:00:00 2001 From: Dmitry Ostrikov Date: Wed, 19 Jun 2024 17:05:01 +0700 Subject: [PATCH 6/6] revert(repeater): get rid if same repeater re-deployment This reverts commit 0db132139e41fc89c538665946d4d721326a40a1. --- src/SecTester.Repeater/Bus/IRepeaterBus.cs | 2 +- .../Bus/SocketIoRepeaterBus.cs | 6 ++++-- src/SecTester.Repeater/Repeater.cs | 2 +- .../Bus/SocketIoRepeaterBusTests.cs | 16 +++++++++++++++- test/SecTester.Repeater.Tests/RepeaterTests.cs | 2 +- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/SecTester.Repeater/Bus/IRepeaterBus.cs b/src/SecTester.Repeater/Bus/IRepeaterBus.cs index 53ddda1..cb8113b 100644 --- a/src/SecTester.Repeater/Bus/IRepeaterBus.cs +++ b/src/SecTester.Repeater/Bus/IRepeaterBus.cs @@ -11,5 +11,5 @@ public interface IRepeaterBus : IAsyncDisposable event Action UpgradeAvailable; Task Connect(); - Task Deploy(CancellationToken? cancellationToken = null); + Task Deploy(string? repeaterId, CancellationToken? cancellationToken = null); } diff --git a/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs b/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs index d91dd59..7a4d167 100644 --- a/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs +++ b/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs @@ -90,7 +90,7 @@ public async ValueTask DisposeAsync() GC.SuppressFinalize(this); } - public async Task Deploy(CancellationToken? cancellationToken = null) + public async Task Deploy(string? repeaterId, CancellationToken? cancellationToken = null) { try { @@ -98,8 +98,10 @@ public async Task Deploy(CancellationToken? cancellationToken = null) _connection.On("deployed", response => tcs.TrySetResult(response.GetValue())); + var args = string.IsNullOrWhiteSpace(repeaterId) ? Array.Empty() : new object[] { new RepeaterInfo(repeaterId!) }; + await _connection - .EmitAsync("deploy", Array.Empty()) + .EmitAsync("deploy", args) .ConfigureAwait(false); using var _ = cancellationToken?.Register(() => tcs.TrySetCanceled()); diff --git a/src/SecTester.Repeater/Repeater.cs b/src/SecTester.Repeater/Repeater.cs index f3fa84f..8bcbd9e 100644 --- a/src/SecTester.Repeater/Repeater.cs +++ b/src/SecTester.Repeater/Repeater.cs @@ -58,7 +58,7 @@ public async Task Start(CancellationToken cancellationToken = default) SubscribeToEvents(); await _bus.Connect().ConfigureAwait(false); - RepeaterId = await _bus.Deploy(cancellationToken).ConfigureAwait(false); + RepeaterId = await _bus.Deploy(RepeaterId, cancellationToken).ConfigureAwait(false); Status = RunningStatus.Running; } diff --git a/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs b/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs index 7825366..a757883 100644 --- a/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs +++ b/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs @@ -161,6 +161,20 @@ public async Task Deploy_Success() await _connection.Received().EmitAsync("deploy", Arg.Is(x => x.Length == 0)); } + [Fact] + public async Task Deploy_GivenRepeaterId_Success() + { + // arrange + _socketIoMessage.GetValue().Returns(new RepeaterInfo(RepeaterId)); + _connection.On("deployed", Arg.Invoke(_socketIoMessage)); + + // act + await _sut.Deploy(RepeaterId); + + // assert + await _connection.Received().EmitAsync("deploy", Arg.Is(x => x.Length == 1 && ((x[0] as RepeaterInfo)!).RepeaterId == RepeaterId)); + } + [Fact] public async Task Deploy_NotReceivingRepeaterId_ThrowsError() { @@ -182,7 +196,7 @@ public async Task Deploy_GivenCancellationToken_ThrowsError() cancellationTokenSource.Cancel(); // act - var act = () => _sut.Deploy(cancellationTokenSource.Token); + var act = () => _sut.Deploy(null, cancellationTokenSource.Token); // assert await act.Should().ThrowAsync(); diff --git a/test/SecTester.Repeater.Tests/RepeaterTests.cs b/test/SecTester.Repeater.Tests/RepeaterTests.cs index 6a2708e..cce68f4 100644 --- a/test/SecTester.Repeater.Tests/RepeaterTests.cs +++ b/test/SecTester.Repeater.Tests/RepeaterTests.cs @@ -40,7 +40,7 @@ public void Dispose() public async Task Start_DeploysItself() { // arrange - _bus.Deploy(Arg.Any()).Returns(Task.FromResult(Id)); + _bus.Deploy(Arg.Any(), Arg.Any()).Returns(Task.FromResult(Id)); // act await _sut.Start();