github.com/banzaicloud/operator-tools@v0.28.10/pkg/inventory/inventory_test.go (about) 1 // Copyright © 2020 Banzai Cloud 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package inventory 16 17 import ( 18 "reflect" 19 "testing" 20 21 appsv1 "k8s.io/api/apps/v1" 22 corev1 "k8s.io/api/core/v1" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apimachinery/pkg/runtime/schema" 27 "k8s.io/utils/diff" 28 29 "github.com/banzaicloud/operator-tools/pkg/utils" 30 ) 31 32 func TestCreateObjectsInventory(t *testing.T) { 33 objs := []runtime.Object{ 34 &corev1.Service{ 35 ObjectMeta: metav1.ObjectMeta{ 36 Namespace: "test-ns", 37 Name: "test-svc", 38 }, 39 TypeMeta: metav1.TypeMeta{ 40 Kind: "Service", 41 APIVersion: "v1", 42 }, 43 }, 44 &appsv1.Deployment{ 45 ObjectMeta: metav1.ObjectMeta{ 46 Namespace: "test-ns", 47 Name: "test-deployment", 48 }, 49 TypeMeta: metav1.TypeMeta{ 50 Kind: "Deployment", 51 APIVersion: "apps/v1", 52 }, 53 }, 54 } 55 56 cm, _ := CreateObjectsInventory("test-ns", "test-inv", objs) 57 58 expectedConfigMap := &corev1.ConfigMap{ 59 TypeMeta: metav1.TypeMeta{ 60 Kind: "ConfigMap", 61 APIVersion: "v1", 62 }, 63 ObjectMeta: metav1.ObjectMeta{ 64 Namespace: "test-ns", 65 Name: "test-inv", 66 }, 67 Immutable: utils.BoolPointer(false), 68 Data: map[string]string{ 69 referencesKey: "/v1/Service/test-ns/test-svc,apps/v1/Deployment/test-ns/test-deployment", 70 }, 71 } 72 73 if !reflect.DeepEqual(expectedConfigMap, cm) { 74 t.Error(diff.ObjectDiff(expectedConfigMap, cm)) 75 } 76 } 77 78 func TestGetObjectsFromInventory(t *testing.T) { 79 inventory := corev1.ConfigMap{ 80 TypeMeta: metav1.TypeMeta{ 81 Kind: "ConfigMap", 82 APIVersion: "v1", 83 }, 84 ObjectMeta: metav1.ObjectMeta{ 85 Namespace: "test-ns", 86 Name: "test-inv", 87 }, 88 Immutable: utils.BoolPointer(false), 89 Data: map[string]string{ 90 referencesKey: "/v1/Service/test-ns/test-svc,apps/v1/Deployment/test-ns/test-deployment", 91 }, 92 } 93 94 objects := GetObjectsFromInventory(inventory) 95 96 expectedSvcObj := &unstructured.Unstructured{} 97 expectedSvcObj.SetNamespace("test-ns") 98 expectedSvcObj.SetName("test-svc") 99 expectedSvcObj.SetGroupVersionKind(schema.GroupVersionKind{ 100 Group: "", 101 Version: "v1", 102 Kind: "Service", 103 }) 104 105 expectedDeplObj := &unstructured.Unstructured{} 106 expectedDeplObj.SetNamespace("test-ns") 107 expectedDeplObj.SetName("test-deployment") 108 expectedDeplObj.SetGroupVersionKind(schema.GroupVersionKind{ 109 Group: "apps", 110 Version: "v1", 111 Kind: "Deployment", 112 }) 113 114 expectedObjects := []runtime.Object{ 115 expectedSvcObj, 116 expectedDeplObj, 117 } 118 119 if !reflect.DeepEqual(expectedObjects, objects) { 120 t.Error(diff.ObjectDiff(expectedObjects, objects)) 121 } 122 }