From d1c63b768fe39651f222d23f6b24e3fb3d04b757 Mon Sep 17 00:00:00 2001 From: Luis Schroeder Date: Thu, 7 Mar 2024 15:28:09 +0100 Subject: [PATCH 1/3] Implemented functionality to convert the test result format into JSON via piping --- arm-ttk/Format-Json.ps1 | 55 +++++++++++++++++++++++++++++++++++++++++ arm-ttk/README.md | 9 ++++++- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 arm-ttk/Format-Json.ps1 diff --git a/arm-ttk/Format-Json.ps1 b/arm-ttk/Format-Json.ps1 new file mode 100644 index 00000000..e7dcc6c7 --- /dev/null +++ b/arm-ttk/Format-Json.ps1 @@ -0,0 +1,55 @@ +Function Format-Json { + <# + .SYNOPSIS + Takes results from ARMTTK and exports them as a JSON blob. + .DESCRIPTION + Takes results from ARMTTK and exports them as JSON. The test cases include the filename, name of the test, + whether the test was successful, and help text for how to resolve the error if the test failed. + #> + [CmdletBinding()] + Param ( + # Object containing a single test result or an array of test results + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [psobject]$TestResult + ) + + Begin { + # Initialize the array to collect processed test cases + $TestCases = @() + } + + Process { + # Process each TestResult item one by one as they come from the pipeline + $TestCase = @{ + filepath = $TestResult.file.fullpath + name = $TestResult.name + success = $TestResult.Passed + } + + if ($TestResult.Passed) { + $TestCase.optional = $false + } + elseif ($null -ne $($TestResult.Warnings)) { + $TestCase.optional = $true + $TestCase.message = "$($TestResult.Warnings.Message.Replace('"', '\"')) in template file $($TestResult.file.name)" + } + elseif ($null -ne $($TestResult.Errors)) { + $TestCase.optional = $false + $TestCase.message = "$($TestResult.Errors.Exception.Message.Replace('"', '\"')) in template file $($TestResult.file.name)" + } + else { + $TestCase.optional = $true + $TestCase.message = "Unknown error in template file " + $TestResult.file.name + } + + $TestCases += $TestCase + } + + End { + # Convert the array of hashtables to JSON + $JSON = $TestCases | ConvertTo-Json -Depth 5 + + # Print the JSON string to the console + Write-Output $JSON + } +} \ No newline at end of file diff --git a/arm-ttk/README.md b/arm-ttk/README.md index 9ff36d78..02b772b7 100644 --- a/arm-ttk/README.md +++ b/arm-ttk/README.md @@ -24,7 +24,14 @@ This will run the full suite of applicable tests on your template. To run a spe You can also skip tests any tests: Test-AzTemplate -TemplatePath $thePathToYourTemplate -Skip apiVersions-Should-Be-Recent - # This will exclude the tests indicated by the -Skip parameter from the test run and results + # This will exclude the tests indicated by the -Skip parameter from the test run and results + +You can also change the output format to JSON: + + Test-AzTemplate -TemplatePath $thePathToYourTemplate | Format-Json + # This will output the test results from Test-AzTemplate to the console in JSON format + Test-AzMarketplacePackage -TemplatePath $thePathToYourTemplate | Format-Json + # This will output the test results from AzMarketplacePackage to the console in JSON format ## Running Tests on Linux From 4ee4d5eb83769f39a910f9ddd8bda46914c1757d Mon Sep 17 00:00:00 2001 From: Luis Gaspar Schroeder Date: Thu, 21 Mar 2024 11:51:00 +0100 Subject: [PATCH 2/3] Removed the Depth parameter --- arm-ttk/Format-Json.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arm-ttk/Format-Json.ps1 b/arm-ttk/Format-Json.ps1 index e7dcc6c7..12c91994 100644 --- a/arm-ttk/Format-Json.ps1 +++ b/arm-ttk/Format-Json.ps1 @@ -47,7 +47,7 @@ Function Format-Json { End { # Convert the array of hashtables to JSON - $JSON = $TestCases | ConvertTo-Json -Depth 5 + $JSON = $TestCases | ConvertTo-Json # Print the JSON string to the console Write-Output $JSON From 4427fdab9bc59661b803beeca84a089302f39bc3 Mon Sep 17 00:00:00 2001 From: Luis Gaspar Schroeder Date: Thu, 21 Mar 2024 11:55:40 +0100 Subject: [PATCH 3/3] Improved functionality description --- arm-ttk/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arm-ttk/README.md b/arm-ttk/README.md index 02b772b7..d384f9b8 100644 --- a/arm-ttk/README.md +++ b/arm-ttk/README.md @@ -26,7 +26,7 @@ You can also skip tests any tests: Test-AzTemplate -TemplatePath $thePathToYourTemplate -Skip apiVersions-Should-Be-Recent # This will exclude the tests indicated by the -Skip parameter from the test run and results -You can also change the output format to JSON: +You can also change the output format to get detailed test result descriptions in JSON: Test-AzTemplate -TemplatePath $thePathToYourTemplate | Format-Json # This will output the test results from Test-AzTemplate to the console in JSON format