Dynamic drift tests
Dynamically compare JSON files against expected values. Will recursively compare the JSON files and report any differences on a property-by-property basis.
Currently tests value mismatches, missing properties, null value and array size checks using the Compare-MtJsonObject function.
Usage
Test MT.1060 is a dynamic test that compares a JSON file against a baseline. This test is useful for validating configurations or settings that may change over time, ensuring they remain consistent with expected values. The dynamic nature of this test means you have to set them up correctly.
- Create a
driftfolder in your Maester repository. - Inside the
driftfolder, create one or more subfolders. This will be the name of the drift test. - In each subfolder, create a
baseline.jsonfile that contains the expected configuration or settings.
Test MT.1060 will automatically discover these subfolders and compare the baseline.json file against current.json file in the same folder. The current.json file should be generated by your application or script that produces the current configuration or settings, right before running the tests.
If you create drift\helloworld\baseline.json the tests will be called MT1060.helloworld.x.
All these tests are tagged with MT1060, MT1060.{number}, MT1060.{name} and MT1060.{name}.{number} where {name} is the name of the subfolder and {number} is the test number (1-4). You can use these tags to not run specific tests or to run all tests in a specific subfolder.
Settings
If you have a baseline.json where you want to skip some properties from being validated, you can create a settings.json file in the same folder as the baseline.json file.
{
"ExcludeProperties": [
"someString"
]
}
This will exclude the someString property from being validated in the drift test.
Description
MT1060 actually contains of 4 tests
MT1060.{name}.1
This tests validates if the baseline.json file exists in the drift folder and is valid json.
MT1060.{name}.2
This tests validates if the current.json file exists in the drift folder and is valid json.
MT1060.{name}.3
This tests checks if there are no missing properties in the current.json file compared to the baseline.json file. If there are missing properties, it will report them as issues.
MT1060.{name}.4
This tests checks if there are properties that don't have the same value in the current.json file compared to the baseline.json file. If there are properties with different values, it will report them as issues.
Example
With this drift\HelloWorld\baseline.json file:
{
"name": "Hello World",
"version": "1.0.0",
"description": "A simple hello world application",
"settings": {
"greeting": "Hello, World!",
"language": "en-US"
}
}
And this drift\HelloWorld\current.json file:
{
"name": "Hello World!",
"version": "1.0.1",
"settings": {
"greeting": "Hello, Universe",
"language": "en-UK"
}
}
Will result in the following issues:
MT1060.HelloWorld.1
Passed ✅
MT1060.HelloWorld.2
Passed ✅
MT1060.HelloWorld.3
Test result ❌
The following properties are in the baseline but not in the current:
description
MT1060.HelloWorld.4
Test result ❌
| Property | Reason | Expected Value | Actual Value | Description |
|---|---|---|---|---|
name | ValueMismatch | Hello World | Hello World! | Value mismatch at name |
version | ValueMismatch | 1.0.0 | 1.0.1 | Value mismatch at version |
settings.greeting | ValueMismatch | Hello, World! | Hello, Universe | Value mismatch at settings.greeting |
settings.language | ValueMismatch | en-US | en-UK | Value mismatch at settings.language |