Skip to content

Commit ceb9b05

Browse files
authored
fix: Retain the internal Couchbase builder configuration if the user overrides the default configuration (#1040)
1 parent 871a9cd commit ceb9b05

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

src/Testcontainers.Couchbase/CouchbaseBuilder.cs

+25-22
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,19 @@ public sealed class CouchbaseBuilder : ContainerBuilder<CouchbaseBuilder, Couchb
4141

4242
public const string DefaultPassword = "password";
4343

44-
private readonly KeyValuePair<string, string> _basicAuthenticationHeader = new KeyValuePair<string, string>("Authorization", "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(string.Join(":", DefaultUsername, DefaultPassword))));
44+
private static readonly KeyValuePair<string, string> BasicAuthenticationHeader = new KeyValuePair<string, string>("Authorization", "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(string.Join(":", DefaultUsername, DefaultPassword))));
4545

46-
private readonly IWaitUntil _waitUntilNodeIsReady = new HttpWaitStrategy().ForPath("/pools").ForPort(MgmtPort);
46+
private static readonly IWaitUntil WaitUntilNodeIsReady = new HttpWaitStrategy().ForPath("/pools").ForPort(MgmtPort);
4747

48-
private readonly ISet<CouchbaseService> _enabledServices = new HashSet<CouchbaseService>();
48+
private static readonly ISet<CouchbaseService> EnabledServices = new HashSet<CouchbaseService>();
49+
50+
static CouchbaseBuilder()
51+
{
52+
EnabledServices.Add(CouchbaseService.Data);
53+
EnabledServices.Add(CouchbaseService.Index);
54+
EnabledServices.Add(CouchbaseService.Query);
55+
EnabledServices.Add(CouchbaseService.Search);
56+
}
4957

5058
/// <summary>
5159
/// Initializes a new instance of the <see cref="CouchbaseBuilder" /> class.
@@ -54,11 +62,6 @@ public CouchbaseBuilder()
5462
: this(new CouchbaseConfiguration())
5563
{
5664
DockerResourceConfiguration = Init().DockerResourceConfiguration;
57-
58-
_enabledServices.Add(CouchbaseService.Data);
59-
_enabledServices.Add(CouchbaseService.Index);
60-
_enabledServices.Add(CouchbaseService.Query);
61-
_enabledServices.Add(CouchbaseService.Search);
6265
}
6366

6467
/// <summary>
@@ -81,41 +84,41 @@ public override CouchbaseContainer Build()
8184

8285
var waitStrategy = Wait.ForUnixContainer();
8386

84-
if (_enabledServices.Any())
87+
if (EnabledServices.Any())
8588
{
8689
waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request
8790
=> request
8891
.ForPath("/pools/default")
8992
.ForPort(MgmtPort)
9093
.ForResponseMessageMatching(IsNodeHealthyAsync)
91-
.WithHeader(_basicAuthenticationHeader.Key, _basicAuthenticationHeader.Value));
94+
.WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value));
9295
}
9396

94-
if (_enabledServices.Contains(CouchbaseService.Query))
97+
if (EnabledServices.Contains(CouchbaseService.Query))
9598
{
9699
waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request
97100
=> request
98101
.ForPath("/admin/ping")
99102
.ForPort(QueryPort)
100-
.WithHeader(_basicAuthenticationHeader.Key, _basicAuthenticationHeader.Value));
103+
.WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value));
101104
}
102105

103-
if (_enabledServices.Contains(CouchbaseService.Analytics))
106+
if (EnabledServices.Contains(CouchbaseService.Analytics))
104107
{
105108
waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request
106109
=> request
107110
.ForPath("/admin/ping")
108111
.ForPort(AnalyticsPort)
109-
.WithHeader(_basicAuthenticationHeader.Key, _basicAuthenticationHeader.Value));
112+
.WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value));
110113
}
111114

112-
if (_enabledServices.Contains(CouchbaseService.Eventing))
115+
if (EnabledServices.Contains(CouchbaseService.Eventing))
113116
{
114117
waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request
115118
=> request
116119
.ForPath("/api/v1/config")
117120
.ForPort(EventingPort)
118-
.WithHeader(_basicAuthenticationHeader.Key, _basicAuthenticationHeader.Value));
121+
.WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value));
119122
}
120123

121124
var couchbaseBuilder = DockerResourceConfiguration.WaitStrategies.Count() > 1 ? this : WithWaitStrategy(waitStrategy);
@@ -180,7 +183,7 @@ private CouchbaseBuilder WithBucket(params CouchbaseBucket[] bucket)
180183
/// <param name="ct">Cancellation token.</param>
181184
private async Task ConfigureCouchbaseAsync(IContainer container, CancellationToken ct = default)
182185
{
183-
await WaitStrategy.WaitUntilAsync(() => _waitUntilNodeIsReady.UntilAsync(container), TimeSpan.FromSeconds(2), TimeSpan.FromMinutes(5), ct)
186+
await WaitStrategy.WaitUntilAsync(() => WaitUntilNodeIsReady.UntilAsync(container), TimeSpan.FromSeconds(2), TimeSpan.FromMinutes(5), ct)
184187
.ConfigureAwait(false);
185188

186189
using (var httpClient = new HttpClient())
@@ -197,7 +200,7 @@ await EnsureSuccessStatusCodeAsync(response)
197200
}
198201
}
199202

200-
using (var request = new SetupNodeServicesRequest(_enabledServices.ToArray()))
203+
using (var request = new SetupNodeServicesRequest(EnabledServices.ToArray()))
201204
{
202205
using (var response = await httpClient.SendAsync(request, ct)
203206
.ConfigureAwait(false))
@@ -207,7 +210,7 @@ await EnsureSuccessStatusCodeAsync(response)
207210
}
208211
}
209212

210-
using (var request = new SetupMemoryQuotasRequest(_enabledServices.ToArray()))
213+
using (var request = new SetupMemoryQuotasRequest(EnabledServices.ToArray()))
211214
{
212215
using (var response = await httpClient.SendAsync(request, ct)
213216
.ConfigureAwait(false))
@@ -217,7 +220,7 @@ await EnsureSuccessStatusCodeAsync(response)
217220
}
218221
}
219222

220-
using (var request = new ConfigureExternalAddressesRequest(container, _enabledServices.ToArray()))
223+
using (var request = new ConfigureExternalAddressesRequest(container, EnabledServices.ToArray()))
221224
{
222225
using (var response = await httpClient.SendAsync(request, ct)
223226
.ConfigureAwait(false))
@@ -262,7 +265,7 @@ await EnsureSuccessStatusCodeAsync(response)
262265
.ForPath("/pools/default/buckets/" + bucket.Name)
263266
.ForPort(MgmtPort)
264267
.ForResponseMessageMatching(AllServicesEnabledAsync)
265-
.WithHeader(_basicAuthenticationHeader.Key, _basicAuthenticationHeader.Value)))
268+
.WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value)))
266269
.Build()
267270
.Last();
268271

@@ -326,7 +329,7 @@ private async Task<bool> AllServicesEnabledAsync(HttpResponseMessage response)
326329
.Select(service => service.GetString())
327330
.Where(service => service != null);
328331

329-
return _enabledServices.All(enabledService => services.Any(service => service.StartsWith(enabledService.Identifier)));
332+
return EnabledServices.All(enabledService => services.Any(service => service.StartsWith(enabledService.Identifier)));
330333
}
331334
catch
332335
{

0 commit comments

Comments
 (0)