blob: ec5cb18436a126b3963c1bdd789e07e3540b0983 [file] [log] [blame]
garciadeblas83775ba2025-07-23 18:35:24 +02001#######################################################################################
2# Copyright ETSI Contributors and Others.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#######################################################################################
17
18# Tests of App instance management
19
20use ../../krm *
21use ../app.nu
22use ../location.nu
23use ../replace.nu
24
25
26# --- all-in-one example (example 1) ---
27
28export def "test app example one" []: [
29 nothing -> nothing
30] {
31 let expected: list<record> = (
32 open artifacts/sw-catalogs/apps/example1/expected_result.yaml
33 )
34
35 let fleet_repos_base: path = (mktemp -t -d)
36
37 let environment: record = {
38 FLEET_REPOS_BASE: $fleet_repos_base
39 CATALOG_REPOS_BASE: ($env.pwd | path join artifacts)
40 APPNAME: myapp01
41 APPNAMESPACE: app-namespace
42 PROFILE_TYPE: apps
43 PROFILE_NAME: mycluster01
44 secret-values-for-postgres-operator-myapp01: {
45 POSTGRES_OPERATOR_HOST: postgres-operator-host
46 POSTGRES_OPERATOR_PORT: 5432
47 POSTGRES_OPERATOR_USER: postgres-operator-user
48 POSTGRES_OPERATOR_PASSWORD: postgres-operator-password
49 }
50 secret-values-for-postgres-operator-ui-myapp01: {
51 POSTGRES_OPERATOR_UI_HOST: postgres-operator-ui-host
52 POSTGRES_OPERATOR_UI_PORT: 8080
53 POSTGRES_OPERATOR_UI_USER: postgres-operator-ui-user
54 POSTGRES_OPERATOR_UI_PASSWORD: postgres-operator-ui-password
55 }
56 }
57
58 let actual: list<record> = (
59 open artifacts/sw-catalogs/apps/example1/app-instance-from-model.yaml
60 | app create --dry-run $environment
61 )
62
63 # Overwrites the encrypted part of the secrets in both
64 let actual_trimmed: list<record> = (
65 $actual
66 # For each KSU's ResourceList
67 | each {|k|
68 $k
69 # Delete sops key from all secrets
70 | ( patch resource reject key $.sops '' Secret )
71 # Replace encrypted value by empty string
72 | ( patch resource update key $.data."values.yaml" 'ENCRYPTED' '' Secret )
73 }
74 )
75 let expected_trimmed: list<record> = (
76 $expected
77 # For each KSU's ResourceList
78 | each {|k|
79 $k
80 # Delete sops key from all secrets
81 | ( patch resource reject key $.sops '' Secret )
82 # Replace encrypted value by empty string
83 | ( patch resource update key $.data."values.yaml" 'ENCRYPTED' '' Secret )
84 }
85 )
86
87 # Checks
88 assert equal $actual_trimmed $expected_trimmed
89
90 # Cleanup
91 rm -rf $fleet_repos_base
92}
93
94
95# --- all-in-one example (example 2) ---
96
97export def "test app example two" []: [
98 nothing -> nothing
99] {
100 let expected: list<record> = (
101 open artifacts/sw-catalogs/apps/example2/expected_result.yaml
102 )
103 let fleet_repos_base: path = (mktemp -t -d)
104
105 let environment: record = {
106 FLEET_REPOS_BASE: $fleet_repos_base
107 CATALOG_REPOS_BASE: ($env.pwd | path join artifacts)
108 APPNAME: myapp02
109 APPNAMESPACE: app-namespace
110 PROFILE_TYPE: apps
111 PROFILE_NAME: mycluster02
112 secret-values-for-postgres-operator-myapp02: {
113 POSTGRES_OPERATOR_HOST: postgres-operator-host
114 POSTGRES_OPERATOR_PORT: 5432
115 POSTGRES_OPERATOR_USER: postgres-operator-user
116 POSTGRES_OPERATOR_PASSWORD: postgres-operator-password
117 }
118 secret-values-for-postgres-operator-ui-myapp01: {
119 POSTGRES_OPERATOR_UI_HOST: postgres-operator-ui-host
120 POSTGRES_OPERATOR_UI_PORT: 8080
121 POSTGRES_OPERATOR_UI_USER: postgres-operator-ui-user
122 POSTGRES_OPERATOR_UI_PASSWORD: postgres-operator-ui-password
123 }
124 }
125
126 let actual: list<record> = (
127 open artifacts/sw-catalogs/apps/example2/app-instance-from-model.yaml
128 | app create --dry-run $environment
129 )
130
131 # Overwrites the encrypted part of the secrets in both
132 let actual_trimmed: list<record> = (
133 $actual
134 # For each KSU's ResourceList
135 | each {|k|
136 $k
137 # Delete sops key from all secrets
138 | ( patch resource reject key $.sops '' Secret )
139 # Replace encrypted value by empty string
140 | ( patch resource update key $.data."values.yaml" 'ENCRYPTED' '' Secret )
141 }
142 )
143 let expected_trimmed: list<record> = (
144 $expected
145 # For each KSU's ResourceList
146 | each {|k|
147 $k
148 # Delete sops key from all secrets
149 # | ( patch resource reject key $.sops '' Secret )
150 # Replace encrypted value by empty string
151 # | ( patch resource update key $.data."values.yaml" 'ENCRYPTED' '' Secret )
152 }
153 )
154
155 # Checks
156 assert equal $actual_trimmed $expected_trimmed
157
158 # Cleanup
159 rm -rf $fleet_repos_base
160}
161
162
163export def "test app example two written to folder" []: [
164 nothing -> nothing
165] {
166 let expected: list<record> = (
167 open artifacts/sw-catalogs/apps/example2/expected_result.yaml
168 )
169 let fleet_repos_base: path = (mktemp -t -d)
170
171 let environment: record = {
172 FLEET_REPOS_BASE: $fleet_repos_base
173 CATALOG_REPOS_BASE: ($env.pwd | path join artifacts)
174 APPNAME: myapp02
175 APPNAMESPACE: app-namespace
176 PROFILE_TYPE: apps
177 PROFILE_NAME: mycluster02
178 secret-values-for-postgres-operator-myapp02: {
179 POSTGRES_OPERATOR_HOST: postgres-operator-host
180 POSTGRES_OPERATOR_PORT: 5432
181 POSTGRES_OPERATOR_USER: postgres-operator-user
182 POSTGRES_OPERATOR_PASSWORD: postgres-operator-password
183 }
184 secret-values-for-postgres-operator-ui-myapp01: {
185 POSTGRES_OPERATOR_UI_HOST: postgres-operator-ui-host
186 POSTGRES_OPERATOR_UI_PORT: 8080
187 POSTGRES_OPERATOR_UI_USER: postgres-operator-ui-user
188 POSTGRES_OPERATOR_UI_PASSWORD: postgres-operator-ui-password
189 }
190 }
191
192 # Retrieve instance model
193 let instance_model: record = (open artifacts/sw-catalogs/apps/example2/app-instance-from-model.yaml)
194
195 # Write to folder
196 $instance_model | app create $environment
197
198 # Calculate the actual ResourceLists from the target folders
199 let targets: list<string> = (
200 $instance_model
201 | replace vars $environment
202 | get $.spec.ksus
203 | each {|k|
204 $k
205 | get "target"
206 | location to absolute path
207 }
208 )
209 let actual: list<record> = (
210 $targets
211 | each {|t|
212 {} | convert folder to resourcelist $t
213 }
214 )
215
216 # Overwrites the encrypted part of the secrets in both
217 let actual_trimmed: list<record> = (
218 $actual
219 # For each KSU's ResourceList
220 | each {|k|
221 $k
222 # Delete sops key from all secrets
223 | ( patch resource reject key $.sops '' Secret )
224 # Replace encrypted value by empty string
225 | ( patch resource update key $.data."values.yaml" 'ENCRYPTED' '' Secret )
226 }
227 )
228 let expected_trimmed: list<record> = (
229 $expected
230 # For each KSU's ResourceList
231 | each {|k|
232 $k
233 # Delete sops key from all secrets
234 # | ( patch resource reject key $.sops '' Secret )
235 # Replace encrypted value by empty string
236 # | ( patch resource update key $.data."values.yaml" 'ENCRYPTED' '' Secret )
237 }
238 )
239
240 # Ensures that the items from both ResourceLists are sorted in the same order and removes irrelevant indexes and keys from `kpt`
241 let actual_fixed: list<record> = ($actual_trimmed | get $.items.0 | sort-by $.kind | sort-by $.metadata.name
242 | each {|k|
243 $k
244 | reject -i $.metadata.annotations."config.kubernetes.io/index"
245 | reject -i $.metadata.annotations."internal.config.kubernetes.io/index"
246 | reject -i $.metadata.annotations."internal.config.kubernetes.io/seqindent"
247 })
248 let expected_fixed = ($expected_trimmed | get $.items.0 | sort-by $.kind | sort-by $.metadata.name
249 | each {|k|
250 $k
251 | reject -i $.metadata.annotations."config.kubernetes.io/index"
252 | reject -i $.metadata.annotations."internal.config.kubernetes.io/index"
253 | reject -i $.metadata.annotations."internal.config.kubernetes.io/seqindent"
254 })
255
256 # Checks
257 assert equal $actual_fixed $expected_fixed
258
259 # Cleanup
260 rm -rf $fleet_repos_base
261}