| 1 |
// Copyright 2020 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package krusty_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"sigs.k8s.io/kustomize/api/internal/kusterr" |
| 10 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 11 |
) |
| 12 |
|
| 13 |
func TestBasicIO_1(t *testing.T) { |
| 14 |
th := kusttest_test.MakeHarness(t) |
| 15 |
th.WriteK(".", ` |
| 16 |
resources: |
| 17 |
- service.yaml |
| 18 |
`) |
| 19 |
th.WriteF("service.yaml", ` |
| 20 |
apiVersion: v1 |
| 21 |
kind: Service |
| 22 |
metadata: |
| 23 |
annotations: |
| 24 |
port: 8080 |
| 25 |
happy: true |
| 26 |
color: green |
| 27 |
name: demo |
| 28 |
spec: |
| 29 |
clusterIP: None |
| 30 |
`) |
| 31 |
m := th.Run(".", th.MakeDefaultOptions()) |
| 32 |
// The annotations are sorted by key, hence the order change. |
| 33 |
// Quotes are added intentionally. |
| 34 |
th.AssertActualEqualsExpected( |
| 35 |
m, ` |
| 36 |
apiVersion: v1 |
| 37 |
kind: Service |
| 38 |
metadata: |
| 39 |
annotations: |
| 40 |
color: green |
| 41 |
happy: "true" |
| 42 |
port: "8080" |
| 43 |
name: demo |
| 44 |
spec: |
| 45 |
clusterIP: None |
| 46 |
`) |
| 47 |
} |
| 48 |
|
| 49 |
func TestBasicIO_2(t *testing.T) { |
| 50 |
th := kusttest_test.MakeHarness(t) |
| 51 |
opts := th.MakeDefaultOptions() |
| 52 |
th.WriteK(".", ` |
| 53 |
resources: |
| 54 |
- service.yaml |
| 55 |
`) |
| 56 |
// All the annotation values are quoted in the input. |
| 57 |
th.WriteF("service.yaml", ` |
| 58 |
apiVersion: v1 |
| 59 |
kind: Service |
| 60 |
metadata: |
| 61 |
annotations: |
| 62 |
port: "8080" |
| 63 |
happy: "true" |
| 64 |
color: green |
| 65 |
name: demo |
| 66 |
spec: |
| 67 |
clusterIP: None |
| 68 |
`) |
| 69 |
m := th.Run(".", opts) |
| 70 |
// The annotations are sorted by key, hence the order change. |
| 71 |
th.AssertActualEqualsExpected(m, ` |
| 72 |
apiVersion: v1 |
| 73 |
kind: Service |
| 74 |
metadata: |
| 75 |
annotations: |
| 76 |
color: green |
| 77 |
happy: "true" |
| 78 |
port: "8080" |
| 79 |
name: demo |
| 80 |
spec: |
| 81 |
clusterIP: None |
| 82 |
`) |
| 83 |
} |
| 84 |
|
| 85 |
// test for https://github.com/kubernetes-sigs/kustomize/issues/3812#issuecomment-862339267 |
| 86 |
func TestBasicIO3812(t *testing.T) { |
| 87 |
th := kusttest_test.MakeHarness(t) |
| 88 |
th.WriteK(".", `resources: |
| 89 |
- service.yaml |
| 90 |
`) |
| 91 |
|
| 92 |
th.WriteF("service.yaml", ` |
| 93 |
apiVersion: v1 |
| 94 |
kind: Service |
| 95 |
metadata: |
| 96 |
name: kapacitor |
| 97 |
labels: |
| 98 |
app.kubernetes.io/name: tick-kapacitor |
| 99 |
spec: |
| 100 |
selector: |
| 101 |
app.kubernetes.io/name: tick-kapacitor |
| 102 |
- name: http |
| 103 |
port: 9092 |
| 104 |
protocol: TCP |
| 105 |
type: ClusterIP |
| 106 |
`) |
| 107 |
|
| 108 |
err := th.RunWithErr(".", th.MakeDefaultOptions()) |
| 109 |
if !kusterr.IsMalformedYAMLError(err) { |
| 110 |
t.Fatalf("unexpected error: %q", err) |
| 111 |
} |
| 112 |
} |