From c6e56261ef3f98ca94d03188982e707daba9f23b Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Tue, 25 Jun 2024 22:44:15 +0300 Subject: [PATCH] chore(pdb): expand tests (#843) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes # **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [ ] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] 🔃 Refactor of current code **🧪 How Has This Been Tested?** **📃 Notes:** **✔️ Checklist:** - [ ] ⚖️ My code follows the style guidelines of this project - [ ] 👀 I have performed a self-review of my own code - [ ] #️⃣ I have commented my code, particularly in hard-to-understand areas - [ ] 📄 I have made corresponding changes to the documentation - [ ] ⚠️ My changes generate no new warnings - [ ] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [ ] ⬆️ I increased versions for any altered app according to semantic versioning - [ ] I made sure the title starts with `feat(chart-name):`, `fix(chart-name):` or `chore(chart-name):` **➕ App addition** If this PR is an app addition please make sure you have done the following. - [ ] 🖼️ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._ --- .../tests/podDisruptionBudget/spec_test.yaml | 43 +++++++++++++++---- .../podDisruptionBudget/validation_test.yaml | 16 ++++++- library/common/Chart.yaml | 2 +- .../lib/podDistruptionBudget/_validation.tpl | 4 ++ 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/library/common-test/tests/podDisruptionBudget/spec_test.yaml b/library/common-test/tests/podDisruptionBudget/spec_test.yaml index ac81f013..f8942967 100644 --- a/library/common-test/tests/podDisruptionBudget/spec_test.yaml +++ b/library/common-test/tests/podDisruptionBudget/spec_test.yaml @@ -5,7 +5,7 @@ release: name: test-release-name namespace: test-release-namespace tests: - - it: should pass with zero in keys + - it: should pass with zero in keys (minAvailable) set: podDisruptionBudget: my-pdb1: @@ -13,7 +13,6 @@ tests: customLabels: label1: value1 minAvailable: 0 - maxUnavailable: 0 asserts: - documentIndex: &pdbDoc 0 isKind: @@ -23,12 +22,28 @@ tests: path: spec content: minAvailable: 0 + + - it: should pass with zero in keys (maxUnavailable) + set: + podDisruptionBudget: + my-pdb1: + enabled: true + customLabels: + label1: value1 + maxUnavailable: 0 + asserts: + - documentIndex: &pdbDoc 0 + isKind: + of: PodDisruptionBudget + - documentIndex: *pdbDoc + isSubset: + path: spec + content: maxUnavailable: 0 - - it: should pass keys from tpl + - it: should pass keys from tpl (minAvailable) set: k1: 0 - k2: 0 k3: AlwaysAllow podDisruptionBudget: my-pdb1: @@ -36,7 +51,6 @@ tests: customLabels: label1: value1 minAvailable: "{{ .Values.k1 }}" - maxUnavailable: "{{ .Values.k2 }}" unhealthyPodEvictionPolicy: "{{ .Values.k3 }}" asserts: - documentIndex: *pdbDoc @@ -44,9 +58,24 @@ tests: path: spec content: minAvailable: 0 - maxUnavailable: 0 unhealthyPodEvictionPolicy: AlwaysAllow + - it: should pass keys from tpl (maxUnavailable) + set: + k2: 0 + podDisruptionBudget: + my-pdb1: + enabled: true + customLabels: + label1: value1 + maxUnavailable: "{{ .Values.k2 }}" + asserts: + - documentIndex: *pdbDoc + isSubset: + path: spec + content: + maxUnavailable: 0 + - it: should pass with percentage in keys set: percentage: 50% @@ -55,14 +84,12 @@ tests: enabled: true customLabels: label1: value1 - minAvailable: 50% maxUnavailable: "{{ .Values.percentage }}" asserts: - documentIndex: *pdbDoc isSubset: path: spec content: - minAvailable: 50% maxUnavailable: 50% - it: should pass with minAvailable defined diff --git a/library/common-test/tests/podDisruptionBudget/validation_test.yaml b/library/common-test/tests/podDisruptionBudget/validation_test.yaml index 22278f89..c1c74bdc 100644 --- a/library/common-test/tests/podDisruptionBudget/validation_test.yaml +++ b/library/common-test/tests/podDisruptionBudget/validation_test.yaml @@ -8,8 +8,8 @@ tests: - it: should fail with name longer than 63 characters set: podDisruptionBudget: - my-poddisruptionbudget-super-long-name-that-is-longer-than-63-characters: - enabled: true + ? my-poddisruptionbudget-super-long-name-that-is-longer-than-63-characters + : enabled: true asserts: - failedTemplate: errorMessage: Name [test-release-name-common-test-my-poddisruptionbudget-super-long-name-that-is-longer-than-63-characters] is not valid. Must start and end with an alphanumeric lowercase character. It can contain '-'. And must be at most 63 characters. @@ -153,3 +153,15 @@ tests: asserts: - failedTemplate: errorMessage: Pod Disruption Budget - Selected pod [my-pod] is not defined + + - it: should fail with both minAvailable and maxUnavailable + set: + podDisruptionBudget: + my-poddisruptionbudget: + enabled: true + targetSelector: my-pod + minAvailable: 1 + maxUnavailable: 1 + asserts: + - failedTemplate: + errorMessage: Pod Disruption Budget - Expected one of [minAvailable, maxUnavailable] to be defined in [podDisruptionBudget.my-poddisruptionbudget], but got both diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index ad498a21..10943df4 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,7 +15,7 @@ maintainers: name: common sources: null type: library -version: 24.1.3 +version: 24.1.4 annotations: artifacthub.io/category: "integration-delivery" artifacthub.io/license: "BUSL-1.1" diff --git a/library/common/templates/lib/podDistruptionBudget/_validation.tpl b/library/common/templates/lib/podDistruptionBudget/_validation.tpl index 8b5e0794..b92fc57e 100644 --- a/library/common/templates/lib/podDistruptionBudget/_validation.tpl +++ b/library/common/templates/lib/podDistruptionBudget/_validation.tpl @@ -41,6 +41,10 @@ objectData: {{- end -}} {{- end -}} + {{- if and ($objectData.minAvailable) ($objectData.maxUnavailable) -}} + {{- fail (printf "Pod Disruption Budget - Expected one of [%s] to be defined in [podDisruptionBudget.%s], but got both" (join ", " $keys) $objectData.shortName) -}} + {{- end -}} + {{- if not $hasKey -}} {{- fail (printf "Pod Disruption Budget - Expected at least one of [%s] to be defined in [podDisruptionBudget.%s]" (join ", " $keys) $objectData.shortName) -}} {{- end -}}