%3CLINGO-SUB%20id%3D%22lingo-sub-1500276%22%20slang%3D%22en-US%22%3EGetting%20Started%20with%20Testing%20Infrastructure%20Code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1500276%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHi%2C%20my%20name%20is%20Bernie%20White%2C%20I%20am%20a%20Premier%20Field%20Engineer%20at%20Microsoft.%20Today%20I%E2%80%99d%20like%20to%20show%20you%20how%20you%20can%20get%20started%20with%20testing%20infrastructure%20code%20using%20an%20open%20source%20project%20called%20PSRule%20within%20Azure%20Pipelines.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWithin%20a%20DevOps%20culture%2C%20implementing%20testing%20embedded%20in%20a%20continuous%20integration%20(CI)%20process%20is%20an%20essential%20practice.%20Integrating%20testing%20into%20a%20CI%20process%20allows%20issues%20to%20be%20found%20early%2C%20ideally%20before%20code%20is%20released%20into%20an%20environment.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20application%20code%2C%20testing%20within%20CI%20is%20widely%20accepted%20and%20adopted%20as%20a%20best%20practice.%3C%2FP%3E%0A%3CP%3ETesting%20infrastructure%20code%2C%20on%20the%20other%20hand%20is%20often%20less%20widely%20adopted.%20Most%20customers%20understand%20the%20benefits%20of%20implementing%20security%2C%20best%20practices%20and%20organization%20standards%20into%20CI%20but%20are%20not%20comfortable%20implementing%20such%20controls.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20post%20aims%20to%20introduce%20you%20to%20an%20open%20source%20tool%20that%20allows%20you%20to%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EQuickly%20implement%20best%20practice%20testing%20within%20CI%20for%20Azure%20Resource%20Manager%20(ARM)%20templates.%3C%2FLI%3E%0A%3CLI%3EEvolve%20over%20time%20to%20meet%20organizational%20standards%20and%20compliance%20requirements.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1301947608%22%20id%3D%22toc-hId--1301947608%22%3EIntroducing%20PSRule%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPSRule%20is%20a%20cross-platform%20PowerShell%20module%20(Windows%2C%20Linux%2C%20and%20MacOS)%20with%20commands%20to%20test%20infrastructure%20code.%20PSRule%20is%20modular%2C%20allowing%20the%20creation%20of%20rules%20using%20PowerShell%20code.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FPSRule%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fmicrosoft%2FPSRule%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20Azure%2C%20a%20set%20of%20over%20100%20rules%20already%20exist.%20These%20rules%20can%20be%20added%20to%20an%20Infrastructure%20as%20Code%20(IaC)%20pipeline%20quickly%20and%20then%20extended%20as%20organizational%20requirements%20and%20standards%20mature.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FPSRule.Rules.Azure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fmicrosoft%2FPSRule.Rules.Azure%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThree%20(3)%20steps%20for%20getting%20started%20with%20PSRule%20for%20Azure%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ELink%20parameters%20files%20to%20templates%3C%2FLI%3E%0A%3CLI%3EConfigure%20a%20CI%20pipeline%3C%2FLI%3E%0A%3CLI%3ESuppress%20exceptions%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1185565225%22%20id%3D%22toc-hId-1185565225%22%3EStep%201%3A%20Linking%20parameters%20files%20to%20templates%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20of%20the%20challenges%20testing%20infrastructure%20code%20is%20that%20ARM%20templates%20are%20designed%20to%20be%20reusable.%20Template%20reusability%20is%20a%20big%20pro%2C%20however%20in%20the%20context%20of%20testing%20it%20is%20hard%20to%20understand%20if%20a%20templated%20resource%20is%20compliant.%20A%20simple%20parameter%20may%20be%20the%20difference%20between%20a%20compliant%20or%20non-compliant%20configuration.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConsider%20%3CEM%3EsecureTransferRequired%20%3D%20false%3C%2FEM%3E%2C%20does%20not%20enforce%20encrypted%20data%20transfer%20for%20a%20storage%20account%20while%20%3CEM%3EsecureTransferRequired%20%3D%20true%3C%2FEM%3E%20does.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%20%20%20%20%22comments%22%3A%20%22Storage%20Account%22%2C%0A%20%20%20%20%22type%22%3A%20%22Microsoft.Storage%2FstorageAccounts%22%2C%0A%20%20%20%20%22apiVersion%22%3A%20%222019-06-01%22%2C%0A%20%20%20%20%22name%22%3A%20%22%5Bparameters('storageAccountName')%5D%22%2C%0A%20%20%20%20%22location%22%3A%20%22%5Bparameters('location')%5D%22%2C%0A%20%20%20%20%22sku%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%22%5Bparameters('sku')%5D%22%2C%0A%20%20%20%20%20%20%20%20%22tier%22%3A%20%22Standard%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22kind%22%3A%20%22StorageV2%22%2C%0A%20%20%20%20%22properties%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22supportsHttpsTrafficOnly%22%3A%20%22%5Bparameters('secureTransferRequired')%5D%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22tags%22%3A%20%22%5Bparameters('tags')%5D%22%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20solve%20this%20context%20issue%2C%20PSRule%20provides%20a%20method%20to%20link%20parameter%20files%20to%20template%20files%20and%20resolve%20parameters%2C%20variables%2C%20conditions%20and%20functions%20within%20the%20ARM%20template.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20link%20parameter%20files%20to%20a%20template%20file%2C%20include%20a%20metadata%20block%20within%20the%20parameter%20file.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%20%20%20%20%22%24schema%22%3A%20%22https%3A%2F%2Fschema.management.azure.com%2Fschemas%2F2015-01-01%2FdeploymentParameters.json%23%22%2C%0A%20%20%20%20%22contentVersion%22%3A%20%221.0.0.0%22%2C%0A%20%20%20%20%22metadata%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22template%22%3A%20%22.%2FResources.Storage.Template.json%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22parameters%22%3A%20%7B%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20metadata%20block%20is%20ignored%20by%20ARM.%20By%20specifying%20the%20%3CEM%3Emetadata.template%3C%2FEM%3E%20property%2C%20we%20can%20identify%20the%20specific%20template%20this%20parameter%20file%20refers%20to.%20The%20example%20above%20refers%20to%20a%20template%20file%20in%20the%20same%20directory%20as%20the%20parameter%20file%20by%20using%20%E2%80%9C.%2F%E2%80%9D.%20Similarly%2C%20the%20following%20example%20refers%20to%20a%20template%20file%20relative%20to%20the%20current%20working%20directory%2C%20which%20would%20for%20this%20example%20be%20the%20root%20of%20the%20repository.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%20%20%20%20%22%24schema%22%3A%20%22https%3A%2F%2Fschema.management.azure.com%2Fschemas%2F2015-01-01%2FdeploymentParameters.json%23%22%2C%0A%20%20%20%20%22contentVersion%22%3A%20%221.0.0.0%22%2C%0A%20%20%20%20%22metadata%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22template%22%3A%20%22templates%2Fkeyvault%2Fv1%2Ftemplate.json%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22parameters%22%3A%20%7B%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--621889238%22%20id%3D%22toc-hId--621889238%22%3EStep%202%3A%20Configure%20a%20CI%20pipeline%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20example%2C%20we%20are%20going%20to%20use%20the%20PSRule%20extension%20for%20Azure%20DevOps%20in%20YAML%20pipelines.%20Install%20the%20PSRule%20extension%20from%20the%20Virtual%20Marketplace%20here%3A%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dbewhite.ps-rule%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dbewhite.ps-rule%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfter%20installing%20the%20extension%2C%20create%20a%20new%20YAML%20pipeline%20by%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ESigning%20into%20your%20Azure%20DevOps%20organization%20and%20navigate%20to%20your%20project.%3C%2FLI%3E%0A%3CLI%3EIn%20your%20project%2C%20navigate%20to%20the%20Pipelines%20page.%20Then%20choose%20the%20action%20to%20create%20a%20new%20pipeline.%3C%2FLI%3E%0A%3CLI%3EWalk%20through%20the%20steps%20of%20the%20wizard%20by%20first%20selecting%20%3CEM%3EAzure%20Repos%20Git%20(YAML)%3C%2FEM%3E%20as%20the%20location%20of%20your%20source%20code.%3C%2FLI%3E%0A%3CLI%3EWhen%20the%20list%20of%20repositories%20appears%2C%20select%20your%20desired%20infrastructure%20code%20repository.%3C%2FLI%3E%0A%3CLI%3EChoose%20the%20starter%20pipeline%20template.%3C%2FLI%3E%0A%3CLI%3EReplace%20the%20contents%20of%20%3CEM%3Eazure-pipelines.yml%3C%2FEM%3E%20with%20the%20following%20code.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-yaml%22%3E%3CCODE%3E%23%0A%23%20PSRule%20with%20Azure%20Pipelines%0A%23%0A%0Atrigger%3A%0A-%20master%0A%0Apool%3A%0A%20%20vmImage%3A%20'ubuntu-latest'%0A%0Asteps%3A%0A%0A%23%20Install%20PSRule.Rules.Azure%20from%20the%20PowerShell%20Gallery%0A-%20task%3A%20ps-rule-install%400%0A%20%20inputs%3A%0A%20%20%20%20module%3A%20PSRule.Rules.Azure%20%20%20%23%20Install%20PSRule.Rules.Azure%20from%20the%20PowerShell%20Gallery.%0A%0A%23%20Export%20resource%20data%20from%20parameter%20files%20within%20the%20current%20working%20directory.%0A-%20powershell%3A%20Get-AzRuleTemplateLink%20%7C%20Export-AzTemplateRuleData%20-OutputPath%20out%2Ftemplates%2F%3B%0A%20%20displayName%3A%20'Export%20template%20data'%0A%0A%23%20Run%20analysis%20from%20JSON%20files%20using%20the%20%60PSRule.Rules.Azure%60%20module%20and%20custom%20rules%20from%20%60.ps-rule%2F%60.%0A-%20task%3A%20ps-rule-assert%400%0A%20%20inputs%3A%0A%20%20%20%20inputType%3A%20inputPath%0A%20%20%20%20inputPath%3A%20'out%2Ftemplates%2F*.json'%20%20%20%20%20%20%20%20%23%20Read%20exported%20resource%20data%20from%20'out%2Ftemplates%2F'.%0A%20%20%20%20modules%3A%20'PSRule.Rules.Azure'%20%20%20%20%20%20%20%20%20%20%20%20%23%20Analyze%20objects%20using%20the%20rules%20within%20the%20PSRule.Rules.Azure%20PowerShell%20module.%0A%20%20%20%20%23%20Optionally%2C%20also%20analyze%20objects%20using%20custom%20rules%20from%20'.ps-rule%2F'.%0A%20%20%20%20source%3A%20'.ps-rule%2F'%0A%20%20%20%20%23%20Optionally%2C%20save%20results%20to%20an%20NUnit%20report.%0A%20%20%20%20outputFormat%3A%20NUnit3%0A%20%20%20%20outputPath%3A%20reports%2Fps-rule-resources.xml%0A%0A%23%20Publish%20NUnit%20report%20as%20test%20results%0A-%20task%3A%20PublishTestResults%402%0A%20%20displayName%3A%20'Publish%20PSRule%20results'%0A%20%20inputs%3A%0A%20%20%20%20testRunTitle%3A%20'PSRule'%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20The%20title%20to%20use%20for%20the%20test%20run.%0A%20%20%20%20testRunner%3A%20NUnit%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Import%20report%20using%20the%20NUnit%20format.%0A%20%20%20%20testResultsFiles%3A%20'reports%2Fps-rule-*.xml'%20%20%20%20%20%20%20%23%20Use%20previously%20saved%20NUnit%20reports.%0A%20%20%20%20mergeTestResults%3A%20true%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Merge%20multiple%20reports.%0A%20%20condition%3A%20succeededOrFailed()%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Run%20this%20task%20if%20previous%20steps%20succeeded%20of%20failed.%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ESelect%20Save%20and%20run%2C%20then%20select%20Commit%2C%20and%20then%20choose%20Save%20and%20run%20again.%3C%2FLI%3E%0A%3CLI%3EA%20new%20run%20is%20started.%20Wait%20for%20the%20run%20to%20finish.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIf%20any%20issue%20were%20found%2C%20they%20will%20be%20called%20out%20as%20errors%20in%20the%20output.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22psrule-one-check-failed.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F202589i0BA769FAA301D565%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22psrule-one-check-failed.png%22%20alt%3D%22Errors%20on%20a%20pull%20request%20blocking%20a%20merge%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EErrors%20on%20a%20pull%20request%20blocking%20a%20merge%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22BernieWhite_0-1593555305108.png%22%20style%3D%22width%3A%20999px%3B%22%3E-ERR%3AREF-NOT-FOUND-%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EErrors%20from%20pipeline%20run%20in%20Azure%20Pipelines%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22BernieWhite_1-1593555305157.png%22%20style%3D%22width%3A%20999px%3B%22%3E-ERR%3AREF-NOT-FOUND-%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EJob%20output%20from%20a%20previous%20Azure%20Pipeline%20run%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1865623595%22%20id%3D%22toc-hId-1865623595%22%3EStep%203%3A%20Suppress%20exceptions%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhile%20best%20practices%20are%20great%2C%20there%20are%20situations%20that%20may%20need%20to%20deviate%20from%20pre-built%20rules.%20To%20do%20this%2C%20we%20can%20suppress%20or%20exclude%20rules%20from%20running.%20To%20add%20an%20exception%2C%20create%20the%20%3CEM%3Eps-rule.yaml%3C%2FEM%3E%20file%20in%20the%20root%20of%20the%20repository.%20Add%20a%20key%20for%20each%20rule%20name%20to%20be%20suppressed%20and%20an%20array%20of%20suppressed%20objects%20under%20the%20suppression%20property.%20The%20rule%20name%20and%20object%20name%20can%20be%20retrieved%20from%20the%20error%20output.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20example%20below%20%3CEM%3EAzure.AKS.Version%3C%2FEM%3E%20is%20the%20name%20of%20the%20rule%2C%20and%20%3CEM%3Ecluster1%3C%2FEM%3E%20is%20the%20name%20of%20the%20object%20to%20be%20suppressed.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-yaml%22%3E%3CCODE%3E%23%20YAML%3A%20Using%20the%20suppression%20property%0Asuppression%3A%0A%20%20Azure.AKS.Version%3A%20%23%20Rule%20name%0A%20%20-%20cluster1%20%20%20%20%20%20%20%20%20%23%20Suppressed%20object%201%0A%20%20-%20cluster2%20%20%20%20%20%20%20%20%20%23%20Suppressed%20object%202%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20also%20read%20PSRule%20-ERR%3AREF-NOT-FOUND-FAQ%20for%20additional%20options.%20You%20can%20also%20report%20an%20issue%20here%3A%20%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F202387i2AF42EB98507117E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22BernieWhite_0-1593555305108.png%22%20alt%3D%22Errors%20from%20pipeline%20run%20in%20Azure%20Pipelines%22%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FPSRule.Rules.Azure%2Fissues%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fmicrosoft%2FPSRule.Rules.Azure%2Fissues%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20closing%2C%20I%20hope%20this%20post%20gives%20you%20a%20few%20ideas%20to%20start%20moving%20forward%20with%20testing%20infrastructure%20code.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CSTRONG%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%3EDisclaimer%3C%2FFONT%3E%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%3E%3C%2FFONT%3E%3C%2FSTRONG%3E%3CFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%3EThe%20sample%20scripts%20are%20not%20supported%20under%20any%20Microsoft%20standard%20support%20program%20or%20service.%20The%20sample%20scripts%20are%20provided%20AS%20IS%20without%20warranty%20of%20any%20kind.%20Microsoft%20further%20disclaims%20all%20implied%20warranties%20including%2C%20without%20limitation%2C%20any%20implied%20warranties%20of%20merchantability%20or%20of%20fitness%20for%20a%20particular%20purpose.%20The%20entire%20risk%20arising%20out%20of%20the%20use%20or%20performance%20of%20the%20sample%20scripts%20and%20documentation%20remains%20with%20you.%20In%20no%20event%20shall%20Microsoft%2C%20its%20authors%2C%20or%20anyone%20else%20involved%20in%20the%20creation%2C%20production%2C%20or%20delivery%20of%20the%20scripts%20be%20liable%20for%20any%20damages%20whatsoever%20(including%2C%20without%20limitation%2C%20damages%20for%20loss%20of%20business%20profits%2C%20business%20interruption%2C%20loss%20of%20business%20information%2C%20or%20other%20pecuniary%20loss)%20arising%20out%20of%20the%20use%20of%20or%20inability%20to%20use%20the%20sample%20scripts%20or%20documentation%2C%20even%20if%20Microsoft%20has%20been%20advised%20of%20the%20possibility%20of%20such%20damages.%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1500276%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20post%20I%E2%80%99d%20like%20to%20show%20you%20how%20you%20can%20get%20started%20with%20testing%20infrastructure%20code%20in%20three%20steps.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22psrule-one-check-failed-02.png%22%20style%3D%22width%3A%20682px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F202588i7639F9BA22BF4D63%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22psrule-one-check-failed-02.png%22%20alt%3D%22psrule-one-check-failed-02.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1500276%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBernieWhite%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

 

