From eec253d14c5086d22f19d088a26125d6e967ac09 Mon Sep 17 00:00:00 2001 From: ymqytw Date: Tue, 23 Jan 2018 12:55:52 -0800 Subject: [PATCH] fix golint, refactor test --- pkg/kinflate/util/gvkn_sort.go | 6 +- pkg/kinflate/util/labelsandannotations.go | 44 +-- .../util/labelsandannotations_test.go | 346 +++++++++--------- .../util/labelsandannotationsconfig.go | 4 + pkg/kinflate/util/prefixname.go | 16 +- pkg/kinflate/util/prefixname_test.go | 8 +- pkg/kinflate/util/util.go | 59 ++- pkg/kinflate/util/util_test.go | 37 +- 8 files changed, 267 insertions(+), 253 deletions(-) diff --git a/pkg/kinflate/util/gvkn_sort.go b/pkg/kinflate/util/gvkn_sort.go index 5e6bb374b..b65613974 100644 --- a/pkg/kinflate/util/gvkn_sort.go +++ b/pkg/kinflate/util/gvkn_sort.go @@ -21,8 +21,8 @@ type ByGVKN []GroupVersionKindName func (a ByGVKN) Len() int { return len(a) } func (a ByGVKN) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByGVKN) Less(i, j int) bool { - if a[i].gvk.String() != a[j].gvk.String() { - return a[i].gvk.String() < a[j].gvk.String() + if a[i].GVK.String() != a[j].GVK.String() { + return a[i].GVK.String() < a[j].GVK.String() } - return a[i].name < a[j].name + return a[i].Name < a[j].Name } diff --git a/pkg/kinflate/util/labelsandannotations.go b/pkg/kinflate/util/labelsandannotations.go index 581a127d1..3f8768565 100644 --- a/pkg/kinflate/util/labelsandannotations.go +++ b/pkg/kinflate/util/labelsandannotations.go @@ -22,35 +22,41 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) -type ApplyAdditionalMapOptions struct { - additionalMap map[string]string - pathConfigs []PathConfig +// MapTransformationOptions contains a map string->string and path configs +// The map will be applied to the fields specified in path configs. +type MapTransformationOptions struct { + m map[string]string + pathConfigs []PathConfig } -var _ Transformer = &ApplyAdditionalMapOptions{} +var _ Transformer = &MapTransformationOptions{} -func (o *ApplyAdditionalMapOptions) CompleteForLabels(m map[string]string, pathConfigs []PathConfig) { - o.additionalMap = m +// CompleteForLabels fills up the MapTransformationOptions for labels transformation. +func (o *MapTransformationOptions) CompleteForLabels(m map[string]string, pathConfigs []PathConfig) { + o.m = m if pathConfigs == nil { pathConfigs = DefaultLabelsPathConfigs } o.pathConfigs = pathConfigs } -func (o *ApplyAdditionalMapOptions) CompleteForAnnotations(m map[string]string, pathConfigs []PathConfig) { - o.additionalMap = m +// CompleteForAnnotations fills up the MapTransformationOptions for annotations transformation. +func (o *MapTransformationOptions) CompleteForAnnotations(m map[string]string, pathConfigs []PathConfig) { + o.m = m if pathConfigs == nil { pathConfigs = DefaultAnnotationsPathConfigs } o.pathConfigs = pathConfigs } -func (o *ApplyAdditionalMapOptions) Transform(m map[GroupVersionKindName]*unstructured.Unstructured) error { +// Transform apply each pair in the MapTransformationOptions to the +// fields specified in MapTransformationOptions. +func (o *MapTransformationOptions) Transform(m map[GroupVersionKindName]*unstructured.Unstructured) error { for gvkn := range m { obj := m[gvkn] objMap := obj.UnstructuredContent() for _, path := range o.pathConfigs { - if !SelectByGVK(gvkn.gvk, path.GroupVersionKind) { + if !SelectByGVK(gvkn.GVK, path.GroupVersionKind) { continue } err := mutateField(objMap, path.Path, path.CreateIfNotPresent, o.addMap) @@ -62,26 +68,12 @@ func (o *ApplyAdditionalMapOptions) Transform(m map[GroupVersionKindName]*unstru return nil } -func (o *ApplyAdditionalMapOptions) TransformBytes(in []byte) ([]byte, error) { - m, err := Decode(in) - if err != nil { - return nil, err - } - - err = o.Transform(m) - if err != nil { - return nil, err - } - - return Encode(m) -} - -func (o *ApplyAdditionalMapOptions) addMap(in interface{}) (interface{}, error) { +func (o *MapTransformationOptions) addMap(in interface{}) (interface{}, error) { m, ok := in.(map[string]interface{}) if !ok { return nil, fmt.Errorf("%#v is expectd to be %T", in, m) } - for k, v := range o.additionalMap { + for k, v := range o.m { m[k] = v } return m, nil diff --git a/pkg/kinflate/util/labelsandannotations_test.go b/pkg/kinflate/util/labelsandannotations_test.go index a060b3583..13bb71fd5 100644 --- a/pkg/kinflate/util/labelsandannotations_test.go +++ b/pkg/kinflate/util/labelsandannotations_test.go @@ -24,17 +24,17 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" ) -var labelsOps = ApplyAdditionalMapOptions{ - additionalMap: map[string]string{"label-key1": "label-value1", "label-key2": "label-value2"}, - pathConfigs: DefaultLabelsPathConfigs, +var labelsOps = MapTransformationOptions{ + m: map[string]string{"label-key1": "label-value1", "label-key2": "label-value2"}, + pathConfigs: DefaultLabelsPathConfigs, } -var annotationsOps = ApplyAdditionalMapOptions{ - additionalMap: map[string]string{"anno-key1": "anno-value1", "anno-key2": "anno-value2"}, - pathConfigs: DefaultAnnotationsPathConfigs, +var annotationsOps = MapTransformationOptions{ + m: map[string]string{"anno-key1": "anno-value1", "anno-key2": "anno-value2"}, + pathConfigs: DefaultAnnotationsPathConfigs, } -func getConfigmap() *unstructured.Unstructured { +func makeConfigmap() *unstructured.Unstructured { return &unstructured.Unstructured{ Object: map[string]interface{}{ "apiVersion": "v1", @@ -46,7 +46,7 @@ func getConfigmap() *unstructured.Unstructured { } } -func getDeployment() *unstructured.Unstructured { +func makeDeployment() *unstructured.Unstructured { return &unstructured.Unstructured{ Object: map[string]interface{}{ "group": "apps", @@ -76,7 +76,7 @@ func getDeployment() *unstructured.Unstructured { } } -func getService() *unstructured.Unstructured { +func makeService() *unstructured.Unstructured { return &unstructured.Unstructured{ Object: map[string]interface{}{ "apiVersion": "v1", @@ -96,225 +96,243 @@ func getService() *unstructured.Unstructured { } } -func getTestMap() map[GroupVersionKindName]*unstructured.Unstructured { +func makeTestMap() map[GroupVersionKindName]*unstructured.Unstructured { return map[GroupVersionKindName]*unstructured.Unstructured{ { - gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, - name: "cm1", - }: getConfigmap(), + GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, + Name: "cm1", + }: makeConfigmap(), { - gvk: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, - name: "deploy1", - }: getDeployment(), + GVK: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, + Name: "deploy1", + }: makeDeployment(), { - gvk: schema.GroupVersionKind{Version: "v1", Kind: "Service"}, - name: "svc1", - }: getService(), + GVK: schema.GroupVersionKind{Version: "v1", Kind: "Service"}, + Name: "svc1", + }: makeService(), } } -var labeledObj1 = unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "v1", - "kind": "ConfigMap", - "metadata": map[string]interface{}{ - "name": "cm1", - "labels": map[string]interface{}{ - "label-key1": "label-value1", - "label-key2": "label-value2", - }, - }, - }, -} - -var labeledObj2 = unstructured.Unstructured{ - Object: map[string]interface{}{ - "group": "apps", - "apiVersion": "v1", - "kind": "Deployment", - "metadata": map[string]interface{}{ - "name": "deploy1", - "labels": map[string]interface{}{ - "label-key1": "label-value1", - "label-key2": "label-value2", - }, - }, - "spec": map[string]interface{}{ - "selector": map[string]interface{}{ - "matchLabels": map[string]interface{}{ +func makeLabeledConfigMap() *unstructured.Unstructured { + return &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "cm1", + "labels": map[string]interface{}{ "label-key1": "label-value1", "label-key2": "label-value2", }, }, - "template": map[string]interface{}{ - "metadata": map[string]interface{}{ - "labels": map[string]interface{}{ - "old-label": "old-value", + }, + } +} + +func makeLabeledDeployment() *unstructured.Unstructured { + return &unstructured.Unstructured{ + Object: map[string]interface{}{ + "group": "apps", + "apiVersion": "v1", + "kind": "Deployment", + "metadata": map[string]interface{}{ + "name": "deploy1", + "labels": map[string]interface{}{ + "label-key1": "label-value1", + "label-key2": "label-value2", + }, + }, + "spec": map[string]interface{}{ + "selector": map[string]interface{}{ + "matchLabels": map[string]interface{}{ "label-key1": "label-value1", "label-key2": "label-value2", }, }, - "spec": map[string]interface{}{ - "containers": []interface{}{ - map[string]interface{}{ - "name": "nginx", - "image": "nginx:1.7.9", + "template": map[string]interface{}{ + "metadata": map[string]interface{}{ + "labels": map[string]interface{}{ + "old-label": "old-value", + "label-key1": "label-value1", + "label-key2": "label-value2", + }, + }, + "spec": map[string]interface{}{ + "containers": []interface{}{ + map[string]interface{}{ + "name": "nginx", + "image": "nginx:1.7.9", + }, }, }, }, }, }, - }, + } } -var labeledObj3 = unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "v1", - "kind": "Service", - "metadata": map[string]interface{}{ - "name": "svc1", - "labels": map[string]interface{}{ - "label-key1": "label-value1", - "label-key2": "label-value2", - }, - }, - "spec": map[string]interface{}{ - "ports": []interface{}{ - map[string]interface{}{ - "name": "port1", - "port": "12345", +func makeLabeledService() *unstructured.Unstructured { + return &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "Service", + "metadata": map[string]interface{}{ + "name": "svc1", + "labels": map[string]interface{}{ + "label-key1": "label-value1", + "label-key2": "label-value2", }, }, - "selector": map[string]interface{}{ - "label-key1": "label-value1", - "label-key2": "label-value2", + "spec": map[string]interface{}{ + "ports": []interface{}{ + map[string]interface{}{ + "name": "port1", + "port": "12345", + }, + }, + "selector": map[string]interface{}{ + "label-key1": "label-value1", + "label-key2": "label-value2", + }, }, }, - }, + } } -var labeledM = map[GroupVersionKindName]*unstructured.Unstructured{ - { - gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, - name: "cm1", - }: &labeledObj1, - { - gvk: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, - name: "deploy1", - }: &labeledObj2, - { - gvk: schema.GroupVersionKind{Version: "v1", Kind: "Service"}, - name: "svc1", - }: &labeledObj3, +func makeLabeledMap() map[GroupVersionKindName]*unstructured.Unstructured { + return map[GroupVersionKindName]*unstructured.Unstructured{ + { + GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, + Name: "cm1", + }: makeLabeledConfigMap(), + { + GVK: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, + Name: "deploy1", + }: makeLabeledDeployment(), + { + GVK: schema.GroupVersionKind{Version: "v1", Kind: "Service"}, + Name: "svc1", + }: makeLabeledService(), + } } func TestLabelsRun(t *testing.T) { - m := getTestMap() + m := makeTestMap() err := labelsOps.Transform(m) if err != nil { t.Fatalf("unexpected error: %v", err) } - if !reflect.DeepEqual(m, labeledM) { - err = CompareMap(m, labeledM) + expected := makeLabeledMap() + if !reflect.DeepEqual(m, expected) { + err = compareMap(m, expected) t.Fatalf("actual doesn't match expected: %v", err) } } -var annotatedObj1 = unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "v1", - "kind": "ConfigMap", - "metadata": map[string]interface{}{ - "name": "cm1", - "annotations": map[string]interface{}{ - "anno-key1": "anno-value1", - "anno-key2": "anno-value2", +func makeAnnotatededConfigMap() *unstructured.Unstructured { + return &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "cm1", + "annotations": map[string]interface{}{ + "anno-key1": "anno-value1", + "anno-key2": "anno-value2", + }, }, }, - }, + } } -var annotatedObj2 = unstructured.Unstructured{ - Object: map[string]interface{}{ - "group": "apps", - "apiVersion": "v1", - "kind": "Deployment", - "metadata": map[string]interface{}{ - "name": "deploy1", - "annotations": map[string]interface{}{ - "anno-key1": "anno-value1", - "anno-key2": "anno-value2", - }, - }, - "spec": map[string]interface{}{ - "template": map[string]interface{}{ - "metadata": map[string]interface{}{ - "annotations": map[string]interface{}{ - "anno-key1": "anno-value1", - "anno-key2": "anno-value2", - }, - "labels": map[string]interface{}{ - "old-label": "old-value", - }, +func makeAnnotatededDeployment() *unstructured.Unstructured { + return &unstructured.Unstructured{ + Object: map[string]interface{}{ + "group": "apps", + "apiVersion": "v1", + "kind": "Deployment", + "metadata": map[string]interface{}{ + "name": "deploy1", + "annotations": map[string]interface{}{ + "anno-key1": "anno-value1", + "anno-key2": "anno-value2", }, - "spec": map[string]interface{}{ - "containers": []interface{}{ - map[string]interface{}{ - "name": "nginx", - "image": "nginx:1.7.9", + }, + "spec": map[string]interface{}{ + "template": map[string]interface{}{ + "metadata": map[string]interface{}{ + "annotations": map[string]interface{}{ + "anno-key1": "anno-value1", + "anno-key2": "anno-value2", + }, + "labels": map[string]interface{}{ + "old-label": "old-value", + }, + }, + "spec": map[string]interface{}{ + "containers": []interface{}{ + map[string]interface{}{ + "name": "nginx", + "image": "nginx:1.7.9", + }, }, }, }, }, }, - }, + } } -var annotatedObj3 = unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "v1", - "kind": "Service", - "metadata": map[string]interface{}{ - "name": "svc1", - "annotations": map[string]interface{}{ - "anno-key1": "anno-value1", - "anno-key2": "anno-value2", +func makeAnnotatededService() *unstructured.Unstructured { + return &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "Service", + "metadata": map[string]interface{}{ + "name": "svc1", + "annotations": map[string]interface{}{ + "anno-key1": "anno-value1", + "anno-key2": "anno-value2", + }, }, - }, - "spec": map[string]interface{}{ - "ports": []interface{}{ - map[string]interface{}{ - "name": "port1", - "port": "12345", + "spec": map[string]interface{}{ + "ports": []interface{}{ + map[string]interface{}{ + "name": "port1", + "port": "12345", + }, }, }, }, - }, + } } -var annotatedM = map[GroupVersionKindName]*unstructured.Unstructured{ - { - gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, - name: "cm1", - }: &annotatedObj1, - { - gvk: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, - name: "deploy1", - }: &annotatedObj2, - { - gvk: schema.GroupVersionKind{Version: "v1", Kind: "Service"}, - name: "svc1", - }: &annotatedObj3, +func makeAnnotatedMap() map[GroupVersionKindName]*unstructured.Unstructured { + return map[GroupVersionKindName]*unstructured.Unstructured{ + { + GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, + Name: "cm1", + }: makeAnnotatededConfigMap(), + { + GVK: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, + Name: "deploy1", + }: makeAnnotatededDeployment(), + { + GVK: schema.GroupVersionKind{Version: "v1", Kind: "Service"}, + Name: "svc1", + }: makeAnnotatededService(), + } } func TestAnnotationsRun(t *testing.T) { - m := getTestMap() + m := makeTestMap() err := annotationsOps.Transform(m) if err != nil { t.Fatalf("unexpected error: %v", err) } - if !reflect.DeepEqual(m, annotatedM) { - err = CompareMap(m, annotatedM) + expected := makeAnnotatedMap() + if !reflect.DeepEqual(m, expected) { + err = compareMap(m, expected) t.Fatalf("actual doesn't match expected: %v", err) } } diff --git a/pkg/kinflate/util/labelsandannotationsconfig.go b/pkg/kinflate/util/labelsandannotationsconfig.go index 0861cc2b2..79aa621f3 100644 --- a/pkg/kinflate/util/labelsandannotationsconfig.go +++ b/pkg/kinflate/util/labelsandannotationsconfig.go @@ -20,6 +20,8 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" ) +// DefaultLabelsPathConfigs is the default configuration for mutating labels and +// selector fields for native k8s APIs. var DefaultLabelsPathConfigs = []PathConfig{ { Path: []string{"metadata", "labels"}, @@ -107,6 +109,8 @@ var DefaultLabelsPathConfigs = []PathConfig{ }, } +// DefaultLabelsPathConfigs is the default configuration for mutating annotations +// fields for native k8s APIs. var DefaultAnnotationsPathConfigs = []PathConfig{ { Path: []string{"metadata", "annotations"}, diff --git a/pkg/kinflate/util/prefixname.go b/pkg/kinflate/util/prefixname.go index 16ef2f80a..2fd62ae0d 100644 --- a/pkg/kinflate/util/prefixname.go +++ b/pkg/kinflate/util/prefixname.go @@ -51,7 +51,7 @@ func (o *PrefixNameOptions) Transform(m map[GroupVersionKindName]*unstructured.U obj := m[gvkn] objMap := obj.UnstructuredContent() for _, path := range o.pathConfigs { - if !SelectByGVK(gvkn.gvk, path.GroupVersionKind) { + if !SelectByGVK(gvkn.GVK, path.GroupVersionKind) { continue } err := mutateField(objMap, path.Path, path.CreateIfNotPresent, o.addPrefix) @@ -63,20 +63,6 @@ func (o *PrefixNameOptions) Transform(m map[GroupVersionKindName]*unstructured.U return nil } -func (o *PrefixNameOptions) TransformBytes(in []byte) ([]byte, error) { - m, err := Decode(in) - if err != nil { - return nil, err - } - - err = o.Transform(m) - if err != nil { - return nil, err - } - - return Encode(m) -} - func (o *PrefixNameOptions) addPrefix(in interface{}) (interface{}, error) { s, ok := in.(string) if !ok { diff --git a/pkg/kinflate/util/prefixname_test.go b/pkg/kinflate/util/prefixname_test.go index aee95618a..1a63a5edd 100644 --- a/pkg/kinflate/util/prefixname_test.go +++ b/pkg/kinflate/util/prefixname_test.go @@ -51,12 +51,12 @@ var namePrefixedCm2 = unstructured.Unstructured{ var namePrefixedM = map[GroupVersionKindName]*unstructured.Unstructured{ { - gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, - name: "cm1", + GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, + Name: "cm1", }: &namePrefixedCm1, { - gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, - name: "cm2", + GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, + Name: "cm2", }: &namePrefixedCm2, } diff --git a/pkg/kinflate/util/util.go b/pkg/kinflate/util/util.go index cc4075d5a..f3cc7ebae 100644 --- a/pkg/kinflate/util/util.go +++ b/pkg/kinflate/util/util.go @@ -20,7 +20,6 @@ import ( "bytes" "fmt" "io" - "reflect" "sort" "github.com/ghodss/yaml" @@ -31,13 +30,18 @@ import ( k8syaml "k8s.io/apimachinery/pkg/util/yaml" ) +// GroupVersionKindName contains GroupVersionKind and original name of the resource. type GroupVersionKindName struct { - gvk schema.GroupVersionKind - // name of the resource. - name string + // GroupVersionKind of the resource. + GVK schema.GroupVersionKind + // original name of the resource before transformation. + Name string } -func Decode(in []byte) (map[GroupVersionKindName]*unstructured.Unstructured, error) { +// Decode decodes a list of objects in byte array format. +// Decoded object will be inserted in `into` if it's not nil. Otherwise, it will +// construct a new map and return it. +func Decode(in []byte, into map[GroupVersionKindName]*unstructured.Unstructured) (map[GroupVersionKindName]*unstructured.Unstructured, error) { decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewReader(in), 1024) objs := []*unstructured.Unstructured{} @@ -54,7 +58,9 @@ func Decode(in []byte) (map[GroupVersionKindName]*unstructured.Unstructured, err return nil, err } - m := map[GroupVersionKindName]*unstructured.Unstructured{} + if into == nil { + into = map[GroupVersionKindName]*unstructured.Unstructured{} + } for i := range objs { metaAccessor, err := meta.Accessor(objs[i]) if err != nil { @@ -73,14 +79,15 @@ func Decode(in []byte) (map[GroupVersionKindName]*unstructured.Unstructured, err } gvk := gv.WithKind(kind) gvkn := GroupVersionKindName{ - gvk: gvk, - name: name, + GVK: gvk, + Name: name, } - m[gvkn] = objs[i] + into[gvkn] = objs[i] } - return m, nil + return into, nil } +// Encode encodes the map `in` and output the encoded objects separated by `---`. func Encode(in map[GroupVersionKindName]*unstructured.Unstructured) ([]byte, error) { gvknList := []GroupVersionKindName{} for gvkn := range in { @@ -112,7 +119,13 @@ func Encode(in map[GroupVersionKindName]*unstructured.Unstructured) ([]byte, err return buf.Bytes(), nil } -// SelectByGVK returns true if selector is a superset of in; otherwise, false. +// SelectByGVK returns true if `selector` selects `in`; otherwise, false. +// If `selector` and `in` are the same, return true. +// If `selector` is nil, it is considered as a wildcard and always return true. +// e.g. selector CAN select +// . +// selector CANNOT select +// . func SelectByGVK(in schema.GroupVersionKind, selector *schema.GroupVersionKind) bool { if selector == nil { return true @@ -135,30 +148,6 @@ func SelectByGVK(in schema.GroupVersionKind, selector *schema.GroupVersionKind) return true } -func CompareMap(m1, m2 map[GroupVersionKindName]*unstructured.Unstructured) error { - if len(m1) != len(m2) { - keySet1 := []GroupVersionKindName{} - keySet2 := []GroupVersionKindName{} - for gvkn := range m1 { - keySet1 = append(keySet1, gvkn) - } - for gvkn := range m1 { - keySet2 = append(keySet2, gvkn) - } - return fmt.Errorf("maps has different number of entries: %#v doesn't equals %#v", keySet1, keySet2) - } - for gvkn, obj1 := range m1 { - obj2, found := m2[gvkn] - if !found { - return fmt.Errorf("%#v doesn't exist in %#v", gvkn, m2) - } - if !reflect.DeepEqual(obj1, obj2) { - return fmt.Errorf("%#v doesn't match %#v", obj1, obj2) - } - } - return nil -} - type mutateFunc func(interface{}) (interface{}, error) func mutateField(m map[string]interface{}, pathToField []string, createIfNotPresent bool, fns ...mutateFunc) error { diff --git a/pkg/kinflate/util/util_test.go b/pkg/kinflate/util/util_test.go index 25539473e..499dd55e4 100644 --- a/pkg/kinflate/util/util_test.go +++ b/pkg/kinflate/util/util_test.go @@ -17,6 +17,7 @@ limitations under the License. package util import ( + "fmt" "reflect" "testing" @@ -57,19 +58,19 @@ func createMap() map[GroupVersionKindName]*unstructured.Unstructured { } return map[GroupVersionKindName]*unstructured.Unstructured{ { - gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, - name: "cm1", + GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, + Name: "cm1", }: &cm1, { - gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, - name: "cm2", + GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, + Name: "cm2", }: &cm2, } } func TestDecode(t *testing.T) { expected := createMap() - m, err := Decode(encoded) + m, err := Decode(encoded, nil) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -103,7 +104,7 @@ func TestFilterByGVK(t *testing.T) { expected: true, }, { - description: "gvk matches", + description: "GVK matches", in: schema.GroupVersionKind{ Group: "group1", Version: "version1", @@ -195,3 +196,27 @@ func TestFilterByGVK(t *testing.T) { } } } + +func compareMap(m1, m2 map[GroupVersionKindName]*unstructured.Unstructured) error { + if len(m1) != len(m2) { + keySet1 := []GroupVersionKindName{} + keySet2 := []GroupVersionKindName{} + for GVKn := range m1 { + keySet1 = append(keySet1, GVKn) + } + for GVKn := range m1 { + keySet2 = append(keySet2, GVKn) + } + return fmt.Errorf("maps has different number of entries: %#v doesn't equals %#v", keySet1, keySet2) + } + for GVKn, obj1 := range m1 { + obj2, found := m2[GVKn] + if !found { + return fmt.Errorf("%#v doesn't exist in %#v", GVKn, m2) + } + if !reflect.DeepEqual(obj1, obj2) { + return fmt.Errorf("%#v doesn't match %#v", obj1, obj2) + } + } + return nil +}