From ea2dff12e6df93ac27684707ea48f7781eb669b5 Mon Sep 17 00:00:00 2001 From: Philip Hamer <48808444+philipatl@users.noreply.github.com> Date: Tue, 11 Jan 2022 18:53:23 -0500 Subject: [PATCH] add test case for issue 106277 (PR 106268) (#106532) * add test case for issue 106277 - fix edit of non-registered custom API types * remove unneeded struct, as suggested --- .../pkg/cmd/util/editor/editoptions_test.go | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editoptions_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editoptions_test.go index 3142a8c7019..e4b9dd258db 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editoptions_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editoptions_test.go @@ -18,6 +18,7 @@ package editor import ( "reflect" + "strings" "testing" corev1 "k8s.io/api/core/v1" @@ -25,6 +26,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/resource" @@ -277,3 +279,196 @@ func TestManagedFieldsExtractAndRestore(t *testing.T) { }) } } + +type testVisitor struct { + updatedInfos []*resource.Info +} + +func (tv *testVisitor) Visit(f resource.VisitorFunc) error { + var err error + for _, ui := range tv.updatedInfos { + err = f(ui, err) + } + return err +} + +var unregMapping = &meta.RESTMapping{ + Resource: schema.GroupVersionResource{ + Group: "a", + Version: "b", + Resource: "c", + }, + GroupVersionKind: schema.GroupVersionKind{ + Group: "a", + Version: "b", + Kind: "d", + }, +} + +func TestEditOptions_visitToPatch(t *testing.T) { + + expectedErr := func(err error) bool { + return err != nil && strings.Contains(err.Error(), "At least one of apiVersion, kind and name was changed") + } + + type args struct { + originalInfos []*resource.Info + patchVisitor resource.Visitor + results *editResults + } + tests := []struct { + name string + args args + checkErr func(err error) bool + }{ + { + name: "name-diff", + args: args{ + originalInfos: []*resource.Info{ + { + Namespace: "ns", + Name: "before", + Object: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "Thingy", + "metadata": map[string]interface{}{ + "uid": "12345", + "namespace": "ns", + "name": "before", + }, + "spec": map[string]interface{}{}, + }, + }, + Mapping: unregMapping, + }, + }, + patchVisitor: &testVisitor{ + updatedInfos: []*resource.Info{ + { + Namespace: "ns", + Name: "after", + Object: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "Thingy", + "metadata": map[string]interface{}{ + "uid": "12345", + "namespace": "ns", + "name": "after", + }, + "spec": map[string]interface{}{}, + }, + }, + Mapping: unregMapping, + }, + }, + }, + results: &editResults{}, + }, + checkErr: expectedErr, + }, + { + name: "kind-diff", + args: args{ + originalInfos: []*resource.Info{ + { + Namespace: "ns", + Name: "myname", + Object: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "Thingy", + "metadata": map[string]interface{}{ + "uid": "12345", + "namespace": "ns", + "name": "myname", + }, + "spec": map[string]interface{}{}, + }, + }, + Mapping: unregMapping, + }, + }, + patchVisitor: &testVisitor{ + updatedInfos: []*resource.Info{ + { + Namespace: "ns", + Name: "myname", + Object: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "OtherThingy", + "metadata": map[string]interface{}{ + "uid": "12345", + "namespace": "ns", + "name": "myname", + }, + "spec": map[string]interface{}{}, + }, + }, + Mapping: unregMapping, + }, + }, + }, + results: &editResults{}, + }, + checkErr: expectedErr, + }, + { + name: "apiver-diff", + args: args{ + originalInfos: []*resource.Info{ + { + Namespace: "ns", + Name: "myname", + Object: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "Thingy", + "metadata": map[string]interface{}{ + "uid": "12345", + "namespace": "ns", + "name": "myname", + }, + "spec": map[string]interface{}{}, + }, + }, + Mapping: unregMapping, + }, + }, + patchVisitor: &testVisitor{ + updatedInfos: []*resource.Info{ + { + Namespace: "ns", + Name: "myname", + Object: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1alpha1", + "kind": "Thingy", + "metadata": map[string]interface{}{ + "uid": "12345", + "namespace": "ns", + "name": "myname", + }, + "spec": map[string]interface{}{}, + }, + }, + Mapping: unregMapping, + }, + }, + }, + results: &editResults{}, + }, + checkErr: expectedErr, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + o := &EditOptions{} + if err := o.visitToPatch(tt.args.originalInfos, tt.args.patchVisitor, tt.args.results); !tt.checkErr(err) { + t.Errorf("EditOptions.visitToPatch() %s error = %v", tt.name, err) + } + }) + } +}