mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-21 22:41:42 +00:00
175 lines
4.5 KiB
Go
175 lines
4.5 KiB
Go
/*
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package transformers
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/kubernetes-sigs/kustomize/pkg/resmap"
|
|
"github.com/kubernetes-sigs/kustomize/pkg/resource"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
)
|
|
|
|
func TestNameHashTransformer(t *testing.T) {
|
|
objs := resmap.ResMap{
|
|
resource.NewResId(cmap, "cm1"): resource.NewBehaviorlessResource(
|
|
&unstructured.Unstructured{
|
|
Object: map[string]interface{}{
|
|
"apiVersion": "v1",
|
|
"kind": "ConfigMap",
|
|
"metadata": map[string]interface{}{
|
|
"name": "cm1",
|
|
},
|
|
},
|
|
}),
|
|
resource.NewResId(deploy, "deploy1"): resource.NewBehaviorlessResource(
|
|
&unstructured.Unstructured{
|
|
Object: map[string]interface{}{
|
|
"group": "apps",
|
|
"apiVersion": "v1",
|
|
"kind": "Deployment",
|
|
"metadata": map[string]interface{}{
|
|
"name": "deploy1",
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"template": map[string]interface{}{
|
|
"metadata": map[string]interface{}{
|
|
"labels": map[string]interface{}{
|
|
"old-label": "old-value",
|
|
},
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"containers": []interface{}{
|
|
map[string]interface{}{
|
|
"name": "nginx",
|
|
"image": "nginx:1.7.9",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
resource.NewResId(service, "svc1"): resource.NewBehaviorlessResource(
|
|
&unstructured.Unstructured{
|
|
Object: map[string]interface{}{
|
|
"apiVersion": "v1",
|
|
"kind": "Service",
|
|
"metadata": map[string]interface{}{
|
|
"name": "svc1",
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"ports": []interface{}{
|
|
map[string]interface{}{
|
|
"name": "port1",
|
|
"port": "12345",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
resource.NewResId(secret, "secret1"): resource.NewBehaviorlessResource(
|
|
&unstructured.Unstructured{
|
|
Object: map[string]interface{}{
|
|
"apiVersion": "v1",
|
|
"kind": "Secret",
|
|
"metadata": map[string]interface{}{
|
|
"name": "secret1",
|
|
},
|
|
},
|
|
}),
|
|
}
|
|
|
|
expected := resmap.ResMap{
|
|
resource.NewResId(cmap, "cm1"): resource.NewBehaviorlessResource(
|
|
&unstructured.Unstructured{
|
|
Object: map[string]interface{}{
|
|
"apiVersion": "v1",
|
|
"kind": "ConfigMap",
|
|
"metadata": map[string]interface{}{
|
|
"name": "cm1-m462kdfb68",
|
|
},
|
|
},
|
|
}),
|
|
resource.NewResId(deploy, "deploy1"): resource.NewBehaviorlessResource(
|
|
&unstructured.Unstructured{
|
|
Object: map[string]interface{}{
|
|
"group": "apps",
|
|
"apiVersion": "v1",
|
|
"kind": "Deployment",
|
|
"metadata": map[string]interface{}{
|
|
"name": "deploy1",
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"template": map[string]interface{}{
|
|
"metadata": map[string]interface{}{
|
|
"labels": map[string]interface{}{
|
|
"old-label": "old-value",
|
|
},
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"containers": []interface{}{
|
|
map[string]interface{}{
|
|
"name": "nginx",
|
|
"image": "nginx:1.7.9",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
resource.NewResId(service, "svc1"): resource.NewBehaviorlessResource(
|
|
&unstructured.Unstructured{
|
|
Object: map[string]interface{}{
|
|
"apiVersion": "v1",
|
|
"kind": "Service",
|
|
"metadata": map[string]interface{}{
|
|
"name": "svc1",
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"ports": []interface{}{
|
|
map[string]interface{}{
|
|
"name": "port1",
|
|
"port": "12345",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
resource.NewResId(secret, "secret1"): resource.NewBehaviorlessResource(
|
|
&unstructured.Unstructured{
|
|
Object: map[string]interface{}{
|
|
"apiVersion": "v1",
|
|
"kind": "Secret",
|
|
"metadata": map[string]interface{}{
|
|
"name": "secret1-7kc45hd5f7",
|
|
},
|
|
},
|
|
}),
|
|
}
|
|
|
|
tran := NewNameHashTransformer()
|
|
tran.Transform(objs)
|
|
|
|
if !reflect.DeepEqual(objs, expected) {
|
|
err := expected.ErrorIfNotEqual(objs)
|
|
t.Fatalf("actual doesn't match expected: %v", err)
|
|
}
|
|
}
|