github.com/Beeketing/helm@v2.12.1+incompatible/pkg/tiller/kind_sorter_test.go (about) 1 /* 2 Copyright The Helm 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 tiller 18 19 import ( 20 "bytes" 21 "testing" 22 23 util "k8s.io/helm/pkg/releaseutil" 24 ) 25 26 func TestKindSorter(t *testing.T) { 27 manifests := []Manifest{ 28 { 29 Name: "i", 30 Head: &util.SimpleHead{Kind: "ClusterRole"}, 31 }, 32 { 33 Name: "j", 34 Head: &util.SimpleHead{Kind: "ClusterRoleBinding"}, 35 }, 36 { 37 Name: "e", 38 Head: &util.SimpleHead{Kind: "ConfigMap"}, 39 }, 40 { 41 Name: "u", 42 Head: &util.SimpleHead{Kind: "CronJob"}, 43 }, 44 { 45 Name: "2", 46 Head: &util.SimpleHead{Kind: "CustomResourceDefinition"}, 47 }, 48 { 49 Name: "n", 50 Head: &util.SimpleHead{Kind: "DaemonSet"}, 51 }, 52 { 53 Name: "r", 54 Head: &util.SimpleHead{Kind: "Deployment"}, 55 }, 56 { 57 Name: "!", 58 Head: &util.SimpleHead{Kind: "HonkyTonkSet"}, 59 }, 60 { 61 Name: "v", 62 Head: &util.SimpleHead{Kind: "Ingress"}, 63 }, 64 { 65 Name: "t", 66 Head: &util.SimpleHead{Kind: "Job"}, 67 }, 68 { 69 Name: "c", 70 Head: &util.SimpleHead{Kind: "LimitRange"}, 71 }, 72 { 73 Name: "a", 74 Head: &util.SimpleHead{Kind: "Namespace"}, 75 }, 76 { 77 Name: "f", 78 Head: &util.SimpleHead{Kind: "PersistentVolume"}, 79 }, 80 { 81 Name: "g", 82 Head: &util.SimpleHead{Kind: "PersistentVolumeClaim"}, 83 }, 84 { 85 Name: "o", 86 Head: &util.SimpleHead{Kind: "Pod"}, 87 }, 88 { 89 Name: "3", 90 Head: &util.SimpleHead{Kind: "PodSecurityPolicy"}, 91 }, 92 { 93 Name: "q", 94 Head: &util.SimpleHead{Kind: "ReplicaSet"}, 95 }, 96 { 97 Name: "p", 98 Head: &util.SimpleHead{Kind: "ReplicationController"}, 99 }, 100 { 101 Name: "b", 102 Head: &util.SimpleHead{Kind: "ResourceQuota"}, 103 }, 104 { 105 Name: "k", 106 Head: &util.SimpleHead{Kind: "Role"}, 107 }, 108 { 109 Name: "l", 110 Head: &util.SimpleHead{Kind: "RoleBinding"}, 111 }, 112 { 113 Name: "d", 114 Head: &util.SimpleHead{Kind: "Secret"}, 115 }, 116 { 117 Name: "m", 118 Head: &util.SimpleHead{Kind: "Service"}, 119 }, 120 { 121 Name: "h", 122 Head: &util.SimpleHead{Kind: "ServiceAccount"}, 123 }, 124 { 125 Name: "s", 126 Head: &util.SimpleHead{Kind: "StatefulSet"}, 127 }, 128 { 129 Name: "1", 130 Head: &util.SimpleHead{Kind: "StorageClass"}, 131 }, 132 { 133 Name: "w", 134 Head: &util.SimpleHead{Kind: "APIService"}, 135 }, 136 { 137 Name: "z", 138 Head: &util.SimpleHead{Kind: "PodDisruptionBudget"}, 139 }, 140 } 141 142 for _, test := range []struct { 143 description string 144 order SortOrder 145 expected string 146 }{ 147 {"install", InstallOrder, "abc3zde1fgh2ijklmnopqrstuvw!"}, 148 {"uninstall", UninstallOrder, "wvmutsrqponlkji2hgf1edz3cba!"}, 149 } { 150 var buf bytes.Buffer 151 t.Run(test.description, func(t *testing.T) { 152 if got, want := len(test.expected), len(manifests); got != want { 153 t.Fatalf("Expected %d names in order, got %d", want, got) 154 } 155 defer buf.Reset() 156 for _, r := range sortByKind(manifests, test.order) { 157 buf.WriteString(r.Name) 158 } 159 if got := buf.String(); got != test.expected { 160 t.Errorf("Expected %q, got %q", test.expected, got) 161 } 162 }) 163 } 164 } 165 166 // TestKindSorterSubSort verifies manifests of same kind are also sorted alphanumeric 167 func TestKindSorterSubSort(t *testing.T) { 168 manifests := []Manifest{ 169 { 170 Name: "a", 171 Head: &util.SimpleHead{Kind: "ClusterRole"}, 172 }, 173 { 174 Name: "A", 175 Head: &util.SimpleHead{Kind: "ClusterRole"}, 176 }, 177 { 178 Name: "0", 179 Head: &util.SimpleHead{Kind: "ConfigMap"}, 180 }, 181 { 182 Name: "1", 183 Head: &util.SimpleHead{Kind: "ConfigMap"}, 184 }, 185 { 186 Name: "z", 187 Head: &util.SimpleHead{Kind: "ClusterRoleBinding"}, 188 }, 189 { 190 Name: "!", 191 Head: &util.SimpleHead{Kind: "ClusterRoleBinding"}, 192 }, 193 { 194 Name: "u2", 195 Head: &util.SimpleHead{Kind: "Unknown"}, 196 }, 197 { 198 Name: "u1", 199 Head: &util.SimpleHead{Kind: "Unknown"}, 200 }, 201 { 202 Name: "t3", 203 Head: &util.SimpleHead{Kind: "Unknown2"}, 204 }, 205 } 206 for _, test := range []struct { 207 description string 208 order SortOrder 209 expected string 210 }{ 211 // expectation is sorted by kind (unknown is last) and then sub sorted alphabetically within each group 212 {"cm,clusterRole,clusterRoleBinding,Unknown,Unknown2", InstallOrder, "01Aa!zu1u2t3"}, 213 } { 214 var buf bytes.Buffer 215 t.Run(test.description, func(t *testing.T) { 216 defer buf.Reset() 217 for _, r := range sortByKind(manifests, test.order) { 218 buf.WriteString(r.Name) 219 } 220 if got := buf.String(); got != test.expected { 221 t.Errorf("Expected %q, got %q", test.expected, got) 222 } 223 }) 224 } 225 }