blob: 77cbcd4428f556dc1091e8249aa484c81492a1f2 [file] [log] [blame]
garciadeblas83775ba2025-07-23 18:35:24 +02001#!/usr/bin/env -S nu --stdin
2#######################################################################################
3# Copyright ETSI Contributors and Others.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#######################################################################################
18
19
20use std assert
21use ../../krm/jsonpatch.nu *
22
23
24# --- create operation tests ---
25
26export def "test jsonpatch create operation add" []: [
27 nothing -> nothing
28] {
29 let op: string = "add"
30 let path: string = "/spec/template/spec/securityContext"
31 let value: record = {
32 runAsUser: 10000
33 fsGroup: 1337
34 }
35
36 let actual: record = create operation $op $path $value
37 let expected: record = {
38 op: $op,
39 path: $path,
40 value: $value
41 }
42
43 assert equal $actual $expected
44}
45
46
47export def "test jsonpatch create operation remove" []: [
48 nothing -> nothing
49] {
50 let op: string = "remove"
51 let path: string = "/spec/template/spec/securityContext"
52
53 let actual: record = create operation $op $path
54 let expected: record = {
55 op: $op,
56 path: $path
57 }
58
59 assert equal $actual $expected
60}
61
62
63export def "test jsonpatch create operation replace" []: [
64 nothing -> nothing
65] {
66 let op: string = "replace"
67 let path: string = "/spec/template/spec/securityContext"
68 let value: record = {
69 runAsUser: 10000
70 fsGroup: 1337
71 }
72
73 let actual: record = create operation $op $path $value
74 let expected: record = {
75 op: $op,
76 path: $path,
77 value: $value
78 }
79
80 assert equal $actual $expected
81}
82
83
84export def "test jsonpatch create operation move" []: [
85 nothing -> nothing
86] {
87 let op: string = "move"
88 let from: string = "/spec/template/spec/securityContext"
89 let path: string = "/spec/template/spec/newSecurityContext"
90
91 let actual: record = create operation $op $path '' $from
92 let expected: record = {
93 op: $op,
94 from: $from,
95 path: $path
96 }
97
98 assert equal $actual $expected
99}
100
101
102export def "test jsonpatch create operation copy" []: [
103 nothing -> nothing
104] {
105 let op: string = "copy"
106 let from: string = "/spec/template/spec/securityContext"
107 let path: string = "/spec/template/spec/newSecurityContext"
108
109 let actual: record = create operation $op $path '' $from
110 let expected: record = {
111 op: $op,
112 from: $from,
113 path: $path
114 }
115
116 assert equal $actual $expected
117}
118
119
120export def "test jsonpatch create operation invalid op" []: [
121 nothing -> nothing
122] {
123 let op: string = "invalid"
124 let path: string = "/spec/template/spec/securityContext"
125
126 let error_occurred: error = try {
127 create operation $op $path
128 } catch {
129 |err| $err.msg
130 }
131
132 assert equal $error_occurred "Invalid operation type. Supported values are 'add', 'remove', 'replace', 'move', 'copy'. See RFC6902 for details."
133}
134
135
136export def "test jsonpatch create operation missing value for add/replace" []: [
137 nothing -> nothing
138] {
139 let op: string = "add"
140 let path: string = "/spec/template/spec/securityContext"
141
142 let error_occurred: error = try {
143 create operation $op $path
144 } catch {
145 |err| $err.msg
146 }
147
148 assert equal $error_occurred "Value is required for 'add' and 'replace' operations."
149}
150
151
152export def "test jsonpatch create operation missing from for move/copy" []: [
153 nothing -> nothing
154] {
155 let op: string = "move"
156 let path: string = "/spec/template/spec/newSecurityContext"
157
158 let error_occurred: error = try {
159 create operation $op $path
160 } catch {
161 |err| $err.msg
162 }
163
164 assert equal $error_occurred "Source path is required for 'move' and 'copy' operations."
165}
166
167
168# --- create JSON patch tests ---
169
170export def "test jsonpatch create JSON patch basic" []: [
171 nothing -> nothing
172] {
173 let target: record = {
174 kind: "Deployment"
175 name: "podinfo"
176 }
177 let operation: record = {
178 op: "add"
179 path: "/spec/template/spec/securityContext"
180 value: {
181 runAsUser: 10000
182 fsGroup: 1337
183 }
184 }
185
186 let actual: record = create $target $operation
187 let expected: record = {
188 target: $target,
189 patch: ([$operation] | to yaml)
190 }
191
192 assert equal $actual.target $expected.target
193 assert equal $actual.patch $expected.patch
194}
195
196
197export def "test jsonpatch create JSON patch multiple operations" []: [
198 nothing -> nothing
199] {
200 let target: record = {
201 kind: "Deployment"
202 name: "podinfo"
203 }
204 let operation1: record = {
205 op: "add"
206 path: "/spec/template/spec/securityContext"
207 value: {
208 runAsUser: 10000
209 fsGroup: 1337
210 }
211 }
212 let operation2: record = {
213 op: "replace"
214 path: "/spec/replicas"
215 value: 3
216 }
217
218 let actual: record = create $target $operation1 $operation2
219 let expected: record = {
220 target: $target,
221 patch: (
222 [$operation1, $operation2] | to yaml
223 )
224 }
225
226 assert equal $actual.target $expected.target
227 assert equal $actual.patch $expected.patch
228}
229
230
231export def "test jsonpatch create JSON patch using operation helper" []: [
232 nothing -> nothing
233] {
234 let target: record = {
235 kind: "Deployment"
236 name: "podinfo"
237 }
238 let operation: record = (
239 create operation add "/spec/template/spec/securityContext" {runAsUser: 10000, fsGroup: 1337}
240 )
241
242 let actual: record = create $target $operation
243 let expected: record = {
244 target: $target,
245 patch: ([$operation] | to yaml)
246 }
247
248 assert equal $actual.target $expected.target
249 assert equal $actual.patch $expected.patch
250}