AZDOPS / AzDM

MIT License
5 stars 1 forks source link

Pester tests in verify pipeline to verify all json values match the supported pattern. #4

Open bjompen opened 1 month ago

bjompen commented 1 month ago

It should be fairly easy to set up pester tests that does basic pattern validation on all fields in the json templates. For example, a test case structure with something like this would make it fairly simple to add and validate the input before trying to run any code.

$testCases = @(
  @{
    FileName = 'config.json'
    Setting = 'core/excludeProjects'
    Pattern = '[A-Za-z]'
  },
  @{
    FileName = '/myProject/repos/myProject.repos.json'
    Setting = 'repos.names'
    Pattern = '[A-Za-z0-9-.]'
  },
  @{
    FileName = '/myProject/pipelines/myProject.pipelines.json'
    Setting = 'pipelines.names'
    Pattern = '[A-Za-z0-9-.]'
  }
)

Describe 'Verifying settings' {
  It 'Setting <_.Setting> should match the pattern <_.Pattern>' -TestCases $testCases {
    $fileContent = Get-Content "$($AZDMGlobalConfig.azdm_core.rootfolder)\$($_.FileName)" | ConvertFrom-Json
    $fileContent."$($_.Setting)" | Should -Match $_.Pattern
  }
}
bjompen commented 1 month ago

Bonus - and simpler. Add pester to the verify pipeline to make sure all needed properties are in place. For example: In projectName.json we must have the "project:{} key set and the folder settings for repos, pipelines, and artifacts. Verify the properties are there and if they are not, do not allow merge since the build will fail!