-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathRuntimeConfigLoaderTests.cs
More file actions
239 lines (195 loc) · 10.9 KB
/
RuntimeConfigLoaderTests.cs
File metadata and controls
239 lines (195 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using Azure.DataApiBuilder.Config;
using Azure.DataApiBuilder.Config.Converters;
using Azure.DataApiBuilder.Config.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
namespace Azure.DataApiBuilder.Service.Tests.Configuration;
[TestClass]
public class RuntimeConfigLoaderTests
{
[DataTestMethod]
[DataRow("dab-config.CosmosDb_NoSql.json")]
[DataRow("dab-config.MsSql.json")]
[DataRow("dab-config.MySql.json")]
[DataRow("dab-config.PostgreSql.json")]
public async Task CanLoadStandardConfig(string configPath)
{
string fileContents = await File.ReadAllTextAsync(configPath);
IFileSystem fs = new MockFileSystem(new Dictionary<string, MockFileData>() { { "dab-config.json", new MockFileData(fileContents) } });
FileSystemRuntimeConfigLoader loader = new(fs);
Assert.IsTrue(loader.TryLoadConfig("dab-config.json", out RuntimeConfig _), "Failed to load config");
}
/// <summary>
/// Test validates that when child files are present all datasources are loaded correctly.
/// </summary>
[DataTestMethod]
[DataRow("Multidab-config.CosmosDb_NoSql.json", new string[] { "Multidab-config.MsSql.json", "Multidab-config.MySql.json", "Multidab-config.PostgreSql.json" })]
public async Task CanLoadValidMultiSourceConfig(string configPath, IEnumerable<string> dataSourceFiles)
{
string fileContents = await File.ReadAllTextAsync(configPath);
// Parse the base JSON string
JObject baseJsonObject = JObject.Parse(fileContents);
// Create a new JArray to hold the values to be appended
JArray valuesToAppend = new(dataSourceFiles);
// Add or append the values to the base JSON
baseJsonObject.Add("data-source-files", valuesToAppend);
// Convert the modified JSON object back to a JSON string
string resultJson = baseJsonObject.ToString();
IFileSystem fs = new MockFileSystem(new Dictionary<string, MockFileData>() { { "dab-config.json", new MockFileData(resultJson) } });
FileSystemRuntimeConfigLoader loader = new(fs);
Assert.IsTrue(loader.TryLoadConfig("dab-config.json", out RuntimeConfig runtimeConfig), "Should successfully load config");
Assert.IsTrue(runtimeConfig.ListAllDataSources().Count() == 4, "Should have 4 data sources");
Assert.IsTrue(runtimeConfig.CosmosDataSourceUsed, "Should have CosmosDb data source");
Assert.IsTrue(runtimeConfig.SqlDataSourceUsed, "Should have Sql data source");
Assert.AreEqual(DatabaseType.CosmosDB_NoSQL, runtimeConfig.DataSource.DatabaseType, "Default datasource should be of root file database type.");
}
/// <summary>
/// Test validates that load fails when datasource files have duplicate entities.
/// Example: Publisher entity present in the 3 sql.json files.
/// </summary>
[DataTestMethod]
[DataRow("dab-config.CosmosDb_NoSql.json", new string[] { "dab-config.MsSql.json", "dab-config.MySql.json", "dab-config.PostgreSql.json" })]
public async Task FailLoadMultiDataSourceConfigDuplicateEntities(string configPath, IEnumerable<string> dataSourceFiles)
{
string fileContents = await File.ReadAllTextAsync(configPath);
// Parse the base JSON string
JObject baseJsonObject = JObject.Parse(fileContents);
// Create a new JArray to hold the values to be appended
JArray valuesToAppend = new(dataSourceFiles);
// Add or append the values to the base JSON
baseJsonObject.Add("data-source-files", valuesToAppend);
// Convert the modified JSON object back to a JSON string
string resultJson = baseJsonObject.ToString();
IFileSystem fs = new MockFileSystem(new Dictionary<string, MockFileData>() { { "dab-config.json", new MockFileData(resultJson) } });
FileSystemRuntimeConfigLoader loader = new(fs);
StringWriter sw = new();
Console.SetError(sw);
loader.TryLoadConfig("dab-config.json", out RuntimeConfig _);
string error = sw.ToString();
Assert.IsTrue(error.StartsWith("Deserialization of the configuration file failed during a post-processing step."));
Assert.IsTrue(error.Contains("An item with the same key has already been added."));
}
/// <summary>
/// Test validates that when child files are present all autoentities are loaded correctly.
/// </summary>
[DataTestMethod]
[DataRow("Multidab-config.CosmosDb_NoSql.json", new string[] { "Multidab-config.MsSql.json", "Multidab-config.MySql.json", "Multidab-config.PostgreSql.json" }, 36)]
public async Task CanLoadValidMultiSourceConfigWithAutoentities(string configPath, IEnumerable<string> dataSourceFiles, int expectedEntities)
{
string fileContents = await File.ReadAllTextAsync(configPath);
// Parse the base JSON string
JObject baseJsonObject = JObject.Parse(fileContents);
// Create a new JArray to hold the values to be appended
JArray valuesToAppend = new(dataSourceFiles);
// Add or append the values to the base JSON
baseJsonObject.Add("data-source-files", valuesToAppend);
// Convert the modified JSON object back to a JSON string
string resultJson = baseJsonObject.ToString();
IFileSystem fs = new MockFileSystem(new Dictionary<string, MockFileData>() { { "dab-config.json", new MockFileData(resultJson) } });
FileSystemRuntimeConfigLoader loader = new(fs);
Assert.IsTrue(loader.TryLoadConfig("dab-config.json", out RuntimeConfig runtimeConfig), "Should successfully load config");
Assert.IsTrue(runtimeConfig.SqlDataSourceUsed, "Should have Sql data source");
Assert.AreEqual(expectedEntities, runtimeConfig.Entities.Entities.Count, "Number of entities is not what is expected.");
}
/// <summary>
/// Validates that when a parent config has azure-key-vault options configured,
/// child configs can resolve @akv('...') references using the parent's AKV configuration.
/// Uses a local .akv file to simulate Azure Key Vault without requiring a real vault.
/// Regression test for https://github.com/Azure/data-api-builder/issues/3322
/// </summary>
[TestMethod]
public async Task ChildConfigResolvesAkvReferencesFromParentAkvOptions()
{
string akvFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".akv");
string childFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".json");
try
{
// Create a local .akv secrets file with test secrets.
await File.WriteAllTextAsync(akvFilePath, "my-connection-secret=Server=tcp:127.0.0.1,1433;Trusted_Connection=True;\n");
// Parent config with azure-key-vault pointing to the local .akv file.
string parentConfig = $@"{{
""$schema"": ""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch/dab.draft.schema.json"",
""data-source"": {{
""database-type"": ""mssql"",
""connection-string"": ""Server=tcp:127.0.0.1,1433;Persist Security Info=False;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=False;Connection Timeout=5;""
}},
""azure-key-vault"": {{
""endpoint"": ""{akvFilePath.Replace("\\", "\\\\")}""
}},
""data-source-files"": [""{childFilePath.Replace("\\", "\\\\")}""],
""runtime"": {{
""rest"": {{ ""enabled"": true }},
""graphql"": {{ ""enabled"": true }},
""host"": {{
""cors"": {{ ""origins"": [] }},
""authentication"": {{ ""provider"": ""StaticWebApps"" }}
}}
}},
""entities"": {{}}
}}";
// Child config with @akv('...') reference in its connection string.
string childConfig = @"{
""$schema"": ""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch/dab.draft.schema.json"",
""data-source"": {
""database-type"": ""mssql"",
""connection-string"": ""@akv('my-connection-secret')""
},
""runtime"": {
""rest"": { ""enabled"": true },
""graphql"": { ""enabled"": true },
""host"": {
""cors"": { ""origins"": [] },
""authentication"": { ""provider"": ""StaticWebApps"" }
}
},
""entities"": {
""AkvChildEntity"": {
""source"": ""dbo.AkvTable"",
""permissions"": [{ ""role"": ""anonymous"", ""actions"": [""read""] }]
}
}
}";
await File.WriteAllTextAsync(childFilePath, childConfig);
MockFileSystem fs = new(new Dictionary<string, MockFileData>()
{
{ "dab-config.json", new MockFileData(parentConfig) }
});
FileSystemRuntimeConfigLoader loader = new(fs);
DeserializationVariableReplacementSettings replacementSettings = new(
azureKeyVaultOptions: new AzureKeyVaultOptions() { Endpoint = akvFilePath, UserProvidedEndpoint = true },
doReplaceEnvVar: true,
doReplaceAkvVar: true,
envFailureMode: EnvironmentVariableReplacementFailureMode.Ignore);
Assert.IsTrue(
loader.TryLoadConfig("dab-config.json", out RuntimeConfig runtimeConfig, replacementSettings: replacementSettings),
"Config should load successfully when child config has @akv() references resolvable via parent AKV options.");
Assert.IsTrue(runtimeConfig.Entities.ContainsKey("AkvChildEntity"), "Child config entity should be merged into the parent config.");
// Verify the child's connection string was resolved from the .akv file.
string childDataSourceName = runtimeConfig.GetDataSourceNameFromEntityName("AkvChildEntity");
DataSource childDataSource = runtimeConfig.GetDataSourceFromDataSourceName(childDataSourceName);
Assert.IsTrue(
childDataSource.ConnectionString.Contains("127.0.0.1"),
"Child config connection string should have the AKV secret resolved.");
}
finally
{
if (File.Exists(akvFilePath))
{
File.Delete(akvFilePath);
}
if (File.Exists(childFilePath))
{
File.Delete(childFilePath);
}
}
}
}