Hi, my name is Bernie White, I am a Premier Field Engineer at Microsoft. Today I’d like to show you how you can get started with testing infrastructure code using an open source project called PSRule within Azure Pipelines.

 

Within a DevOps culture, implementing testing embedded in a continuous integration (CI) process is an essential practice. Integrating testing into a CI process allows issues to be found early, ideally before code is released into an environment.

 

For application code, testing within CI is widely accepted and adopted as a best practice.

Testing infrastructure code, on the other hand is often less widely adopted. Most customers understand the benefits of implementing security, best practices and organization standards into CI but are not comfortable implementing such controls.

 

This post aims to introduce you to an open source tool that allows you to:

  1. Quickly implement best practice testing within CI for Azure Resource Manager (ARM) templates.
  2. Evolve over time to meet organizational standards and compliance requirements.

 

Introducing PSRule

 

PSRule is a cross-platform PowerShell module (Windows, Linux, and MacOS) with commands to test infrastructure code. PSRule is modular, allowing the creation of rules using PowerShell code.

 

https://github.com/microsoft/PSRule

 

For Azure, a set of over 100 rules already exist. These rules can be added to an Infrastructure as Code (IaC) pipeline quickly and then extended as organizational requirements and standards mature.

 

https://github.com/microsoft/PSRule.Rules.Azure

 

