Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing E2E test failures due to missing env variables. #4684

Merged
merged 5 commits into from
Oct 23, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -182,9 +182,9 @@ private ContainerRegistryInfo GetTestContainerRegistryInfo()
{
var containerRegistry = new ContainerRegistryInfo
{
Server = Environment.GetEnvironmentVariable("TestContainerRegistryServer"),
Username = Environment.GetEnvironmentVariable("TestContainerRegistryServer")?.Split('.')[0],
Password = Environment.GetEnvironmentVariable("TestContainerRegistryPassword"),
Server = EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestContainerRegistryServer),
Username = EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestContainerRegistryServer)?.Split('.')[0],
Password = EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestContainerRegistryPassword),
};

if (string.IsNullOrEmpty(containerRegistry.Server) || string.IsNullOrEmpty(containerRegistry.Password))
42 changes: 40 additions & 2 deletions src/Microsoft.Health.Fhir.Tests.Common/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
@@ -4,16 +4,54 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.Health.Fhir.CosmosDb.Features.Storage;

namespace Microsoft.Health.Fhir.Tests.Common
{
public static class EnvironmentVariables
{
public static string GetEnvironmentVariableWithDefault(string environmentVariableName, string defaultValue)
public const string DefaultCosmosDbId = "FhirTests";
public const string LocalSqlConnectionString = "server=(local);Integrated Security=true;TrustServerCertificate=True";
public const string MasterDatabaseName = "master";
public const string StorageEmulatorUri = "http://127.0.0.1:10000/devstoreaccount1";

private static readonly IDictionary<string, string> DefaultValueMap = new Dictionary<string, string>
{
{ KnownEnvironmentVariableNames.AuthorizationResource, string.Empty },
{ KnownEnvironmentVariableNames.AuthorizationScope, string.Empty },
{ KnownEnvironmentVariableNames.AzureSubscriptionClientId, string.Empty },
{ KnownEnvironmentVariableNames.AzureSubscriptionServiceConnectionId, string.Empty },
{ KnownEnvironmentVariableNames.AzureSubscriptionTenantId, string.Empty },
{ KnownEnvironmentVariableNames.CosmosDbDatabaseId, DefaultCosmosDbId },
{ KnownEnvironmentVariableNames.CosmosDbHost, CosmosDbLocalEmulator.Host },
{ KnownEnvironmentVariableNames.CosmosDbKey, CosmosDbLocalEmulator.Key },
{ KnownEnvironmentVariableNames.CosmosDbPreferredLocations, string.Empty },
{ KnownEnvironmentVariableNames.CosmosDbUseManagedIdentity, string.Empty },
{ KnownEnvironmentVariableNames.CrucibleEnvironmentUrl, string.Empty },
{ KnownEnvironmentVariableNames.SqlServerConnectionString, LocalSqlConnectionString },
{ KnownEnvironmentVariableNames.SystemAccessToken, string.Empty },
{ KnownEnvironmentVariableNames.TestContainerRegistryPassword, string.Empty },
{ KnownEnvironmentVariableNames.TestContainerRegistryServer, string.Empty },
{ KnownEnvironmentVariableNames.TestEnvironmentName, string.Empty },
{ KnownEnvironmentVariableNames.TestEnvironmentUrl, string.Empty },
{ KnownEnvironmentVariableNames.TestIntegrationStoreUri, StorageEmulatorUri },
};

public static string GetEnvironmentVariable(string environmentVariableName, string defaultValue = default)
{
var environmentVariable = Environment.GetEnvironmentVariable(environmentVariableName);
if (!string.IsNullOrWhiteSpace(environmentVariable))
{
return environmentVariable;
}

if (DefaultValueMap.TryGetValue(environmentVariableName, out environmentVariable) && !string.IsNullOrWhiteSpace(environmentVariable))
{
return environmentVariable;
}

return string.IsNullOrWhiteSpace(environmentVariable) ? defaultValue : environmentVariable;
return defaultValue;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

namespace Microsoft.Health.Fhir.Tests.Common
{
public static class KnownEnvironmentVariableNames
{
public const string AuthorizationResource = "Resource";
public const string AuthorizationScope = "Scope";
public const string AzureSubscriptionClientId = "AZURESUBSCRIPTION_CLIENT_ID";
public const string AzureSubscriptionServiceConnectionId = "AZURESUBSCRIPTION_SERVICE_CONNECTION_ID";
public const string AzureSubscriptionTenantId = "AZURESUBSCRIPTION_TENANT_ID";
public const string CosmosDbDatabaseId = "CosmosDb__DatabaseId";
public const string CosmosDbHost = "CosmosDb__Host";
public const string CosmosDbKey = "CosmosDb__Key";
public const string CosmosDbPreferredLocations = "CosmosDb__PreferredLocations";
public const string CosmosDbUseManagedIdentity = "CosmosDb__UseManagedIdentity";
public const string CrucibleEnvironmentUrl = "CrucibleEnvironmentUrl";
public const string SqlServerConnectionString = "SqlServer:ConnectionString";
public const string SystemAccessToken = "SYSTEM_ACCESSTOKEN";
public const string TestContainerRegistryPassword = "TestContainerRegistryPassword";
public const string TestContainerRegistryServer = "TestContainerRegistryServer";
public const string TestEnvironmentName = "TestEnvironmentName";
public const string TestEnvironmentUrl = "TestEnvironmentUrl";
public const string TestIntegrationStoreUri = "TestIntegrationStoreUri";
}
}
Original file line number Diff line number Diff line change
@@ -302,5 +302,6 @@
<ItemGroup>
<ProjectReference Include="..\Microsoft.Health.Fhir.Api\Microsoft.Health.Fhir.Api.csproj" />
<ProjectReference Include="..\Microsoft.Health.Fhir.Core\Microsoft.Health.Fhir.Core.csproj" />
<ProjectReference Include="..\Microsoft.Health.Fhir.CosmosDb\Microsoft.Health.Fhir.CosmosDb.csproj" />
v-iyamauchi marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
using System.Linq;
using System.Threading.Tasks;
using Hl7.Fhir.Rest;
using Microsoft.Health.Fhir.Tests.Common;
using Microsoft.Health.Fhir.Tests.Common.FixtureParameters;
using Microsoft.Health.Fhir.Tests.E2E.Crucible.Client;
using Microsoft.Health.Fhir.Tests.E2E.Rest;
@@ -24,11 +25,11 @@ public CrucibleDataSource(DataStore dataStore)
TestRun = new Lazy<Task>(TestRunAsync);
}

public static string CrucibleEnvironmentUrl => Environment.GetEnvironmentVariable("CrucibleEnvironmentUrl");
public static string CrucibleEnvironmentUrl => EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.CrucibleEnvironmentUrl);
v-iyamauchi marked this conversation as resolved.
Show resolved Hide resolved

public static string TestEnvironmentUrl => _dataStore.Equals(DataStore.SqlServer) ? Environment.GetEnvironmentVariable($"TestEnvironmentUrl{Constants.TestEnvironmentVariableVersionSqlSuffix}") : Environment.GetEnvironmentVariable($"TestEnvironmentUrl{Constants.TestEnvironmentVariableVersionSuffix}");
public static string TestEnvironmentUrl => _dataStore.Equals(DataStore.SqlServer) ? EnvironmentVariables.GetEnvironmentVariable($"{KnownEnvironmentVariableNames.TestEnvironmentUrl}{Constants.TestEnvironmentVariableVersionSqlSuffix}") : EnvironmentVariables.GetEnvironmentVariable($"{KnownEnvironmentVariableNames.TestEnvironmentUrl}{Constants.TestEnvironmentVariableVersionSuffix}");

public static string TestEnvironmentName => _dataStore.Equals(DataStore.SqlServer) ? Environment.GetEnvironmentVariable("TestEnvironmentName") + Constants.TestEnvironmentVariableVersionSqlSuffix : Environment.GetEnvironmentVariable("TestEnvironmentName") + Constants.TestEnvironmentVariableVersionSuffix;
public static string TestEnvironmentName => _dataStore.Equals(DataStore.SqlServer) ? EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestEnvironmentName) + Constants.TestEnvironmentVariableVersionSqlSuffix : EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestEnvironmentName) + Constants.TestEnvironmentVariableVersionSuffix;

