github.com/splunk/dan1-qbec@v0.7.3/internal/objsort/sort_test.go (about) 1 /* 2 Copyright 2019 Splunk Inc. 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 objsort 18 19 import ( 20 "errors" 21 "fmt" 22 "testing" 23 24 "github.com/splunk/qbec/internal/model" 25 "github.com/stretchr/testify/assert" 26 "k8s.io/apimachinery/pkg/runtime/schema" 27 ) 28 29 type data struct { 30 component string 31 apiVersion string 32 kind string 33 name string 34 namespace string 35 } 36 37 func object(d data) model.K8sLocalObject { 38 return model.NewK8sLocalObject(map[string]interface{}{ 39 "apiVersion": d.apiVersion, 40 "kind": d.kind, 41 "metadata": map[string]interface{}{ 42 "namespace": d.namespace, 43 "name": d.name, 44 }, 45 }, "app1", "", d.component, "dev") 46 } 47 48 func TestBasicSort(t *testing.T) { 49 inputs := []model.K8sLocalObject{ 50 object(data{"c0", "v1", "BarBaz", "c1-secret", ""}), 51 object(data{"c1", "v1", "Secret", "c1-secret", ""}), 52 object(data{"c1", "v1", "Namespace", "c1-ns", ""}), 53 object(data{"c1", "v1", "ServiceAccount", "c1-sa", "c1-ns"}), 54 object(data{"cluster", "v1", "PodSecurityPolicy", "cluster-psp", ""}), 55 object(data{"c2", "extensions/v1beta1", "Deployment", "deploy1", "c1-ns"}), 56 object(data{"c2", "extensions/v1beta1", "FooBar", "fb1", "c1-ns"}), 57 object(data{"c3", "rbac.authorization.k8s.io/v1beta1", "RoleBinding", "rb1", "c1-ns"}), 58 object(data{"c3", "rbac.authorization.k8s.io/v1beta1", "ClusterRole", "cr", ""}), 59 } 60 sorted := Sort(inputs, Config{ 61 OrderingProvider: func(item model.K8sQbecMeta) int { 62 if item.GetName() == "c1-ns" { 63 return 1 64 } 65 return 0 66 }, 67 NamespacedIndicator: func(gvk schema.GroupVersionKind) (bool, error) { 68 if gvk.Kind == "PodSecurityPolicy" || gvk.Kind == "FooBar" || gvk.Kind == "ClusterRoleBinding" { 69 return false, nil 70 } 71 if gvk.Kind == "BarBaz" { 72 return false, errors.New("no indicator for BarBaz") 73 } 74 return true, nil 75 }, 76 }) 77 var results []string 78 for _, s := range sorted { 79 results = append(results, fmt.Sprintf("%s:%s:%s", s.GetKind(), s.GetName(), s.GetNamespace())) 80 } 81 expected := []string{ 82 "Namespace:c1-ns:", 83 "FooBar:fb1:c1-ns", 84 "PodSecurityPolicy:cluster-psp:", 85 "ServiceAccount:c1-sa:c1-ns", 86 "Secret:c1-secret:", 87 "ClusterRole:cr:", 88 "RoleBinding:rb1:c1-ns", 89 "Deployment:deploy1:c1-ns", 90 "BarBaz:c1-secret:", 91 } 92 assert.EqualValues(t, expected, results) 93 } 94 95 func TestBasicSortMeta(t *testing.T) { 96 inputs := []model.K8sQbecMeta{ 97 object(data{"c0", "v1", "BarBaz", "c1-secret", ""}), 98 object(data{"c1", "v1", "Secret", "c1-secret", ""}), 99 object(data{"c1", "v1", "Namespace", "c1-ns", ""}), 100 object(data{"c1", "v1", "ServiceAccount", "c1-sa", "c1-ns"}), 101 object(data{"cluster", "v1", "PodSecurityPolicy", "cluster-psp", ""}), 102 object(data{"c2", "extensions/v1beta1", "Deployment", "deploy1", "c1-ns"}), 103 object(data{"c2", "extensions/v1beta1", "FooBar", "fb1", "c1-ns"}), 104 object(data{"c3", "rbac.authorization.k8s.io/v1beta1", "RoleBinding", "rb1", "c1-ns"}), 105 object(data{"c3", "rbac.authorization.k8s.io/v1beta1", "ClusterRole", "cr", ""}), 106 } 107 sorted := SortMeta(inputs, Config{ 108 OrderingProvider: func(item model.K8sQbecMeta) int { 109 if item.GetName() == "c1-ns" { 110 return 1 111 } 112 return 0 113 }, 114 NamespacedIndicator: func(gvk schema.GroupVersionKind) (bool, error) { 115 if gvk.Kind == "PodSecurityPolicy" || gvk.Kind == "FooBar" || gvk.Kind == "ClusterRoleBinding" { 116 return false, nil 117 } 118 if gvk.Kind == "BarBaz" { 119 return false, errors.New("no indicator for BarBaz") 120 } 121 return true, nil 122 }, 123 }) 124 var results []string 125 for _, s := range sorted { 126 results = append(results, fmt.Sprintf("%s:%s:%s", s.GetKind(), s.GetName(), s.GetNamespace())) 127 } 128 expected := []string{ 129 "Namespace:c1-ns:", 130 "FooBar:fb1:c1-ns", 131 "PodSecurityPolicy:cluster-psp:", 132 "ServiceAccount:c1-sa:c1-ns", 133 "Secret:c1-secret:", 134 "ClusterRole:cr:", 135 "RoleBinding:rb1:c1-ns", 136 "Deployment:deploy1:c1-ns", 137 "BarBaz:c1-secret:", 138 } 139 assert.EqualValues(t, expected, results) 140 }