|
9 | 9 | using Npgsql; |
10 | 10 | using Steeltoe.Common.HealthChecks; |
11 | 11 | using Steeltoe.Common.TestResources; |
| 12 | +using Steeltoe.Configuration.CloudFoundry; |
12 | 13 | using Steeltoe.Configuration.CloudFoundry.ServiceBindings; |
13 | 14 | using Steeltoe.Configuration.Kubernetes.ServiceBindings; |
14 | 15 | using Steeltoe.Connectors.PostgreSql; |
@@ -394,6 +395,97 @@ public async Task Binds_options_with_Kubernetes_service_bindings() |
394 | 395 | }, options => options.WithoutStrictOrdering()); |
395 | 396 | } |
396 | 397 |
|
| 398 | + [Fact] |
| 399 | + public async Task Binds_options_with_third_party_service_bindings() |
| 400 | + { |
| 401 | + var appSettings = new Dictionary<string, string?> |
| 402 | + { |
| 403 | + ["Steeltoe:Client:PostgreSql:products-db:ConnectionString"] = "Include Error Detail=true;host=localhost", |
| 404 | + ["Steeltoe:Client:PostgreSql:orders-db:ConnectionString"] = "Log Parameters=true;port=9999" |
| 405 | + }; |
| 406 | + |
| 407 | + var reader = new CloudFoundryMemorySettingsReader |
| 408 | + { |
| 409 | + ServicesJson = """ |
| 410 | + { |
| 411 | + "custom-postgres-broker": [ |
| 412 | + { |
| 413 | + "name": "products-db", |
| 414 | + "credentials": { |
| 415 | + "custom-hostname-key": "example.cloud.com", |
| 416 | + "custom-port-key": 2345, |
| 417 | + "custom-username-key": "products-user", |
| 418 | + "custom-password-key": "products-secret", |
| 419 | + "custom-database-name-key": "product-database" |
| 420 | + } |
| 421 | + }, |
| 422 | + { |
| 423 | + "name": "orders-db", |
| 424 | + "credentials": { |
| 425 | + "custom-hostname-key": "example.cloud.com", |
| 426 | + "custom-port-key": 2345, |
| 427 | + "custom-username-key": "orders-user", |
| 428 | + "custom-password-key": "orders-secret", |
| 429 | + "custom-database-name-key": "order-database" |
| 430 | + } |
| 431 | + }, |
| 432 | + ] |
| 433 | + } |
| 434 | + """ |
| 435 | + }; |
| 436 | + |
| 437 | + WebApplicationBuilder builder = TestWebApplicationBuilderFactory.Create(); |
| 438 | + builder.Configuration.AddInMemoryCollection(appSettings); |
| 439 | + builder.Configuration.AddCloudFoundry(reader); |
| 440 | + MapCustomServiceBindings("custom-postgres-broker"); |
| 441 | + builder.AddPostgreSql(options => options.SkipDefaultServiceBindings = true, null); |
| 442 | + await using WebApplication app = builder.Build(); |
| 443 | + |
| 444 | + var optionsMonitor = app.Services.GetRequiredService<IOptionsMonitor<PostgreSqlOptions>>(); |
| 445 | + |
| 446 | + PostgreSqlOptions productsDbOptions = optionsMonitor.Get("products-db"); |
| 447 | + |
| 448 | + ExtractConnectionStringParameters(productsDbOptions.ConnectionString).Should().BeEquivalentTo(new List<string> |
| 449 | + { |
| 450 | + "Include Error Detail=True", |
| 451 | + "Host=example.cloud.com", |
| 452 | + "Port=2345", |
| 453 | + "Database=product-database", |
| 454 | + "Username=products-user", |
| 455 | + "Password=products-secret" |
| 456 | + }, options => options.WithoutStrictOrdering()); |
| 457 | + |
| 458 | + PostgreSqlOptions ordersDbOptions = optionsMonitor.Get("orders-db"); |
| 459 | + |
| 460 | + ExtractConnectionStringParameters(ordersDbOptions.ConnectionString).Should().BeEquivalentTo(new List<string> |
| 461 | + { |
| 462 | + "Log Parameters=True", |
| 463 | + "Host=example.cloud.com", |
| 464 | + "Port=2345", |
| 465 | + "Database=order-database", |
| 466 | + "Username=orders-user", |
| 467 | + "Password=orders-secret" |
| 468 | + }, options => options.WithoutStrictOrdering()); |
| 469 | + |
| 470 | + void MapCustomServiceBindings(string brokerName) |
| 471 | + { |
| 472 | + var options = builder.Configuration.GetSection("vcap").Get<CloudFoundryServicesOptions>(); |
| 473 | + |
| 474 | + foreach (CloudFoundryService service in options?.Services.Where(pair => pair.Key == brokerName).SelectMany(pair => pair.Value) ?? []) |
| 475 | + { |
| 476 | + builder.Configuration.AddInMemoryCollection(new Dictionary<string, string?> |
| 477 | + { |
| 478 | + // Map credentials into the property names expected by NpgsqlConnectionStringBuilder. |
| 479 | + [$"steeltoe:service-bindings:postgresql:{service.Name}:host"] = service.Credentials["custom-hostname-key"].Value, |
| 480 | + [$"steeltoe:service-bindings:postgresql:{service.Name}:port"] = service.Credentials["custom-port-key"].Value, |
| 481 | + [$"steeltoe:service-bindings:postgresql:{service.Name}:username"] = service.Credentials["custom-username-key"].Value, |
| 482 | + [$"steeltoe:service-bindings:postgresql:{service.Name}:password"] = service.Credentials["custom-password-key"].Value, |
| 483 | + [$"steeltoe:service-bindings:postgresql:{service.Name}:database"] = service.Credentials["custom-database-name-key"].Value |
| 484 | + }); |
| 485 | + } |
| 486 | + } |
| 487 | + } |
| 488 | + |
397 | 489 | [Fact] |
398 | 490 | public async Task Registers_ConnectorFactory() |
399 | 491 | { |
|
0 commit comments