blob: ae674b86fef5fc37d109366f558fe9c40809e915 [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/concatenate.nu *
22
23
24# --- resourcelists tests ---
25
26export def "test concatenate resourcelists empty inputs" []: [
27 nothing -> nothing
28] {
29 let input: record = {}
30 let resourcelist2: record = {}
31
32 let actual: record = resourcelists $resourcelist2
33 let expected: record = {
34 apiVersion: "config.kubernetes.io/v1"
35 kind: "ResourceList"
36 items: []
37 }
38
39 assert equal $actual $expected
40}
41
42
43export def "test concatenate resourcelists empty stdin" []: [
44 nothing -> nothing
45] {
46 let input: record = {
47 apiVersion: "config.kubernetes.io/v1"
48 kind: "ResourceList"
49 items: ["item1", "item2"]
50 }
51 let resourcelist2: record = {}
52
53 # Simulate empty stdin by passing input as an argument
54 let actual: record = resourcelists $input
55 let expected: record = $input
56
57 assert equal $actual $expected
58}
59
60
61export def "test concatenate resourcelists empty second list" []: [
62 nothing -> nothing
63] {
64 let input: record = {
65 apiVersion: "config.kubernetes.io/v1"
66 kind: "ResourceList"
67 items: ["item1", "item2"]
68 }
69 let resourcelist2: record = {}
70
71 # Simulate empty stdin by passing input as an argument
72 let actual: record = resourcelists $resourcelist2
73 let expected: record = {
74 apiVersion: "config.kubernetes.io/v1"
75 kind: "ResourceList"
76 items: []
77 }
78
79 # Since we're testing with stdin, we need to simulate it
80 let actual_with_stdin: record = (
81 echo $input | resourcelists $resourcelist2
82 )
83 let expected_with_stdin: record = $input
84
85 assert equal $actual_with_stdin $expected_with_stdin
86}
87
88
89export def "test concatenate resourcelists merge lists" []: [
90 nothing -> nothing
91] {
92 let input: record = {
93 apiVersion: "config.kubernetes.io/v1"
94 kind: "ResourceList"
95 items: ["item1", "item2"]
96 }
97 let resourcelist2: record = {
98 apiVersion: "config.kubernetes.io/v1"
99 kind: "ResourceList"
100 items: ["item3", "item4"]
101 }
102
103 let actual: record = (
104 echo $input | resourcelists $resourcelist2
105 )
106 let expected: record = {
107 apiVersion: "config.kubernetes.io/v1"
108 kind: "ResourceList"
109 items: ["item1", "item2", "item3", "item4"]
110 }
111
112 assert equal $actual $expected
113}
114
115
116export def "test concatenate resourcelists non-empty inputs with no items" []: [
117 nothing -> nothing
118] {
119 let input: record = {
120 apiVersion: "config.kubernetes.io/v1"
121 kind: "ResourceList"
122 }
123 let resourcelist2: record = {
124 apiVersion: "config.kubernetes.io/v1"
125 kind: "ResourceList"
126 }
127
128 let actual: record = (
129 echo $input | resourcelists $resourcelist2
130 )
131 let expected: record = {
132 apiVersion: "config.kubernetes.io/v1"
133 kind: "ResourceList"
134 items: []
135 }
136
137 assert equal $actual $expected
138}
139
140
141
142# --- manifests tests ---
143
144export def "test concatenate manifests empty inputs" []: [
145 nothing -> nothing
146] {
147 let mnfst2: any = null
148
149 let actual: list<any> = manifests $mnfst2
150 let expected: list<any> = []
151
152 assert equal $actual $expected
153}
154
155
156export def "test concatenate manifests empty stdin" []: [
157 nothing -> nothing
158] {
159 let mnfst2: record = {
160 name: "example"
161 kind: "Deployment"
162 }
163
164 let actual: list<any> = manifests $mnfst2
165 let expected: list<any> = [ $mnfst2 ]
166
167 assert equal $actual $expected
168}
169
170
171export def "test concatenate manifests empty second manifest" []: [
172 nothing -> nothing
173] {
174 let mnfst1: record = {
175 name: "example1"
176 kind: "Deployment"
177 }
178
179 let mnfst2: any = null
180
181 let actual: list<any> = (
182 echo $mnfst1 | manifests $mnfst2
183 )
184 let expected: list<any> = [ $mnfst1 ]
185
186 assert equal $actual $expected
187}
188
189
190export def "test concatenate manifests single records" []: [
191 nothing -> nothing
192] {
193 let mnfst1: record = {
194 name: "example1"
195 kind: "Deployment"
196 }
197 let mnfst2: record = {
198 name: "example2"
199 kind: "Service"
200 }
201
202 let actual: list<any> = (
203 echo $mnfst1 | manifests $mnfst2
204 )
205 let expected: list<any> = [ $mnfst1, $mnfst2 ]
206
207 assert equal $actual $expected
208}
209
210
211export def "test concatenate manifests lists of records" []: [
212 nothing -> nothing
213] {
214 let mnfst1: list<any> = [
215 { name: "example1", kind: "Deployment" }
216 { name: "example2", kind: "Service" }
217 ]
218 let mnfst2: list<any> = [
219 { name: "example3", kind: "Pod" }
220 { name: "example4", kind: "ConfigMap" }
221 ]
222
223 let actual: list<any> = (
224 echo $mnfst1 | manifests $mnfst2
225 )
226 let expected: list<any> = [
227 { name: "example1", kind: "Deployment" }
228 { name: "example2", kind: "Service" }
229 { name: "example3", kind: "Pod" }
230 { name: "example4", kind: "ConfigMap" }
231 ]
232
233 assert equal $actual $expected
234}
235
236
237export def "test concatenate manifests invalid input" []: [
238 nothing -> nothing
239] {
240 let mnfst2: string = "Invalid manifest"
241
242 let actual_error: error = (
243 try {
244 manifests $mnfst2
245 } catch {
246 |err| $err.msg
247 }
248 )
249
250 assert equal $actual_error "Error: Expected a record or a list of records, but received string."
251}