public Lazy<Task> TestRun { get; }

Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using Microsoft.Health.Fhir.Tests.Common;
using Microsoft.Health.Fhir.Web;
using static Microsoft.Health.Fhir.Tests.Common.EnvironmentVariables;

@@ -13,8 +14,8 @@ namespace Microsoft.Health.Fhir.Tests.E2E.Common
/// </summary>
public static class AuthenticationSettings
{
public static string Scope => GetEnvironmentVariableWithDefault("Scope", DevelopmentIdentityProviderConfiguration.Audience);
public static string Scope => GetEnvironmentVariable(KnownEnvironmentVariableNames.AuthorizationScope, DevelopmentIdentityProviderConfiguration.Audience);

public static string Resource => GetEnvironmentVariableWithDefault("Resource", DevelopmentIdentityProviderConfiguration.Audience);
public static string Resource => GetEnvironmentVariable(KnownEnvironmentVariableNames.AuthorizationResource, DevelopmentIdentityProviderConfiguration.Audience);
}
}
Original file line number Diff line number Diff line change
@@ -17,11 +17,11 @@ public TestApplication(string id)

private string Id { get; }

public string ClientId => GetEnvironmentVariableWithDefault($"app_{Id}_id", Id);
public string ClientId => GetEnvironmentVariable($"app_{Id}_id", Id);

