2020-01-07 17:05:08 +03:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
2019-05-08 01:46:11 +03:00
|
|
|
#
|
|
|
|
# Unit tests for Kubernetes metadata rules
|
|
|
|
#
|
|
|
|
|
|
|
|
[CmdletBinding()]
|
2020-01-07 17:05:08 +03:00
|
|
|
param ()
|
2019-05-08 01:46:11 +03:00
|
|
|
|
2022-12-11 11:03:06 +03:00
|
|
|
BeforeAll {
|
|
|
|
# Setup error handling
|
|
|
|
$ErrorActionPreference = 'Stop';
|
|
|
|
Set-StrictMode -Version latest;
|
2019-05-08 01:46:11 +03:00
|
|
|
|
2022-12-11 11:03:06 +03:00
|
|
|
if ($Env:SYSTEM_DEBUG -eq 'true') {
|
|
|
|
$VerbosePreference = 'Continue';
|
|
|
|
}
|
2019-05-08 01:46:11 +03:00
|
|
|
|
2022-12-11 11:03:06 +03:00
|
|
|
# Setup tests paths
|
|
|
|
$rootPath = $PWD;
|
|
|
|
Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSRule.Rules.Kubernetes) -Force;
|
|
|
|
$here = (Resolve-Path $PSScriptRoot).Path;
|
|
|
|
}
|
2019-05-08 01:46:11 +03:00
|
|
|
|
|
|
|
Describe 'Kubernetes.Metadata' {
|
2022-12-11 11:03:06 +03:00
|
|
|
BeforeAll {
|
|
|
|
$testParams = @{
|
|
|
|
Module = 'PSRule.Rules.Kubernetes'
|
|
|
|
Option = Join-Path -Path $here -ChildPath ps-rule.yaml
|
|
|
|
InputPath = Join-Path -Path $here -ChildPath Resources.Metadata.yaml
|
|
|
|
}
|
2019-05-08 01:46:11 +03:00
|
|
|
|
2022-12-11 11:03:06 +03:00
|
|
|
$result = Invoke-PSRule @testParams -WarningAction Ignore;
|
|
|
|
}
|
2019-05-08 01:46:11 +03:00
|
|
|
|
|
|
|
Context 'Resource metadata' {
|
|
|
|
It 'Kubernetes.Metadata' {
|
|
|
|
$filteredResult = $result | Where-Object { $_.RuleName -eq 'Kubernetes.Metadata' };
|
|
|
|
|
|
|
|
# Fail
|
|
|
|
$ruleResult = @($filteredResult | Where-Object { $_.Outcome -eq 'Fail' });
|
|
|
|
$ruleResult | Should -Not -BeNullOrEmpty;
|
|
|
|
$ruleResult.Length | Should -Be 2;
|
2020-02-10 16:40:57 +03:00
|
|
|
$ruleResult.TargetName | Should -BeIn 'deployment/deployment-B', 'service/service-B';
|
2019-05-08 01:46:11 +03:00
|
|
|
|
|
|
|
# Pass
|
|
|
|
$ruleResult = @($filteredResult | Where-Object { $_.Outcome -eq 'Pass' });
|
|
|
|
$ruleResult | Should -Not -BeNullOrEmpty;
|
|
|
|
$ruleResult.Length | Should -Be 2;
|
2020-02-10 16:40:57 +03:00
|
|
|
$ruleResult.TargetName | Should -BeIn 'deployment/deployment-A', 'service/service-A';
|
2019-05-08 01:46:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|