Three (3) steps for getting started with PSRule for Azure:

  1. Link parameters files to templates
  2. Configure a CI pipeline
  3. Suppress exceptions

 

Step 1: Linking parameters files to templates

 

One of the challenges testing infrastructure code is that ARM templates are designed to be reusable. Template reusability is a big pro, however in the context of testing it is hard to understand if a templated resource is compliant. A simple parameter may be the difference between a compliant or non-compliant configuration.

 

Consider secureTransferRequired = false, does not enforce encrypted data transfer for a storage account while secureTransferRequired = true does.

 

 

{
    "comments": "Storage Account",
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2019-06-01",
    "name": "[parameters('storageAccountName')]",
    "location": "[parameters('location')]",
    "sku": {
        "name": "[parameters('sku')]",
        "tier": "Standard"
    },
    "kind": "StorageV2",
    "properties": {
        "supportsHttpsTrafficOnly": "[parameters('secureTransferRequired')]"
    },
    "tags": "[parameters('tags')]"
}

 

 

To solve this context issue, PSRule provides a method to link parameter files to template files and resolve parameters, variables, conditions and functions within the ARM template.

 

To link parameter files to a template file, include a metadata block within the parameter file.

 

 

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "metadata": {
        "template": "./Resources.Storage.Template.json"
    },
    "parameters": { }
}

 

 

