|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Amazon.ServiceDiscovery; |
| 5 | +using Amazon.ServiceDiscovery.Model; |
| 6 | +using Testcontainers.Floci; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Testcontainers.Floci.Tests; |
| 10 | + |
| 11 | +public sealed class CloudMapServiceTest : IAsyncLifetime |
| 12 | +{ |
| 13 | + private readonly FlociContainer _floci = new FlociBuilder(TestImages.Floci) |
| 14 | + .WithCloudMap(new CloudMapConfig()) |
| 15 | + .Build(); |
| 16 | + |
| 17 | + public Task InitializeAsync() => _floci.StartAsync(); |
| 18 | + |
| 19 | + public Task DisposeAsync() => _floci.DisposeAsync().AsTask(); |
| 20 | + |
| 21 | + private AmazonServiceDiscoveryClient CreateClient() |
| 22 | + { |
| 23 | + return new AmazonServiceDiscoveryClient( |
| 24 | + _floci.AccessKey, |
| 25 | + _floci.SecretKey, |
| 26 | + new AmazonServiceDiscoveryConfig |
| 27 | + { |
| 28 | + ServiceURL = _floci.GetEndpoint(), |
| 29 | + AuthenticationRegion = _floci.Region, |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task RegistersAndDiscoversInstance() |
| 35 | + { |
| 36 | + using var sd = CreateClient(); |
| 37 | + |
| 38 | + const string namespaceName = "test-namespace"; |
| 39 | + const string serviceName = "test-service"; |
| 40 | + const string instanceId = "inst-1"; |
| 41 | + |
| 42 | + string? namespaceId = null; |
| 43 | + string? serviceId = null; |
| 44 | + |
| 45 | + try |
| 46 | + { |
| 47 | + // Create an HTTP namespace — returns an OperationId because it's async. |
| 48 | + var createNsResponse = await sd.CreateHttpNamespaceAsync(new CreateHttpNamespaceRequest |
| 49 | + { |
| 50 | + Name = namespaceName, |
| 51 | + }); |
| 52 | + |
| 53 | + var namespaceOperationId = createNsResponse.OperationId; |
| 54 | + |
| 55 | + // Poll until SUCCESS. With OperationCompletionDelaySeconds=0 this is immediate, but |
| 56 | + // we loop a few times with a short back-off to handle any in-flight processing. |
| 57 | + namespaceId = await PollOperationToSuccessAsync(sd, namespaceOperationId, "NAMESPACE"); |
| 58 | + |
| 59 | + // Create a service within the namespace. |
| 60 | + var createSvcResponse = await sd.CreateServiceAsync(new CreateServiceRequest |
| 61 | + { |
| 62 | + Name = serviceName, |
| 63 | + NamespaceId = namespaceId, |
| 64 | + }); |
| 65 | + |
| 66 | + serviceId = createSvcResponse.Service.Id; |
| 67 | + |
| 68 | + // Register an instance. |
| 69 | + var registerResponse = await sd.RegisterInstanceAsync(new RegisterInstanceRequest |
| 70 | + { |
| 71 | + ServiceId = serviceId, |
| 72 | + InstanceId = instanceId, |
| 73 | + Attributes = new Dictionary<string, string> |
| 74 | + { |
| 75 | + ["AWS_INSTANCE_IPV4"] = "10.0.0.1", |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + await PollOperationToSuccessAsync(sd, registerResponse.OperationId, target: null); |
| 80 | + |
| 81 | + // List instances via the control-plane API and assert the registered one is present. |
| 82 | + // (DiscoverInstancesAsync targets a separate data-plane endpoint that isn't reachable |
| 83 | + // from the emulator's single ServiceURL; ListInstancesAsync uses the same endpoint.) |
| 84 | + var listResponse = await sd.ListInstancesAsync(new ListInstancesRequest |
| 85 | + { |
| 86 | + ServiceId = serviceId, |
| 87 | + }); |
| 88 | + |
| 89 | + Assert.Contains(listResponse.Instances, i => i.Id == instanceId); |
| 90 | + } |
| 91 | + finally |
| 92 | + { |
| 93 | + // Best-effort teardown — ignore failures so the test can report its own result. |
| 94 | + try |
| 95 | + { |
| 96 | + if (serviceId != null) |
| 97 | + { |
| 98 | + var deregResponse = await sd.DeregisterInstanceAsync(new DeregisterInstanceRequest |
| 99 | + { |
| 100 | + ServiceId = serviceId, |
| 101 | + InstanceId = instanceId, |
| 102 | + }); |
| 103 | + await PollOperationToSuccessAsync(sd, deregResponse.OperationId, target: null); |
| 104 | + } |
| 105 | + } |
| 106 | + catch { /* best-effort */ } |
| 107 | + |
| 108 | + try |
| 109 | + { |
| 110 | + if (serviceId != null) |
| 111 | + { |
| 112 | + await sd.DeleteServiceAsync(new DeleteServiceRequest { Id = serviceId }); |
| 113 | + } |
| 114 | + } |
| 115 | + catch { /* best-effort */ } |
| 116 | + |
| 117 | + try |
| 118 | + { |
| 119 | + if (namespaceId != null) |
| 120 | + { |
| 121 | + await sd.DeleteNamespaceAsync(new DeleteNamespaceRequest { Id = namespaceId }); |
| 122 | + } |
| 123 | + } |
| 124 | + catch { /* best-effort */ } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Polls <see cref="AmazonServiceDiscoveryClient.GetOperationAsync" /> until the operation |
| 130 | + /// reaches <see cref="OperationStatus.SUCCESS" /> and returns the value of |
| 131 | + /// <paramref name="target" /> from <c>Operation.Targets</c> (or <see langword="null" /> when |
| 132 | + /// no target value is needed). |
| 133 | + /// </summary> |
| 134 | + private static async Task<string?> PollOperationToSuccessAsync( |
| 135 | + AmazonServiceDiscoveryClient client, |
| 136 | + string operationId, |
| 137 | + string? target) |
| 138 | + { |
| 139 | + for (var attempt = 0; attempt < 20; attempt++) |
| 140 | + { |
| 141 | + var response = await client.GetOperationAsync(new GetOperationRequest |
| 142 | + { |
| 143 | + OperationId = operationId, |
| 144 | + }); |
| 145 | + |
| 146 | + if (response.Operation.Status == OperationStatus.SUCCESS) |
| 147 | + { |
| 148 | + if (target == null) |
| 149 | + { |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + response.Operation.Targets.TryGetValue(target, out var value); |
| 154 | + return value; |
| 155 | + } |
| 156 | + |
| 157 | + if (response.Operation.Status == OperationStatus.FAIL) |
| 158 | + { |
| 159 | + throw new InvalidOperationException( |
| 160 | + $"Cloud Map operation {operationId} failed: {response.Operation.ErrorMessage}"); |
| 161 | + } |
| 162 | + |
| 163 | + await Task.Delay(TimeSpan.FromMilliseconds(500)); |
| 164 | + } |
| 165 | + |
| 166 | + throw new TimeoutException($"Cloud Map operation {operationId} did not reach SUCCESS within the polling limit."); |
| 167 | + } |
| 168 | +} |
0 commit comments