2016-09-15 12:52:42 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-02-13 17:36:12 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2016-09-15 12:52:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReplaceAliases(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
arg string
|
2017-03-24 12:02:10 +00:00
|
|
|
expected schema.GroupVersionResource
|
2017-02-13 17:36:12 +00:00
|
|
|
srvRes []*metav1.APIResourceList
|
2016-09-15 12:52:42 +00:00
|
|
|
}{
|
2016-12-15 20:48:29 +00:00
|
|
|
{
|
2017-02-13 17:36:12 +00:00
|
|
|
name: "rc-resolves-to-replicationcontrollers",
|
|
|
|
arg: "rc",
|
2017-03-24 12:02:10 +00:00
|
|
|
expected: schema.GroupVersionResource{Resource: "replicationcontrollers"},
|
2017-02-13 17:36:12 +00:00
|
|
|
srvRes: []*metav1.APIResourceList{},
|
2016-12-15 20:48:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "storageclasses-no-replacement",
|
|
|
|
arg: "storageclasses",
|
2017-03-24 12:02:10 +00:00
|
|
|
expected: schema.GroupVersionResource{Resource: "storageclasses"},
|
2017-02-13 17:36:12 +00:00
|
|
|
srvRes: []*metav1.APIResourceList{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hpa-priority",
|
|
|
|
arg: "hpa",
|
2018-01-24 09:40:21 +00:00
|
|
|
expected: schema.GroupVersionResource{Resource: "superhorizontalpodautoscalers", Group: "autoscaling"},
|
2017-02-13 17:36:12 +00:00
|
|
|
srvRes: []*metav1.APIResourceList{
|
|
|
|
{
|
|
|
|
GroupVersion: "autoscaling/v1",
|
|
|
|
APIResources: []metav1.APIResource{
|
|
|
|
{
|
|
|
|
Name: "superhorizontalpodautoscalers",
|
|
|
|
ShortNames: []string{"hpa"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupVersion: "autoscaling/v1",
|
|
|
|
APIResources: []metav1.APIResource{
|
|
|
|
{
|
|
|
|
Name: "horizontalpodautoscalers",
|
|
|
|
ShortNames: []string{"hpa"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-12-15 20:48:29 +00:00
|
|
|
},
|
2018-01-24 09:40:21 +00:00
|
|
|
{
|
|
|
|
name: "resource-override",
|
|
|
|
arg: "dpl",
|
|
|
|
expected: schema.GroupVersionResource{Resource: "deployments", Group: "foo"},
|
|
|
|
srvRes: []*metav1.APIResourceList{
|
|
|
|
{
|
|
|
|
GroupVersion: "foo/v1",
|
|
|
|
APIResources: []metav1.APIResource{
|
|
|
|
{
|
|
|
|
Name: "deployments",
|
|
|
|
ShortNames: []string{"dpl"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupVersion: "extension/v1beta1",
|
|
|
|
APIResources: []metav1.APIResource{
|
|
|
|
{
|
|
|
|
Name: "deployments",
|
|
|
|
ShortNames: []string{"deploy"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-01-26 06:45:38 +00:00
|
|
|
{
|
|
|
|
name: "resource-match-preferred",
|
|
|
|
arg: "pods",
|
|
|
|
expected: schema.GroupVersionResource{Resource: "pods", Group: ""},
|
|
|
|
srvRes: []*metav1.APIResourceList{
|
|
|
|
{
|
|
|
|
GroupVersion: "v1",
|
|
|
|
APIResources: []metav1.APIResource{{Name: "pods", SingularName: "pod"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupVersion: "acme.com/v1",
|
|
|
|
APIResources: []metav1.APIResource{{Name: "poddlers", ShortNames: []string{"pods", "pod"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "resource-match-singular-preferred",
|
|
|
|
arg: "pod",
|
|
|
|
expected: schema.GroupVersionResource{Resource: "pod", Group: ""},
|
|
|
|
srvRes: []*metav1.APIResourceList{
|
|
|
|
{
|
|
|
|
GroupVersion: "v1",
|
|
|
|
APIResources: []metav1.APIResource{{Name: "pods", SingularName: "pod"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
GroupVersion: "acme.com/v1",
|
|
|
|
APIResources: []metav1.APIResource{{Name: "poddlers", ShortNames: []string{"pods", "pod"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-09-15 12:52:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-13 17:36:12 +00:00
|
|
|
ds := &fakeDiscoveryClient{}
|
2017-11-15 03:16:10 +00:00
|
|
|
mapper := NewShortcutExpander(testapi.Default.RESTMapper(), ds)
|
2016-09-15 12:52:42 +00:00
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-02-13 17:36:12 +00:00
|
|
|
ds.serverResourcesHandler = func() ([]*metav1.APIResourceList, error) {
|
|
|
|
return test.srvRes, nil
|
|
|
|
}
|
2017-03-24 12:02:10 +00:00
|
|
|
actual := mapper.expandResourceShortcut(schema.GroupVersionResource{Resource: test.arg})
|
|
|
|
if actual != test.expected {
|
|
|
|
t.Errorf("%s: unexpected argument: expected %s, got %s", test.name, test.expected, actual)
|
2016-09-15 12:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 17:36:12 +00:00
|
|
|
|
2016-12-15 20:48:29 +00:00
|
|
|
func TestKindFor(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in schema.GroupVersionResource
|
|
|
|
expected schema.GroupVersionKind
|
2017-02-13 17:36:12 +00:00
|
|
|
srvRes []*metav1.APIResourceList
|
2016-12-15 20:48:29 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: schema.GroupVersionResource{Group: "storage.k8s.io", Version: "", Resource: "sc"},
|
2017-04-25 08:13:38 +00:00
|
|
|
expected: schema.GroupVersionKind{Group: "storage.k8s.io", Version: "v1", Kind: "StorageClass"},
|
2017-02-13 17:36:12 +00:00
|
|
|
srvRes: []*metav1.APIResourceList{
|
|
|
|
{
|
2017-04-25 08:13:38 +00:00
|
|
|
GroupVersion: "storage.k8s.io/v1",
|
2017-02-13 17:36:12 +00:00
|
|
|
APIResources: []metav1.APIResource{
|
|
|
|
{
|
|
|
|
Name: "storageclasses",
|
|
|
|
ShortNames: []string{"sc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-12-15 20:48:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: schema.GroupVersionResource{Group: "", Version: "", Resource: "sc"},
|
2017-04-25 08:13:38 +00:00
|
|
|
expected: schema.GroupVersionKind{Group: "storage.k8s.io", Version: "v1", Kind: "StorageClass"},
|
2017-02-13 17:36:12 +00:00
|
|
|
srvRes: []*metav1.APIResourceList{
|
|
|
|
{
|
2017-04-25 08:13:38 +00:00
|
|
|
GroupVersion: "storage.k8s.io/v1",
|
2017-02-13 17:36:12 +00:00
|
|
|
APIResources: []metav1.APIResource{
|
|
|
|
{
|
|
|
|
Name: "storageclasses",
|
|
|
|
ShortNames: []string{"sc"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-12-15 20:48:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-13 17:36:12 +00:00
|
|
|
ds := &fakeDiscoveryClient{}
|
2017-11-15 03:16:10 +00:00
|
|
|
mapper := NewShortcutExpander(testapi.Default.RESTMapper(), ds)
|
2016-12-15 20:48:29 +00:00
|
|
|
|
|
|
|
for i, test := range tests {
|
2017-02-13 17:36:12 +00:00
|
|
|
ds.serverResourcesHandler = func() ([]*metav1.APIResourceList, error) {
|
|
|
|
return test.srvRes, nil
|
|
|
|
}
|
2016-12-15 20:48:29 +00:00
|
|
|
ret, err := mapper.KindFor(test.in)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%d: unexpected error returned %s", i, err.Error())
|
|
|
|
}
|
|
|
|
if ret != test.expected {
|
|
|
|
t.Errorf("%d: unexpected data returned %#v, expected %#v", i, ret, test.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|