|
| 1 | +/* |
| 2 | +Copyright 2026 The kcp 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 manifest |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 23 | +) |
| 24 | + |
| 25 | +func TestSortObjectsByHierarchy(t *testing.T) { |
| 26 | + crd := newUnstructured("apiextensions.k8s.io/v1", "CustomResourceDefinition", "test-crd") |
| 27 | + apiExport := newUnstructured("apis.kcp.io/v1alpha1", "APIExport", "test-export") |
| 28 | + apiBinding := newUnstructured("apis.kcp.io/v1alpha1", "APIBinding", "test-binding") |
| 29 | + namespace := newUnstructured("v1", "Namespace", "test-ns") |
| 30 | + configMap := newUnstructured("v1", "ConfigMap", "test-cm") |
| 31 | + deployment := newUnstructured("apps/v1", "Deployment", "test-deploy") |
| 32 | + service := newUnstructured("v1", "Service", "test-svc") |
| 33 | + |
| 34 | + testcases := []struct { |
| 35 | + name string |
| 36 | + input []*unstructured.Unstructured |
| 37 | + expected []*unstructured.Unstructured |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "empty input", |
| 41 | + input: []*unstructured.Unstructured{}, |
| 42 | + expected: []*unstructured.Unstructured{}, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "single object", |
| 46 | + input: []*unstructured.Unstructured{configMap}, |
| 47 | + expected: []*unstructured.Unstructured{configMap}, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "already sorted", |
| 51 | + input: []*unstructured.Unstructured{crd, apiExport, apiBinding, namespace, configMap}, |
| 52 | + expected: []*unstructured.Unstructured{crd, apiExport, apiBinding, namespace, configMap}, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "reverse order", |
| 56 | + input: []*unstructured.Unstructured{configMap, namespace, apiBinding, apiExport, crd}, |
| 57 | + expected: []*unstructured.Unstructured{crd, apiExport, apiBinding, namespace, configMap}, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "mixed order", |
| 61 | + input: []*unstructured.Unstructured{namespace, configMap, crd, deployment, apiBinding, apiExport}, |
| 62 | + expected: []*unstructured.Unstructured{crd, apiExport, apiBinding, namespace, configMap, deployment}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "multiple objects of same kind", |
| 66 | + input: []*unstructured.Unstructured{configMap, crd, configMap, crd}, |
| 67 | + expected: []*unstructured.Unstructured{crd, crd, configMap, configMap}, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "only regular objects", |
| 71 | + input: []*unstructured.Unstructured{service, configMap, deployment}, |
| 72 | + expected: []*unstructured.Unstructured{service, configMap, deployment}, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range testcases { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + SortObjectsByHierarchy(tt.input) |
| 79 | + |
| 80 | + if len(tt.input) != len(tt.expected) { |
| 81 | + t.Fatalf("Expected %d objects, got %d", len(tt.expected), len(tt.input)) |
| 82 | + } |
| 83 | + |
| 84 | + for i, obj := range tt.input { |
| 85 | + if obj != tt.expected[i] { |
| 86 | + t.Fatalf("At index %d: expected %s, got %s", i, tt.expected[i].GetKind(), obj.GetKind()) |
| 87 | + } |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func newUnstructured(apiVersion, kind, name string) *unstructured.Unstructured { |
| 94 | + return &unstructured.Unstructured{ |
| 95 | + Object: map[string]any{ |
| 96 | + "apiVersion": apiVersion, |
| 97 | + "kind": kind, |
| 98 | + "metadata": map[string]any{ |
| 99 | + "name": name, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | +} |
0 commit comments