The metadata block is ignored by ARM. By specifying the metadata.template property, we can identify the specific template this parameter file refers to. The example above refers to a template file in the same directory as the parameter file by using “./”. Similarly, the following example refers to a template file relative to the current working directory, which would for this example be the root of the repository.

 

 

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "metadata": {
        "template": "templates/keyvault/v1/template.json"
    },
    "parameters": {}
}

 

 

Step 2: Configure a CI pipeline

 

In the example, we are going to use the PSRule extension for Azure DevOps in YAML pipelines. Install the PSRule extension from the Virtual Marketplace here: https://marketplace.visualstudio.com/items?itemName=bewhite.ps-rule

 

After installing the extension, create a new YAML pipeline by:

  • Signing into your Azure DevOps organization and navigate to your project.
  • In your project, navigate to the Pipelines page. Then choose the action to create a new pipeline.
  • Walk through the steps of the wizard by first selecting Azure Repos Git (YAML) as the location of your source code.
  • When the list of repositories appears, select your desired infrastructure code repository.
  • Choose the starter pipeline template.
  • Replace the contents of azure-pipelines.yml with the following code.

 

 

#
# PSRule with Azure Pipelines
#

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:

# Install PSRule.Rules.Azure from the PowerShell Gallery
- task: ps-rule-install@0
  inputs:
    module: PSRule.Rules.Azure   # Install PSRule.Rules.Azure from the PowerShell Gallery.

