From 66dbef3e518cfe07be33a2b40c9cc00acbeb0284 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Feb 2026 16:44:31 +0000 Subject: [PATCH] Make Cosmos DB tests read COSMOSDB_ENDPOINT and COSMOSDB_KEY from environment variables (#4156) * Initial plan * Make Cosmos DB tests read COSMOSDB_ENDPOINT and COSMOSDB_KEY from environment variables Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Rename EmulatorEndpoint/EmulatorKey static fields to use s_ prefix convention Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> --- .../CosmosChatHistoryProviderTests.cs | 14 +++++++------- .../CosmosCheckpointStoreTests.cs | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs b/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs index 4c142d3c1e..0ab605e4d6 100644 --- a/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs @@ -43,9 +43,9 @@ public sealed class CosmosChatHistoryProviderTests : IAsyncLifetime, IDisposable private static AgentSession CreateMockSession() => new Moq.Mock().Object; - // Cosmos DB Emulator connection settings - private const string EmulatorEndpoint = "https://localhost:8081"; - private const string EmulatorKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; + // Cosmos DB Emulator connection settings (can be overridden via COSMOSDB_ENDPOINT and COSMOSDB_KEY environment variables) + private static readonly string s_emulatorEndpoint = Environment.GetEnvironmentVariable("COSMOSDB_ENDPOINT") ?? "https://localhost:8081"; + private static readonly string s_emulatorKey = Environment.GetEnvironmentVariable("COSMOSDB_KEY") ?? "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; private const string TestContainerId = "ChatMessages"; private const string HierarchicalTestContainerId = "HierarchicalChatMessages"; // Use unique database ID per test class instance to avoid conflicts @@ -67,12 +67,12 @@ public sealed class CosmosChatHistoryProviderTests : IAsyncLifetime, IDisposable // Set COSMOS_PRESERVE_CONTAINERS=true to keep containers and data for inspection this._preserveContainer = string.Equals(Environment.GetEnvironmentVariable("COSMOS_PRESERVE_CONTAINERS"), "true", StringComparison.OrdinalIgnoreCase); - this._connectionString = $"AccountEndpoint={EmulatorEndpoint};AccountKey={EmulatorKey}"; + this._connectionString = $"AccountEndpoint={s_emulatorEndpoint};AccountKey={s_emulatorKey}"; try { // Only create CosmosClient for test setup - the actual tests will use connection string constructors - this._setupClient = new CosmosClient(EmulatorEndpoint, EmulatorKey); + this._setupClient = new CosmosClient(s_emulatorEndpoint, s_emulatorKey); // Test connection by attempting to create database var databaseResponse = await this._setupClient.CreateDatabaseIfNotExistsAsync(s_testDatabaseId); @@ -497,7 +497,7 @@ public sealed class CosmosChatHistoryProviderTests : IAsyncLifetime, IDisposable // Act TokenCredential credential = new DefaultAzureCredential(); - using var provider = new CosmosChatHistoryProvider(EmulatorEndpoint, credential, s_testDatabaseId, HierarchicalTestContainerId, + using var provider = new CosmosChatHistoryProvider(s_emulatorEndpoint, credential, s_testDatabaseId, HierarchicalTestContainerId, _ => new CosmosChatHistoryProvider.State("session-789", "tenant-123", "user-456")); // Assert @@ -513,7 +513,7 @@ public sealed class CosmosChatHistoryProviderTests : IAsyncLifetime, IDisposable // Arrange & Act this.SkipIfEmulatorNotAvailable(); - using var cosmosClient = new CosmosClient(EmulatorEndpoint, EmulatorKey); + using var cosmosClient = new CosmosClient(s_emulatorEndpoint, s_emulatorKey); using var provider = new CosmosChatHistoryProvider(cosmosClient, s_testDatabaseId, HierarchicalTestContainerId, _ => new CosmosChatHistoryProvider.State("session-789", "tenant-123", "user-456")); diff --git a/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosCheckpointStoreTests.cs b/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosCheckpointStoreTests.cs index f1f840cebf..0974045a9d 100644 --- a/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosCheckpointStoreTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosCheckpointStoreTests.cs @@ -28,9 +28,9 @@ namespace Microsoft.Agents.AI.CosmosNoSql.UnitTests; [Collection("CosmosDB")] public class CosmosCheckpointStoreTests : IAsyncLifetime, IDisposable { - // Cosmos DB Emulator connection settings - private const string EmulatorEndpoint = "https://localhost:8081"; - private const string EmulatorKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; + // Cosmos DB Emulator connection settings (can be overridden via COSMOSDB_ENDPOINT and COSMOSDB_KEY environment variables) + private static readonly string s_emulatorEndpoint = Environment.GetEnvironmentVariable("COSMOSDB_ENDPOINT") ?? "https://localhost:8081"; + private static readonly string s_emulatorKey = Environment.GetEnvironmentVariable("COSMOSDB_KEY") ?? "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; private const string TestContainerId = "Checkpoints"; // Use unique database ID per test class instance to avoid conflicts #pragma warning disable CA1802 // Use literals where appropriate @@ -64,11 +64,11 @@ public class CosmosCheckpointStoreTests : IAsyncLifetime, IDisposable // Set COSMOS_PRESERVE_CONTAINERS=true to keep containers and data for inspection this._preserveContainer = string.Equals(Environment.GetEnvironmentVariable("COSMOS_PRESERVE_CONTAINERS"), "true", StringComparison.OrdinalIgnoreCase); - this._connectionString = $"AccountEndpoint={EmulatorEndpoint};AccountKey={EmulatorKey}"; + this._connectionString = $"AccountEndpoint={s_emulatorEndpoint};AccountKey={s_emulatorKey}"; try { - this._cosmosClient = new CosmosClient(EmulatorEndpoint, EmulatorKey); + this._cosmosClient = new CosmosClient(s_emulatorEndpoint, s_emulatorKey); // Test connection by attempting to create database this._database = await this._cosmosClient.CreateDatabaseIfNotExistsAsync(s_testDatabaseId);