|
| 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 declarative |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 25 | + "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest" |
| 26 | +) |
| 27 | + |
| 28 | +func Test_Sort(t *testing.T) { |
| 29 | + crd1 := &unstructured.Unstructured{ |
| 30 | + Object: map[string]interface{}{ |
| 31 | + "apiVersion": "apiextensions.k8s.io/v1beta1", |
| 32 | + "kind": "CustomResourceDefinition", |
| 33 | + "metadata": map[string]interface{}{ |
| 34 | + "name": "test-crd", |
| 35 | + "namespace": "kube-system", |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | + crdObj1, _ := manifest.NewObject(crd1) |
| 40 | + |
| 41 | + ns1 := &unstructured.Unstructured{ |
| 42 | + Object: map[string]interface{}{ |
| 43 | + "apiVersion": "v1", |
| 44 | + "kind": "Namespace", |
| 45 | + "metadata": map[string]interface{}{ |
| 46 | + "name": "test-crd", |
| 47 | + "namespace": "kube-system", |
| 48 | + }, |
| 49 | + }, |
| 50 | + } |
| 51 | + nsObj1, _ := manifest.NewObject(ns1) |
| 52 | + |
| 53 | + sa1 := &unstructured.Unstructured{ |
| 54 | + Object: map[string]interface{}{ |
| 55 | + "apiVersion": "v1", |
| 56 | + "kind": "ServiceAccount", |
| 57 | + "metadata": map[string]interface{}{ |
| 58 | + "name": "foo-operator", |
| 59 | + "namespace": "kube-system", |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + saObj1, _ := manifest.NewObject(sa1) |
| 64 | + |
| 65 | + sa2 := &unstructured.Unstructured{ |
| 66 | + Object: map[string]interface{}{ |
| 67 | + "apiVersion": "v1", |
| 68 | + "kind": "ServiceAccount", |
| 69 | + "metadata": map[string]interface{}{ |
| 70 | + "name": "bar-operator", |
| 71 | + "namespace": "kube-system", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + saObj2, _ := manifest.NewObject(sa2) |
| 76 | + |
| 77 | + clusterRole1 := &unstructured.Unstructured{ |
| 78 | + Object: map[string]interface{}{ |
| 79 | + "apiVersion": "rbac.authorization.k8s.io/v1", |
| 80 | + "kind": "ClusterRole", |
| 81 | + "metadata": map[string]interface{}{ |
| 82 | + "name": "test-clusterrole", |
| 83 | + "namespace": "kube-system", |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + clusterRoleObj1, _ := manifest.NewObject(clusterRole1) |
| 88 | + |
| 89 | + clusterRoleBinding1 := &unstructured.Unstructured{ |
| 90 | + Object: map[string]interface{}{ |
| 91 | + "apiVersion": "rbac.authorization.k8s.io/v1", |
| 92 | + "kind": "ClusterRoleBinding", |
| 93 | + "metadata": map[string]interface{}{ |
| 94 | + "name": "test-clusterrolebinding", |
| 95 | + "namespace": "kube-system", |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + clusterRoleBindingObj1, _ := manifest.NewObject(clusterRoleBinding1) |
| 100 | + |
| 101 | + cm1 := &unstructured.Unstructured{ |
| 102 | + Object: map[string]interface{}{ |
| 103 | + "apiVersion": "v1", |
| 104 | + "kind": "ConfigMap", |
| 105 | + "metadata": map[string]interface{}{ |
| 106 | + "name": "test-configmap", |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | + cmObj1, _ := manifest.NewObject(cm1) |
| 111 | + |
| 112 | + secret1 := &unstructured.Unstructured{ |
| 113 | + Object: map[string]interface{}{ |
| 114 | + "apiVersion": "v1", |
| 115 | + "kind": "Secrets", //TODO: current code set use Secrets not Secret.. |
| 116 | + "metadata": map[string]interface{}{ |
| 117 | + "name": "test-secret", |
| 118 | + }, |
| 119 | + }, |
| 120 | + } |
| 121 | + secretObj1, _ := manifest.NewObject(secret1) |
| 122 | + |
| 123 | + dp1 := &unstructured.Unstructured{ |
| 124 | + Object: map[string]interface{}{ |
| 125 | + "apiVersion": "apps/v1", |
| 126 | + "kind": "Deployment", |
| 127 | + "metadata": map[string]interface{}{ |
| 128 | + "name": "frontend111", |
| 129 | + }, |
| 130 | + }, |
| 131 | + } |
| 132 | + dpObj1, _ := manifest.NewObject(dp1) |
| 133 | + |
| 134 | + dp2 := &unstructured.Unstructured{ |
| 135 | + Object: map[string]interface{}{ |
| 136 | + "apiVersion": "extensions/v1beta1", |
| 137 | + "kind": "Deployment", |
| 138 | + "metadata": map[string]interface{}{ |
| 139 | + "name": "frontend222", |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + dpObj2, _ := manifest.NewObject(dp2) |
| 144 | + |
| 145 | + hpa1 := &unstructured.Unstructured{ |
| 146 | + Object: map[string]interface{}{ |
| 147 | + "apiVersion": "autoscaling/v1", |
| 148 | + "kind": "HorizontalPodAutoscaler", |
| 149 | + "metadata": map[string]interface{}{ |
| 150 | + "name": "test-autoscaler", |
| 151 | + }, |
| 152 | + }, |
| 153 | + } |
| 154 | + hpaObj1, _ := manifest.NewObject(hpa1) |
| 155 | + |
| 156 | + svc1 := &unstructured.Unstructured{ |
| 157 | + Object: map[string]interface{}{ |
| 158 | + "apiVersion": "v1", |
| 159 | + "kind": "Service", |
| 160 | + "metadata": map[string]interface{}{ |
| 161 | + "name": "test-service", |
| 162 | + }, |
| 163 | + }, |
| 164 | + } |
| 165 | + svcObj1, _ := manifest.NewObject(svc1) |
| 166 | + |
| 167 | + pod1 := &unstructured.Unstructured{ |
| 168 | + Object: map[string]interface{}{ |
| 169 | + "apiVersion": "v1", |
| 170 | + "kind": "Pod", |
| 171 | + "metadata": map[string]interface{}{ |
| 172 | + "name": "test-pod", |
| 173 | + }, |
| 174 | + }, |
| 175 | + } |
| 176 | + podObj1, _ := manifest.NewObject(pod1) |
| 177 | + |
| 178 | + var testcases = []struct { |
| 179 | + name string |
| 180 | + input *manifest.Objects |
| 181 | + expected *manifest.Objects |
| 182 | + }{ |
| 183 | + { |
| 184 | + name: "multiple objects", |
| 185 | + input: &manifest.Objects{ |
| 186 | + Items: []*manifest.Object{ |
| 187 | + saObj1, |
| 188 | + crdObj1, |
| 189 | + nsObj1, |
| 190 | + saObj2, |
| 191 | + dpObj1, |
| 192 | + dpObj2, |
| 193 | + clusterRoleObj1, |
| 194 | + hpaObj1, |
| 195 | + podObj1, |
| 196 | + svcObj1, |
| 197 | + clusterRoleBindingObj1, |
| 198 | + cmObj1, |
| 199 | + secretObj1, |
| 200 | + }, |
| 201 | + }, |
| 202 | + expected: &manifest.Objects{ |
| 203 | + Items: []*manifest.Object{ |
| 204 | + crdObj1, |
| 205 | + nsObj1, |
| 206 | + saObj2, |
| 207 | + saObj1, |
| 208 | + clusterRoleObj1, |
| 209 | + clusterRoleBindingObj1, |
| 210 | + cmObj1, |
| 211 | + secretObj1, |
| 212 | + podObj1, |
| 213 | + dpObj1, |
| 214 | + dpObj2, |
| 215 | + hpaObj1, |
| 216 | + svcObj1, |
| 217 | + }, |
| 218 | + }, |
| 219 | + }, |
| 220 | + } |
| 221 | + for _, tc := range testcases { |
| 222 | + t.Run(tc.name, func(t *testing.T) { |
| 223 | + ctx := context.Background() |
| 224 | + tc.input.Sort(DefaultObjectOrder(ctx)) |
| 225 | + assert.Equal(t, tc.expected, tc.input) |
| 226 | + }) |
| 227 | + } |
| 228 | + |
| 229 | +} |
0 commit comments