public string ClientSecret => GetEnvironmentVariableWithDefault($"app_{Id}_secret", Id);
public string ClientSecret => GetEnvironmentVariable($"app_{Id}_secret", Id);

public string GrantType => GetEnvironmentVariableWithDefault($"app_{Id}_grant_type", "client_credentials");
public string GrantType => GetEnvironmentVariable($"app_{Id}_grant_type", "client_credentials");

public bool Equals(TestApplication other)
{
Original file line number Diff line number Diff line change
@@ -17,9 +17,9 @@ public TestUser(string id)

private string Id { get; }

public string UserId => GetEnvironmentVariableWithDefault($"user_{Id}_id", Id);
public string UserId => GetEnvironmentVariable($"user_{Id}_id", Id);

public string Password => GetEnvironmentVariableWithDefault($"user_{Id}_secret", Id);
public string Password => GetEnvironmentVariable($"user_{Id}_secret", Id);

public string GrantType => "password";

Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
using Azure.Storage.Blobs.Specialized;
using EnsureThat;
using IdentityServer4.Models;
using Microsoft.Health.Fhir.Tests.Common;

namespace Microsoft.Health.Fhir.Tests.E2E
{
@@ -45,14 +46,29 @@ public static BlobClient GetBlobClient(Uri blobUri)
throw new Exception("Invalid blob URI");
}

string blobContainerName = segments[0];
string blobName = string.Join("/", segments.Skip(1));
string storageServiceUri = string.Empty;
string blobContainerName = string.Empty;
string blobName = string.Empty;
if (!IsLocalRun(blobUri))
{
storageServiceUri = $"https://{storageAccountName}.blob.core.windows.net";
blobContainerName = segments[0];
blobName = string.Join("/", segments.Skip(1));
}
else
{
if (segments.Length < 3)
{
throw new Exception("Invalid blob URI for a local run.");
}

// Construct the storage service URI
Uri storageServiceUri = new Uri($"https://{storageAccountName}.blob.core.windows.net");
storageServiceUri = EnvironmentVariables.StorageEmulatorUri;
blobContainerName = segments[1];
blobName = segments[2];
}

// Use the other GetBlobClient method
return GetBlobClient(storageServiceUri, blobContainerName, blobName);
return GetBlobClient(new Uri(storageServiceUri), blobContainerName, blobName);
}

public static BlobServiceClient GetBlobServiceClient(Uri storageServiceUri)
@@ -64,10 +80,10 @@ public static BlobServiceClient GetBlobServiceClient(Uri storageServiceUri)

TokenCredential credential = IsAzurePipelinesRun()
? new AzurePipelinesCredential(
Environment.GetEnvironmentVariable("AZURESUBSCRIPTION_TENANT_ID"),
Environment.GetEnvironmentVariable("AZURESUBSCRIPTION_CLIENT_ID"),
Environment.GetEnvironmentVariable("AZURESUBSCRIPTION_SERVICE_CONNECTION_ID"),
Environment.GetEnvironmentVariable("SYSTEM_ACCESSTOKEN"))
EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.AzureSubscriptionTenantId),
EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.AzureSubscriptionClientId),
EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.AzureSubscriptionServiceConnectionId),
EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.SystemAccessToken))
: new DefaultAzureCredential();

