From ceb9b05d056d567ec7b7c60108f6ea602cd9329e Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Wed, 1 Nov 2023 17:24:40 +0100 Subject: [PATCH] fix: Retain the internal Couchbase builder configuration if the user overrides the default configuration (#1040) --- .../CouchbaseBuilder.cs | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/Testcontainers.Couchbase/CouchbaseBuilder.cs b/src/Testcontainers.Couchbase/CouchbaseBuilder.cs index 53048cbd6..d5892a0ee 100644 --- a/src/Testcontainers.Couchbase/CouchbaseBuilder.cs +++ b/src/Testcontainers.Couchbase/CouchbaseBuilder.cs @@ -41,11 +41,19 @@ public sealed class CouchbaseBuilder : ContainerBuilder _basicAuthenticationHeader = new KeyValuePair("Authorization", "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(string.Join(":", DefaultUsername, DefaultPassword)))); + private static readonly KeyValuePair BasicAuthenticationHeader = new KeyValuePair("Authorization", "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(string.Join(":", DefaultUsername, DefaultPassword)))); - private readonly IWaitUntil _waitUntilNodeIsReady = new HttpWaitStrategy().ForPath("/pools").ForPort(MgmtPort); + private static readonly IWaitUntil WaitUntilNodeIsReady = new HttpWaitStrategy().ForPath("/pools").ForPort(MgmtPort); - private readonly ISet _enabledServices = new HashSet(); + private static readonly ISet EnabledServices = new HashSet(); + + static CouchbaseBuilder() + { + EnabledServices.Add(CouchbaseService.Data); + EnabledServices.Add(CouchbaseService.Index); + EnabledServices.Add(CouchbaseService.Query); + EnabledServices.Add(CouchbaseService.Search); + } /// /// Initializes a new instance of the class. @@ -54,11 +62,6 @@ public CouchbaseBuilder() : this(new CouchbaseConfiguration()) { DockerResourceConfiguration = Init().DockerResourceConfiguration; - - _enabledServices.Add(CouchbaseService.Data); - _enabledServices.Add(CouchbaseService.Index); - _enabledServices.Add(CouchbaseService.Query); - _enabledServices.Add(CouchbaseService.Search); } /// @@ -81,41 +84,41 @@ public override CouchbaseContainer Build() var waitStrategy = Wait.ForUnixContainer(); - if (_enabledServices.Any()) + if (EnabledServices.Any()) { waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request => request .ForPath("/pools/default") .ForPort(MgmtPort) .ForResponseMessageMatching(IsNodeHealthyAsync) - .WithHeader(_basicAuthenticationHeader.Key, _basicAuthenticationHeader.Value)); + .WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value)); } - if (_enabledServices.Contains(CouchbaseService.Query)) + if (EnabledServices.Contains(CouchbaseService.Query)) { waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request => request .ForPath("/admin/ping") .ForPort(QueryPort) - .WithHeader(_basicAuthenticationHeader.Key, _basicAuthenticationHeader.Value)); + .WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value)); } - if (_enabledServices.Contains(CouchbaseService.Analytics)) + if (EnabledServices.Contains(CouchbaseService.Analytics)) { waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request => request .ForPath("/admin/ping") .ForPort(AnalyticsPort) - .WithHeader(_basicAuthenticationHeader.Key, _basicAuthenticationHeader.Value)); + .WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value)); } - if (_enabledServices.Contains(CouchbaseService.Eventing)) + if (EnabledServices.Contains(CouchbaseService.Eventing)) { waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request => request .ForPath("/api/v1/config") .ForPort(EventingPort) - .WithHeader(_basicAuthenticationHeader.Key, _basicAuthenticationHeader.Value)); + .WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value)); } var couchbaseBuilder = DockerResourceConfiguration.WaitStrategies.Count() > 1 ? this : WithWaitStrategy(waitStrategy); @@ -180,7 +183,7 @@ private CouchbaseBuilder WithBucket(params CouchbaseBucket[] bucket) /// Cancellation token. private async Task ConfigureCouchbaseAsync(IContainer container, CancellationToken ct = default) { - await WaitStrategy.WaitUntilAsync(() => _waitUntilNodeIsReady.UntilAsync(container), TimeSpan.FromSeconds(2), TimeSpan.FromMinutes(5), ct) + await WaitStrategy.WaitUntilAsync(() => WaitUntilNodeIsReady.UntilAsync(container), TimeSpan.FromSeconds(2), TimeSpan.FromMinutes(5), ct) .ConfigureAwait(false); using (var httpClient = new HttpClient()) @@ -197,7 +200,7 @@ await EnsureSuccessStatusCodeAsync(response) } } - using (var request = new SetupNodeServicesRequest(_enabledServices.ToArray())) + using (var request = new SetupNodeServicesRequest(EnabledServices.ToArray())) { using (var response = await httpClient.SendAsync(request, ct) .ConfigureAwait(false)) @@ -207,7 +210,7 @@ await EnsureSuccessStatusCodeAsync(response) } } - using (var request = new SetupMemoryQuotasRequest(_enabledServices.ToArray())) + using (var request = new SetupMemoryQuotasRequest(EnabledServices.ToArray())) { using (var response = await httpClient.SendAsync(request, ct) .ConfigureAwait(false)) @@ -217,7 +220,7 @@ await EnsureSuccessStatusCodeAsync(response) } } - using (var request = new ConfigureExternalAddressesRequest(container, _enabledServices.ToArray())) + using (var request = new ConfigureExternalAddressesRequest(container, EnabledServices.ToArray())) { using (var response = await httpClient.SendAsync(request, ct) .ConfigureAwait(false)) @@ -262,7 +265,7 @@ await EnsureSuccessStatusCodeAsync(response) .ForPath("/pools/default/buckets/" + bucket.Name) .ForPort(MgmtPort) .ForResponseMessageMatching(AllServicesEnabledAsync) - .WithHeader(_basicAuthenticationHeader.Key, _basicAuthenticationHeader.Value))) + .WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value))) .Build() .Last(); @@ -326,7 +329,7 @@ private async Task AllServicesEnabledAsync(HttpResponseMessage response) .Select(service => service.GetString()) .Where(service => service != null); - return _enabledServices.All(enabledService => services.Any(service => service.StartsWith(enabledService.Identifier))); + return EnabledServices.All(enabledService => services.Any(service => service.StartsWith(enabledService.Identifier))); } catch {