|
| 1 | +package kustomize |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/rancher/wrangler/pkg/yaml" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + |
| 10 | + kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 11 | + "sigs.k8s.io/kustomize/api/types" |
| 12 | +) |
| 13 | + |
| 14 | +func assertActualTrimSpaceEqualsExpected(t *testing.T, actual string, expected string) { |
| 15 | + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(actual)) |
| 16 | +} |
| 17 | + |
| 18 | +func writeSmallBase(th kusttest_test.Harness) { |
| 19 | + th.WriteK("/app/base", ` |
| 20 | +namePrefix: a- |
| 21 | +commonLabels: |
| 22 | + app: myApp |
| 23 | +resources: |
| 24 | +- deployment.yaml |
| 25 | +- service.yaml |
| 26 | +`) |
| 27 | + th.WriteF("/app/base/service.yaml", ` |
| 28 | +apiVersion: v1 |
| 29 | +kind: Service |
| 30 | +metadata: |
| 31 | + name: myService |
| 32 | +spec: |
| 33 | + selector: |
| 34 | + backend: bungie |
| 35 | + ports: |
| 36 | + - port: 7002 |
| 37 | +`) |
| 38 | + th.WriteF("/app/base/deployment.yaml", ` |
| 39 | +apiVersion: apps/v1 |
| 40 | +kind: Deployment |
| 41 | +metadata: |
| 42 | + name: myDeployment |
| 43 | +spec: |
| 44 | + template: |
| 45 | + metadata: |
| 46 | + labels: |
| 47 | + backend: awesome |
| 48 | + spec: |
| 49 | + containers: |
| 50 | + - name: whatever |
| 51 | + image: whatever |
| 52 | +`) |
| 53 | +} |
| 54 | + |
| 55 | +func TestSmallOverlay(t *testing.T) { |
| 56 | + th := kusttest_test.MakeHarness(t) |
| 57 | + writeSmallBase(th) |
| 58 | + th.WriteK("/app/overlay", ` |
| 59 | +namePrefix: b- |
| 60 | +commonLabels: |
| 61 | + env: prod |
| 62 | + quotedFruit: "peach" |
| 63 | + quotedBoolean: "true" |
| 64 | +resources: |
| 65 | +- ../base |
| 66 | +patchesStrategicMerge: |
| 67 | +- deployment/deployment.yaml |
| 68 | +images: |
| 69 | +- name: whatever |
| 70 | + newTag: 1.8.0 |
| 71 | +`) |
| 72 | + |
| 73 | + th.WriteF("/app/overlay/configmap/app.env", ` |
| 74 | +DB_USERNAME=admin |
| 75 | +DB_PASSWORD=somepw |
| 76 | +`) |
| 77 | + th.WriteF("/app/overlay/configmap/app-init.ini", ` |
| 78 | +FOO=bar |
| 79 | +BAR=baz |
| 80 | +`) |
| 81 | + th.WriteF("/app/overlay/deployment/deployment.yaml", ` |
| 82 | +apiVersion: apps/v1 |
| 83 | +kind: Deployment |
| 84 | +metadata: |
| 85 | + name: myDeployment |
| 86 | +spec: |
| 87 | + replicas: 1000 |
| 88 | +`) |
| 89 | + result, err := kustomize(th.GetFSys(), "/app/overlay", "") |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("kustomize failed: %v", err) |
| 92 | + } |
| 93 | + data, err := yaml.ToBytes(result) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("yaml.ToBytes failed: %v", err) |
| 96 | + } |
| 97 | + assertActualTrimSpaceEqualsExpected(t, string(data), ` |
| 98 | +apiVersion: v1 |
| 99 | +kind: Service |
| 100 | +metadata: |
| 101 | + labels: |
| 102 | + app: myApp |
| 103 | + env: prod |
| 104 | + quotedBoolean: "true" |
| 105 | + quotedFruit: peach |
| 106 | + name: b-a-myService |
| 107 | +spec: |
| 108 | + ports: |
| 109 | + - port: 7002 |
| 110 | + selector: |
| 111 | + app: myApp |
| 112 | + backend: bungie |
| 113 | + env: prod |
| 114 | + quotedBoolean: "true" |
| 115 | + quotedFruit: peach |
| 116 | +
|
| 117 | +--- |
| 118 | +apiVersion: apps/v1 |
| 119 | +kind: Deployment |
| 120 | +metadata: |
| 121 | + labels: |
| 122 | + app: myApp |
| 123 | + env: prod |
| 124 | + quotedBoolean: "true" |
| 125 | + quotedFruit: peach |
| 126 | + name: b-a-myDeployment |
| 127 | +spec: |
| 128 | + replicas: 1000 |
| 129 | + selector: |
| 130 | + matchLabels: |
| 131 | + app: myApp |
| 132 | + env: prod |
| 133 | + quotedBoolean: "true" |
| 134 | + quotedFruit: peach |
| 135 | + template: |
| 136 | + metadata: |
| 137 | + labels: |
| 138 | + app: myApp |
| 139 | + backend: awesome |
| 140 | + env: prod |
| 141 | + quotedBoolean: "true" |
| 142 | + quotedFruit: peach |
| 143 | + spec: |
| 144 | + containers: |
| 145 | + - image: whatever:1.8.0 |
| 146 | + name: whatever |
| 147 | +`) |
| 148 | +} |
| 149 | + |
| 150 | +func TestSharedPatchDisAllowed(t *testing.T) { |
| 151 | + th := kusttest_test.MakeHarness(t) |
| 152 | + writeSmallBase(th) |
| 153 | + th.WriteK("/app/overlay", ` |
| 154 | +commonLabels: |
| 155 | + env: prod |
| 156 | +resources: |
| 157 | +- ../base |
| 158 | +patchesStrategicMerge: |
| 159 | +- ../shared/deployment-patch.yaml |
| 160 | +`) |
| 161 | + th.WriteF("/app/shared/deployment-patch.yaml", ` |
| 162 | +apiVersion: apps/v1 |
| 163 | +kind: Deployment |
| 164 | +metadata: |
| 165 | + name: myDeployment |
| 166 | +spec: |
| 167 | + replicas: 1000 |
| 168 | +`) |
| 169 | + _, err := kustomize(th.GetFSys(), "/app/overlay", "") |
| 170 | + if !strings.Contains( |
| 171 | + err.Error(), |
| 172 | + "security; file '/app/shared/deployment-patch.yaml' is not in or below '/app/overlay'") { |
| 173 | + t.Fatalf("unexpected error: %s", err) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func TestSharedPatchAllowed(t *testing.T) { |
| 178 | + th := kusttest_test.MakeHarness(t) |
| 179 | + writeSmallBase(th) |
| 180 | + th.WriteK("/app/overlay", ` |
| 181 | +commonLabels: |
| 182 | + env: prod |
| 183 | +resources: |
| 184 | +- ../base |
| 185 | +patchesStrategicMerge: |
| 186 | +- ../shared/deployment-patch.yaml |
| 187 | +`) |
| 188 | + th.WriteF("/app/shared/deployment-patch.yaml", ` |
| 189 | +apiVersion: apps/v1 |
| 190 | +kind: Deployment |
| 191 | +metadata: |
| 192 | + name: myDeployment |
| 193 | +spec: |
| 194 | + replicas: 1000 |
| 195 | +`) |
| 196 | + result, err := kustomize(th.GetFSys(), "/app/overlay", "--load-restrictor "+types.LoadRestrictionsNone.String()) |
| 197 | + if err != nil { |
| 198 | + t.Fatalf("kustomize failed: %v", err) |
| 199 | + } |
| 200 | + data, err := yaml.ToBytes(result) |
| 201 | + if err != nil { |
| 202 | + t.Fatalf("yaml.ToBytes failed: %v", err) |
| 203 | + } |
| 204 | + assertActualTrimSpaceEqualsExpected(t, string(data), ` |
| 205 | +apiVersion: v1 |
| 206 | +kind: Service |
| 207 | +metadata: |
| 208 | + labels: |
| 209 | + app: myApp |
| 210 | + env: prod |
| 211 | + name: a-myService |
| 212 | +spec: |
| 213 | + ports: |
| 214 | + - port: 7002 |
| 215 | + selector: |
| 216 | + app: myApp |
| 217 | + backend: bungie |
| 218 | + env: prod |
| 219 | +
|
| 220 | +--- |
| 221 | +apiVersion: apps/v1 |
| 222 | +kind: Deployment |
| 223 | +metadata: |
| 224 | + labels: |
| 225 | + app: myApp |
| 226 | + env: prod |
| 227 | + name: a-myDeployment |
| 228 | +spec: |
| 229 | + replicas: 1000 |
| 230 | + selector: |
| 231 | + matchLabels: |
| 232 | + app: myApp |
| 233 | + env: prod |
| 234 | + template: |
| 235 | + metadata: |
| 236 | + labels: |
| 237 | + app: myApp |
| 238 | + backend: awesome |
| 239 | + env: prod |
| 240 | + spec: |
| 241 | + containers: |
| 242 | + - image: whatever |
| 243 | + name: whatever |
| 244 | +`) |
| 245 | +} |
0 commit comments