Skip to content

Commit a4d9848

Browse files
Copilotsamtrion
andcommitted
fix: Configure Firestore client for emulator with insecure credentials
Co-authored-by: samtrion <3283596+samtrion@users.noreply.github.com>
1 parent 5582501 commit a4d9848

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed
Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
namespace NetEvolve.HealthChecks.Tests.Integration.GCP.Firestore;
22

3+
using System;
34
using System.Threading.Tasks;
4-
using DotNet.Testcontainers.Builders;
5+
using global::Google.Cloud.Firestore;
6+
using global::Google.Cloud.Firestore.V1;
7+
using global::Grpc.Core;
58
using Microsoft.Extensions.Logging.Abstractions;
69
using Testcontainers.Firestore;
710
using TUnit.Core.Interfaces;
811

912
public sealed class FirestoreDatabase : IAsyncInitializer, IAsyncDisposable
1013
{
11-
private readonly FirestoreContainer _database = new FirestoreBuilder().WithLogger(NullLogger.Instance).Build();
14+
private readonly FirestoreContainer _container = new FirestoreBuilder().WithLogger(NullLogger.Instance).Build();
15+
16+
private FirestoreClient? _client;
17+
private FirestoreDb? _database;
1218

1319
public const string ProjectId = "test-project";
1420

15-
public string EmulatorHost => _database.GetEmulatorEndpoint();
21+
public FirestoreDb Database => _database ?? throw new InvalidOperationException("Database not initialized");
22+
23+
public async ValueTask DisposeAsync()
24+
{
25+
await _container.DisposeAsync().ConfigureAwait(false);
26+
}
27+
28+
public async Task InitializeAsync()
29+
{
30+
await _container.StartAsync().ConfigureAwait(false);
31+
32+
// Parse endpoint to get host:port
33+
var fullEndpoint = _container.GetEmulatorEndpoint();
34+
var uri = new Uri(fullEndpoint);
35+
var hostPort = $"{uri.Host}:{uri.Port}";
1636

17-
public async ValueTask DisposeAsync() => await _database.DisposeAsync().ConfigureAwait(false);
37+
// Create Firestore client configured for emulator
38+
var clientBuilder = new FirestoreClientBuilder
39+
{
40+
Endpoint = hostPort,
41+
ChannelCredentials = ChannelCredentials.Insecure,
42+
};
1843

19-
public async Task InitializeAsync() => await _database.StartAsync().ConfigureAwait(false);
44+
_client = await clientBuilder.BuildAsync().ConfigureAwait(false);
45+
_database = await FirestoreDb.CreateAsync(ProjectId, _client).ConfigureAwait(false);
46+
}
2047
}

tests/NetEvolve.HealthChecks.Tests.Integration/GCP/Firestore/FirestoreHealthCheckTests.cs

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace NetEvolve.HealthChecks.Tests.Integration.GCP.Firestore;
33
using System;
44
using System.Collections.Generic;
55
using System.Threading.Tasks;
6-
using global::Google.Cloud.Firestore;
76
using Microsoft.Extensions.Configuration;
87
using Microsoft.Extensions.DependencyInjection;
98
using Microsoft.Extensions.Diagnostics.HealthChecks;
@@ -23,23 +22,15 @@ public async Task AddFirestore_UseOptions_Healthy() =>
2322
await RunAndVerify(
2423
healthChecks => healthChecks.AddFirestore("TestContainerHealthy", options => options.Timeout = 10000),
2524
HealthStatus.Healthy,
26-
serviceBuilder: services =>
27-
{
28-
Environment.SetEnvironmentVariable("FIRESTORE_EMULATOR_HOST", _database.EmulatorHost);
29-
_ = services.AddSingleton(_ => FirestoreDb.Create(FirestoreDatabase.ProjectId));
30-
}
25+
serviceBuilder: services => _ = services.AddSingleton(_ => _database.Database)
3126
);
3227

3328
[Test]
3429
public async Task AddFirestore_UseOptions_Degraded() =>
3530
await RunAndVerify(
3631
healthChecks => healthChecks.AddFirestore("TestContainerDegraded", options => options.Timeout = 0),
3732
HealthStatus.Degraded,
38-
serviceBuilder: services =>
39-
{
40-
Environment.SetEnvironmentVariable("FIRESTORE_EMULATOR_HOST", _database.EmulatorHost);
41-
_ = services.AddSingleton(_ => FirestoreDb.Create(FirestoreDatabase.ProjectId));
42-
}
33+
serviceBuilder: services => _ = services.AddSingleton(_ => _database.Database)
4334
);
4435

4536
[Test]
@@ -57,11 +48,7 @@ await RunAndVerify(
5748
);
5849
},
5950
HealthStatus.Healthy,
60-
serviceBuilder: services =>
61-
{
62-
Environment.SetEnvironmentVariable("FIRESTORE_EMULATOR_HOST", _database.EmulatorHost);
63-
_ = services.AddKeyedSingleton("firestore", (_, _) => FirestoreDb.Create(FirestoreDatabase.ProjectId));
64-
}
51+
serviceBuilder: services => _ = services.AddKeyedSingleton("firestore", (_, _) => _database.Database)
6552
);
6653

6754
[Test]
@@ -77,11 +64,7 @@ await RunAndVerify(
7764
};
7865
_ = config.AddInMemoryCollection(values);
7966
},
80-
serviceBuilder: services =>
81-
{
82-
Environment.SetEnvironmentVariable("FIRESTORE_EMULATOR_HOST", _database.EmulatorHost);
83-
_ = services.AddSingleton(_ => FirestoreDb.Create(FirestoreDatabase.ProjectId));
84-
}
67+
serviceBuilder: services => _ = services.AddSingleton(_ => _database.Database)
8568
);
8669

8770
[Test]
@@ -97,11 +80,7 @@ await RunAndVerify(
9780
};
9881
_ = config.AddInMemoryCollection(values);
9982
},
100-
serviceBuilder: services =>
101-
{
102-
Environment.SetEnvironmentVariable("FIRESTORE_EMULATOR_HOST", _database.EmulatorHost);
103-
_ = services.AddSingleton(_ => FirestoreDb.Create(FirestoreDatabase.ProjectId));
104-
}
83+
serviceBuilder: services => _ = services.AddSingleton(_ => _database.Database)
10584
);
10685

10786
[Test]
@@ -117,10 +96,6 @@ await RunAndVerify(
11796
};
11897
_ = config.AddInMemoryCollection(values);
11998
},
120-
serviceBuilder: services =>
121-
{
122-
Environment.SetEnvironmentVariable("FIRESTORE_EMULATOR_HOST", _database.EmulatorHost);
123-
_ = services.AddSingleton(_ => FirestoreDb.Create(FirestoreDatabase.ProjectId));
124-
}
99+
serviceBuilder: services => _ = services.AddSingleton(_ => _database.Database)
125100
);
126101
}

0 commit comments

Comments
 (0)