-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathValidateConfigTests.cs
More file actions
363 lines (311 loc) · 14.5 KB
/
ValidateConfigTests.cs
File metadata and controls
363 lines (311 loc) · 14.5 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Core.Configurations;
using Azure.DataApiBuilder.Core.Models;
using Serilog;
namespace Cli.Tests;
/// <summary>
/// Test for config file initialization.
/// </summary>
[TestClass]
public class ValidateConfigTests
: VerifyBase
{
private MockFileSystem? _fileSystem;
private FileSystemRuntimeConfigLoader? _runtimeConfigLoader;
[TestInitialize]
public void TestInitialize()
{
_fileSystem = FileSystemUtils.ProvisionMockFileSystem();
_runtimeConfigLoader = new FileSystemRuntimeConfigLoader(_fileSystem);
ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory();
SetLoggerForCliConfigGenerator(loggerFactory.CreateLogger<ConfigGenerator>());
SetCliUtilsLogger(loggerFactory.CreateLogger<Utils>());
}
[TestCleanup]
public void TestCleanup()
{
_fileSystem = null;
_runtimeConfigLoader = null;
// Clear environment variables set in tests.
Environment.SetEnvironmentVariable($"connection-string", null);
Environment.SetEnvironmentVariable($"database-type", null);
Environment.SetEnvironmentVariable($"sp_param1_int", null);
Environment.SetEnvironmentVariable($"sp_param2_bool", null);
}
/// <summary>
/// This method validates that the IsConfigValid method returns false when the config is invalid.
/// </summary>
[TestMethod]
public void TestConfigWithCustomPropertyAsInvalid()
{
((MockFileSystem)_fileSystem!).AddFile(TEST_RUNTIME_CONFIG_FILE, CONFIG_WITH_CUSTOM_PROPERTIES);
ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);
bool isConfigValid = ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!);
Assert.IsFalse(isConfigValid);
}
/// <summary>
/// This method verifies that the relationship validation does not cause unhandled
/// exceptions, and that the errors generated include the expected messaging.
/// This case is a regression test due to the metadata needed not always being
/// populated in the SqlMetadataProvider if for example a bad connection string
/// is given.
/// </summary>
[TestMethod]
public void TestErrorHandlingForRelationshipValidationWithNonWorkingConnectionString()
{
// Arrange
((MockFileSystem)_fileSystem!).AddFile(TEST_RUNTIME_CONFIG_FILE, COMPLETE_CONFIG_WITH_RELATIONSHIPS_NON_WORKING_CONN_STRING);
ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);
StringWriter writer = new();
// Capture console output to get error messaging.
Console.SetOut(writer);
// Act
ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!);
string errorMessage = writer.ToString();
// Assert
Assert.IsTrue(errorMessage.Contains(DataApiBuilderException.CONNECTION_STRING_ERROR_MESSAGE));
}
/// <summary>
/// Validates that the IsConfigValid method returns false when a config is passed with
/// both rest and graphQL disabled globally.
/// </summary>
[TestMethod]
public void TestConfigWithInvalidConfigProperties()
{
((MockFileSystem)_fileSystem!).AddFile(TEST_RUNTIME_CONFIG_FILE, CONFIG_WITH_DISABLED_GLOBAL_REST_GRAPHQL);
ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);
bool isConfigValid = ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!);
Assert.IsFalse(isConfigValid);
}
/// <summary>
/// This method validates that the IsConfigValid method returns false when the config is empty.
/// This is to validate that no exceptions are thrown with validate for failures during config deserialization.
/// </summary>
[TestMethod]
public void TestValidateWithEmptyConfig()
{
// create an empty config file
((MockFileSystem)_fileSystem!).AddFile(TEST_RUNTIME_CONFIG_FILE, string.Empty);
ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);
try
{
Assert.IsFalse(ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!));
}
catch (Exception ex)
{
Assert.Fail($"Unexpected Exception thrown: {ex.Message}");
}
}
/// <summary>
/// This Test is used to verify that the validate command is able to catch invalid values for the depth-limit property.
/// </summary>
[DataTestMethod]
[DataRow("null", true, DisplayName = "Invalid Value: 'null'. Only integer values are allowed.")]
[DataRow("20", true, DisplayName = "Invalid Value: '20'. Integer values provided as strings are not allowed.")]
[DataRow(0, false, DisplayName = "Invalid Value: 0. Only values between 1 and 2147483647 are allowed along with -1.")]
[DataRow(-2, false, DisplayName = "Invalid Value: -2. Negative values are not allowed except -1.")]
[DataRow(2147483648, false, DisplayName = "Invalid Value: 2147483648. Only values between 1 and 2147483647 are allowed along with -1.")]
[DataRow("seven", true, DisplayName = "Invalid Value: 'seven'. Only integer values are allowed.")]
public void TestValidateConfigFailsWithInvalidGraphQLDepthLimit(object? depthLimit, bool isStringValue)
{
string depthLimitSection = isStringValue ? $@"""depth-limit"": ""{depthLimit}""" : $@"""depth-limit"": {depthLimit}";
string jsonData = TestHelper.GenerateConfigWithGivenDepthLimit(depthLimitSection);
// create an empty config file
((MockFileSystem)_fileSystem!).AddFile(TEST_RUNTIME_CONFIG_FILE, jsonData);
ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);
try
{
Assert.IsFalse(ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!));
}
catch (Exception ex)
{
Assert.Fail($"Unexpected Exception thrown: {ex.Message}");
}
}
/// <summary>
/// This Test is used to verify that DAB fails when the JWT properties are missing for OAuth based providers
/// </summary>
[DataTestMethod]
[DataRow("AzureAD")]
[DataRow("EntraID")]
[DataRow("Custom")]
public void TestMissingJwtProperties(string authScheme)
{
string ConfigWithJwtAuthentication = $"{{{SAMPLE_SCHEMA_DATA_SOURCE}, {RUNTIME_SECTION_JWT_AUTHENTICATION_PLACEHOLDER}, \"entities\": {{ }}}}";
ConfigWithJwtAuthentication = ConfigWithJwtAuthentication.Replace("<>", authScheme, StringComparison.OrdinalIgnoreCase);
// create an empty config file
((MockFileSystem)_fileSystem!).AddFile(TEST_RUNTIME_CONFIG_FILE, ConfigWithJwtAuthentication);
ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);
try
{
Assert.IsFalse(ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!));
}
catch (Exception ex)
{
Assert.Fail($"Unexpected Exception thrown: {ex.Message}");
}
}
/// <summary>
/// This Test is used to verify that the validate command is able to catch when data source field or entities field is missing.
/// </summary>
[TestMethod]
public void TestValidateConfigFailsWithNoEntities()
{
string ConfigWithoutEntities = $"{{{SAMPLE_SCHEMA_DATA_SOURCE},{RUNTIME_SECTION}}}";
// create an empty config file
((MockFileSystem)_fileSystem!).AddFile(TEST_RUNTIME_CONFIG_FILE, ConfigWithoutEntities);
ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);
try
{
Assert.IsFalse(ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!));
}
catch (Exception ex)
{
Assert.Fail($"Unexpected Exception thrown: {ex.Message}");
}
}
/// <summary>
/// This Test is used to verify that the validate command is able to catch when data source field is missing.
/// </summary>
[TestMethod]
public void TestValidateConfigFailsWithNoDataSource()
{
string ConfigWithoutDataSource = $"{{{SCHEMA_PROPERTY},{RUNTIME_SECTION_WITH_EMPTY_ENTITIES}}}";
// create an empty config file
((MockFileSystem)_fileSystem!).AddFile(TEST_RUNTIME_CONFIG_FILE, ConfigWithoutDataSource);
ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);
try
{
Assert.IsFalse(ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!));
}
catch (Exception ex)
{
Assert.Fail($"Unexpected Exception thrown: {ex.Message}");
}
}
/// <summary>
/// This method implicitly validates that RuntimeConfigValidator::ValidateConfigSchema(...) successfully
/// executes against a config file referencing environment variables.
/// [CLI] ConfigGenerator::IsConfigValid(...)
/// |_ [Engine] RuntimeConfigValidator::TryValidateConfig(...)
/// |_ [Engine] RuntimeConfigValidator::ValidateConfigSchema(...)
/// ValidateConfigSchema(...) doesn't execute successfully when a RuntimeConfig object has unresolved environment variables.
/// Example:
/// Input file snipppet:
/// "data-source": {
/// "database-type": "@env('DATABASE_TYPE')", // ENUM
/// "connection-string": "@env('CONN_STRING')" // STRING
/// }
/// ...
/// "source": {
/// "type": ""stored-procedure",
/// "object": "s001.book",
/// "parameters": {
/// "param1": "@env('sp_param1_int')", // INT
/// "param2": "@env('sp_param3_bool')" // BOOL
/// }
/// }
/// </summary>
[TestMethod]
public void ValidateConfigSchemaWhereConfigReferencesEnvironmentVariables()
{
// Arrange
Environment.SetEnvironmentVariable($"connection-string", SAMPLE_TEST_CONN_STRING);
Environment.SetEnvironmentVariable($"database-type", "mssql");
Environment.SetEnvironmentVariable($"sp_param1_int", "123");
Environment.SetEnvironmentVariable($"sp_param3_bool", "true");
// Capture console output to get error messaging.
StringWriter writer = new();
Console.SetOut(writer);
((MockFileSystem)_fileSystem!).AddFile(
path: TEST_RUNTIME_CONFIG_FILE,
mockFile: CONFIG_ENV_VARS);
ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);
// Act
ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!);
// Assert
string loggerOutput = writer.ToString();
Assert.IsFalse(
condition: loggerOutput.Contains("Failed to validate config against schema due to"),
message: "Unexpected errors encountered when validating config schema in RuntimeConfigValidator::ValidateConfigSchema(...).");
Assert.IsTrue(
condition: loggerOutput.Contains("The config satisfies the schema requirements."),
message: "RuntimeConfigValidator::ValidateConfigSchema(...) didn't communicate successful config schema validation.");
}
/// <summary>
/// Tests that validation fails when AKV options are configured without an endpoint.
/// </summary>
[TestMethod]
public async Task TestValidateAKVOptionsWithoutEndpointFails()
{
// Arrange
ConfigureOptions options = new(
azureKeyVaultRetryPolicyMaxCount: 1,
azureKeyVaultRetryPolicyDelaySeconds: 1,
azureKeyVaultRetryPolicyMaxDelaySeconds: 1,
azureKeyVaultRetryPolicyMode: AKVRetryPolicyMode.Exponential,
azureKeyVaultRetryPolicyNetworkTimeoutSeconds: 1,
config: TEST_RUNTIME_CONFIG_FILE
);
// Act
await ValidatePropertyOptionsFails(options);
}
/// <summary>
/// Tests that validation fails when Azure Log Analytics options are configured without the Auth options.
/// </summary>
[TestMethod]
public async Task TestValidateAzureLogAnalyticsOptionsWithoutAuthFails()
{
// Arrange
ConfigureOptions options = new(
azureLogAnalyticsEnabled: CliBool.True,
azureLogAnalyticsDabIdentifier: "dab-identifier-test",
azureLogAnalyticsFlushIntervalSeconds: 1,
config: TEST_RUNTIME_CONFIG_FILE
);
// Act
await ValidatePropertyOptionsFails(options);
}
/// <summary>
/// Tests that validation fails when File Sink options are configured without the 'path' property.
/// </summary>
[TestMethod]
public async Task TestValidateFileSinkOptionsWithoutPathFails()
{
// Arrange
ConfigureOptions options = new(
fileSinkEnabled: CliBool.True,
fileSinkRollingInterval: RollingInterval.Day,
fileSinkRetainedFileCountLimit: 1,
fileSinkFileSizeLimitBytes: 1024,
config: TEST_RUNTIME_CONFIG_FILE
);
// Act
await ValidatePropertyOptionsFails(options);
}
/// <summary>
/// Helper function that ensures properties with missing options fail validation.
/// </summary>
private async Task ValidatePropertyOptionsFails(ConfigureOptions options)
{
_fileSystem!.AddFile(TEST_RUNTIME_CONFIG_FILE, new MockFileData(INITIAL_CONFIG));
Assert.IsTrue(_fileSystem!.File.Exists(TEST_RUNTIME_CONFIG_FILE));
Mock<RuntimeConfigProvider> mockRuntimeConfigProvider = new(_runtimeConfigLoader);
RuntimeConfigValidator validator = new(mockRuntimeConfigProvider.Object, _fileSystem, new Mock<ILogger<RuntimeConfigValidator>>().Object);
Mock<ILoggerFactory> mockLoggerFactory = new();
Mock<ILogger<JsonConfigSchemaValidator>> mockLogger = new();
mockLoggerFactory
.Setup(factory => factory.CreateLogger(typeof(JsonConfigSchemaValidator).FullName!))
.Returns(mockLogger.Object);
// Act: Attempts to add File Sink options without empty path
bool isSuccess = TryConfigureSettings(options, _runtimeConfigLoader!, _fileSystem!);
// Assert: Settings are configured, config parses, validation fails.
Assert.IsTrue(isSuccess);
string updatedConfig = _fileSystem!.File.ReadAllText(TEST_RUNTIME_CONFIG_FILE);
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(updatedConfig, out RuntimeConfig? config));
JsonSchemaValidationResult result = await validator.ValidateConfigSchema(config, TEST_RUNTIME_CONFIG_FILE, mockLoggerFactory.Object);
Assert.IsFalse(result.IsValid);
}
}