-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathCouchbaseBuilder.cs
543 lines (473 loc) · 21.7 KB
/
CouchbaseBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
namespace Testcontainers.Couchbase;
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
/// <remarks>
/// The Couchbase module runs the following services: Data, Index, Query, Search and creates the <see cref="CouchbaseBucket.Default" /> bucket during the start.
/// </remarks>
[PublicAPI]
public sealed class CouchbaseBuilder : ContainerBuilder<CouchbaseBuilder, CouchbaseContainer, CouchbaseConfiguration>
{
public const string CouchbaseImage = "couchbase:community-7.0.2";
public const ushort MgmtPort = 8091;
public const ushort MgmtSslPort = 18091;
public const ushort ViewPort = 8092;
public const ushort ViewSslPort = 18092;
public const ushort QueryPort = 8093;
public const ushort QuerySslPort = 18093;
public const ushort SearchPort = 8094;
public const ushort SearchSslPort = 18094;
public const ushort AnalyticsPort = 8095;
public const ushort AnalyticsSslPort = 18095;
public const ushort EventingPort = 8096;
public const ushort EventingSslPort = 18096;
public const ushort KvPort = 11210;
public const ushort KvSslPort = 11207;
public const string DefaultUsername = "Administrator";
public const string DefaultPassword = "password";
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 static readonly IWaitUntil WaitUntilNodeIsReady = new HttpWaitStrategy().ForPath("/pools").ForPort(MgmtPort);
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.
/// </summary>
public CouchbaseBuilder()
: this(new CouchbaseConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}
/// <summary>
/// Initializes a new instance of the <see cref="CouchbaseBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private CouchbaseBuilder(CouchbaseConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}
/// <inheritdoc />
protected override CouchbaseConfiguration DockerResourceConfiguration { get; }
/// <inheritdoc />
public override CouchbaseContainer Build()
{
Validate();
var waitStrategy = Wait.ForUnixContainer();
if (EnabledServices.Any())
{
waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request
=> request
.ForPath("/pools/default")
.ForPort(MgmtPort)
.ForResponseMessageMatching(IsNodeHealthyAsync)
.WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value));
}
if (EnabledServices.Contains(CouchbaseService.Query))
{
waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request
=> request
.ForPath("/admin/ping")
.ForPort(QueryPort)
.WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value));
}
if (EnabledServices.Contains(CouchbaseService.Analytics))
{
waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request
=> request
.ForPath("/admin/ping")
.ForPort(AnalyticsPort)
.WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value));
}
if (EnabledServices.Contains(CouchbaseService.Eventing))
{
waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request
=> request
.ForPath("/api/v1/config")
.ForPort(EventingPort)
.WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value));
}
var couchbaseBuilder = DockerResourceConfiguration.WaitStrategies.Count() > 1 ? this : WithWaitStrategy(waitStrategy);
return new CouchbaseContainer(couchbaseBuilder.DockerResourceConfiguration, TestcontainersSettings.Logger);
}
/// <inheritdoc />
protected override CouchbaseBuilder Init()
{
return base.Init()
.WithImage(CouchbaseImage)
.WithPortBinding(MgmtPort, true)
.WithPortBinding(MgmtSslPort, true)
.WithPortBinding(ViewPort, true)
.WithPortBinding(ViewSslPort, true)
.WithPortBinding(QueryPort, true)
.WithPortBinding(QuerySslPort, true)
.WithPortBinding(SearchPort, true)
.WithPortBinding(SearchSslPort, true)
.WithPortBinding(AnalyticsPort, true)
.WithPortBinding(AnalyticsSslPort, true)
.WithPortBinding(EventingPort, true)
.WithPortBinding(EventingSslPort, true)
.WithPortBinding(KvPort, true)
.WithPortBinding(KvSslPort, true)
.WithBucket(CouchbaseBucket.Default)
.WithStartupCallback(ConfigureCouchbaseAsync);
}
/// <inheritdoc />
protected override CouchbaseBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new CouchbaseConfiguration(resourceConfiguration));
}
/// <inheritdoc />
protected override CouchbaseBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new CouchbaseConfiguration(resourceConfiguration));
}
/// <inheritdoc />
protected override CouchbaseBuilder Merge(CouchbaseConfiguration oldValue, CouchbaseConfiguration newValue)
{
return new CouchbaseBuilder(new CouchbaseConfiguration(oldValue, newValue));
}
/// <summary>
/// Adds a Couchbase bucket.
/// </summary>
/// <param name="bucket">The Couchbase bucket.</param>
/// <returns>A configured instance of <see cref="CouchbaseBuilder" />.</returns>
private CouchbaseBuilder WithBucket(params CouchbaseBucket[] bucket)
{
return Merge(DockerResourceConfiguration, new CouchbaseConfiguration(buckets: bucket));
}
/// <summary>
/// Configures the Couchbase node.
/// </summary>
/// <param name="container">The container.</param>
/// <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)
.ConfigureAwait(false);
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new UriBuilder(Uri.UriSchemeHttp, container.Hostname, container.GetMappedPublicPort(MgmtPort)).Uri;
using (var request = new RenameNodeRequest(container))
{
using (var response = await httpClient.SendAsync(request, ct)
.ConfigureAwait(false))
{
await EnsureSuccessStatusCodeAsync(response)
.ConfigureAwait(false);
}
}
using (var request = new SetupNodeServicesRequest(EnabledServices.ToArray()))
{
using (var response = await httpClient.SendAsync(request, ct)
.ConfigureAwait(false))
{
await EnsureSuccessStatusCodeAsync(response)
.ConfigureAwait(false);
}
}
using (var request = new SetupMemoryQuotasRequest(EnabledServices.ToArray()))
{
using (var response = await httpClient.SendAsync(request, ct)
.ConfigureAwait(false))
{
await EnsureSuccessStatusCodeAsync(response)
.ConfigureAwait(false);
}
}
using (var request = new ConfigureExternalAddressesRequest(container, EnabledServices.ToArray()))
{
using (var response = await httpClient.SendAsync(request, ct)
.ConfigureAwait(false))
{
await EnsureSuccessStatusCodeAsync(response)
.ConfigureAwait(false);
}
}
foreach (var bucket in DockerResourceConfiguration.Buckets)
{
using (var request = new CreateBucketRequest(bucket))
{
using (var response = await httpClient.SendAsync(request, ct)
.ConfigureAwait(false))
{
await EnsureSuccessStatusCodeAsync(response)
.ConfigureAwait(false);
}
}
}
// This HTTP request initiates the provisioning of the single-node cluster.
// All subsequent requests following this HTTP request require credentials.
// Setting the credentials upfront interfere with other HTTP requests.
// We got frequently: System.IO.IOException The response ended prematurely.
using (var request = new SetupCredentialsRequest())
{
using (var response = await httpClient.SendAsync(request, ct)
.ConfigureAwait(false))
{
await EnsureSuccessStatusCodeAsync(response)
.ConfigureAwait(false);
}
}
}
// As long as we do not expose the bucket API, we do not need to iterate over all of them.
var waitUntilBucketIsCreated = DockerResourceConfiguration.Buckets.Aggregate(Wait.ForUnixContainer(), (waitStrategy, bucket)
=> waitStrategy.UntilHttpRequestIsSucceeded(request
=> request
.ForPath("/pools/default/buckets/" + bucket.Name)
.ForPort(MgmtPort)
.ForResponseMessageMatching(AllServicesEnabledAsync)
.WithHeader(BasicAuthenticationHeader.Key, BasicAuthenticationHeader.Value)))
.Build()
.Last();
await WaitStrategy.WaitUntilAsync(() => waitUntilBucketIsCreated.UntilAsync(container), TimeSpan.FromSeconds(2), TimeSpan.FromMinutes(5), ct)
.ConfigureAwait(false);
}
/// <summary>
/// Determines whether the single-node is healthy or not.
/// </summary>
/// <remarks>
/// https://docs.couchbase.com/server/current/rest-api/rest-cluster-get.html#http-method-and-uri
/// </remarks>
/// <param name="response">The HTTP response that contains the cluster information.</param>
/// <returns>A value indicating whether the single-node is healthy or not.</returns>
private async Task<bool> IsNodeHealthyAsync(HttpResponseMessage response)
{
var jsonString = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
try
{
var status = JsonDocument.Parse(jsonString)
.RootElement
.GetProperty("nodes")
.EnumerateArray()
.ElementAt(0)
.GetProperty("status")
.GetString();
return "healthy".Equals(status);
}
catch
{
return false;
}
}
/// <summary>
/// Determines whether all services are enabled for a bucket or not.
/// </summary>
/// <remarks>
/// https://docs.couchbase.com/server/current/rest-api/rest-buckets-summary.html#http-methods-and-uris.
/// </remarks>
/// <param name="response">The HTTP response that contains the bucket information.</param>
/// <returns>A value indicating whether all services are enabled for a bucket or not.</returns>
private async Task<bool> AllServicesEnabledAsync(HttpResponseMessage response)
{
var jsonString = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
try
{
var services = JsonDocument.Parse(jsonString)
.RootElement
.GetProperty("nodes")
.EnumerateArray()
.ElementAt(0)
.GetProperty("services")
.EnumerateArray()
.Select(service => service.GetString())
.Where(service => service != null);
return EnabledServices.All(enabledService => services.Any(service => service.StartsWith(enabledService.Identifier)));
}
catch
{
return false;
}
}
/// <summary>
/// Throws an exception if the <see cref="HttpResponseMessage.IsSuccessStatusCode" /> property for the HTTP response is <see langword="false" />
/// </summary>
/// <param name="response">The HTTP response.</param>
/// <exception cref="InvalidOperationException">The HTTP response is unsuccessful.</exception>
private static async Task EnsureSuccessStatusCodeAsync(HttpResponseMessage response)
{
try
{
response.EnsureSuccessStatusCode();
}
catch (Exception e)
{
var content = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
throw new InvalidOperationException(content, e);
}
}
/// <summary>
/// An HTTP request that renames the Couchbase node.
/// </summary>
private sealed class RenameNodeRequest : HttpRequestMessage
{
/// <summary>
/// Initializes a new instance of the <see cref="RenameNodeRequest" /> class.
/// </summary>
/// <remarks>
/// https://docs.couchbase.com/server/current/rest-api/rest-name-node.html#http-method-and-uri.
/// </remarks>
/// <param name="container">The Couchbase container.</param>
public RenameNodeRequest(IContainer container)
: base(HttpMethod.Post, "/node/controller/rename")
{
IDictionary<string, string> content = new Dictionary<string, string>();
content.Add("hostname", container.IpAddress);
Content = new FormUrlEncodedContent(content);
}
}
/// <summary>
/// An HTTP request that enables the Couchbase node services.
/// </summary>
private sealed class SetupNodeServicesRequest : HttpRequestMessage
{
/// <summary>
/// Initializes a new instance of the <see cref="SetupNodeServicesRequest" /> class.
/// </summary>
/// <remarks>
/// https://docs.couchbase.com/server/current/rest-api/rest-set-up-services.html#http-method-and-uri.
/// </remarks>
/// <param name="enabledServices">The enabled Couchbase node services.</param>
public SetupNodeServicesRequest(params CouchbaseService[] enabledServices)
: base(HttpMethod.Post, "/node/controller/setupServices")
{
IDictionary<string, string> content = new Dictionary<string, string>();
content.Add("services", string.Join(",", enabledServices.Select(enabledService => enabledService.Identifier)));
Content = new FormUrlEncodedContent(content);
}
}
/// <summary>
/// An HTTP request that sets the Couchbase node service memory quotas.
/// </summary>
private sealed class SetupMemoryQuotasRequest : HttpRequestMessage
{
/// <summary>
/// Initializes a new instance of the <see cref="SetupNodeServicesRequest" /> class.
/// </summary>
/// <remarks>
/// https://docs.couchbase.com/server/current/rest-api/rest-configure-memory.html#http-method-and-uri
/// </remarks>
/// <param name="enabledServices">The enabled Couchbase node services.</param>
public SetupMemoryQuotasRequest(params CouchbaseService[] enabledServices)
: base(HttpMethod.Post, "/pools/default")
{
IDictionary<string, string> content = new Dictionary<string, string>();
foreach (var enabledService in enabledServices)
{
if (!enabledService.HasQuota)
{
continue;
}
if (CouchbaseService.Data.Equals(enabledService))
{
content.Add("memoryQuota", enabledService.MinimumQuotaMb.ToString());
}
else
{
content.Add(enabledService.Identifier + "MemoryQuota", enabledService.MinimumQuotaMb.ToString());
}
}
Content = new FormUrlEncodedContent(content);
}
}
/// <summary>
/// An HTTP request that configures the Couchbase node external addresses.
/// </summary>
private sealed class ConfigureExternalAddressesRequest : HttpRequestMessage
{
/// <summary>
/// Initializes a new instance of the <see cref="ConfigureExternalAddressesRequest" /> class.
/// </summary>
/// <remarks>
/// https://docs.couchbase.com/server/current/rest-api/rest-set-up-alternate-address.html#http-method-and-uri.
/// </remarks>
/// <param name="container">The Couchbase container.</param>
/// <param name="enabledServices">The enabled Couchbase node services.</param>
public ConfigureExternalAddressesRequest(IContainer container, params CouchbaseService[] enabledServices)
: base(HttpMethod.Put, "/node/controller/setupAlternateAddresses/external")
{
IDictionary<string, string> content = new Dictionary<string, string>();
content.Add("hostname", container.Hostname);
content.Add("mgmt", container.GetMappedPublicPort(MgmtPort).ToString());
content.Add("mgmtSSL", container.GetMappedPublicPort(MgmtSslPort).ToString());
if (enabledServices.Contains(CouchbaseService.Data))
{
content.Add("kv", container.GetMappedPublicPort(KvPort).ToString());
content.Add("kvSSL", container.GetMappedPublicPort(KvSslPort).ToString());
content.Add("capi", container.GetMappedPublicPort(ViewPort).ToString());
content.Add("capiSSL", container.GetMappedPublicPort(ViewSslPort).ToString());
}
if (enabledServices.Contains(CouchbaseService.Query))
{
content.Add("n1ql", container.GetMappedPublicPort(QueryPort).ToString());
content.Add("n1qlSSL", container.GetMappedPublicPort(QuerySslPort).ToString());
}
if (enabledServices.Contains(CouchbaseService.Search))
{
content.Add("fts", container.GetMappedPublicPort(SearchPort).ToString());
content.Add("ftsSSL", container.GetMappedPublicPort(SearchSslPort).ToString());
}
if (enabledServices.Contains(CouchbaseService.Analytics))
{
content.Add("cbas", container.GetMappedPublicPort(AnalyticsPort).ToString());
content.Add("cbasSSL", container.GetMappedPublicPort(AnalyticsSslPort).ToString());
}
if (enabledServices.Contains(CouchbaseService.Eventing))
{
content.Add("eventingAdminPort", container.GetMappedPublicPort(EventingPort).ToString());
content.Add("eventingSSL", container.GetMappedPublicPort(EventingSslPort).ToString());
}
Content = new FormUrlEncodedContent(content);
}
}
/// <summary>
/// An HTTP request that sets the Couchbase administrator credentials.
/// </summary>
private sealed class SetupCredentialsRequest : HttpRequestMessage
{
/// <summary>
/// Initializes a new instance of the <see cref="SetupCredentialsRequest" /> class.
/// <remarks>
/// https://docs.couchbase.com/server/current/rest-api/rest-establish-credentials.html#http-method-and-uri.
/// </remarks>
/// </summary>
public SetupCredentialsRequest()
: base(HttpMethod.Post, "/settings/web")
{
IDictionary<string, string> content = new Dictionary<string, string>();
content.Add("username", DefaultUsername);
content.Add("password", DefaultPassword);
content.Add("port", "SAME");
Content = new FormUrlEncodedContent(content);
}
}
/// <summary>
/// An HTTP request that creates a Couchbase bucket.
/// </summary>
private sealed class CreateBucketRequest : HttpRequestMessage
{
/// <summary>
/// Initializes a new instance of the <see cref="CreateBucketRequest" /> class.
/// </summary>
/// <remarks>
/// https://docs.couchbase.com/server/current/rest-api/rest-bucket-create.html#http-methods-and-uris.
/// </remarks>
/// <param name="bucket">The Couchbase bucket.</param>
public CreateBucketRequest(CouchbaseBucket bucket)
: base(HttpMethod.Post, "/pools/default/buckets")
{
IDictionary<string, string> content = new Dictionary<string, string>();
content.Add("name", bucket.Name);
content.Add("flushEnabled", bucket.FlushEnabled ? "1" : "0");
content.Add("ramQuota", bucket.QuotaMiB.ToString());
content.Add("replicaNumber", bucket.ReplicaNumber.ToString());
Content = new FormUrlEncodedContent(content);
}
}
}