| garciadeblas | 83775ba | 2025-07-23 18:35:24 +0200 | [diff] [blame] | 1 | #!/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 | |
| 20 | use std assert |
| 21 | use std null-device |
| 22 | use ../../krm/convert.nu * |
| 23 | |
| 24 | |
| 25 | # --- replace environment variables tests --- |
| 26 | |
| 27 | export def "test convert replace environment variables no vars" []: [ |
| 28 | nothing -> nothing |
| 29 | ] { |
| 30 | let text: string = "Hello, $USER!" |
| 31 | |
| 32 | $env.USER = "test_user" |
| 33 | let actual: string = ( |
| 34 | echo $text | replace environment variables |
| 35 | ) |
| 36 | let expected: string = "Hello, test_user!" |
| 37 | |
| 38 | assert equal $actual $expected |
| 39 | } |
| 40 | |
| 41 | |
| 42 | export def "test convert replace environment variables string vars" []: [ |
| 43 | nothing -> nothing |
| 44 | ] { |
| 45 | let text: string = "Hello, $USER! Your HOME is $HOME." |
| 46 | let vars_to_replace: string = "$USER,$HOME" |
| 47 | |
| 48 | load-env { |
| 49 | USER: "test_user" |
| 50 | HOME: "/home/test_user" |
| 51 | } |
| 52 | |
| 53 | let actual: string = ( |
| 54 | echo $text | replace environment variables $vars_to_replace |
| 55 | ) |
| 56 | let expected: string = "Hello, test_user! Your HOME is /home/test_user." |
| 57 | |
| 58 | assert equal $actual $expected |
| 59 | } |
| 60 | |
| 61 | |
| 62 | export def "test convert replace environment variables list vars" []: [ |
| 63 | nothing -> nothing |
| 64 | ] { |
| 65 | let text: string = "Hello, $USER! Your HOME is $HOME." |
| 66 | let vars_to_replace: list<string> = ["USER", "HOME"] |
| 67 | |
| 68 | load-env { |
| 69 | USER: "test_user" |
| 70 | HOME: "/home/test_user" |
| 71 | } |
| 72 | |
| 73 | let actual: string = ( |
| 74 | echo $text | replace environment variables $vars_to_replace |
| 75 | ) |
| 76 | let expected: string = "Hello, test_user! Your HOME is /home/test_user." |
| 77 | |
| 78 | assert equal $actual $expected |
| 79 | } |
| 80 | |
| 81 | |
| 82 | export def "test convert replace environment variables invalid input" []: [ |
| 83 | nothing -> nothing |
| 84 | ] { |
| 85 | let text: string = "Hello, $USER!" |
| 86 | let vars_to_replace: int = 123 |
| 87 | |
| 88 | let error_occurred: error = try { |
| 89 | echo $text | replace environment variables $vars_to_replace |
| 90 | |
| 91 | } catch { |
| 92 | |err| $err.msg |
| 93 | } |
| 94 | |
| 95 | assert equal $error_occurred "Error: Expected a string or list of strings, but received int" |
| 96 | } |
| 97 | |
| 98 | |
| 99 | export def "test convert replace environment variables no replacement" []: [ |
| 100 | nothing -> nothing |
| 101 | ] { |
| 102 | let text: string = "Hello, $NON_EXISTENT_VAR!" |
| 103 | let actual: string = ( |
| 104 | echo $text | replace environment variables |
| 105 | ) |
| 106 | |
| 107 | let expected: string = "Hello, !" |
| 108 | |
| 109 | assert equal $actual $expected |
| 110 | } |
| 111 | |
| 112 | |
| 113 | # --- folder to resourcelist tests --- |
| 114 | |
| 115 | export def "test convert folder to resourcelist empty input" []: [ |
| 116 | nothing -> nothing |
| 117 | ] { |
| 118 | let folder: path = "./artifacts/empty" |
| 119 | let expected: record = { |
| 120 | apiVersion: "config.kubernetes.io/v1" |
| 121 | kind: "ResourceList" |
| 122 | items: [] |
| 123 | } |
| 124 | |
| 125 | let actual: record = (folder to resourcelist $folder) |
| 126 | assert equal $actual $expected |
| 127 | } |
| 128 | |
| 129 | |
| 130 | export def "test convert folder to resourcelist no substitution" []: [ |
| 131 | nothing -> nothing |
| 132 | ] { |
| 133 | let folder: path = "./artifacts/jenkins/templates" |
| 134 | let input_list: record = { |
| 135 | apiVersion: "config.kubernetes.io/v1" |
| 136 | kind: "ResourceList" |
| 137 | items: ["item1", "item2"] |
| 138 | } |
| 139 | |
| 140 | let actual: record = ( |
| 141 | echo $input_list | folder to resourcelist $folder |
| 142 | ) |
| 143 | let expected_items: list<string> = ["item1", "item2"] | append ( |
| 144 | kpt fn source $folder |
| 145 | | from yaml |
| 146 | | get items |
| 147 | ) |
| 148 | |
| 149 | assert equal $actual.apiVersion "config.kubernetes.io/v1" |
| 150 | assert equal $actual.kind "ResourceList" |
| 151 | assert equal $actual.items $expected_items |
| 152 | } |
| 153 | |
| 154 | |
| 155 | export def "test convert folder to resourcelist with substitution" []: [ |
| 156 | nothing -> nothing |
| 157 | ] { |
| 158 | let folder: path = "./artifacts/namespace/templates" |
| 159 | let input_list: record = { |
| 160 | apiVersion: "config.kubernetes.io/v1" |
| 161 | kind: "ResourceList" |
| 162 | items: ["item1", "item2"] |
| 163 | } |
| 164 | $env.TARGET_NS = "target-namespace" |
| 165 | |
| 166 | let actual: record = ( |
| 167 | echo $input_list | folder to resourcelist --subst-env $folder |
| 168 | ) |
| 169 | let expected_items: list<string> = ["item1", "item2"] | append ( |
| 170 | kpt fn source $folder |
| 171 | | replace environment variables |
| 172 | | from yaml |
| 173 | | get items |
| 174 | ) |
| 175 | |
| 176 | assert equal $actual.apiVersion "config.kubernetes.io/v1" |
| 177 | assert equal $actual.kind "ResourceList" |
| 178 | assert equal $actual.items $expected_items |
| 179 | assert equal $actual.items.2.metadata.name $env.TARGET_NS |
| 180 | } |
| 181 | |
| 182 | |
| 183 | export def "test convert folder to resourcelist with filter" []: [ |
| 184 | nothing -> nothing |
| 185 | ] { |
| 186 | let folder: path = "./artifacts/namespace/templates" |
| 187 | let input_list: record = { |
| 188 | apiVersion: "config.kubernetes.io/v1" |
| 189 | kind: "ResourceList" |
| 190 | items: ["item1", "item2"] |
| 191 | } |
| 192 | $env.TARGET_NS = "target-namespace" |
| 193 | let env_filter = "$TARGET_NS" |
| 194 | |
| 195 | let actual: record = ( |
| 196 | echo $input_list | folder to resourcelist --subst-env $folder $env_filter |
| 197 | ) |
| 198 | let expected_items: list<string> = ["item1", "item2"] | append ( |
| 199 | kpt fn source $folder |
| 200 | | replace environment variables $env_filter |
| 201 | | from yaml |
| 202 | | get items |
| 203 | ) |
| 204 | |
| 205 | assert equal $actual.apiVersion "config.kubernetes.io/v1" |
| 206 | assert equal $actual.kind "ResourceList" |
| 207 | assert equal $actual.items $expected_items |
| 208 | assert equal $actual.items.2.metadata.name $env.TARGET_NS |
| 209 | } |
| 210 | |
| 211 | |
| 212 | export def "test convert folder to resourcelist invalid input" []: [ |
| 213 | nothing -> nothing |
| 214 | ] { |
| 215 | let folder: path = "./non-existent-folder" |
| 216 | let input_list: record = { |
| 217 | apiVersion: "config.kubernetes.io/v1" |
| 218 | kind: "ResourceList" |
| 219 | items: ["item1", "item2"] |
| 220 | } |
| 221 | |
| 222 | let error_occurred: bool = try { |
| 223 | echo $input_list | folder to resourcelist $folder err> (null-device) |
| 224 | } catch { |
| 225 | |err| $err.msg |
| 226 | } |
| 227 | |
| 228 | assert equal $error_occurred "Can't convert to record." |
| 229 | } |
| 230 | |
| 231 | |
| 232 | |
| 233 | # --- manifest to resourcelist tests --- |
| 234 | |
| 235 | export def "test convert manifest to resourcelist empty input" []: [ |
| 236 | nothing -> nothing |
| 237 | ] { |
| 238 | let actual: record = manifest to resourcelist |
| 239 | let expected: record = { |
| 240 | apiVersion: "config.kubernetes.io/v1" |
| 241 | kind: "ResourceList" |
| 242 | items: [] |
| 243 | } |
| 244 | |
| 245 | assert equal $actual $expected |
| 246 | } |
| 247 | |
| 248 | |
| 249 | export def "test convert manifest to resourcelist single record" []: [ |
| 250 | nothing -> nothing |
| 251 | ] { |
| 252 | let manifest: record = { |
| 253 | name: "example" |
| 254 | kind: "Deployment" |
| 255 | } |
| 256 | |
| 257 | let actual: record = ( |
| 258 | $manifest | manifest to resourcelist |
| 259 | ) |
| 260 | let expected: record = { |
| 261 | apiVersion: "config.kubernetes.io/v1" |
| 262 | kind: "ResourceList" |
| 263 | items: [ $manifest ] |
| 264 | } |
| 265 | |
| 266 | assert equal $actual $expected |
| 267 | } |
| 268 | |
| 269 | |
| 270 | export def "test convert manifest to resourcelist list of records" []: [ |
| 271 | nothing -> nothing |
| 272 | ] { |
| 273 | let manifests: list<any> = [ |
| 274 | { name: "example1", kind: "Deployment" } |
| 275 | { name: "example2", kind: "Service" } |
| 276 | ] |
| 277 | |
| 278 | let actual: record = ( |
| 279 | $manifests | manifest to resourcelist |
| 280 | ) |
| 281 | let expected: record = { |
| 282 | apiVersion: "config.kubernetes.io/v1" |
| 283 | kind: "ResourceList" |
| 284 | items: $manifests |
| 285 | } |
| 286 | |
| 287 | assert equal $actual $expected |
| 288 | } |
| 289 | |
| 290 | |
| 291 | export def "test convert manifest to resourcelist invalid input" []: [ |
| 292 | nothing -> nothing |
| 293 | ] { |
| 294 | let invalid_manifest: string = "Invalid manifest" |
| 295 | |
| 296 | let error_occurred: bool = try { |
| 297 | $invalid_manifest | manifest to resourcelist |
| 298 | } catch { |
| 299 | |err| $err.msg |
| 300 | } |
| 301 | |
| 302 | assert equal $error_occurred "Error: Expected a record or a list of records, but received string." |
| 303 | } |
| 304 | |
| 305 | |
| 306 | |
| 307 | # --- resourcelist to folder tests --- |
| 308 | |
| 309 | export def "test convert resourcelist to folder dry run" []: [ |
| 310 | nothing -> nothing |
| 311 | ] { |
| 312 | let rl: record = { |
| 313 | apiVersion: "config.kubernetes.io/v1" |
| 314 | kind: "ResourceList" |
| 315 | items: ["item1", "item2"] |
| 316 | } |
| 317 | |
| 318 | let output: record = ( |
| 319 | $rl | resourcelist to folder "no-folder" true | from yaml |
| 320 | ) |
| 321 | |
| 322 | assert equal $output $rl |
| 323 | } |
| 324 | |
| 325 | |
| 326 | export def "test convert resourcelist to folder no sync" []: [ |
| 327 | nothing -> nothing |
| 328 | ] { |
| 329 | let source_folder: path = "./artifacts/jenkins/templates/" |
| 330 | let rl: record = ( |
| 331 | convert folder to resourcelist $source_folder |
| 332 | ) |
| 333 | let target_folder: string = (mktemp -t -d) |
| 334 | |
| 335 | # Run the command |
| 336 | $rl | resourcelist to folder $target_folder |
| 337 | |
| 338 | # Check if the contents were copied correctly |
| 339 | let actual_contents: list<string> = ( |
| 340 | ls --short-names $target_folder |
| 341 | | get name |
| 342 | | sort |
| 343 | ) |
| 344 | |
| 345 | # Cleanup |
| 346 | rm -rf $target_folder |
| 347 | |
| 348 | # Expected |
| 349 | let expected_contents: list<string> = ( |
| 350 | ls --short-names $source_folder |
| 351 | | get name |
| 352 | | sort |
| 353 | ) |
| 354 | |
| 355 | assert equal $actual_contents $expected_contents |
| 356 | } |
| 357 | |
| 358 | |
| 359 | export def "test convert resourcelist to folder sync" []: [ |
| 360 | nothing -> nothing |
| 361 | ] { |
| 362 | let source_folder: path = "./artifacts/jenkins/templates/" |
| 363 | let rl: record = ( |
| 364 | convert folder to resourcelist $source_folder |
| 365 | ) |
| 366 | let target_folder: string = (mktemp -t -d) |
| 367 | |
| 368 | # Add an extra file to the target folder (it should be removed by the synchronization) |
| 369 | ^touch ($target_folder | path join "extra_file.txt") |
| 370 | |
| 371 | # Run the command |
| 372 | $rl | resourcelist to folder --sync $target_folder |
| 373 | |
| 374 | # Check if the contents were copied correctly |
| 375 | let actual_contents: list<string> = ( |
| 376 | ls --short-names $target_folder |
| 377 | | get name |
| 378 | | sort |
| 379 | ) |
| 380 | |
| 381 | # Cleanup |
| 382 | rm -rf $target_folder |
| 383 | |
| 384 | # Expected |
| 385 | let expected_contents: list<string> = ( |
| 386 | ls --short-names $source_folder |
| 387 | | get name |
| 388 | | sort |
| 389 | ) |
| 390 | |
| 391 | assert equal $actual_contents $expected_contents |
| 392 | } |
| 393 | |
| 394 | |
| 395 | # export def "test convert resourcelist to folder invalid input" []: [ |
| 396 | # nothing -> nothing |
| 397 | # ] { |
| 398 | # let invalid_input: record = { "Invalid input": "invalid value" } |
| 399 | # let target_folder: string = (mktemp -t -d) |
| 400 | |
| 401 | # let error_occurred: any = try { |
| 402 | # $invalid_input | resourcelist to folder $target_folder |
| 403 | # } catch { |
| 404 | # |err| $err.msg |
| 405 | # } |
| 406 | |
| 407 | # # Cleanup |
| 408 | # print $target_folder |
| 409 | # # rm -rf $target_folder |
| 410 | |
| 411 | # assert equal $error_occurred "Can't convert to boolean." |
| 412 | # } |
| 413 | |
| 414 | |
| 415 | export def "test convert resourcelist to folder non-existent folder" []: [ |
| 416 | nothing -> nothing |
| 417 | ] { |
| 418 | let source_folder: path = "./artifacts/jenkins/templates/" |
| 419 | let rl: record = ( |
| 420 | convert folder to resourcelist $source_folder |
| 421 | ) |
| 422 | |
| 423 | let temp_folder: string = (mktemp -t -d) |
| 424 | let target_folder: string = ($temp_folder | path join "new-folder") |
| 425 | mkdir $target_folder |
| 426 | |
| 427 | # Run the command |
| 428 | $rl | resourcelist to folder $target_folder |
| 429 | |
| 430 | # Check if the contents were copied correctly |
| 431 | let actual_contents: list<string> = ( |
| 432 | ls --short-names $target_folder |
| 433 | | get name |
| 434 | | sort |
| 435 | ) |
| 436 | |
| 437 | # Cleanup |
| 438 | rm -rf $temp_folder |
| 439 | |
| 440 | # Expected |
| 441 | let expected_contents: list<string> = ( |
| 442 | ls --short-names $source_folder |
| 443 | | get name |
| 444 | | sort |
| 445 | ) |
| 446 | |
| 447 | assert equal $actual_contents $expected_contents |
| 448 | } |