| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package resource_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
. "sigs.k8s.io/kustomize/api/resource" |
| 12 |
kyaml "sigs.k8s.io/kustomize/kyaml/yaml" |
| 13 |
) |
| 14 |
|
| 15 |
func TestOriginAppend(t *testing.T) { |
| 16 |
tests := []struct { |
| 17 |
in *Origin |
| 18 |
path string |
| 19 |
expected string |
| 20 |
}{ |
| 21 |
{ |
| 22 |
in: &Origin{ |
| 23 |
Path: "prod", |
| 24 |
}, |
| 25 |
path: "service.yaml", |
| 26 |
expected: `path: prod/service.yaml |
| 27 |
`, |
| 28 |
}, |
| 29 |
{ |
| 30 |
in: &Origin{ |
| 31 |
Path: "overlay/prod", |
| 32 |
}, |
| 33 |
path: "github.com/kubernetes-sigs/kustomize/examples/multibases/dev/", |
| 34 |
expected: `path: examples/multibases/dev |
| 35 |
repo: https://github.com/kubernetes-sigs/kustomize |
| 36 |
`, |
| 37 |
}, |
| 38 |
} |
| 39 |
for _, test := range tests { |
| 40 |
actual, err := test.in.Append(test.path).String() |
| 41 |
require.NoError(t, err) |
| 42 |
assert.Equal(t, test.expected, actual) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestOriginString(t *testing.T) { |
| 47 |
tests := []struct { |
| 48 |
in *Origin |
| 49 |
expected string |
| 50 |
}{ |
| 51 |
{ |
| 52 |
in: &Origin{ |
| 53 |
Path: "prod/service.yaml", |
| 54 |
Repo: "github.com/kubernetes-sigs/kustomize/examples/multibases/dev/", |
| 55 |
Ref: "v1.0.6", |
| 56 |
}, |
| 57 |
expected: `path: prod/service.yaml |
| 58 |
repo: github.com/kubernetes-sigs/kustomize/examples/multibases/dev/ |
| 59 |
ref: v1.0.6 |
| 60 |
`, |
| 61 |
}, |
| 62 |
{ |
| 63 |
in: &Origin{ |
| 64 |
Path: "prod/service.yaml", |
| 65 |
Repo: "github.com/kubernetes-sigs/kustomize/examples/multibases/dev/", |
| 66 |
}, |
| 67 |
expected: `path: prod/service.yaml |
| 68 |
repo: github.com/kubernetes-sigs/kustomize/examples/multibases/dev/ |
| 69 |
`, |
| 70 |
}, |
| 71 |
{ |
| 72 |
in: &Origin{ |
| 73 |
Path: "prod/service.yaml", |
| 74 |
}, |
| 75 |
expected: `path: prod/service.yaml |
| 76 |
`, |
| 77 |
}, |
| 78 |
} |
| 79 |
|
| 80 |
for _, test := range tests { |
| 81 |
actual, err := test.in.String() |
| 82 |
require.NoError(t, err) |
| 83 |
assert.Equal(t, test.expected, actual) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func TestTransformationsString(t *testing.T) { |
| 88 |
origin1 := &Origin{ |
| 89 |
Repo: "github.com/myrepo", |
| 90 |
Ref: "master", |
| 91 |
ConfiguredIn: "config.yaml", |
| 92 |
ConfiguredBy: kyaml.ResourceIdentifier{ |
| 93 |
TypeMeta: kyaml.TypeMeta{ |
| 94 |
APIVersion: "builtin", |
| 95 |
Kind: "Generator", |
| 96 |
}, |
| 97 |
NameMeta: kyaml.NameMeta{ |
| 98 |
Name: "my-name", |
| 99 |
Namespace: "my-namespace", |
| 100 |
}, |
| 101 |
}, |
| 102 |
} |
| 103 |
origin2 := &Origin{ |
| 104 |
ConfiguredIn: "../base/config.yaml", |
| 105 |
ConfiguredBy: kyaml.ResourceIdentifier{ |
| 106 |
TypeMeta: kyaml.TypeMeta{ |
| 107 |
APIVersion: "builtin", |
| 108 |
Kind: "Generator", |
| 109 |
}, |
| 110 |
NameMeta: kyaml.NameMeta{ |
| 111 |
Name: "my-name", |
| 112 |
Namespace: "my-namespace", |
| 113 |
}, |
| 114 |
}, |
| 115 |
} |
| 116 |
tests := []struct { |
| 117 |
in Transformations |
| 118 |
expected string |
| 119 |
}{ |
| 120 |
{ |
| 121 |
in: Transformations{origin1}, |
| 122 |
expected: `- repo: github.com/myrepo |
| 123 |
ref: master |
| 124 |
configuredIn: config.yaml |
| 125 |
configuredBy: |
| 126 |
apiVersion: builtin |
| 127 |
kind: Generator |
| 128 |
name: my-name |
| 129 |
namespace: my-namespace |
| 130 |
`, |
| 131 |
}, |
| 132 |
{ |
| 133 |
in: Transformations{origin1, origin2}, |
| 134 |
expected: `- repo: github.com/myrepo |
| 135 |
ref: master |
| 136 |
configuredIn: config.yaml |
| 137 |
configuredBy: |
| 138 |
apiVersion: builtin |
| 139 |
kind: Generator |
| 140 |
name: my-name |
| 141 |
namespace: my-namespace |
| 142 |
- configuredIn: ../base/config.yaml |
| 143 |
configuredBy: |
| 144 |
apiVersion: builtin |
| 145 |
kind: Generator |
| 146 |
name: my-name |
| 147 |
namespace: my-namespace |
| 148 |
`, |
| 149 |
}, |
| 150 |
{ |
| 151 |
in: Transformations{}, |
| 152 |
expected: `[] |
| 153 |
`, |
| 154 |
}, |
| 155 |
} |
| 156 |
for _, test := range tests { |
| 157 |
actual, err := test.in.String() |
| 158 |
require.NoError(t, err) |
| 159 |
assert.Equal(t, test.expected, actual) |
| 160 |
} |
| 161 |
} |