Skip to content

Commit

Permalink
fix: Retain the internal Couchbase builder configuration if the user …
Browse files Browse the repository at this point in the history
…overrides the default configuration (#1040)
  • Loading branch information
HofmeisterAn authored Nov 1, 2023
1 parent 871a9cd commit ceb9b05
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/Testcontainers.Couchbase/CouchbaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@ public sealed class CouchbaseBuilder : ContainerBuilder<CouchbaseBuilder, Couchb

public const string DefaultPassword = "password";

private readonly KeyValuePair<string, string> _basicAuthenticationHeader = new KeyValuePair<string, string>("Authorization", "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(string.Join(":", DefaultUsername, DefaultPassword))));
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))));

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

private readonly ISet<CouchbaseService> _enabledServices = new HashSet<CouchbaseService>();
private static readonly ISet<CouchbaseService> EnabledServices = new HashSet<CouchbaseService>();

static CouchbaseBuilder()
{
EnabledServices.Add(CouchbaseService.Data);
EnabledServices.Add(CouchbaseService.Index);
EnabledServices.Add(CouchbaseService.Query);
EnabledServices.Add(CouchbaseService.Search);
}

/// <summary>
/// Initializes a new instance of the <see cref="CouchbaseBuilder" /> class.
Expand All @@ -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);
}

/// <summary>
Expand All @@ -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);
Expand Down Expand Up @@ -180,7 +183,7 @@ private CouchbaseBuilder WithBucket(params CouchbaseBucket[] bucket)
/// <param name="ct">Cancellation token.</param>
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())
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -326,7 +329,7 @@ private async Task<bool> 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
{
Expand Down

0 comments on commit ceb9b05

Please sign in to comment.