blob: 784ba13c726201af3c5df851b895d4b54e73baac [file] [log] [blame]
Serge Bazanskibe538db2020-11-12 00:22:42 +01001// Copyright 2017 The kubecfg authors
2//
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
16package utils
17
18import (
19 "path/filepath"
20 "reflect"
21 "sort"
22 "testing"
23
24 openapi_v2 "github.com/googleapis/gnostic/openapiv2"
25 log "github.com/sirupsen/logrus"
26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28 "k8s.io/client-go/discovery"
29 fakedisco "k8s.io/client-go/discovery/fake"
30 "k8s.io/client-go/restmapper"
31 ktesting "k8s.io/client-go/testing"
32)
33
34type FakeDiscovery struct {
35 fakedisco.FakeDiscovery
36 schemaGetter discovery.OpenAPISchemaInterface
37}
38
39func NewFakeDiscovery(schemaGetter discovery.OpenAPISchemaInterface) *FakeDiscovery {
40 fakePtr := &ktesting.Fake{}
41 return &FakeDiscovery{
42 FakeDiscovery: fakedisco.FakeDiscovery{Fake: fakePtr},
43 schemaGetter: schemaGetter,
44 }
45}
46
47func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
48 action := ktesting.ActionImpl{}
49 action.Verb = "get"
50 c.Fake.Invokes(action, nil)
51
52 return c.schemaGetter.OpenAPISchema()
53}
54
55func TestDepSort(t *testing.T) {
56 log.SetLevel(log.DebugLevel)
57
58 disco := NewFakeDiscovery(schemaFromFile{dir: filepath.FromSlash("../testdata")})
59 disco.Resources = []*metav1.APIResourceList{
60 {
61 GroupVersion: "v1",
62 APIResources: []metav1.APIResource{
63 {
64 Name: "configmaps",
65 Kind: "ConfigMap",
66 Namespaced: true,
67 },
68 {
69 Name: "namespaces",
70 Kind: "Namespace",
71 Namespaced: false,
72 },
73 {
74 Name: "replicationcontrollers",
75 Kind: "ReplicationController",
76 Namespaced: true,
77 },
78 },
79 },
80 }
81
82 mapper := restmapper.NewDiscoveryRESTMapper([]*restmapper.APIGroupResources{{
83 Group: metav1.APIGroup{
84 Name: "",
85 Versions: []metav1.GroupVersionForDiscovery{{
86 GroupVersion: "v1",
87 Version: "v1",
88 }},
89 },
90 VersionedResources: map[string][]metav1.APIResource{
91 "v1": disco.Resources[0].APIResources,
92 },
93 }})
94
95 newObj := func(apiVersion, kind string) *unstructured.Unstructured {
96 return &unstructured.Unstructured{
97 Object: map[string]interface{}{
98 "apiVersion": apiVersion,
99 "kind": kind,
100 },
101 }
102 }
103
104 objs := []*unstructured.Unstructured{
105 newObj("v1", "ReplicationController"),
106 newObj("v1", "ConfigMap"),
107 newObj("v1", "Namespace"),
108 newObj("admissionregistration.k8s.io/v1beta1", "MutatingWebhookConfiguration"),
109 newObj("bogus/v1", "UnknownKind"),
110 newObj("apiextensions.k8s.io/v1beta1", "CustomResourceDefinition"),
111 }
112
113 sorter, err := DependencyOrder(disco, mapper, objs)
114 if err != nil {
115 t.Fatalf("DependencyOrder error: %v", err)
116 }
117 sort.Sort(sorter)
118
119 for i, o := range objs {
120 t.Logf("obj[%d] after sort is %v", i, o.GroupVersionKind())
121 }
122
123 if objs[0].GetKind() != "CustomResourceDefinition" {
124 t.Error("CRD should be sorted first")
125 }
126 if objs[1].GetKind() != "Namespace" {
127 t.Error("Namespace should be sorted second")
128 }
129 if objs[4].GetKind() != "ReplicationController" {
130 t.Error("RC should be sorted after non-pod objects")
131 }
132 if objs[5].GetKind() != "MutatingWebhookConfiguration" {
133 t.Error("Webhook should be sorted last")
134 }
135}
136
137func TestAlphaSort(t *testing.T) {
138 newObj := func(ns, name, kind string) *unstructured.Unstructured {
139 o := unstructured.Unstructured{}
140 o.SetNamespace(ns)
141 o.SetName(name)
142 o.SetKind(kind)
143 return &o
144 }
145
146 objs := []*unstructured.Unstructured{
147 newObj("default", "mysvc", "Deployment"),
148 newObj("", "default", "StorageClass"),
149 newObj("", "default", "ClusterRole"),
150 newObj("default", "mydeploy", "Deployment"),
151 newObj("default", "mysvc", "Secret"),
152 }
153
154 expected := []*unstructured.Unstructured{
155 objs[2],
156 objs[1],
157 objs[3],
158 objs[0],
159 objs[4],
160 }
161
162 sort.Sort(AlphabeticalOrder(objs))
163
164 if !reflect.DeepEqual(objs, expected) {
165 t.Errorf("actual != expected: %v != %v", objs, expected)
166 }
167}