|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Net.Http.Json; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Azure.DataApiBuilder.Config.ObjectModel; |
| 10 | +using Azure.DataApiBuilder.Core.Configurations; |
| 11 | +using Azure.DataApiBuilder.Service.Controllers; |
| 12 | +using Microsoft.AspNetCore.TestHost; |
| 13 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 14 | +using static Azure.DataApiBuilder.Service.Tests.Configuration.ConfigurationEndpoints; |
| 15 | +using static Azure.DataApiBuilder.Service.Tests.Configuration.TestConfigFileReader; |
| 16 | + |
| 17 | +namespace Azure.DataApiBuilder.Service.Tests.Configuration; |
| 18 | + |
| 19 | +[TestClass] |
| 20 | +public class LoadConfigViaEndpointTests |
| 21 | +{ |
| 22 | + [TestMethod("Testing that missing environment variables won't cause runtime failure."), TestCategory(TestCategory.COSMOSDBNOSQL)] |
| 23 | + [DataRow(CONFIGURATION_ENDPOINT)] |
| 24 | + [DataRow(CONFIGURATION_ENDPOINT_V2)] |
| 25 | + public async Task CanLoadConfigWithMissingEnvironmentVariables(string configurationEndpoint) |
| 26 | + { |
| 27 | + TestServer server = new(Program.CreateWebHostFromInMemoryUpdateableConfBuilder(Array.Empty<string>())); |
| 28 | + HttpClient httpClient = server.CreateClient(); |
| 29 | + |
| 30 | + (RuntimeConfig config, JsonContent content) = GetParameterContent(configurationEndpoint); |
| 31 | + |
| 32 | + HttpResponseMessage postResult = |
| 33 | + await httpClient.PostAsync(configurationEndpoint, content); |
| 34 | + Assert.AreEqual(HttpStatusCode.OK, postResult.StatusCode); |
| 35 | + |
| 36 | + RuntimeConfigProvider configProvider = server.Services.GetService(typeof(RuntimeConfigProvider)) as RuntimeConfigProvider; |
| 37 | + RuntimeConfig loadedConfig = configProvider.GetConfig(); |
| 38 | + |
| 39 | + Assert.AreEqual(config.Schema, loadedConfig.Schema); |
| 40 | + } |
| 41 | + |
| 42 | + [TestMethod("Testing that environment variables can be replaced at runtime not only when config is loaded."), TestCategory(TestCategory.COSMOSDBNOSQL)] |
| 43 | + [DataRow(CONFIGURATION_ENDPOINT)] |
| 44 | + [DataRow(CONFIGURATION_ENDPOINT_V2)] |
| 45 | + [Ignore("We don't want to environment variable substitution in late configuration, but test is left in for if this changes.")] |
| 46 | + public async Task CanLoadConfigWithEnvironmentVariables(string configurationEndpoint) |
| 47 | + { |
| 48 | + Environment.SetEnvironmentVariable("schema", "schema.graphql"); |
| 49 | + TestServer server = new(Program.CreateWebHostFromInMemoryUpdateableConfBuilder(Array.Empty<string>())); |
| 50 | + HttpClient httpClient = server.CreateClient(); |
| 51 | + |
| 52 | + (RuntimeConfig config, JsonContent content) = GetParameterContent(configurationEndpoint); |
| 53 | + |
| 54 | + HttpResponseMessage postResult = |
| 55 | + await httpClient.PostAsync(configurationEndpoint, content); |
| 56 | + Assert.AreEqual(HttpStatusCode.OK, postResult.StatusCode); |
| 57 | + |
| 58 | + RuntimeConfigProvider configProvider = server.Services.GetService(typeof(RuntimeConfigProvider)) as RuntimeConfigProvider; |
| 59 | + RuntimeConfig loadedConfig = configProvider.GetConfig(); |
| 60 | + |
| 61 | + Assert.AreNotEqual(config.Schema, loadedConfig.Schema); |
| 62 | + Assert.AreEqual(Environment.GetEnvironmentVariable("schema"), loadedConfig.Schema); |
| 63 | + } |
| 64 | + |
| 65 | + [TestCleanup] |
| 66 | + public void Cleanup() |
| 67 | + { |
| 68 | + Environment.SetEnvironmentVariable("schema", null); |
| 69 | + } |
| 70 | + |
| 71 | + private static (RuntimeConfig, JsonContent) GetParameterContent(string endpoint) |
| 72 | + { |
| 73 | + RuntimeConfig config = ReadCosmosConfigurationFromFile() with { Schema = "@env('schema')" }; |
| 74 | + |
| 75 | + if (endpoint == CONFIGURATION_ENDPOINT) |
| 76 | + { |
| 77 | + ConfigurationPostParameters @params = new( |
| 78 | + Configuration: config.ToJson(), |
| 79 | + Schema: @" |
| 80 | + type Entity { |
| 81 | + id: ID! |
| 82 | + name: String! |
| 83 | + } |
| 84 | + ", |
| 85 | + ConnectionString: "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", |
| 86 | + AccessToken: null |
| 87 | + ); |
| 88 | + |
| 89 | + return (config, JsonContent.Create(@params)); |
| 90 | + } |
| 91 | + else if (endpoint == CONFIGURATION_ENDPOINT_V2) |
| 92 | + { |
| 93 | + ConfigurationPostParametersV2 @params = new( |
| 94 | + Configuration: config.ToJson(), |
| 95 | + ConfigurationOverrides: "{}", |
| 96 | + Schema: @" |
| 97 | + type Entity { |
| 98 | + id: ID! |
| 99 | + name: String! |
| 100 | + } |
| 101 | + ", |
| 102 | + AccessToken: null |
| 103 | + ); |
| 104 | + |
| 105 | + return (config, JsonContent.Create(@params)); |
| 106 | + } |
| 107 | + |
| 108 | + throw new ArgumentException($"Unknown endpoint: {endpoint}"); |
| 109 | + } |
| 110 | +} |
0 commit comments