Feature 11074: Enhanced OSM declarative modelling for applications. OSM's SDK for intent manipulation
Change-Id: I6d03faa143eafcf30380b3b854c54f177dcf8f25
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/docker/osm-nushell-krm-functions/krm/tests/artifacts/configmap/templates/configmap.yaml b/docker/osm-nushell-krm-functions/krm/tests/artifacts/configmap/templates/configmap.yaml
new file mode 100644
index 0000000..a758a7b
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/artifacts/configmap/templates/configmap.yaml
@@ -0,0 +1,24 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+apiVersion: v1
+data:
+ mykey: myvalue
+kind: ConfigMap
+metadata:
+ creationTimestamp: null
+ name: my-cm
diff --git a/docker/osm-nushell-krm-functions/krm/tests/artifacts/empty/.gitkeep b/docker/osm-nushell-krm-functions/krm/tests/artifacts/empty/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/artifacts/empty/.gitkeep
diff --git a/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/manifests/bitnamicharts-repo.yaml b/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/manifests/bitnamicharts-repo.yaml
new file mode 100644
index 0000000..354b837
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/manifests/bitnamicharts-repo.yaml
@@ -0,0 +1,27 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+---
+apiVersion: source.toolkit.fluxcd.io/v1beta2
+kind: HelmRepository
+metadata:
+ name: bitnamicharts
+ namespace: jenkins
+spec:
+ interval: 10m0s
+ type: oci
+ url: oci://registry-1.docker.io/bitnamicharts
diff --git a/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/manifests/jenkins-hr.yaml b/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/manifests/jenkins-hr.yaml
new file mode 100644
index 0000000..c87a95e
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/manifests/jenkins-hr.yaml
@@ -0,0 +1,37 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+---
+apiVersion: helm.toolkit.fluxcd.io/v2beta1
+kind: HelmRelease
+metadata:
+ name: jenkins
+ namespace: jenkins
+spec:
+ chart:
+ spec:
+ chart: jenkins
+ reconcileStrategy: ChartVersion
+ sourceRef:
+ kind: HelmRepository
+ name: bitnamicharts
+ namespace: jenkins
+ install:
+ createNamespace: true
+ interval: 3m0s
+ targetNamespace: jenkins
+ values: {}
diff --git a/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/templates/jenkins-ks.yaml b/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/templates/jenkins-ks.yaml
new file mode 100644
index 0000000..bbf4d7b
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/templates/jenkins-ks.yaml
@@ -0,0 +1,31 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+---
+apiVersion: kustomize.toolkit.fluxcd.io/v1
+kind: Kustomization
+metadata:
+ name: jenkins
+ namespace: jenkins
+spec:
+ interval: 1h0m0s
+ path: ./apps/jenkins/manifests
+ prune: true
+ sourceRef:
+ kind: GitRepository
+ name: sw-catalogs
+ namespace: flux-system
diff --git a/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/templates/jenkins-ns.yaml b/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/templates/jenkins-ns.yaml
new file mode 100644
index 0000000..ca2fff8
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/artifacts/jenkins/templates/jenkins-ns.yaml
@@ -0,0 +1,24 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+apiVersion: v1
+kind: Namespace
+metadata:
+ creationTimestamp: null
+ name: jenkins
+spec: {}
+status: {}
diff --git a/docker/osm-nushell-krm-functions/krm/tests/artifacts/namespace/templates/namespace.yaml b/docker/osm-nushell-krm-functions/krm/tests/artifacts/namespace/templates/namespace.yaml
new file mode 100644
index 0000000..d4c0386
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/artifacts/namespace/templates/namespace.yaml
@@ -0,0 +1,22 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+apiVersion: v1
+kind: Namespace
+metadata:
+ name: ${TARGET_NS}
+
diff --git a/docker/osm-nushell-krm-functions/krm/tests/artifacts/secret/templates/secret.yaml b/docker/osm-nushell-krm-functions/krm/tests/artifacts/secret/templates/secret.yaml
new file mode 100644
index 0000000..54346c5
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/artifacts/secret/templates/secret.yaml
@@ -0,0 +1,24 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+apiVersion: v1
+data:
+ mykey: bXl2YWx1ZQ==
+kind: Secret
+metadata:
+ creationTimestamp: null
+ name: my-secret
diff --git a/docker/osm-nushell-krm-functions/krm/tests/concatenate.nu b/docker/osm-nushell-krm-functions/krm/tests/concatenate.nu
new file mode 100644
index 0000000..ae674b8
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/concatenate.nu
@@ -0,0 +1,251 @@
+#!/usr/bin/env -S nu --stdin
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+
+use std assert
+use ../../krm/concatenate.nu *
+
+
+# --- resourcelists tests ---
+
+export def "test concatenate resourcelists empty inputs" []: [
+ nothing -> nothing
+] {
+ let input: record = {}
+ let resourcelist2: record = {}
+
+ let actual: record = resourcelists $resourcelist2
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: []
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test concatenate resourcelists empty stdin" []: [
+ nothing -> nothing
+] {
+ let input: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+ let resourcelist2: record = {}
+
+ # Simulate empty stdin by passing input as an argument
+ let actual: record = resourcelists $input
+ let expected: record = $input
+
+ assert equal $actual $expected
+}
+
+
+export def "test concatenate resourcelists empty second list" []: [
+ nothing -> nothing
+] {
+ let input: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+ let resourcelist2: record = {}
+
+ # Simulate empty stdin by passing input as an argument
+ let actual: record = resourcelists $resourcelist2
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: []
+ }
+
+ # Since we're testing with stdin, we need to simulate it
+ let actual_with_stdin: record = (
+ echo $input | resourcelists $resourcelist2
+ )
+ let expected_with_stdin: record = $input
+
+ assert equal $actual_with_stdin $expected_with_stdin
+}
+
+
+export def "test concatenate resourcelists merge lists" []: [
+ nothing -> nothing
+] {
+ let input: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+ let resourcelist2: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item3", "item4"]
+ }
+
+ let actual: record = (
+ echo $input | resourcelists $resourcelist2
+ )
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2", "item3", "item4"]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test concatenate resourcelists non-empty inputs with no items" []: [
+ nothing -> nothing
+] {
+ let input: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ }
+ let resourcelist2: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ }
+
+ let actual: record = (
+ echo $input | resourcelists $resourcelist2
+ )
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: []
+ }
+
+ assert equal $actual $expected
+}
+
+
+
+# --- manifests tests ---
+
+export def "test concatenate manifests empty inputs" []: [
+ nothing -> nothing
+] {
+ let mnfst2: any = null
+
+ let actual: list<any> = manifests $mnfst2
+ let expected: list<any> = []
+
+ assert equal $actual $expected
+}
+
+
+export def "test concatenate manifests empty stdin" []: [
+ nothing -> nothing
+] {
+ let mnfst2: record = {
+ name: "example"
+ kind: "Deployment"
+ }
+
+ let actual: list<any> = manifests $mnfst2
+ let expected: list<any> = [ $mnfst2 ]
+
+ assert equal $actual $expected
+}
+
+
+export def "test concatenate manifests empty second manifest" []: [
+ nothing -> nothing
+] {
+ let mnfst1: record = {
+ name: "example1"
+ kind: "Deployment"
+ }
+
+ let mnfst2: any = null
+
+ let actual: list<any> = (
+ echo $mnfst1 | manifests $mnfst2
+ )
+ let expected: list<any> = [ $mnfst1 ]
+
+ assert equal $actual $expected
+}
+
+
+export def "test concatenate manifests single records" []: [
+ nothing -> nothing
+] {
+ let mnfst1: record = {
+ name: "example1"
+ kind: "Deployment"
+ }
+ let mnfst2: record = {
+ name: "example2"
+ kind: "Service"
+ }
+
+ let actual: list<any> = (
+ echo $mnfst1 | manifests $mnfst2
+ )
+ let expected: list<any> = [ $mnfst1, $mnfst2 ]
+
+ assert equal $actual $expected
+}
+
+
+export def "test concatenate manifests lists of records" []: [
+ nothing -> nothing
+] {
+ let mnfst1: list<any> = [
+ { name: "example1", kind: "Deployment" }
+ { name: "example2", kind: "Service" }
+ ]
+ let mnfst2: list<any> = [
+ { name: "example3", kind: "Pod" }
+ { name: "example4", kind: "ConfigMap" }
+ ]
+
+ let actual: list<any> = (
+ echo $mnfst1 | manifests $mnfst2
+ )
+ let expected: list<any> = [
+ { name: "example1", kind: "Deployment" }
+ { name: "example2", kind: "Service" }
+ { name: "example3", kind: "Pod" }
+ { name: "example4", kind: "ConfigMap" }
+ ]
+
+ assert equal $actual $expected
+}
+
+
+export def "test concatenate manifests invalid input" []: [
+ nothing -> nothing
+] {
+ let mnfst2: string = "Invalid manifest"
+
+ let actual_error: error = (
+ try {
+ manifests $mnfst2
+ } catch {
+ |err| $err.msg
+ }
+ )
+
+ assert equal $actual_error "Error: Expected a record or a list of records, but received string."
+}
diff --git a/docker/osm-nushell-krm-functions/krm/tests/convert.nu b/docker/osm-nushell-krm-functions/krm/tests/convert.nu
new file mode 100644
index 0000000..09710eb
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/convert.nu
@@ -0,0 +1,448 @@
+#!/usr/bin/env -S nu --stdin
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+
+use std assert
+use std null-device
+use ../../krm/convert.nu *
+
+
+# --- replace environment variables tests ---
+
+export def "test convert replace environment variables no vars" []: [
+ nothing -> nothing
+] {
+ let text: string = "Hello, $USER!"
+
+ $env.USER = "test_user"
+ let actual: string = (
+ echo $text | replace environment variables
+ )
+ let expected: string = "Hello, test_user!"
+
+ assert equal $actual $expected
+}
+
+
+export def "test convert replace environment variables string vars" []: [
+ nothing -> nothing
+] {
+ let text: string = "Hello, $USER! Your HOME is $HOME."
+ let vars_to_replace: string = "$USER,$HOME"
+
+ load-env {
+ USER: "test_user"
+ HOME: "/home/test_user"
+ }
+
+ let actual: string = (
+ echo $text | replace environment variables $vars_to_replace
+ )
+ let expected: string = "Hello, test_user! Your HOME is /home/test_user."
+
+ assert equal $actual $expected
+}
+
+
+export def "test convert replace environment variables list vars" []: [
+ nothing -> nothing
+] {
+ let text: string = "Hello, $USER! Your HOME is $HOME."
+ let vars_to_replace: list<string> = ["USER", "HOME"]
+
+ load-env {
+ USER: "test_user"
+ HOME: "/home/test_user"
+ }
+
+ let actual: string = (
+ echo $text | replace environment variables $vars_to_replace
+ )
+ let expected: string = "Hello, test_user! Your HOME is /home/test_user."
+
+ assert equal $actual $expected
+}
+
+
+export def "test convert replace environment variables invalid input" []: [
+ nothing -> nothing
+] {
+ let text: string = "Hello, $USER!"
+ let vars_to_replace: int = 123
+
+ let error_occurred: error = try {
+ echo $text | replace environment variables $vars_to_replace
+
+ } catch {
+ |err| $err.msg
+ }
+
+ assert equal $error_occurred "Error: Expected a string or list of strings, but received int"
+}
+
+
+export def "test convert replace environment variables no replacement" []: [
+ nothing -> nothing
+] {
+ let text: string = "Hello, $NON_EXISTENT_VAR!"
+ let actual: string = (
+ echo $text | replace environment variables
+ )
+
+ let expected: string = "Hello, !"
+
+ assert equal $actual $expected
+}
+
+
+# --- folder to resourcelist tests ---
+
+export def "test convert folder to resourcelist empty input" []: [
+ nothing -> nothing
+] {
+ let folder: path = "./artifacts/empty"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: []
+ }
+
+ let actual: record = (folder to resourcelist $folder)
+ assert equal $actual $expected
+}
+
+
+export def "test convert folder to resourcelist no substitution" []: [
+ nothing -> nothing
+] {
+ let folder: path = "./artifacts/jenkins/templates"
+ let input_list: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+
+ let actual: record = (
+ echo $input_list | folder to resourcelist $folder
+ )
+ let expected_items: list<string> = ["item1", "item2"] | append (
+ kpt fn source $folder
+ | from yaml
+ | get items
+ )
+
+ assert equal $actual.apiVersion "config.kubernetes.io/v1"
+ assert equal $actual.kind "ResourceList"
+ assert equal $actual.items $expected_items
+}
+
+
+export def "test convert folder to resourcelist with substitution" []: [
+ nothing -> nothing
+] {
+ let folder: path = "./artifacts/namespace/templates"
+ let input_list: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+ $env.TARGET_NS = "target-namespace"
+
+ let actual: record = (
+ echo $input_list | folder to resourcelist --subst-env $folder
+ )
+ let expected_items: list<string> = ["item1", "item2"] | append (
+ kpt fn source $folder
+ | replace environment variables
+ | from yaml
+ | get items
+ )
+
+ assert equal $actual.apiVersion "config.kubernetes.io/v1"
+ assert equal $actual.kind "ResourceList"
+ assert equal $actual.items $expected_items
+ assert equal $actual.items.2.metadata.name $env.TARGET_NS
+}
+
+
+export def "test convert folder to resourcelist with filter" []: [
+ nothing -> nothing
+] {
+ let folder: path = "./artifacts/namespace/templates"
+ let input_list: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+ $env.TARGET_NS = "target-namespace"
+ let env_filter = "$TARGET_NS"
+
+ let actual: record = (
+ echo $input_list | folder to resourcelist --subst-env $folder $env_filter
+ )
+ let expected_items: list<string> = ["item1", "item2"] | append (
+ kpt fn source $folder
+ | replace environment variables $env_filter
+ | from yaml
+ | get items
+ )
+
+ assert equal $actual.apiVersion "config.kubernetes.io/v1"
+ assert equal $actual.kind "ResourceList"
+ assert equal $actual.items $expected_items
+ assert equal $actual.items.2.metadata.name $env.TARGET_NS
+}
+
+
+export def "test convert folder to resourcelist invalid input" []: [
+ nothing -> nothing
+] {
+ let folder: path = "./non-existent-folder"
+ let input_list: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+
+ let error_occurred: bool = try {
+ echo $input_list | folder to resourcelist $folder err> (null-device)
+ } catch {
+ |err| $err.msg
+ }
+
+ assert equal $error_occurred "Can't convert to record."
+}
+
+
+
+# --- manifest to resourcelist tests ---
+
+export def "test convert manifest to resourcelist empty input" []: [
+ nothing -> nothing
+] {
+ let actual: record = manifest to resourcelist
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: []
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test convert manifest to resourcelist single record" []: [
+ nothing -> nothing
+] {
+ let manifest: record = {
+ name: "example"
+ kind: "Deployment"
+ }
+
+ let actual: record = (
+ $manifest | manifest to resourcelist
+ )
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [ $manifest ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test convert manifest to resourcelist list of records" []: [
+ nothing -> nothing
+] {
+ let manifests: list<any> = [
+ { name: "example1", kind: "Deployment" }
+ { name: "example2", kind: "Service" }
+ ]
+
+ let actual: record = (
+ $manifests | manifest to resourcelist
+ )
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: $manifests
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test convert manifest to resourcelist invalid input" []: [
+ nothing -> nothing
+] {
+ let invalid_manifest: string = "Invalid manifest"
+
+ let error_occurred: bool = try {
+ $invalid_manifest | manifest to resourcelist
+ } catch {
+ |err| $err.msg
+ }
+
+ assert equal $error_occurred "Error: Expected a record or a list of records, but received string."
+}
+
+
+
+# --- resourcelist to folder tests ---
+
+export def "test convert resourcelist to folder dry run" []: [
+ nothing -> nothing
+] {
+ let rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+
+ let output: record = (
+ $rl | resourcelist to folder "no-folder" true | from yaml
+ )
+
+ assert equal $output $rl
+}
+
+
+export def "test convert resourcelist to folder no sync" []: [
+ nothing -> nothing
+] {
+ let source_folder: path = "./artifacts/jenkins/templates/"
+ let rl: record = (
+ convert folder to resourcelist $source_folder
+ )
+ let target_folder: string = (mktemp -t -d)
+
+ # Run the command
+ $rl | resourcelist to folder $target_folder
+
+ # Check if the contents were copied correctly
+ let actual_contents: list<string> = (
+ ls --short-names $target_folder
+ | get name
+ | sort
+ )
+
+ # Cleanup
+ rm -rf $target_folder
+
+ # Expected
+ let expected_contents: list<string> = (
+ ls --short-names $source_folder
+ | get name
+ | sort
+ )
+
+ assert equal $actual_contents $expected_contents
+}
+
+
+export def "test convert resourcelist to folder sync" []: [
+ nothing -> nothing
+] {
+ let source_folder: path = "./artifacts/jenkins/templates/"
+ let rl: record = (
+ convert folder to resourcelist $source_folder
+ )
+ let target_folder: string = (mktemp -t -d)
+
+ # Add an extra file to the target folder (it should be removed by the synchronization)
+ ^touch ($target_folder | path join "extra_file.txt")
+
+ # Run the command
+ $rl | resourcelist to folder --sync $target_folder
+
+ # Check if the contents were copied correctly
+ let actual_contents: list<string> = (
+ ls --short-names $target_folder
+ | get name
+ | sort
+ )
+
+ # Cleanup
+ rm -rf $target_folder
+
+ # Expected
+ let expected_contents: list<string> = (
+ ls --short-names $source_folder
+ | get name
+ | sort
+ )
+
+ assert equal $actual_contents $expected_contents
+}
+
+
+# export def "test convert resourcelist to folder invalid input" []: [
+# nothing -> nothing
+# ] {
+# let invalid_input: record = { "Invalid input": "invalid value" }
+# let target_folder: string = (mktemp -t -d)
+
+# let error_occurred: any = try {
+# $invalid_input | resourcelist to folder $target_folder
+# } catch {
+# |err| $err.msg
+# }
+
+# # Cleanup
+# print $target_folder
+# # rm -rf $target_folder
+
+# assert equal $error_occurred "Can't convert to boolean."
+# }
+
+
+export def "test convert resourcelist to folder non-existent folder" []: [
+ nothing -> nothing
+] {
+ let source_folder: path = "./artifacts/jenkins/templates/"
+ let rl: record = (
+ convert folder to resourcelist $source_folder
+ )
+
+ let temp_folder: string = (mktemp -t -d)
+ let target_folder: string = ($temp_folder | path join "new-folder")
+ mkdir $target_folder
+
+ # Run the command
+ $rl | resourcelist to folder $target_folder
+
+ # Check if the contents were copied correctly
+ let actual_contents: list<string> = (
+ ls --short-names $target_folder
+ | get name
+ | sort
+ )
+
+ # Cleanup
+ rm -rf $temp_folder
+
+ # Expected
+ let expected_contents: list<string> = (
+ ls --short-names $source_folder
+ | get name
+ | sort
+ )
+
+ assert equal $actual_contents $expected_contents
+}
diff --git a/docker/osm-nushell-krm-functions/krm/tests/generator.nu b/docker/osm-nushell-krm-functions/krm/tests/generator.nu
new file mode 100644
index 0000000..5f133a0
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/generator.nu
@@ -0,0 +1,640 @@
+#!/usr/bin/env -S nu --stdin
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+
+use std assert
+use ../../krm/generator.nu *
+
+
+# --- from resourcelist tests ---
+
+export def "test generator from resourcelist empty inputs" []: [
+ nothing -> nothing
+] {
+ let in_rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+ let rl: record = {}
+
+ let actual: record = ($in_rl | from resourcelist $rl)
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator from resourcelist empty stdin" []: [
+ nothing -> nothing
+] {
+ let in_rl: record = {}
+ let rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+
+ let actual: record = ($in_rl | from resourcelist $rl)
+ let expected: record = $rl
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator from resourcelist merge lists" []: [
+ nothing -> nothing
+] {
+ let in_rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+ let rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item3", "item4"]
+ }
+
+ let actual: record = (
+ echo $in_rl | from resourcelist $rl
+ )
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2", "item3", "item4"]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator from resourcelist non-empty inputs with no items" []: [
+ nothing -> nothing
+] {
+ let in_rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ }
+ let rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ }
+
+ let actual: record = (
+ echo $in_rl | from resourcelist $rl
+ )
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: []
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator from resourcelist invalid input parameter" []: [
+ nothing -> nothing
+] {
+ let in_rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+ let rl: record = { "Invalid input": "Invalid value" }
+
+ let error_occurred: any = try {
+ $in_rl | from resourcelist $rl
+ } catch {
+ |err| $err.json | from json | get inner.msg.0
+ }
+
+ assert ($error_occurred | str starts-with "Error: Expected a ResourceList, but received")
+}
+
+
+export def "test generator from resourcelist invalid input from stdin" []: [
+ nothing -> nothing
+] {
+ let in_rl: record = { "Invalid input": "Invalid value" }
+ let rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+
+ let error_occurred: any = try {
+ $in_rl | from resourcelist $rl
+ } catch {
+ |err| $err.json | from json | get inner.msg.0
+ }
+
+ assert ($error_occurred | str starts-with "Error: Expected a ResourceList, but received")
+}
+
+
+# --- from manifest tests ---
+
+export def "test generator from manifest empty inputs" []: [
+ nothing -> nothing
+] {
+ let manifest: any = null
+
+ let actual: record = from manifest $manifest
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: []
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator from manifest empty stdin" []: [
+ nothing -> nothing
+] {
+ let manifest: record = {
+ name: "example"
+ kind: "Deployment"
+ }
+
+ let actual: record = from manifest $manifest
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [ $manifest ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator from manifest merge lists" []: [
+ nothing -> nothing
+] {
+ let in_rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+ let manifest: record = {
+ name: "example"
+ kind: "Deployment"
+ }
+
+ let actual: record = (
+ echo $in_rl | from manifest $manifest
+ )
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2", $manifest]
+ }
+
+ assert equal $actual.apiVersion $expected.apiVersion
+ assert equal $actual.kind $expected.kind
+ assert equal $actual.items $expected.items
+}
+
+
+export def "test generator from manifest list of manifests" []: [
+ nothing -> nothing
+] {
+ let in_rl: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2"]
+ }
+ let manifest: list<any> = [
+ { name: "example1", kind: "Deployment" }
+ { name: "example2", kind: "Service" }
+ ]
+
+ let actual: record = (
+ echo $in_rl | from manifest $manifest
+ )
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: ["item1", "item2", { name: "example1", kind: "Deployment" }, { name: "example2", kind: "Service" }]
+ }
+
+ assert equal $actual.apiVersion $expected.apiVersion
+ assert equal $actual.kind $expected.kind
+ assert equal $actual.items $expected.items
+}
+
+
+export def "test generator from manifest invalid input" []: [
+ nothing -> nothing
+] {
+ let manifest: string = "Invalid manifest"
+
+ let error_occurred: error = try {
+ from manifest $manifest
+ } catch {
+ |err| $err.msg
+ }
+
+ assert equal $error_occurred "Error: Expected a record or a list of records, but received string."
+}
+
+
+
+# --- configmap tests ---
+
+export def "test generator configmap basic" []: [
+ nothing -> nothing
+] {
+ let key_pairs: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let name: string = "example-configmap"
+
+ let actual: record = configmap $key_pairs $name
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1",
+ kind: ResourceList,
+ items: [
+ {
+ apiVersion: "v1"
+ kind: "ConfigMap"
+ metadata: { name: $name, namespace: "default" }
+ data: $key_pairs
+ }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator configmap with namespace" []: [
+ nothing -> nothing
+] {
+ let key_pairs: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let name: string = "example-configmap"
+ let namespace: string = "custom-namespace"
+
+ let actual: record = configmap $key_pairs $name $namespace
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1",
+ kind: ResourceList,
+ items: [
+ {
+ apiVersion: "v1"
+ kind: "ConfigMap"
+ metadata: { name: $name, namespace: $namespace }
+ data: $key_pairs
+ }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator configmap with filename" []: [
+ nothing -> nothing
+] {
+ let key_pairs: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let name: string = "example-configmap"
+ let filename: string = "example-configmap.yaml"
+
+ let actual: record = configmap --filename $filename $key_pairs $name
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1",
+ kind: ResourceList,
+ items: [
+ {
+ apiVersion: "v1"
+ kind: "ConfigMap"
+ metadata: {
+ name: $name,
+ namespace: "default",
+ annotations: {
+ "config.kubernetes.io/path": $filename,
+ "internal.config.kubernetes.io/path": $filename
+ }
+ }
+ data: $key_pairs
+ }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator configmap with filename and index" []: [
+ nothing -> nothing
+] {
+ let key_pairs: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let name: string = "example-configmap"
+ let filename: string = "example-configmap.yaml"
+ let index: int = 0
+
+ let actual: record = configmap --filename $filename --index $index $key_pairs $name
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1",
+ kind: ResourceList,
+ items: [
+ {
+ apiVersion: "v1"
+ kind: "ConfigMap"
+ metadata: {
+ name: $name,
+ namespace: "default",
+ annotations: {
+ "config.kubernetes.io/path": $filename,
+ "internal.config.kubernetes.io/path": $filename,
+ "config.kubernetes.io/index": "0",
+ "internal.config.kubernetes.io/index": "0"
+ }
+ }
+ data: $key_pairs
+ }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+
+# TODO:
+
+# --- secret tests ---
+
+export def "test generator secret basic" []: [
+ nothing -> nothing
+] {
+ let key_pairs: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let name: string = "example-secret"
+
+ let actual: record = secret $key_pairs $name
+ let expected_encoded_values: record = {
+ key1: ("value1" | encode base64),
+ key2: ("value2" | encode base64)
+ }
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1",
+ kind: ResourceList,
+ items: [
+ {
+ apiVersion: "v1"
+ kind: "Secret"
+ metadata: { name: $name, namespace: "default" }
+ data: $expected_encoded_values
+ }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator secret with namespace" []: [
+ nothing -> nothing
+] {
+ let key_pairs: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let name: string = "example-secret"
+ let namespace: string = "custom-namespace"
+
+ let actual: record = secret $key_pairs $name $namespace
+ let expected_encoded_values: record = {
+ key1: ("value1" | encode base64),
+ key2: ("value2" | encode base64)
+ }
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1",
+ kind: ResourceList,
+ items: [
+ {
+ apiVersion: "v1"
+ kind: "Secret"
+ metadata: { name: $name, namespace: $namespace }
+ data: $expected_encoded_values
+ }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator secret with filename" []: [
+ nothing -> nothing
+] {
+ let key_pairs: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let name: string = "example-secret"
+ let filename: string = "example-secret.yaml"
+
+ let actual: record = (
+ secret
+ --filename $filename
+ $key_pairs
+ $name
+ )
+ let expected_encoded_values: record = {
+ key1: ("value1" | encode base64),
+ key2: ("value2" | encode base64)
+ }
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1",
+ kind: ResourceList,
+ items: [
+ {
+ apiVersion: "v1"
+ kind: "Secret"
+ metadata: {
+ name: $name,
+ namespace: "default",
+ annotations: {
+ "config.kubernetes.io/path": $filename,
+ "internal.config.kubernetes.io/path": $filename
+ }
+ }
+ data: $expected_encoded_values
+ }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator secret with filename and index" []: [
+ nothing -> nothing
+] {
+ let key_pairs: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let name: string = "example-secret"
+ let filename: string = "example-secret.yaml"
+ let index: int = 0
+
+ let actual: record = (
+ secret
+ --filename $filename
+ --index $index
+ $key_pairs
+ $name
+ )
+ let expected_encoded_values: record = {
+ key1: ("value1" | encode base64),
+ key2: ("value2" | encode base64)
+ }
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1",
+ kind: ResourceList,
+ items: [
+ {
+ apiVersion: "v1"
+ kind: "Secret"
+ metadata: {
+ name: $name,
+ namespace: "default",
+ annotations: {
+ "config.kubernetes.io/path": $filename,
+ "internal.config.kubernetes.io/path": $filename,
+ "config.kubernetes.io/index": "0",
+ "internal.config.kubernetes.io/index": "0"
+ }
+ }
+ data: $expected_encoded_values
+ }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator secret with type" []: [
+ nothing -> nothing
+] {
+ let key_pairs: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let name: string = "example-secret"
+ let type: string = "Opaque"
+
+ let actual: record = secret --type $type $key_pairs $name
+ let expected_encoded_values: record = {
+ key1: ("value1" | encode base64),
+ key2: ("value2" | encode base64)
+ }
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1",
+ kind: ResourceList,
+ items: [
+ {
+ apiVersion: "v1"
+ kind: "Secret"
+ metadata: { name: $name, namespace: "default" }
+ type: $type
+ data: $expected_encoded_values
+ }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test generator secret with age encryption" []: [
+ nothing -> nothing
+] {
+ let test_public_key: string = "age1hsrtxphk7exrdc0kt8dgr8a8r3hx88v3xpsw0ezaxvefsy9asegqknppc0"
+ let test_private_key: string = "AGE-SECRET-KEY-12CC3A4LEDYF4S26UV6Z2MEG7ZQL9PTU5NHH6N3FN6FLJ5HACW9LQX0UWP2"
+
+ let key_pairs: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let name: string = "example-secret"
+ let filename: string = "example-secret.yaml"
+
+ # Here we extract the encrypted manifest only
+ # File name and index are also removed, since they were not taken into account for age encryptio
+ let result: record = (
+ secret
+ --filename $filename
+ --public-age-key $test_public_key
+ $key_pairs
+ $name
+ )
+ | get items.0
+ | reject $.metadata.annotations
+
+ # Verify decryption
+ let tmp_encrypted_file = (mktemp -t --suffix .yaml)
+ $result | save -f $tmp_encrypted_file
+ let actual: record = (
+ $test_private_key
+ | SOPS_AGE_KEY_FILE="/dev/stdin" sops --decrypt $tmp_encrypted_file
+ | from yaml
+ )
+ rm $tmp_encrypted_file # Clean up temporary key file
+
+
+ let expected_encoded_values: record = {
+ key1: ("value1" | encode base64),
+ key2: ("value2" | encode base64)
+ }
+
+ let expected: record = {
+ apiVersion: "v1"
+ kind: "Secret"
+ metadata: {
+ name: $name,
+ namespace: "default"
+ }
+ data: $expected_encoded_values
+ }
+
+ assert equal $actual $expected
+}
diff --git a/docker/osm-nushell-krm-functions/krm/tests/jsonpatch.nu b/docker/osm-nushell-krm-functions/krm/tests/jsonpatch.nu
new file mode 100644
index 0000000..77cbcd4
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/jsonpatch.nu
@@ -0,0 +1,250 @@
+#!/usr/bin/env -S nu --stdin
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+
+use std assert
+use ../../krm/jsonpatch.nu *
+
+
+# --- create operation tests ---
+
+export def "test jsonpatch create operation add" []: [
+ nothing -> nothing
+] {
+ let op: string = "add"
+ let path: string = "/spec/template/spec/securityContext"
+ let value: record = {
+ runAsUser: 10000
+ fsGroup: 1337
+ }
+
+ let actual: record = create operation $op $path $value
+ let expected: record = {
+ op: $op,
+ path: $path,
+ value: $value
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test jsonpatch create operation remove" []: [
+ nothing -> nothing
+] {
+ let op: string = "remove"
+ let path: string = "/spec/template/spec/securityContext"
+
+ let actual: record = create operation $op $path
+ let expected: record = {
+ op: $op,
+ path: $path
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test jsonpatch create operation replace" []: [
+ nothing -> nothing
+] {
+ let op: string = "replace"
+ let path: string = "/spec/template/spec/securityContext"
+ let value: record = {
+ runAsUser: 10000
+ fsGroup: 1337
+ }
+
+ let actual: record = create operation $op $path $value
+ let expected: record = {
+ op: $op,
+ path: $path,
+ value: $value
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test jsonpatch create operation move" []: [
+ nothing -> nothing
+] {
+ let op: string = "move"
+ let from: string = "/spec/template/spec/securityContext"
+ let path: string = "/spec/template/spec/newSecurityContext"
+
+ let actual: record = create operation $op $path '' $from
+ let expected: record = {
+ op: $op,
+ from: $from,
+ path: $path
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test jsonpatch create operation copy" []: [
+ nothing -> nothing
+] {
+ let op: string = "copy"
+ let from: string = "/spec/template/spec/securityContext"
+ let path: string = "/spec/template/spec/newSecurityContext"
+
+ let actual: record = create operation $op $path '' $from
+ let expected: record = {
+ op: $op,
+ from: $from,
+ path: $path
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test jsonpatch create operation invalid op" []: [
+ nothing -> nothing
+] {
+ let op: string = "invalid"
+ let path: string = "/spec/template/spec/securityContext"
+
+ let error_occurred: error = try {
+ create operation $op $path
+ } catch {
+ |err| $err.msg
+ }
+
+ assert equal $error_occurred "Invalid operation type. Supported values are 'add', 'remove', 'replace', 'move', 'copy'. See RFC6902 for details."
+}
+
+
+export def "test jsonpatch create operation missing value for add/replace" []: [
+ nothing -> nothing
+] {
+ let op: string = "add"
+ let path: string = "/spec/template/spec/securityContext"
+
+ let error_occurred: error = try {
+ create operation $op $path
+ } catch {
+ |err| $err.msg
+ }
+
+ assert equal $error_occurred "Value is required for 'add' and 'replace' operations."
+}
+
+
+export def "test jsonpatch create operation missing from for move/copy" []: [
+ nothing -> nothing
+] {
+ let op: string = "move"
+ let path: string = "/spec/template/spec/newSecurityContext"
+
+ let error_occurred: error = try {
+ create operation $op $path
+ } catch {
+ |err| $err.msg
+ }
+
+ assert equal $error_occurred "Source path is required for 'move' and 'copy' operations."
+}
+
+
+# --- create JSON patch tests ---
+
+export def "test jsonpatch create JSON patch basic" []: [
+ nothing -> nothing
+] {
+ let target: record = {
+ kind: "Deployment"
+ name: "podinfo"
+ }
+ let operation: record = {
+ op: "add"
+ path: "/spec/template/spec/securityContext"
+ value: {
+ runAsUser: 10000
+ fsGroup: 1337
+ }
+ }
+
+ let actual: record = create $target $operation
+ let expected: record = {
+ target: $target,
+ patch: ([$operation] | to yaml)
+ }
+
+ assert equal $actual.target $expected.target
+ assert equal $actual.patch $expected.patch
+}
+
+
+export def "test jsonpatch create JSON patch multiple operations" []: [
+ nothing -> nothing
+] {
+ let target: record = {
+ kind: "Deployment"
+ name: "podinfo"
+ }
+ let operation1: record = {
+ op: "add"
+ path: "/spec/template/spec/securityContext"
+ value: {
+ runAsUser: 10000
+ fsGroup: 1337
+ }
+ }
+ let operation2: record = {
+ op: "replace"
+ path: "/spec/replicas"
+ value: 3
+ }
+
+ let actual: record = create $target $operation1 $operation2
+ let expected: record = {
+ target: $target,
+ patch: (
+ [$operation1, $operation2] | to yaml
+ )
+ }
+
+ assert equal $actual.target $expected.target
+ assert equal $actual.patch $expected.patch
+}
+
+
+export def "test jsonpatch create JSON patch using operation helper" []: [
+ nothing -> nothing
+] {
+ let target: record = {
+ kind: "Deployment"
+ name: "podinfo"
+ }
+ let operation: record = (
+ create operation add "/spec/template/spec/securityContext" {runAsUser: 10000, fsGroup: 1337}
+ )
+
+ let actual: record = create $target $operation
+ let expected: record = {
+ target: $target,
+ patch: ([$operation] | to yaml)
+ }
+
+ assert equal $actual.target $expected.target
+ assert equal $actual.patch $expected.patch
+}
diff --git a/docker/osm-nushell-krm-functions/krm/tests/keypair.nu b/docker/osm-nushell-krm-functions/krm/tests/keypair.nu
new file mode 100644
index 0000000..e3a1ab7
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/keypair.nu
@@ -0,0 +1,214 @@
+#!/usr/bin/env -S nu --stdin
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+
+use std assert
+use std null-device
+use ../../krm/keypair.nu *
+
+
+# --- create age tests ---
+
+export def "test keypair create age basic functionality" [] {
+ # Setup
+ let test_dir = (mktemp -t -d)
+ let key_name = "test_key"
+
+ # Execute
+ create age $key_name $test_dir err> (null-device)
+
+ # Assert
+ assert ([$test_dir $"($key_name).key"] | path join | path exists)
+ assert ([$test_dir $"($key_name).pub"] | path join | path exists)
+
+ # Cleanup
+ rm -rf $test_dir
+}
+
+
+export def "test keypair create age overwrites existing keys" [] {
+ # Setup
+ let test_dir = (mktemp -t -d)
+ let key_name = "test_key"
+ touch ([$test_dir $"($key_name).key"] | path join)
+ touch ([$test_dir $"($key_name).pub"] | path join)
+
+ # Execute
+ create age $key_name $test_dir err> (null-device)
+
+ # Assert
+ let key_path = [$test_dir $"($key_name).key"] | path join
+ let pub_path = [$test_dir $"($key_name).pub"] | path join
+ assert ($key_path | path exists)
+ assert ($pub_path | path exists)
+ assert greater (open $key_path | str length) 0
+ assert greater (open $pub_path | str length) 0
+
+ # Cleanup
+ rm -rf $test_dir
+}
+
+
+export def "test keypair create age uses default directory" [] {
+ # Setup
+ let original_credentials_dir = $env.CREDENTIALS_DIR?
+ let test_dir = (mktemp -t -d)
+ $env.CREDENTIALS_DIR = $test_dir
+ let key_name = "test_key"
+
+ # Execute
+ create age $key_name err> (null-device)
+
+ # Assert
+ assert ([$test_dir $"($key_name).key"] | path join | path exists)
+ assert ([$test_dir $"($key_name).pub"] | path join | path exists)
+
+ # Cleanup
+ rm -rf $test_dir
+ $env.CREDENTIALS_DIR = $original_credentials_dir
+}
+
+
+export def "test keypair create age generates valid keys" [] {
+ # Setup
+ let test_dir = (mktemp -t -d)
+ let key_name = "test_key"
+
+ # Execute
+ create age $key_name $test_dir err> (null-device)
+
+ # Assert
+ let pub_path = [$test_dir $"($key_name).pub"] | path join
+ let pub_key = (open $pub_path)
+ assert ($pub_key | str starts-with "age1")
+ assert equal ($pub_key | str length) 63 # Standard length for age public keys
+
+ # Cleanup
+ rm -rf $test_dir
+}
+
+
+# --- encrypt secret manifest tests ---
+
+export def "test keypair encrypt secret manifest basic functionality" [] {
+ # Setup
+ let test_public_key: string = "age1hsrtxphk7exrdc0kt8dgr8a8r3hx88v3xpsw0ezaxvefsy9asegqknppc0"
+ let test_private_key: string = "AGE-SECRET-KEY-12CC3A4LEDYF4S26UV6Z2MEG7ZQL9PTU5NHH6N3FN6FLJ5HACW9LQX0UWP2"
+ let input_yaml: string = "apiVersion: v1\nkind: Secret\nmetadata:\n name: test-secret\ndata:\n username: dXNlcm5hbWU=\n password: cGFzc3dvcmQ="
+
+ # Execute
+ let result = ($input_yaml | encrypt secret manifest $test_public_key)
+
+ # Assert
+ assert ($result | str contains "sops:")
+ assert ($result | str contains "encrypted_regex: ^(data|stringData)$")
+ assert ($result | str contains "ENC[AES256_GCM,data:")
+
+ # Verify decryption
+ let tmp_encrypted_file = (mktemp -t --suffix .yaml)
+ $result | save -f $tmp_encrypted_file
+
+ let decrypted: string = ($test_private_key
+ | SOPS_AGE_KEY_FILE="/dev/stdin" sops --decrypt $tmp_encrypted_file
+ )
+ rm $tmp_encrypted_file # Clean up temporary key file
+
+ assert str contains $decrypted "username: dXNlcm5hbWU="
+ assert str contains $decrypted "password: cGFzc3dvcmQ="
+}
+
+
+export def "test keypair encrypt secret manifest handles empty input" [] {
+ # Setup
+ let test_public_key = "age1hsrtxphk7exrdc0kt8dgr8a8r3hx88v3xpsw0ezaxvefsy9asegqknppc0"
+
+ # Execute and Assert
+ let result: string = (try { ""
+ | encrypt secret manifest $test_public_key
+ } catch { $in | to yaml })
+
+ # assert str contains $result "Error"
+ assert (not ($result | str contains "Error")) $"ERROR: Got ($result)"
+}
+
+
+export def "test keypair encrypt secret manifest encrypts correct fields" [] {
+ # Setup
+ let test_public_key: string = "age1hsrtxphk7exrdc0kt8dgr8a8r3hx88v3xpsw0ezaxvefsy9asegqknppc0"
+ let test_private_key: string = "AGE-SECRET-KEY-12CC3A4LEDYF4S26UV6Z2MEG7ZQL9PTU5NHH6N3FN6FLJ5HACW9LQX0UWP2"
+ let input_yaml: string = "apiVersion: v1\nkind: Secret\nmetadata:\n name: test-secret\ndata:\n username: dXNlcm5hbWU=\n password: cGFzc3dvcmQ=\nstringData:\n api_key: my-api-key"
+
+ # Execute
+ let result: string = ($input_yaml | encrypt secret manifest $test_public_key)
+
+ # Assert
+ assert str contains $result "ENC[AES256_GCM,data:"
+ assert str contains $result "username:"
+ assert str contains $result "password:"
+ assert str contains $result "api_key:"
+ assert (not ($result | str contains "dXNlcm5hbWU="))
+ assert (not ($result | str contains "cGFzc3dvcmQ="))
+ assert (not ($result | str contains "my-api-key"))
+ assert str contains $result "metadata:\n name: test-secret"
+
+ # Verify decryption
+ let tmp_encrypted_file = (mktemp -t --suffix .yaml)
+ $result | save -f $tmp_encrypted_file
+ let decrypted: string = ($test_private_key
+ | SOPS_AGE_KEY_FILE="/dev/stdin" sops --decrypt $tmp_encrypted_file
+ )
+ rm $tmp_encrypted_file # Clean up temporary key file
+ assert str contains $decrypted "username: dXNlcm5hbWU="
+ assert str contains $decrypted "password: cGFzc3dvcmQ="
+ assert str contains $decrypted "api_key: my-api-key"
+}
+
+
+export def "test keypair decrypt secret manifest" [] {
+ # Setup
+ let test_public_key: string = "age1hsrtxphk7exrdc0kt8dgr8a8r3hx88v3xpsw0ezaxvefsy9asegqknppc0"
+ let test_private_key: string = "AGE-SECRET-KEY-12CC3A4LEDYF4S26UV6Z2MEG7ZQL9PTU5NHH6N3FN6FLJ5HACW9LQX0UWP2"
+ let input_record: record = {
+ apiVersion: v1,
+ kind: Secret,
+ metadata: { name: test-secret }
+ data: {
+ username: ('myusername' | encode base64)
+ password: ('mypassword' | encode base64)
+ }
+ }
+
+ # Encrypt
+ let encrypted_record: record = (
+ $input_record
+ | to yaml
+ | encrypt secret manifest $test_public_key
+ | from yaml
+ )
+
+ # Decrypt
+ let decrypted_record: record = (
+ $encrypted_record
+ | to yaml
+ | keypair decrypt secret manifest $test_private_key
+ | from yaml
+ )
+
+ # Test
+ assert equal $input_record $decrypted_record
+}
diff --git a/docker/osm-nushell-krm-functions/krm/tests/mod.nu b/docker/osm-nushell-krm-functions/krm/tests/mod.nu
new file mode 100644
index 0000000..d206563
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/mod.nu
@@ -0,0 +1,67 @@
+#!/usr/bin/env -S nu --stdin
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+use std assert
+use ../../krm *
+use ./keypair.nu *
+use ./concatenate.nu *
+use ./convert.nu *
+use ./strategicmergepatch.nu *
+use ./jsonpatch.nu *
+use ./generator.nu *
+use ./patch.nu *
+use ./overlaypatch.nu *
+
+
+# Test launcher
+def main [] {
+ print "Running tests..."
+
+ let test_commands: list<string> = (
+ scope commands
+ | where ($it.type == "custom")
+ and ($it.name | str starts-with "test ")
+ and not ($it.description | str starts-with "ignore")
+ | get name
+ )
+
+ let count_test_commands: int = ($test_commands | length)
+ let test_commands_together: string = (
+ $test_commands
+ | enumerate
+ | each { |test|
+ [$"print '--> [($test.index + 1)/($count_test_commands)] ($test.item)'", $test.item]
+ }
+ | flatten
+ | str join ";"
+ )
+
+ nu --commands $"source `($env.CURRENT_FILE)`; ($test_commands_together)"
+ print $"\n✅ ALL TESTS COMPLETED SUCCESSFULLY"
+}
+
+
+# --- safe_name tests ---
+
+export def "test safe resource name" []: [
+ nothing -> nothing
+] {
+ let actual: string = safe resource name "This is a_test w/special:characters."
+ let expected: string = "this-is-a-test-w-special-characters-"
+ assert equal $actual $expected
+}
diff --git a/docker/osm-nushell-krm-functions/krm/tests/overlaypatch.nu b/docker/osm-nushell-krm-functions/krm/tests/overlaypatch.nu
new file mode 100644
index 0000000..5473093
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/overlaypatch.nu
@@ -0,0 +1,1992 @@
+#!/usr/bin/env -S nu --stdin
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+
+use std assert
+use ../../krm/patch.nu *
+use ../../krm/overlaypatch.nu *
+
+
+
+# --- add patch tests ---
+
+export def "test overlaypatch add patch to kustomization" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let kustomization_name: string = "example-kustomization"
+ let ks_namespace: string = "default"
+ let target: record = {
+ kind: "Deployment"
+ name: "example-deployment"
+ }
+ let patch_value: record = {
+ op: "replace"
+ path: "/spec/replicas"
+ value: 3
+ }
+
+ let actual: record = $resourcelist | add patch --ks-namespace $ks_namespace $kustomization_name $target $patch_value
+ let expected_patch_content: record = {
+ target: $target
+ patch: ($patch_value | to yaml)
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch add patch to kustomization with existing patches" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ spec: { patches: [{ target: { kind: "Service", name: "example-service" }, patch: "op:\n replace\npath:\n /spec/type\nvalue:\n NodePort" }] }
+ }
+ ]
+ }
+
+ let kustomization_name: string = "example-kustomization"
+ let ks_namespace: string = "default"
+ let target: record = {
+ kind: "Deployment"
+ name: "example-deployment"
+ }
+ let patch_value: record = {
+ op: "replace"
+ path: "/spec/replicas"
+ value: 3
+ }
+
+ let actual: record = $resourcelist | add patch --ks-namespace $ks_namespace $kustomization_name $target $patch_value
+ let expected_patch_content_1st_patch: record = {
+ target: { kind: "Service", name: "example-service" }
+ patch: "op:\n replace\npath:\n /spec/type\nvalue:\n NodePort"
+ }
+ let expected_patch_content_2nd_patch: record = {
+ target: $target
+ patch: ($patch_value | to yaml)
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content_1st_patch, $expected_patch_content_2nd_patch] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+# --- add jsonpatch tests ---
+
+export def "test overlaypatch add jsonpatch add operation" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let kustomization_name: string = "example-kustomization"
+ let ks_namespace: string = "default"
+ let target: record = {
+ kind: "Deployment"
+ name: "example-deployment"
+ }
+ let path: string = "/spec/replicas"
+ let value: any = 3
+
+ let actual: record = $resourcelist | add jsonpatch --ks-namespace $ks_namespace $kustomization_name $target $path $value
+ let expected_patch_content: record = {
+ target: $target
+ patch: (
+ [{ op: "add", path: $path, value: $value }] | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch add jsonpatch replace operation" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let kustomization_name: string = "example-kustomization"
+ let ks_namespace: string = "default"
+ let target: record = {
+ kind: "Deployment"
+ name: "example-deployment"
+ }
+ let path: string = "/spec/replicas"
+ let value: any = 3
+
+ let actual: record = $resourcelist | (
+ add jsonpatch
+ --ks-namespace $ks_namespace
+ --operation "replace"
+ $kustomization_name
+ $target
+ $path
+ $value
+ )
+ let expected_patch_content: record = {
+ target: $target
+ patch: (
+ [{ op: "replace", path: $path, value: $value }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch add jsonpatch remove operation" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ spec: { patches: [] }
+ }
+ ]
+ }
+
+ let kustomization_name: string = "example-kustomization"
+ let ks_namespace: string = "default"
+ let target: record = {
+ kind: "Deployment"
+ name: "example-deployment"
+ }
+ let path: string = "/spec/replicas"
+
+ let actual: record = $resourcelist | (
+ add jsonpatch
+ --ks-namespace $ks_namespace
+ --operation "remove"
+ $kustomization_name
+ $target
+ $path
+ )
+ let expected_patch_content: record = {
+ target: $target
+ patch: (
+ [{ op: "remove", path: $path}]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch add jsonpatch move operation" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let kustomization_name: string = "example-kustomization"
+ let ks_namespace: string = "default"
+ let target: record = {
+ kind: "Deployment"
+ name: "example-deployment"
+ }
+ let path: string = "/spec/new-replicas"
+ let from: string = "/spec/replicas"
+
+ let actual: record = (
+ $resourcelist
+ | add jsonpatch
+ --ks-namespace $ks_namespace
+ --operation "move"
+ $kustomization_name
+ $target
+ $path
+ ''
+ $from
+ )
+ let expected_patch_content: record = {
+ target: $target
+ patch: (
+ [{ op: "move", from: $from, path: $path }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch add jsonpatch copy operation" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ spec: { patches: [] }
+ }
+ ]
+ }
+
+ let kustomization_name: string = "example-kustomization"
+ let ks_namespace: string = "default"
+ let target: record = {
+ kind: "Deployment"
+ name: "example-deployment"
+ }
+ let path: string = "/spec/new-replicas"
+ let from: string = "/spec/replicas"
+
+ let actual: record = (
+ $resourcelist
+ | add jsonpatch
+ --ks-namespace $ks_namespace
+ --operation "copy"
+ $kustomization_name
+ $target
+ $path
+ ''
+ $from
+ )
+ let expected_patch_content: record = {
+ target: $target
+ patch: (
+ [{ op: "copy", from: $from, path: $path }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch add jsonpatch invalid operation" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let kustomization_name: string = "example-kustomization"
+ let ks_namespace: string = "default"
+ let target: record = {
+ kind: "Deployment"
+ name: "example-deployment"
+ }
+ let path: string = "/spec/replicas"
+
+ let error_occurred: any = try {
+ $resourcelist | (
+ add jsonpatch
+ --ks-namespace $ks_namespace
+ --operation "invalid"
+ $kustomization_name
+ $target
+ $path
+ )
+ } catch {
+ |err| $err.msg
+ }
+
+ assert equal $error_occurred "Invalid operation type. Supported values are 'add', 'remove', 'replace', 'move', 'copy'. See RFC6902 for details"
+}
+
+
+# --- helmrelease add inline values tests ---
+
+export def "test overlaypatch helmrelease add inline values with add operation" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = ""
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let values: record = { key1: "value1", key2: "value2" }
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease add inline values
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ $values
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/values", value: $values }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch helmrelease add inline values with replace operation" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = ""
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let values: record = { key1: "value1", key2: "value2" }
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease add inline values
+ --ks-namespace $ks_namespace
+ --operation replace
+ $kustomization_name
+ $helmrelease_name
+ $values
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "replace", path: "/spec/values", value: $values }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch helmrelease add inline values with existing patches" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ spec: { patches: [
+ {
+ target: { kind: "HelmRelease", name: "existing-helmrelease" }
+ patch: (
+ [{ op: "replace", path: "/spec/values/replicaCount", value: 2 }]
+ | to yaml
+ )
+ }
+ ] }
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = ""
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let values: record = { key1: "value1", key2: "value2" }
+
+ let actual: record = $resourcelist | helmrelease add inline values --ks-namespace $ks_namespace $kustomization_name $helmrelease_name $values
+ let expected_patch_content_new: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/values", value: $values }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [
+ {
+ target: { kind: "HelmRelease", name: "existing-helmrelease" }
+ patch: (
+ [{ op: "replace", path: "/spec/values/replicaCount", value: 2 }]
+ | to yaml
+ )
+ },
+ $expected_patch_content_new
+ ] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+# --- helmrelease add values from configmap tests ---
+
+export def "test overlaypatch helmrelease add values from configmap basic" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = ""
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let cm_name: string = "example-configmap"
+ let cm_key: string = "values.yaml"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease add values from configmap
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ $cm_name
+ $cm_key
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: $cm_key } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch helmrelease add values from configmap with target path" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = ""
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let cm_name: string = "example-configmap"
+ let cm_key: string = "values.yaml"
+ let target_path: string = "/custom/path"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease add values from configmap
+ --ks-namespace $ks_namespace
+ --target-path $target_path
+ $kustomization_name
+ $helmrelease_name
+ $cm_name
+ $cm_key
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: $cm_key, targetPath: $target_path } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch helmrelease add values from configmap with existing patches" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ spec: { patches: [
+ {
+ target: { kind: "HelmRelease", name: "existing-helmrelease" }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: "existing-configmap", key: "existing-values.yaml" } }]
+ | to yaml
+ )
+ }
+ ] }
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = ""
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let cm_name: string = "example-configmap"
+ let cm_key: string = "values.yaml"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease add values from configmap
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ $cm_name
+ $cm_key
+ )
+ )
+ let expected_patch_content_existing: record = {
+ target: { kind: "HelmRelease", name: "existing-helmrelease" }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: "existing-configmap", key: "existing-values.yaml" } }]
+ | to yaml
+ )
+ }
+ let expected_patch_content_new: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: $cm_key } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content_existing, $expected_patch_content_new] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+
+# --- helmrelease add values from secret tests ---
+
+export def "test overlaypatch helmrelease add values from secret basic" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = ""
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let secret_name: string = "example-secret"
+ let secret_key: string = "values.yaml"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease add values from secret
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ $secret_name
+ $secret_key
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: $secret_key } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch helmrelease add values from secret with target path" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = ""
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let secret_name: string = "example-secret"
+ let secret_key: string = "values.yaml"
+ let target_path: string = "/custom/path"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease add values from secret
+ --ks-namespace $ks_namespace
+ --target-path $target_path
+ $kustomization_name
+ $helmrelease_name
+ $secret_name
+ $secret_key
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: $secret_key, targetPath: $target_path } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch helmrelease add values from secret with optional flag" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = ""
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let secret_name: string = "example-secret"
+ let secret_key: string = "values.yaml"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease add values from secret
+ --ks-namespace $ks_namespace
+ --optional $kustomization_name
+ $helmrelease_name
+ $secret_name
+ $secret_key
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: $secret_key, optional: true } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch helmrelease add values from secret with hr namespace" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = "example-namespace"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let secret_name: string = "example-secret"
+ let secret_key: string = "values.yaml"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease add values from secret
+ --ks-namespace $ks_namespace
+ --hr-namespace $hr_namespace
+ $kustomization_name
+ $helmrelease_name
+ $secret_name
+ $secret_key
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name, namespace: $hr_namespace }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: $secret_key } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test overlaypatch helmrelease add values from secret with existing patches" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ spec: { patches: [
+ {
+ target: { kind: "HelmRelease", name: "existing-helmrelease" }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: "existing-secret", key: "existing-values.yaml" } }]
+ | to yaml
+ )
+ }
+ ] }
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let hr_namespace: string = ""
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let secret_name: string = "example-secret"
+ let secret_key: string = "values.yaml"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease add values from secret
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ $secret_name
+ $secret_key
+ )
+ )
+ let expected_patch_content_existing: record = {
+ target: { kind: "HelmRelease", name: "existing-helmrelease" }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: "existing-secret", key: "existing-values.yaml" } }]
+ | to yaml
+ )
+ }
+ let expected_patch_content_new: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: $secret_key } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content_existing, $expected_patch_content_new] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+# TODO:
+
+# --- helmrelease set values ---
+
+## Inline values only
+export def "test overlaypatch helmrelease set values with inline values only" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let inline_values: record = { key1: "value1", key2: "value2" }
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ $inline_values
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/values", value: $inline_values }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+## Values from ConfigMap only
+export def "test overlaypatch helmrelease set values with configmap only" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let cm_name: string = "example-configmap"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ {}
+ $cm_name
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+## Values from Secret only
+export def "test overlaypatch helmrelease set values with secret only" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let secret_name: string = "example-secret"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ {}
+ ''
+ $secret_name
+ )
+ )
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: "values.yaml" } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+## Inline values and values from ConfigMap
+export def "test overlaypatch helmrelease set values with inline and configmap" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let inline_values: record = { key1: "value1", key2: "value2" }
+ let cm_name: string = "example-configmap"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ $inline_values
+ $cm_name
+ )
+ )
+ let expected_patch_content_inline_values: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/values", value: $inline_values }]
+ | to yaml
+ )
+ }
+ let expected_patch_content_cm_values: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content_inline_values, $expected_patch_content_cm_values] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+## Inline values and values from secret
+export def "test overlaypatch helmrelease set values with inline and secret" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let inline_values: record = { key1: "value1", key2: "value2" }
+ let secret_name: string = "example-secret"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ $inline_values
+ ''
+ $secret_name
+ )
+ )
+ let expected_patch_content_inline_values: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/values", value: $inline_values }]
+ | to yaml
+ )
+ }
+ let expected_patch_content_secret_values: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: "values.yaml" } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content_inline_values, $expected_patch_content_secret_values] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+## Values from cm and values from secret
+export def "test overlaypatch helmrelease set values with configmap and secret" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let cm_name: string = "example-configmap"
+ let secret_name: string = "example-secret"
+
+ let actual: record = (
+ $resourcelist
+ | (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ {}
+ $cm_name
+ $secret_name
+ )
+ )
+ let expected_patch_content_cm_values: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" } }]
+ | to yaml
+ )
+ }
+ let expected_patch_content_secret_values: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: "values.yaml" } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content_cm_values, $expected_patch_content_secret_values] }
+ }
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+## Inline values, values from cm and values from secret
+export def "test overlaypatch helmrelease set values with inline, configmap, and secret" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let inline_values: record = { key1: "value1", key2: "value2" }
+ let cm_name: string = "example-configmap"
+ let secret_name: string = "example-secret"
+
+ let actual_resourcelist: record = (
+ $resourcelist
+ | (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ $kustomization_name
+ $helmrelease_name
+ $inline_values
+ $cm_name
+ $secret_name
+ )
+ )
+
+ # Expected patches for inline values, ConfigMap, and Secret
+ let expected_patch_inline_values: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/values", value: $inline_values }]
+ | to yaml
+ )
+ }
+ let expected_patch_cm_values: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" } }]
+ | to yaml
+ )
+ }
+ let expected_patch_secret_values: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [{ op: "add", path: "/spec/valuesFrom/-", value: { kind: "Secret", name: $secret_name, key: "values.yaml" } }]
+ | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_inline_values, $expected_patch_cm_values, $expected_patch_secret_values] }
+ }
+ ]
+ }
+
+ assert equal $actual_resourcelist $expected_resourcelist
+}
+
+
+export def "test helmrelease set values with create configmap" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let cm_name: string = "example-configmap"
+ let create_cm_with_values: record = { key1: "value1", key2: "value2" }
+
+ let actual: record = (
+ $resourcelist |
+ (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ --create-cm-with-values $create_cm_with_values
+ $kustomization_name
+ $helmrelease_name
+ {}
+ $cm_name
+ )
+ )
+ let expected_cm_manifest: record = {
+ apiVersion: "v1"
+ kind: "ConfigMap"
+ metadata: {
+ name: $cm_name,
+ namespace: "default"
+ annotations: {
+ # "config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-configmap.yaml",
+ # "internal.config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-configmap.yaml"
+ "config.kubernetes.io/path": "example-configmap.yaml",
+ "internal.config.kubernetes.io/path": "example-configmap.yaml"
+ }
+ }
+ data: {
+ "values.yaml": ($create_cm_with_values | to yaml | str trim)
+ }
+ }
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [
+ {
+ op: "add",
+ path: "/spec/valuesFrom/-",
+ value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" }
+ }
+ ] | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ },
+ $expected_cm_manifest
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test helmrelease set values with create secret" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let secret_name: string = "example-secret"
+ let create_secret_with_values: record = {key1: "value1", key2: "value2"}
+
+ let actual: record = $resourcelist | (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ --create-secret-with-values $create_secret_with_values
+ $kustomization_name
+ $helmrelease_name
+ {}
+ ''
+ $secret_name
+ )
+ let expected_secret_manifest: record = {
+ apiVersion: "v1"
+ kind: "Secret"
+ metadata: {
+ name: $secret_name,
+ namespace: "default"
+ annotations: {
+ # "config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml",
+ # "internal.config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml"
+ "config.kubernetes.io/path": "example-secret.yaml",
+ "internal.config.kubernetes.io/path": "example-secret.yaml"
+ }
+ }
+ data: {
+ "values.yaml": ($create_secret_with_values | to yaml | str trim | encode base64)
+ }
+ }
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [
+ {
+ op: "add",
+ path: "/spec/valuesFrom/-",
+ value: { kind: "Secret", name: $secret_name, key: "values.yaml" }
+ }
+ ] | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ },
+ $expected_secret_manifest
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test helmrelease set values with create configmap and secret" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let cm_name: string = "example-configmap"
+ let secret_name: string = "example-secret"
+ let create_cm_with_values: record = { key1: "value1", key2: "value2" }
+ let create_secret_with_values: record = { key3: "value3", key4: "value4" }
+
+ let actual: record = $resourcelist | (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ --create-cm-with-values $create_cm_with_values
+ --create-secret-with-values $create_secret_with_values
+ $kustomization_name
+ $helmrelease_name
+ {}
+ $cm_name
+ $secret_name
+ )
+ let expected_cm_manifest: record = {
+ apiVersion: "v1"
+ kind: "ConfigMap"
+ metadata: {
+ name: $cm_name,
+ namespace: "default"
+ annotations: {
+ # "config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-configmap.yaml",
+ # "internal.config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-configmap.yaml"
+ "config.kubernetes.io/path": "example-configmap.yaml",
+ "internal.config.kubernetes.io/path": "example-configmap.yaml"
+ }
+ }
+ data: {
+ "values.yaml": ($create_cm_with_values | to yaml | str trim)
+ }
+ }
+ let expected_secret_manifest: record = {
+ apiVersion: "v1"
+ kind: "Secret"
+ metadata: {
+ name: $secret_name,
+ namespace: "default",
+ annotations: {
+ # "config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml",
+ # "internal.config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml"
+ "config.kubernetes.io/path": "example-secret.yaml",
+ "internal.config.kubernetes.io/path": "example-secret.yaml"
+ }
+ }
+ data: {
+ "values.yaml": ($create_secret_with_values | to yaml | str trim | encode base64)
+ }
+ }
+ let expected_patch_content_cm: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [
+ {
+ op: "add",
+ path: "/spec/valuesFrom/-",
+ value: { kind: "ConfigMap", name: $cm_name, key: "values.yaml" }
+ }
+ ] | to yaml
+ )
+ }
+ let expected_patch_content_secret: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [
+ {
+ op: "add",
+ path: "/spec/valuesFrom/-",
+ value: { kind: "Secret", name: $secret_name, key: "values.yaml" }
+ }
+ ] | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content_cm, $expected_patch_content_secret] }
+ },
+ $expected_cm_manifest,
+ $expected_secret_manifest
+ ]
+ }
+
+ assert equal $actual $expected_resourcelist
+}
+
+
+export def "test helmrelease set values with create secret and age encryption" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: "example-kustomization", namespace: "default" }
+ # spec: { patches: [] }
+ spec: {}
+ }
+ ]
+ }
+
+ let ks_namespace: string = "default"
+ let kustomization_name: string = "example-kustomization"
+ let helmrelease_name: string = "example-helmrelease"
+ let secret_name: string = "example-secret"
+ let create_secret_with_values: record = {
+ key1: "value1",
+ key2: "value2"
+ }
+ let test_public_key: string = "age1hsrtxphk7exrdc0kt8dgr8a8r3hx88v3xpsw0ezaxvefsy9asegqknppc0"
+ let test_private_key: string = "AGE-SECRET-KEY-12CC3A4LEDYF4S26UV6Z2MEG7ZQL9PTU5NHH6N3FN6FLJ5HACW9LQX0UWP2"
+
+ let actual: record = $resourcelist | (
+ helmrelease set values
+ --ks-namespace $ks_namespace
+ --create-secret-with-values $create_secret_with_values
+ --public-age-key $test_public_key
+ $kustomization_name
+ $helmrelease_name
+ {}
+ ''
+ $secret_name
+ )
+
+ let expected_secret_manifest: record = {
+ apiVersion: "v1"
+ kind: "Secret"
+ metadata: {
+ name: $secret_name,
+ namespace: "default"
+ annotations: {
+ # "config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml",
+ # "internal.config.kubernetes.io/path": "example-kustomization-example-helmrelease-example-secret.yaml"
+ "config.kubernetes.io/path": "example-secret.yaml",
+ "internal.config.kubernetes.io/path": "example-secret.yaml"
+ }
+ }
+ data: {
+ "values.yaml": ($create_secret_with_values | to yaml | str trim | encode base64)
+ }
+ }
+
+ # NOTE: Here the secret is kept decrypted intentionally, since the same secret encrypted twice is never equal and we will need to decrypt them anyway to check they are equal
+ let expected_patch_content: record = {
+ target: { kind: "HelmRelease", name: $helmrelease_name }
+ patch: (
+ [
+ {
+ op: "add",
+ path: "/spec/valuesFrom/-",
+ value: { kind: "Secret", name: $secret_name, key: "values.yaml" }
+ }
+ ] | to yaml
+ )
+ }
+ let expected_resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ $expected_secret_manifest,
+ {
+ apiVersion: "kustomize.toolkit.fluxcd.io/v1"
+ kind: "Kustomization"
+ metadata: { name: $kustomization_name, namespace: $ks_namespace }
+ spec: { patches: [$expected_patch_content] }
+ }
+ ]
+ }
+
+ # Check that everything except the encrypted secret is equal
+ (assert equal
+ ($actual | patch resource delete '' 'Secret')
+ ($expected_resourcelist | patch resource delete '' 'Secret')
+ )
+
+ # Check that both secrets, once decrypted, are equal
+ let actual_secret_manifest: record = (
+ # First, extracts the manifest of the encrypted Secret
+ $actual
+ | patch resource keep '' 'Secret'
+ | get items.0
+ # Removes the filename annotations, since they are excluded from encryption
+ | reject $.metadata.annotations
+ # Then, decrypts the manifest using the private key
+ | to yaml
+ | keypair decrypt secret manifest $test_private_key
+ | from yaml
+ )
+ (assert equal
+ $actual_secret_manifest
+ ($expected_secret_manifest | reject $.metadata.annotations)
+ )
+}
diff --git a/docker/osm-nushell-krm-functions/krm/tests/patch.nu b/docker/osm-nushell-krm-functions/krm/tests/patch.nu
new file mode 100644
index 0000000..ec2ae70
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/patch.nu
@@ -0,0 +1,1438 @@
+#!/usr/bin/env -S nu --stdin
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+
+use std assert
+use ../../krm/patch.nu *
+
+
+# --- resource keep tests ---
+
+export def "test patch resource keep no filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource keep
+ let expected: record = $resourcelist
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource keep by apiVersion" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource keep "apps/v1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource keep by kind" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource keep '' "Deployment"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource keep by name" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource keep '' '' "example1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource keep by namespace" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource keep '' '' '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource keep multiple filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource keep "apps/v1" "Deployment" '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource keep invalid input" []: [
+ nothing -> nothing
+] {
+ let invalid_input: record = {kind: "Invalid kind"}
+
+ let error_occurred: any = try {
+ $invalid_input | resource keep
+ } catch {
+ |err| $err.msg
+ }
+
+ assert ($error_occurred | str starts-with "Error: Expected a ResourceList, but received")
+}
+
+
+
+# --- resource delete tests ---
+
+export def "test patch resource delete no filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource delete
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: []
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource delete by apiVersion" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource delete "apps/v1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource delete by kind" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource delete '' "Deployment"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource delete by name" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource delete '' '' "example1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource delete by namespace" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource delete '' '' '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource delete multiple filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let actual: record = $resourcelist | resource delete "apps/v1" "Deployment" '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource delete invalid input" []: [
+ nothing -> nothing
+] {
+ let invalid_input: record = {kind: "Invalid kind"}
+
+ let error_occurred: any = try {
+ echo $invalid_input | resource delete
+ } catch {
+ |err| $err.msg
+ }
+
+ assert ($error_occurred | str starts-with "Error: Expected a ResourceList, but received")
+
+}
+
+
+
+# --- resource custom function tests ---
+
+export def "test patch resource custom function no filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let custom_function: closure = { |k: cell-path, v: any| ($in | upsert $k $v) }
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource custom function $custom_function $key_path $value
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", labels: { app: "example" } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource custom function by apiVersion" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let custom_function: closure = { |k: cell-path, v: any| ($in | upsert $k $v) }
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource custom function $custom_function $key_path $value "apps/v1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", labels: { app: "example" } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource custom function by kind" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let custom_function: closure = { |k: cell-path, v: any| ($in | upsert $k $v) }
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource custom function $custom_function $key_path $value '' "Deployment"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", labels: { app: "example" } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource custom function by name" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let custom_function: closure = { |k: cell-path, v: any| ($in | upsert $k $v) }
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource custom function $custom_function $key_path $value '' '' "example1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource custom function by namespace" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let custom_function: closure = { |k: cell-path, v: any| ($in | upsert $k $v) }
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource custom function $custom_function $key_path $value '' '' '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource custom function multiple filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let custom_function: closure = { |k: cell-path, v: any| ($in | upsert $k $v) }
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource custom function $custom_function $key_path $value "apps/v1" "Deployment" '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource custom function invalid input" []: [
+ nothing -> nothing
+] {
+ let invalid_input: record = {kind: "Invalid kind"}
+
+ let error_occurred: any = try {
+ $invalid_input | resource custom function {|item, key_path, value| $item | update $key_path $value } $.metadata.labels { app: "example" }
+ } catch {
+ |err| $err.msg
+ }
+
+ assert ($error_occurred | str starts-with "Error: Expected a ResourceList, but received")
+}
+
+
+
+# --- resource upsert key tests ---
+
+export def "test patch resource upsert key no filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource upsert key $key_path $value
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", labels: { app: "example" } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource upsert key by apiVersion" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource upsert key $key_path $value "apps/v1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", labels: { app: "example" } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource upsert key by kind" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource upsert key $key_path $value '' "Deployment"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", labels: { app: "example" } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource upsert key by name" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource upsert key $key_path $value '' '' "example1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource upsert key by namespace" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource upsert key $key_path $value '' '' '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource upsert key multiple filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.labels
+ let value: any = { app: "example" }
+
+ let actual: record = $resourcelist | resource upsert key $key_path $value "apps/v1" "Deployment" '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", labels: { app: "example" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource upsert key invalid input" []: [
+ nothing -> nothing
+] {
+ let invalid_input: record = {kind: "Invalid kind"}
+
+ let error_occurred: any = try {
+ $invalid_input | resource upsert key $.metadata.labels { app: "example" }
+ } catch {
+ |err| $err.msg
+ }
+
+ assert ($error_occurred | str starts-with "Error: Expected a ResourceList, but received")
+}
+
+
+
+# --- resource filename set tests ---
+
+export def "test patch resource filename set no index" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let filename: string = "example.yaml"
+ let actual: record = $resourcelist | resource filename set $filename
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource filename set with index" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let filename: string = "example.yaml"
+ let index: int = 0
+ let actual: record = $resourcelist | resource filename set --index $index $filename
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename, "config.kubernetes.io/index": "0", "internal.config.kubernetes.io/index": "0" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename, "config.kubernetes.io/index": "0", "internal.config.kubernetes.io/index": "0" } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename, "config.kubernetes.io/index": "0", "internal.config.kubernetes.io/index": "0" } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource filename set by apiVersion" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let filename: string = "example.yaml"
+ let actual: record = $resourcelist | resource filename set $filename "apps/v1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource filename set by kind" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let filename: string = "example.yaml"
+ let actual: record = $resourcelist | resource filename set $filename '' "Deployment"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource filename set by name" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let filename: string = "example.yaml"
+ let actual: record = $resourcelist | resource filename set $filename '' '' "example1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource filename set by namespace" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let filename: string = "example.yaml"
+ let actual: record = $resourcelist | resource filename set $filename '' '' '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch resource filename set multiple filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let filename: string = "example.yaml"
+ let actual: record = $resourcelist | resource filename set $filename "apps/v1" "Deployment" '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { "config.kubernetes.io/path": $filename, "internal.config.kubernetes.io/path": $filename } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+
+# TODO:
+
+# --- list append item tests ---
+
+export def "test patch list append item no filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "example-value"
+ let actual: record = $resourcelist | list append item $key_path $value
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["example-value"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["example-value"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["example-value"] } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list append item existing list" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["initial-value"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["initial-value"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["initial-value"] } } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "example-value"
+ let actual: record = $resourcelist | list append item $key_path $value
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["initial-value", "example-value"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["initial-value", "example-value"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["initial-value", "example-value"] } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list append item existing non-list value" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: "initial-value" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: "initial-value" } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: "initial-value" } } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "example-value"
+
+ let error_occurred: any = try {
+ $resourcelist | list append item $key_path $value
+ } catch {
+ |err| $err.msg
+ }
+
+ assert ($error_occurred | str starts-with "Error: Some matching keys are not lists. Non conformant:")
+}
+
+
+export def "test patch list append item by apiVersion" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "example-value"
+ let actual: record = $resourcelist | list append item $key_path $value "apps/v1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["example-value"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["example-value"] } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list append item by kind" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "example-value"
+ let actual: record = $resourcelist | list append item $key_path $value '' "Deployment"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["example-value"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["example-value"] } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list append item by name" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "example-value"
+ let actual: record = $resourcelist | list append item $key_path $value '' '' "example1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["example-value"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list append item by namespace" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "example-value"
+ let actual: record = $resourcelist | list append item $key_path $value '' '' '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["example-value"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["example-value"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list append item multiple filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default" } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "example-value"
+ let actual: record = $resourcelist | list append item $key_path $value "apps/v1" 'Deployment' '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["example-value"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default" } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other" } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+# TODO:
+
+# --- list drop item tests ---
+
+export def "test patch list drop item no filters" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value1", "value2"] } } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "value1"
+ let actual: record = $resourcelist | list drop item $key_path $value
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value2"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value2"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value2"] } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list drop item existing list with multiple values" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value1", "value2", "value3"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value1", "value2", "value3"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value1", "value2", "value3"] } } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "value2"
+ let actual: record = $resourcelist | list drop item $key_path $value
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value1", "value3"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value1", "value3"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value1", "value3"] } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list drop item existing non-list value" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: "value1" } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: "value1" } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: "value1" } } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "value1"
+
+ let error_occurred: any = try {
+ $resourcelist | list drop item $key_path $value
+ } catch {
+ |err| $err.msg
+ }
+
+ assert ($error_occurred | str starts-with "Error: Some matching keys are not lists. Non conformant:")
+}
+
+
+export def "test patch list drop item by apiVersion" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value1", "value2"] } } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "value1"
+ let actual: record = $resourcelist | list drop item $key_path $value "apps/v1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value2"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value2"] } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list drop item by kind" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value1", "value2"] } } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "value1"
+ let actual: record = $resourcelist | list drop item $key_path $value '' "Deployment"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value2"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value2"] } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list drop item by name" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value1", "value2"] } } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "value1"
+ let actual: record = $resourcelist | list drop item $key_path $value '' '' "example1"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value2"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value1", "value2"] } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
+
+
+export def "test patch list drop item by namespace" []: [
+ nothing -> nothing
+] {
+ let resourcelist: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value1", "value2"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value1", "value2"] } } }
+ ]
+ }
+
+ let key_path: cell-path = $.metadata.annotations.example
+ let value: any = "value1"
+ let actual: record = $resourcelist | list drop item $key_path $value '' '' '' "default"
+ let expected: record = {
+ apiVersion: "config.kubernetes.io/v1"
+ kind: "ResourceList"
+ items: [
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example1", namespace: "default", annotations: { example: ["value2"] } } }
+ { apiVersion: "v1", kind: "Pod", metadata: { name: "example2", namespace: "default", annotations: { example: ["value2"] } } }
+ { apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "example3", namespace: "other", annotations: { example: ["value1", "value2"] } } }
+ ]
+ }
+
+ assert equal $actual $expected
+}
diff --git a/docker/osm-nushell-krm-functions/krm/tests/strategicmergepatch.nu b/docker/osm-nushell-krm-functions/krm/tests/strategicmergepatch.nu
new file mode 100644
index 0000000..095c60a
--- /dev/null
+++ b/docker/osm-nushell-krm-functions/krm/tests/strategicmergepatch.nu
@@ -0,0 +1,144 @@
+#!/usr/bin/env -S nu --stdin
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+
+
+use std assert
+use ../../krm/strategicmergepatch.nu *
+
+
+# --- create strategic merge patch tests ---
+
+export def "test strategicmergepatch create strategic merge patch basic" []: [
+ nothing -> nothing
+] {
+ let target: record = {
+ kind: "Deployment"
+ name: "podinfo"
+ }
+ let patch: record = {
+ apiVersion: "apps/v1"
+ kind: "Deployment"
+ metadata: {
+ name: "not-used"
+ }
+ spec: {
+ template: {
+ metadata: {
+ annotations: {
+ "cluster-autoscaler.kubernetes.io/safe-to-evict": "true"
+ }
+ }
+ }
+ }
+ }
+
+ let actual: record = create $target $patch
+ let expected: record = {
+ target: $target,
+ patch: ($patch | to yaml)
+ }
+
+ assert equal $actual.target $expected.target
+ assert equal $actual.patch $expected.patch
+}
+
+
+export def "test strategicmergepatch create strategic merge patch with dollar-patch directives" []: [
+ nothing -> nothing
+] {
+ let target: record = {
+ kind: "Deployment"
+ name: "podinfo"
+ }
+ let patch: record = {
+ apiVersion: "apps/v1"
+ kind: "Deployment"
+ metadata: {
+ name: "not-used"
+ }
+ spec: {
+ template: {
+ metadata: {
+ annotations: {
+ "cluster-autoscaler.kubernetes.io/safe-to-evict": "true"
+ }
+ }
+ }
+ }
+ "\$patch": "replace"
+ }
+
+ let actual: record = create $target $patch
+ let expected: record = {
+ target: $target,
+ patch: ($patch | to yaml)
+ }
+
+ assert equal $actual.target $expected.target
+ assert equal $actual.patch $expected.patch
+}
+
+
+# export def "test strategicmergepatch create strategic merge patch invalid target" []: [
+# nothing -> nothing
+# ] {
+# let target: record = {"Invalid target": "Invalid value"}
+# let patch: record = {
+# apiVersion: "apps/v1"
+# kind: "Deployment"
+# metadata: {
+# name: "not-used"
+# }
+# spec: {
+# template: {
+# metadata: {
+# annotations: {
+# "cluster-autoscaler.kubernetes.io/safe-to-evict": "true"
+# }
+# }
+# }
+# }
+# }
+
+# let error_occurred: error = try {
+# create $target $patch
+# } catch {
+# |err| $err.msg
+# }
+
+# assert equal $error_occurred "Expected a record"
+# }
+
+
+# export def "test strategicmergepatch create strategic merge patch invalid patch" []: [
+# nothing -> nothing
+# ] {
+# let target: record = {
+# kind: "Deployment"
+# name: "podinfo"
+# }
+# let patch: record = {"Invalid patch": "Invalid value"}
+
+# let error_occurred: error = try {
+# create $target $patch
+# } catch {
+# |err| $err.msg
+# }
+
+# assert equal $error_occurred "Expected a record"
+# }