...

Source file src/k8s.io/client-go/rest/watch/encoder_test.go

Documentation: k8s.io/client-go/rest/watch

     1  /*
     2  Copyright 2014 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 versioned_test
    18  
    19  import (
    20  	"bytes"
    21  	"io"
    22  	"testing"
    23  
    24  	"k8s.io/api/core/v1"
    25  	apiequality "k8s.io/apimachinery/pkg/api/equality"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  	runtimejson "k8s.io/apimachinery/pkg/runtime/serializer/json"
    29  	"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
    30  	"k8s.io/apimachinery/pkg/watch"
    31  	"k8s.io/client-go/kubernetes/scheme"
    32  	restclientwatch "k8s.io/client-go/rest/watch"
    33  )
    34  
    35  // getEncoder mimics how k8s.io/client-go/rest.createSerializers creates a encoder
    36  func getEncoder() runtime.Encoder {
    37  	jsonSerializer := runtimejson.NewSerializer(runtimejson.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, false)
    38  	directCodecFactory := scheme.Codecs.WithoutConversion()
    39  	return directCodecFactory.EncoderForVersion(jsonSerializer, v1.SchemeGroupVersion)
    40  }
    41  
    42  func TestEncodeDecodeRoundTrip(t *testing.T) {
    43  	testCases := []struct {
    44  		Type   watch.EventType
    45  		Object runtime.Object
    46  	}{
    47  		{
    48  			watch.Added,
    49  			&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
    50  		},
    51  		{
    52  			watch.Modified,
    53  			&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
    54  		},
    55  		{
    56  			watch.Deleted,
    57  			&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
    58  		},
    59  		{
    60  			watch.Bookmark,
    61  			&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
    62  		},
    63  	}
    64  	for i, testCase := range testCases {
    65  		buf := &bytes.Buffer{}
    66  
    67  		encoder := restclientwatch.NewEncoder(streaming.NewEncoder(buf, getEncoder()), getEncoder())
    68  		if err := encoder.Encode(&watch.Event{Type: testCase.Type, Object: testCase.Object}); err != nil {
    69  			t.Errorf("%d: unexpected error: %v", i, err)
    70  			continue
    71  		}
    72  
    73  		rc := io.NopCloser(buf)
    74  		decoder := restclientwatch.NewDecoder(streaming.NewDecoder(rc, getDecoder()), getDecoder())
    75  		event, obj, err := decoder.Decode()
    76  		if err != nil {
    77  			t.Errorf("%d: unexpected error: %v", i, err)
    78  			continue
    79  		}
    80  		if !apiequality.Semantic.DeepDerivative(testCase.Object, obj) {
    81  			t.Errorf("%d: expected %#v, got %#v", i, testCase.Object, obj)
    82  		}
    83  		if event != testCase.Type {
    84  			t.Errorf("%d: unexpected type: %#v", i, event)
    85  		}
    86  	}
    87  }
    88  

View as plain text