# Export resource data from parameter files within the current working directory.
- powershell: Get-AzRuleTemplateLink | Export-AzTemplateRuleData -OutputPath out/templates/;
  displayName: 'Export template data'

# Run analysis from JSON files using the `PSRule.Rules.Azure` module and custom rules from `.ps-rule/`.
- task: ps-rule-assert@0
  inputs:
    inputType: inputPath
    inputPath: 'out/templates/*.json'        # Read exported resource data from 'out/templates/'.
    modules: 'PSRule.Rules.Azure'            # Analyze objects using the rules within the PSRule.Rules.Azure PowerShell module.
    # Optionally, also analyze objects using custom rules from '.ps-rule/'.
    source: '.ps-rule/'
    # Optionally, save results to an NUnit report.
    outputFormat: NUnit3
    outputPath: reports/ps-rule-resources.xml

# Publish NUnit report as test results
- task: PublishTestResults@2
  displayName: 'Publish PSRule results'
  inputs:
    testRunTitle: 'PSRule'                          # The title to use for the test run.
    testRunner: NUnit                               # Import report using the NUnit format.
    testResultsFiles: 'reports/ps-rule-*.xml'       # Use previously saved NUnit reports.
    mergeTestResults: true                          # Merge multiple reports.
  condition: succeededOrFailed()                    # Run this task if previous steps succeeded of failed.

 

 

  • Select Save and run, then select Commit, and then choose Save and run again.
  • A new run is started. Wait for the run to finish.

If any issue were found, they will be called out as errors in the output.

 

Errors on a pull request blocking a mergeErrors on a pull request blocking a merge

 

Errors from pipeline run in Azure PipelinesErrors from pipeline run in Azure Pipelines

 

Job output from a previous Azure Pipeline runJob output from a previous Azure Pipeline run

 

Step 3: Suppress exceptions

 

While best practices are great, there are situations that may need to deviate from pre-built rules. To do this, we can suppress or exclude rules from running. To add an exception, create the ps-rule.yaml file in the root of the repository. Add a key for each rule name to be suppressed and an array of suppressed objects under the suppression property. The rule name and object name can be retrieved from the error output.

 

In the example below Azure.AKS.Version is the name of the rule, and cluster1 is the name of the object to be suppressed.

 

 

# YAML: Using the suppression property
suppression:
  Azure.AKS.Version: # Rule name
  - cluster1         # Suppressed object 1
  - cluster2         # Suppressed object 2

 

 

You can also read PSRule FAQ for additional options. You can also report an issue here: https://github.com/microsoft/PSRule.Rules.Azure/issues

 

In closing, I hope this post gives you a few ideas to start moving forward with testing infrastructure code.

 

Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.