var blobServiceClient = new BlobServiceClient(storageServiceUri, credential);
@@ -91,15 +107,15 @@ private static bool IsLocalRun(Uri uri)
private static bool IsAzurePipelinesRun()
{
string[] variableNames = [
"AZURESUBSCRIPTION_CLIENT_ID",
"AZURESUBSCRIPTION_TENANT_ID",
"AZURESUBSCRIPTION_SERVICE_CONNECTION_ID",
"SYSTEM_ACCESSTOKEN",
KnownEnvironmentVariableNames.AzureSubscriptionClientId,
KnownEnvironmentVariableNames.AzureSubscriptionTenantId,
KnownEnvironmentVariableNames.AzureSubscriptionServiceConnectionId,
KnownEnvironmentVariableNames.SystemAccessToken,
];

foreach (var variableName in variableNames)
{
string variableValue = Environment.GetEnvironmentVariable(variableName);
string variableValue = EnvironmentVariables.GetEnvironmentVariable(variableName);
if (string.IsNullOrEmpty(variableValue))
{
return false;
Original file line number Diff line number Diff line change
@@ -109,9 +109,9 @@ private ContainerRegistryInfo GetTestContainerRegistryInfo()
{
var containerRegistry = new ContainerRegistryInfo
{
Server = Environment.GetEnvironmentVariable("TestContainerRegistryServer"),
Username = Environment.GetEnvironmentVariable("TestContainerRegistryServer")?.Split('.')[0],
Password = Environment.GetEnvironmentVariable("TestContainerRegistryPassword"),
Server = EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestContainerRegistryServer),
Username = EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestContainerRegistryServer)?.Split('.')[0],
Password = EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestContainerRegistryPassword),
};

if (string.IsNullOrEmpty(containerRegistry.Server) || string.IsNullOrEmpty(containerRegistry.Password))
Original file line number Diff line number Diff line change
@@ -371,7 +371,7 @@ public async Task GivenAnAnonymizedExportRequestWithoutContainerName_WhenExporti

private async Task<BlobContainerClient> InitializeAnonymizationContainer()
{
var storageUri = new Uri(Environment.GetEnvironmentVariable(TestExportStoreUriEnvironmentVariableName));
var storageUri = new Uri(EnvironmentVariables.GetEnvironmentVariable(TestExportStoreUriEnvironmentVariableName));
BlobServiceClient blobClient = AzureStorageBlobHelper.GetBlobServiceClient(storageUri);
BlobContainerClient container = blobClient.GetBlobContainerClient("anonymization");
await container.CreateIfNotExistsAsync();
Original file line number Diff line number Diff line change
@@ -310,9 +310,9 @@ private ContainerRegistryInfo GetTestContainerRegistryInfo()
{
var containerRegistry = new ContainerRegistryInfo
{
Server = Environment.GetEnvironmentVariable("TestContainerRegistryServer"),
Username = Environment.GetEnvironmentVariable("TestContainerRegistryServer")?.Split('.')[0],
Password = Environment.GetEnvironmentVariable("TestContainerRegistryPassword"),
Server = EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestContainerRegistryServer),
Username = EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestContainerRegistryServer)?.Split('.')[0],
Password = EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestContainerRegistryPassword),
};

if (string.IsNullOrEmpty(containerRegistry.Server) || string.IsNullOrEmpty(containerRegistry.Password))
Original file line number Diff line number Diff line change
@@ -5,13 +5,12 @@

using System;
using Azure.Storage.Blobs;
using Microsoft.Health.Fhir.Tests.Common;

namespace Microsoft.Health.Fhir.Tests.E2E.Rest.Import
{
public class ImportTestStorageAccount
{
private const string TestImportStoreUriEnvironmentVariableName = "TestIntegrationStoreUri";

public ImportTestStorageAccount()
{
InitializeFromEnvironmentVariables();
@@ -23,7 +22,7 @@ public ImportTestStorageAccount()

private void InitializeFromEnvironmentVariables()
{
StorageUri = new Uri(Environment.GetEnvironmentVariable(TestImportStoreUriEnvironmentVariableName));
StorageUri = new Uri(EnvironmentVariables.GetEnvironmentVariable(KnownEnvironmentVariableNames.TestIntegrationStoreUri));
BlobServiceClient = AzureStorageBlobHelper.GetBlobServiceClient(StorageUri);
}
}
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Health.Fhir.Tests.Common;
using Microsoft.Health.Fhir.Tests.Common.FixtureParameters;
using Xunit;

@@ -54,9 +55,9 @@ private static string GetEnvironmentUrl(DataStore dataStore)
switch (dataStore)
{
case DataStore.CosmosDb:
return Environment.GetEnvironmentVariable($"TestEnvironmentUrl{Constants.TestEnvironmentVariableVersionSuffix}");
return EnvironmentVariables.GetEnvironmentVariable($"{KnownEnvironmentVariableNames.TestEnvironmentUrl}{Constants.TestEnvironmentVariableVersionSuffix}");
case DataStore.SqlServer:
return Environment.GetEnvironmentVariable($"TestEnvironmentUrl{Constants.TestEnvironmentVariableVersionSuffix}_Sql");
return EnvironmentVariables.GetEnvironmentVariable($"{KnownEnvironmentVariableNames.TestEnvironmentUrl}{Constants.TestEnvironmentVariableVersionSuffix}_Sql");
default:
throw new ArgumentOutOfRangeException(nameof(dataStore), dataStore, null);
}
Loading
Loading