|
| 1 | +using KustoSchemaTools.Changes; |
| 2 | +using KustoSchemaTools.Configuration; |
| 3 | +using KustoSchemaTools.Model; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Moq; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace KustoSchemaTools.Tests.Integration |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// End-to-end tests demonstrating the complete column order validation workflow |
| 12 | + /// for command-line usage scenarios. |
| 13 | + /// </summary> |
| 14 | + public class ColumnOrderValidationEndToEndTests |
| 15 | + { |
| 16 | + private readonly Mock<ILogger> _loggerMock; |
| 17 | + |
| 18 | + public ColumnOrderValidationEndToEndTests() |
| 19 | + { |
| 20 | + _loggerMock = new Mock<ILogger>(); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void EndToEnd_EnvironmentVariableNotSet_ValidationDisabled_AllowsInvalidColumnOrder() |
| 25 | + { |
| 26 | + // Arrange - Ensure environment variable is not set |
| 27 | + Environment.SetEnvironmentVariable("KUSTO_ENABLE_COLUMN_VALIDATION", null); |
| 28 | + |
| 29 | + try |
| 30 | + { |
| 31 | + var oldDb = CreateDatabase(("Table1", new[] { ("Col1", "string"), ("Col2", "int") })); |
| 32 | + var newDb = CreateDatabase(("Table1", new[] { ("Col1", "string"), ("NewCol", "bool"), ("Col2", "int") })); // Invalid order |
| 33 | + |
| 34 | + // Act - Use the same code path as command-line tools |
| 35 | + var validationSettings = ValidationSettings.FromEnvironment(); |
| 36 | + var changes = DatabaseChanges.GenerateChanges(oldDb, newDb, "TestDB", _loggerMock.Object, validationSettings); |
| 37 | + |
| 38 | + // Simulate checking if deployment should proceed |
| 39 | + var comments = changes.Select(itm => itm.Comment).Where(itm => itm != null).ToList(); |
| 40 | + var isValid = changes.All(itm => itm.Scripts.All(itm => itm.IsValid != false)) && comments.All(itm => itm.FailsRollout == false); |
| 41 | + |
| 42 | + // Assert - Should be valid because validation is disabled by default |
| 43 | + Assert.False(validationSettings.EnableColumnOrderValidation); |
| 44 | + Assert.True(isValid, "When validation is disabled, invalid column order should not block deployment"); |
| 45 | + |
| 46 | + // No validation comments should be present |
| 47 | + var tableChange = changes.FirstOrDefault(c => c.Entity == "Table1"); |
| 48 | + Assert.NotNull(tableChange); |
| 49 | + Assert.Null(tableChange.Comment); |
| 50 | + } |
| 51 | + finally |
| 52 | + { |
| 53 | + Environment.SetEnvironmentVariable("KUSTO_ENABLE_COLUMN_VALIDATION", null); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void EndToEnd_EnvironmentVariableSetToTrue_ValidationEnabled_BlocksInvalidColumnOrder() |
| 59 | + { |
| 60 | + // Arrange - Set environment variable to enable validation |
| 61 | + Environment.SetEnvironmentVariable("KUSTO_ENABLE_COLUMN_VALIDATION", "true"); |
| 62 | + |
| 63 | + try |
| 64 | + { |
| 65 | + var oldDb = CreateDatabase(("Table1", new[] { ("Col1", "string"), ("Col2", "int") })); |
| 66 | + var newDb = CreateDatabase(("Table1", new[] { ("Col1", "string"), ("NewCol", "bool"), ("Col2", "int") })); // Invalid order |
| 67 | + |
| 68 | + // Act - Use the same code path as command-line tools |
| 69 | + var validationSettings = ValidationSettings.FromEnvironment(); |
| 70 | + var changes = DatabaseChanges.GenerateChanges(oldDb, newDb, "TestDB", _loggerMock.Object, validationSettings); |
| 71 | + |
| 72 | + // Simulate checking if deployment should proceed |
| 73 | + var comments = changes.Select(itm => itm.Comment).Where(itm => itm != null).ToList(); |
| 74 | + var isValid = changes.All(itm => itm.Scripts.All(itm => itm.IsValid != false)) && comments.All(itm => itm.FailsRollout == false); |
| 75 | + |
| 76 | + // Assert - Should be invalid because validation is enabled and column order is wrong |
| 77 | + Assert.True(validationSettings.EnableColumnOrderValidation); |
| 78 | + Assert.False(isValid, "When validation is enabled, invalid column order should block deployment"); |
| 79 | + |
| 80 | + // Should have validation failure comment |
| 81 | + var tableChange = changes.FirstOrDefault(c => c.Entity == "Table1"); |
| 82 | + Assert.NotNull(tableChange); |
| 83 | + Assert.NotNull(tableChange.Comment); |
| 84 | + Assert.True(tableChange.Comment.FailsRollout); |
| 85 | + Assert.Contains("Column order violation", tableChange.Comment.Text); |
| 86 | + Assert.Equal(CommentKind.Caution, tableChange.Comment.Kind); |
| 87 | + } |
| 88 | + finally |
| 89 | + { |
| 90 | + Environment.SetEnvironmentVariable("KUSTO_ENABLE_COLUMN_VALIDATION", null); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void EndToEnd_EnvironmentVariableSetToTrue_ValidationEnabled_AllowsValidColumnOrder() |
| 96 | + { |
| 97 | + // Arrange - Set environment variable to enable validation |
| 98 | + Environment.SetEnvironmentVariable("KUSTO_ENABLE_COLUMN_VALIDATION", "true"); |
| 99 | + |
| 100 | + try |
| 101 | + { |
| 102 | + var oldDb = CreateDatabase(("Table1", new[] { ("Col1", "string"), ("Col2", "int") })); |
| 103 | + var newDb = CreateDatabase(("Table1", new[] { ("Col1", "string"), ("Col2", "int"), ("NewCol", "bool") })); // Valid order - new column at end |
| 104 | + |
| 105 | + // Act - Use the same code path as command-line tools |
| 106 | + var validationSettings = ValidationSettings.FromEnvironment(); |
| 107 | + var changes = DatabaseChanges.GenerateChanges(oldDb, newDb, "TestDB", _loggerMock.Object, validationSettings); |
| 108 | + |
| 109 | + // Simulate checking if deployment should proceed |
| 110 | + var comments = changes.Select(itm => itm.Comment).Where(itm => itm != null).ToList(); |
| 111 | + var isValid = changes.All(itm => itm.Scripts.All(itm => itm.IsValid != false)) && comments.All(itm => itm.FailsRollout == false); |
| 112 | + |
| 113 | + // Assert - Should be valid because validation is enabled but column order is correct |
| 114 | + Assert.True(validationSettings.EnableColumnOrderValidation); |
| 115 | + Assert.True(isValid, "When validation is enabled, valid column order should allow deployment"); |
| 116 | + |
| 117 | + // Should not have validation failure comment |
| 118 | + var tableChange = changes.FirstOrDefault(c => c.Entity == "Table1"); |
| 119 | + Assert.NotNull(tableChange); |
| 120 | + if (tableChange.Comment != null) |
| 121 | + { |
| 122 | + Assert.False(tableChange.Comment.FailsRollout); |
| 123 | + Assert.DoesNotContain("Column order violation", tableChange.Comment.Text); |
| 124 | + } |
| 125 | + } |
| 126 | + finally |
| 127 | + { |
| 128 | + Environment.SetEnvironmentVariable("KUSTO_ENABLE_COLUMN_VALIDATION", null); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + [Theory] |
| 133 | + [InlineData("true")] |
| 134 | + [InlineData("TRUE")] |
| 135 | + [InlineData("1")] |
| 136 | + public void EndToEnd_VariousEnvVariableFormats_EnableValidation(string envValue) |
| 137 | + { |
| 138 | + // Arrange |
| 139 | + Environment.SetEnvironmentVariable("KUSTO_ENABLE_COLUMN_VALIDATION", envValue); |
| 140 | + |
| 141 | + try |
| 142 | + { |
| 143 | + var oldDb = CreateDatabase(("Table1", new[] { ("Col1", "string"), ("Col2", "int") })); |
| 144 | + var newDb = CreateDatabase(("Table1", new[] { ("Col1", "string"), ("NewCol", "bool"), ("Col2", "int") })); // Invalid order |
| 145 | + |
| 146 | + // Act |
| 147 | + var validationSettings = ValidationSettings.FromEnvironment(); |
| 148 | + var changes = DatabaseChanges.GenerateChanges(oldDb, newDb, "TestDB", _loggerMock.Object, validationSettings); |
| 149 | + |
| 150 | + var comments = changes.Select(itm => itm.Comment).Where(itm => itm != null).ToList(); |
| 151 | + var isValid = changes.All(itm => itm.Scripts.All(itm => itm.IsValid != false)) && comments.All(itm => itm.FailsRollout == false); |
| 152 | + |
| 153 | + // Assert - All truthy values should enable validation and block deployment |
| 154 | + Assert.True(validationSettings.EnableColumnOrderValidation); |
| 155 | + Assert.False(isValid); |
| 156 | + |
| 157 | + var tableChange = changes.FirstOrDefault(c => c.Entity == "Table1"); |
| 158 | + Assert.NotNull(tableChange); |
| 159 | + Assert.NotNull(tableChange.Comment); |
| 160 | + Assert.True(tableChange.Comment.FailsRollout); |
| 161 | + } |
| 162 | + finally |
| 163 | + { |
| 164 | + Environment.SetEnvironmentVariable("KUSTO_ENABLE_COLUMN_VALIDATION", null); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + [Theory] |
| 169 | + [InlineData("false")] |
| 170 | + [InlineData("FALSE")] |
| 171 | + [InlineData("0")] |
| 172 | + [InlineData("invalid")] |
| 173 | + [InlineData("")] |
| 174 | + public void EndToEnd_VariousEnvVariableFormats_DisableValidation(string envValue) |
| 175 | + { |
| 176 | + // Arrange |
| 177 | + Environment.SetEnvironmentVariable("KUSTO_ENABLE_COLUMN_VALIDATION", envValue); |
| 178 | + |
| 179 | + try |
| 180 | + { |
| 181 | + var oldDb = CreateDatabase(("Table1", new[] { ("Col1", "string"), ("Col2", "int") })); |
| 182 | + var newDb = CreateDatabase(("Table1", new[] { ("Col1", "string"), ("NewCol", "bool"), ("Col2", "int") })); // Invalid order |
| 183 | + |
| 184 | + // Act |
| 185 | + var validationSettings = ValidationSettings.FromEnvironment(); |
| 186 | + var changes = DatabaseChanges.GenerateChanges(oldDb, newDb, "TestDB", _loggerMock.Object, validationSettings); |
| 187 | + |
| 188 | + var comments = changes.Select(itm => itm.Comment).Where(itm => itm != null).ToList(); |
| 189 | + var isValid = changes.All(itm => itm.Scripts.All(itm => itm.IsValid != false)) && comments.All(itm => itm.FailsRollout == false); |
| 190 | + |
| 191 | + // Assert - All falsy values should disable validation and allow deployment |
| 192 | + Assert.False(validationSettings.EnableColumnOrderValidation); |
| 193 | + Assert.True(isValid); |
| 194 | + |
| 195 | + var tableChange = changes.FirstOrDefault(c => c.Entity == "Table1"); |
| 196 | + Assert.NotNull(tableChange); |
| 197 | + Assert.Null(tableChange.Comment); |
| 198 | + } |
| 199 | + finally |
| 200 | + { |
| 201 | + Environment.SetEnvironmentVariable("KUSTO_ENABLE_COLUMN_VALIDATION", null); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + private static Database CreateDatabase(params (string TableName, (string Name, string Type)[] Columns)[] tables) |
| 206 | + { |
| 207 | + var database = new Database |
| 208 | + { |
| 209 | + Name = "TestDB", |
| 210 | + Tables = new Dictionary<string, Table>(), |
| 211 | + Admins = new List<AADObject>(), |
| 212 | + Users = new List<AADObject>(), |
| 213 | + Viewers = new List<AADObject>(), |
| 214 | + Monitors = new List<AADObject>(), |
| 215 | + Ingestors = new List<AADObject>(), |
| 216 | + UnrestrictedViewers = new List<AADObject>(), |
| 217 | + Functions = new Dictionary<string, Function>(), |
| 218 | + MaterializedViews = new Dictionary<string, MaterializedView>(), |
| 219 | + ContinuousExports = new Dictionary<string, ContinuousExport>(), |
| 220 | + ExternalTables = new Dictionary<string, ExternalTable>(), |
| 221 | + EntityGroups = new Dictionary<string, List<Entity>>(), |
| 222 | + Followers = new Dictionary<string, FollowerDatabase>(), |
| 223 | + Deletions = new Deletions(), |
| 224 | + Scripts = new List<DatabaseScript>() |
| 225 | + }; |
| 226 | + |
| 227 | + foreach (var (tableName, columns) in tables) |
| 228 | + { |
| 229 | + var table = new Table |
| 230 | + { |
| 231 | + Columns = new Dictionary<string, string>(), |
| 232 | + Folder = "", |
| 233 | + DocString = "", |
| 234 | + Scripts = new List<DatabaseScript>() |
| 235 | + }; |
| 236 | + |
| 237 | + foreach (var (name, type) in columns) |
| 238 | + { |
| 239 | + table.Columns[name] = type; |
| 240 | + } |
| 241 | + |
| 242 | + database.Tables[tableName] = table; |
| 243 | + } |
| 244 | + |
| 245 | + return database; |
| 246 | + } |
| 247 | + } |
| 248 | +} |
0 commit comments