github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/mapper_test.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package runtime 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 24 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/diff" 25 ) 26 27 func TestResourceMapper(t *testing.T) { 28 gvr := func(g, v, r string) schema.GroupVersionResource { return schema.GroupVersionResource{g, v, r} } 29 30 gvk := func(g, v, k string) schema.GroupVersionKind { return schema.GroupVersionKind{g, v, k} } 31 32 kindsToRegister := []struct { 33 gvr schema.GroupVersionResource 34 subresource string 35 gvk schema.GroupVersionKind 36 }{ 37 // pods 38 {gvr("", "v1", "pods"), "", gvk("", "v1", "Pod")}, 39 // pods/status 40 {gvr("", "v1", "pods"), "status", gvk("", "v1", "Pod")}, 41 // deployments 42 {gvr("apps", "v1", "deployments"), "", gvk("apps", "v1", "Deployment")}, 43 {gvr("apps", "v1beta1", "deployments"), "", gvk("apps", "v1beta1", "Deployment")}, 44 {gvr("apps", "v1alpha1", "deployments"), "", gvk("apps", "v1alpha1", "Deployment")}, 45 {gvr("extensions", "v1beta1", "deployments"), "", gvk("extensions", "v1beta1", "Deployment")}, 46 // deployments/scale (omitted for apps/v1alpha1) 47 {gvr("apps", "v1", "deployments"), "scale", gvk("", "", "Scale")}, 48 {gvr("apps", "v1beta1", "deployments"), "scale", gvk("", "", "Scale")}, 49 {gvr("extensions", "v1beta1", "deployments"), "scale", gvk("", "", "Scale")}, 50 // deployments/status (omitted for apps/v1alpha1) 51 {gvr("apps", "v1", "deployments"), "status", gvk("apps", "v1", "Deployment")}, 52 {gvr("apps", "v1beta1", "deployments"), "status", gvk("apps", "v1beta1", "Deployment")}, 53 {gvr("extensions", "v1beta1", "deployments"), "status", gvk("extensions", "v1beta1", "Deployment")}, 54 } 55 56 testcases := []struct { 57 Name string 58 IdentityFunc func(schema.GroupResource) string 59 ResourcesForV1Deployment []schema.GroupVersionResource 60 ResourcesForV1DeploymentScale []schema.GroupVersionResource 61 ResourcesForV1DeploymentStatus []schema.GroupVersionResource 62 }{ 63 { 64 Name: "no identityfunc", 65 ResourcesForV1Deployment: []schema.GroupVersionResource{gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments"), gvr("apps", "v1alpha1", "deployments")}, 66 ResourcesForV1DeploymentScale: []schema.GroupVersionResource{gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments")}, 67 ResourcesForV1DeploymentStatus: []schema.GroupVersionResource{gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments")}, 68 }, 69 { 70 Name: "empty identityfunc", 71 IdentityFunc: func(schema.GroupResource) string { return "" }, 72 // same group 73 ResourcesForV1Deployment: []schema.GroupVersionResource{gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments"), gvr("apps", "v1alpha1", "deployments")}, 74 ResourcesForV1DeploymentScale: []schema.GroupVersionResource{gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments")}, 75 ResourcesForV1DeploymentStatus: []schema.GroupVersionResource{gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments")}, 76 }, 77 { 78 Name: "common identityfunc", 79 IdentityFunc: func(schema.GroupResource) string { return "x" }, 80 // all resources are seen as equivalent 81 ResourcesForV1Deployment: []schema.GroupVersionResource{gvr("", "v1", "pods"), gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments"), gvr("apps", "v1alpha1", "deployments"), gvr("extensions", "v1beta1", "deployments")}, 82 // all resources with scale are seen as equivalent 83 ResourcesForV1DeploymentScale: []schema.GroupVersionResource{gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments"), gvr("extensions", "v1beta1", "deployments")}, 84 // all resources with status are seen as equivalent 85 ResourcesForV1DeploymentStatus: []schema.GroupVersionResource{gvr("", "v1", "pods"), gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments"), gvr("extensions", "v1beta1", "deployments")}, 86 }, 87 { 88 Name: "colocated deployments", 89 IdentityFunc: func(resource schema.GroupResource) string { 90 if resource.Resource == "deployments" { 91 return "deployments" 92 } 93 return "" 94 }, 95 // all deployments are seen as equivalent 96 ResourcesForV1Deployment: []schema.GroupVersionResource{gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments"), gvr("apps", "v1alpha1", "deployments"), gvr("extensions", "v1beta1", "deployments")}, 97 // all deployments with scale are seen as equivalent 98 ResourcesForV1DeploymentScale: []schema.GroupVersionResource{gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments"), gvr("extensions", "v1beta1", "deployments")}, 99 // all deployments with status are seen as equivalent 100 ResourcesForV1DeploymentStatus: []schema.GroupVersionResource{gvr("apps", "v1", "deployments"), gvr("apps", "v1beta1", "deployments"), gvr("extensions", "v1beta1", "deployments")}, 101 }, 102 } 103 104 for _, tc := range testcases { 105 t.Run(tc.Name, func(t *testing.T) { 106 mapper := NewEquivalentResourceRegistryWithIdentity(tc.IdentityFunc) 107 108 // register 109 for _, data := range kindsToRegister { 110 mapper.RegisterKindFor(data.gvr, data.subresource, data.gvk) 111 } 112 // verify 113 for _, data := range kindsToRegister { 114 if kind := mapper.KindFor(data.gvr, data.subresource); kind != data.gvk { 115 t.Errorf("KindFor(%#v, %v) returned %#v, expected %#v", data.gvr, data.subresource, kind, data.gvk) 116 } 117 } 118 119 // Verify equivalents to primary resource 120 if resources := mapper.EquivalentResourcesFor(gvr("apps", "v1", "deployments"), ""); !reflect.DeepEqual(resources, tc.ResourcesForV1Deployment) { 121 t.Errorf("diff:\n%s", diff.ObjectReflectDiff(tc.ResourcesForV1Deployment, resources)) 122 } 123 // Verify equivalents to subresources 124 if resources := mapper.EquivalentResourcesFor(gvr("apps", "v1", "deployments"), "scale"); !reflect.DeepEqual(resources, tc.ResourcesForV1DeploymentScale) { 125 t.Errorf("diff:\n%s", diff.ObjectReflectDiff(tc.ResourcesForV1DeploymentScale, resources)) 126 } 127 if resources := mapper.EquivalentResourcesFor(gvr("apps", "v1", "deployments"), "status"); !reflect.DeepEqual(resources, tc.ResourcesForV1DeploymentStatus) { 128 t.Errorf("diff:\n%s", diff.ObjectReflectDiff(tc.ResourcesForV1DeploymentStatus, resources)) 129 } 130 }) 131 } 132 }