| 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 ../../krm/strategicmergepatch.nu * |
| 22 | |
| 23 | |
| 24 | # --- create strategic merge patch tests --- |
| 25 | |
| 26 | export def "test strategicmergepatch create strategic merge patch basic" []: [ |
| 27 | nothing -> nothing |
| 28 | ] { |
| 29 | let target: record = { |
| 30 | kind: "Deployment" |
| 31 | name: "podinfo" |
| 32 | } |
| 33 | let patch: record = { |
| 34 | apiVersion: "apps/v1" |
| 35 | kind: "Deployment" |
| 36 | metadata: { |
| 37 | name: "not-used" |
| 38 | } |
| 39 | spec: { |
| 40 | template: { |
| 41 | metadata: { |
| 42 | annotations: { |
| 43 | "cluster-autoscaler.kubernetes.io/safe-to-evict": "true" |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | let actual: record = create $target $patch |
| 51 | let expected: record = { |
| 52 | target: $target, |
| 53 | patch: ($patch | to yaml) |
| 54 | } |
| 55 | |
| 56 | assert equal $actual.target $expected.target |
| 57 | assert equal $actual.patch $expected.patch |
| 58 | } |
| 59 | |
| 60 | |
| 61 | export def "test strategicmergepatch create strategic merge patch with dollar-patch directives" []: [ |
| 62 | nothing -> nothing |
| 63 | ] { |
| 64 | let target: record = { |
| 65 | kind: "Deployment" |
| 66 | name: "podinfo" |
| 67 | } |
| 68 | let patch: record = { |
| 69 | apiVersion: "apps/v1" |
| 70 | kind: "Deployment" |
| 71 | metadata: { |
| 72 | name: "not-used" |
| 73 | } |
| 74 | spec: { |
| 75 | template: { |
| 76 | metadata: { |
| 77 | annotations: { |
| 78 | "cluster-autoscaler.kubernetes.io/safe-to-evict": "true" |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | "\$patch": "replace" |
| 84 | } |
| 85 | |
| 86 | let actual: record = create $target $patch |
| 87 | let expected: record = { |
| 88 | target: $target, |
| 89 | patch: ($patch | to yaml) |
| 90 | } |
| 91 | |
| 92 | assert equal $actual.target $expected.target |
| 93 | assert equal $actual.patch $expected.patch |
| 94 | } |
| 95 | |
| 96 | |
| 97 | # export def "test strategicmergepatch create strategic merge patch invalid target" []: [ |
| 98 | # nothing -> nothing |
| 99 | # ] { |
| 100 | # let target: record = {"Invalid target": "Invalid value"} |
| 101 | # let patch: record = { |
| 102 | # apiVersion: "apps/v1" |
| 103 | # kind: "Deployment" |
| 104 | # metadata: { |
| 105 | # name: "not-used" |
| 106 | # } |
| 107 | # spec: { |
| 108 | # template: { |
| 109 | # metadata: { |
| 110 | # annotations: { |
| 111 | # "cluster-autoscaler.kubernetes.io/safe-to-evict": "true" |
| 112 | # } |
| 113 | # } |
| 114 | # } |
| 115 | # } |
| 116 | # } |
| 117 | |
| 118 | # let error_occurred: error = try { |
| 119 | # create $target $patch |
| 120 | # } catch { |
| 121 | # |err| $err.msg |
| 122 | # } |
| 123 | |
| 124 | # assert equal $error_occurred "Expected a record" |
| 125 | # } |
| 126 | |
| 127 | |
| 128 | # export def "test strategicmergepatch create strategic merge patch invalid patch" []: [ |
| 129 | # nothing -> nothing |
| 130 | # ] { |
| 131 | # let target: record = { |
| 132 | # kind: "Deployment" |
| 133 | # name: "podinfo" |
| 134 | # } |
| 135 | # let patch: record = {"Invalid patch": "Invalid value"} |
| 136 | |
| 137 | # let error_occurred: error = try { |
| 138 | # create $target $patch |
| 139 | # } catch { |
| 140 | # |err| $err.msg |
| 141 | # } |
| 142 | |
| 143 | # assert equal $error_occurred "Expected a record" |
| 144 | # } |