client/pkg/dynamic/client_test.go

102 lines
2.7 KiB
Go

// Copyright © 2019 The Knative 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 dynamic
import (
"testing"
"github.com/magiconair/properties/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic/fake"
)
const testNamespace = "testns"
func newUnstructured(name string) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": crdGroup + "/" + crdVersion,
"kind": crdKind,
"metadata": map[string]interface{}{
"namespace": testNamespace,
"name": name,
"labels": map[string]interface{}{
sourcesLabelKey: sourcesLabelValue,
},
},
},
}
}
func createFakeKnDynamicClient(objects ...runtime.Object) KnDynamicClient {
client := fake.NewSimpleDynamicClient(runtime.NewScheme(), objects...)
return NewKnDynamicClient(client, testNamespace)
}
func TestNamespace(t *testing.T) {
client := createFakeKnDynamicClient(newUnstructured("foo"))
assert.Equal(t, client.Namespace(), testNamespace)
}
func TestListCRDs(t *testing.T) {
client := createFakeKnDynamicClient(
newUnstructured("foo"),
newUnstructured("bar"),
)
t.Run("List CRDs with match", func(t *testing.T) {
options := metav1.ListOptions{}
uList, err := client.ListCRDs(options)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(uList.Items), 2)
})
t.Run("List CRDs without match", func(t *testing.T) {
options := metav1.ListOptions{}
sourcesLabels := labels.Set{"duck.knative.dev/source": "true1"}
options.LabelSelector = sourcesLabels.String()
uList, err := client.ListCRDs(options)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(uList.Items), 0)
})
}
func TestListSourceTypes(t *testing.T) {
client := createFakeKnDynamicClient(
newUnstructured("foo"),
newUnstructured("bar"),
)
t.Run("List source types", func(t *testing.T) {
uList, err := client.ListSourcesTypes()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(uList.Items), 2)
assert.Equal(t, uList.Items[0].GetName(), "foo")
assert.Equal(t, uList.Items[1].GetName(), "bar")
})
}