...

Source file src/k8s.io/client-go/kubernetes_test/fake_client_test.go

Documentation: k8s.io/client-go/kubernetes_test

     1  /*
     2  Copyright 2020 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 kubernetes
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	corev1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/client-go/kubernetes/fake"
    28  	"k8s.io/client-go/kubernetes/scheme"
    29  )
    30  
    31  // This test proves that the kube fake client does not return GVKs.  This is consistent with actual client (see tests below)
    32  // and should not be changed unless the decoding behavior and somehow literal creation (`&corev1.ConfigMap{}`) behavior change.
    33  func Test_ConfigMapFakeClient(t *testing.T) {
    34  	fakeKubeClient := fake.NewSimpleClientset(&corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Namespace: "foo-ns", Name: "foo-name"}})
    35  	cm, err := fakeKubeClient.CoreV1().ConfigMaps("foo-ns").Get(context.TODO(), "foo-name", metav1.GetOptions{})
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	if cm.GetObjectKind().GroupVersionKind() != (schema.GroupVersionKind{}) {
    40  		t.Fatal(cm.GetObjectKind().GroupVersionKind())
    41  	}
    42  	cmList, err := fakeKubeClient.CoreV1().ConfigMaps("foo-ns").List(context.TODO(), metav1.ListOptions{})
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if cmList.GetObjectKind().GroupVersionKind() != (schema.GroupVersionKind{}) {
    47  		t.Fatal(cmList.GetObjectKind().GroupVersionKind())
    48  	}
    49  }
    50  
    51  // This test checks decoding behavior for the actual client to ensure the fake client (tested above) is consistent.
    52  func TestGetDecoding(t *testing.T) {
    53  	// this the duplication of logic from the real Get API for configmaps.  This will prove that the generated client will not return a GVK
    54  	mediaTypes := scheme.Codecs.WithoutConversion().SupportedMediaTypes()
    55  	info, ok := runtime.SerializerInfoForMediaType(mediaTypes, "application/json")
    56  	if !ok {
    57  		t.Fatal("missing serializer")
    58  	}
    59  	decoder := scheme.Codecs.WithoutConversion().DecoderToVersion(info.Serializer, corev1.SchemeGroupVersion)
    60  
    61  	body := []byte(`{"apiVersion": "v1", "kind": "ConfigMap", "metadata":{"Namespace":"foo","Name":"bar"}}`)
    62  	obj := &corev1.ConfigMap{}
    63  	out, _, err := decoder.Decode(body, nil, obj)
    64  	if err != nil || out != obj {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	if obj.GetObjectKind().GroupVersionKind() != (schema.GroupVersionKind{}) {
    69  		t.Fatal(obj.GetObjectKind().GroupVersionKind())
    70  	}
    71  }
    72  
    73  // This test checks decoding behavior for the actual client to ensure the fake client (tested above) is consistent.
    74  func TestListDecoding(t *testing.T) {
    75  	// this the duplication of logic from the real Get API for configmaps.  This will prove that the generated client will not return a GVK
    76  	mediaTypes := scheme.Codecs.WithoutConversion().SupportedMediaTypes()
    77  	info, ok := runtime.SerializerInfoForMediaType(mediaTypes, "application/json")
    78  	if !ok {
    79  		t.Fatal("missing serializer")
    80  	}
    81  	decoder := scheme.Codecs.WithoutConversion().DecoderToVersion(info.Serializer, corev1.SchemeGroupVersion)
    82  
    83  	body := []byte(`{"apiVersion": "v1", "kind": "ConfigMapList", "items":[]}`)
    84  	obj := &corev1.ConfigMapList{}
    85  	out, _, err := decoder.Decode(body, nil, obj)
    86  	if err != nil || out != obj {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	if obj.GetObjectKind().GroupVersionKind() != (schema.GroupVersionKind{}) {
    91  		t.Fatal(obj.GetObjectKind().GroupVersionKind())
    92  	}
    93  }
    94  

View as plain text