k8s.io/client-go@v0.22.2/rest/watch/decoder_test.go (about)

     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  	"encoding/json"
    21  	"io"
    22  	"testing"
    23  	"time"
    24  
    25  	"k8s.io/api/core/v1"
    26  	apiequality "k8s.io/apimachinery/pkg/api/equality"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	runtimejson "k8s.io/apimachinery/pkg/runtime/serializer/json"
    30  	"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
    31  	"k8s.io/apimachinery/pkg/util/wait"
    32  	"k8s.io/apimachinery/pkg/watch"
    33  	"k8s.io/client-go/kubernetes/scheme"
    34  	restclientwatch "k8s.io/client-go/rest/watch"
    35  )
    36  
    37  // getDecoder mimics how k8s.io/client-go/rest.createSerializers creates a decoder
    38  func getDecoder() runtime.Decoder {
    39  	jsonSerializer := runtimejson.NewSerializer(runtimejson.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, false)
    40  	directCodecFactory := scheme.Codecs.WithoutConversion()
    41  	return directCodecFactory.DecoderToVersion(jsonSerializer, v1.SchemeGroupVersion)
    42  }
    43  
    44  func TestDecoder(t *testing.T) {
    45  	table := []watch.EventType{watch.Added, watch.Deleted, watch.Modified, watch.Error, watch.Bookmark}
    46  
    47  	for _, eventType := range table {
    48  		out, in := io.Pipe()
    49  
    50  		decoder := restclientwatch.NewDecoder(streaming.NewDecoder(out, getDecoder()), getDecoder())
    51  		expect := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
    52  		encoder := json.NewEncoder(in)
    53  		eType := eventType
    54  		go func() {
    55  			data, err := runtime.Encode(scheme.Codecs.LegacyCodec(v1.SchemeGroupVersion), expect)
    56  			if err != nil {
    57  				t.Fatalf("Unexpected error %v", err)
    58  			}
    59  			event := metav1.WatchEvent{
    60  				Type:   string(eType),
    61  				Object: runtime.RawExtension{Raw: json.RawMessage(data)},
    62  			}
    63  			if err := encoder.Encode(&event); err != nil {
    64  				t.Errorf("Unexpected error %v", err)
    65  			}
    66  			in.Close()
    67  		}()
    68  
    69  		done := make(chan struct{})
    70  		go func() {
    71  			action, got, err := decoder.Decode()
    72  			if err != nil {
    73  				t.Fatalf("Unexpected error %v", err)
    74  			}
    75  			if e, a := eType, action; e != a {
    76  				t.Errorf("Expected %v, got %v", e, a)
    77  			}
    78  			if e, a := expect, got; !apiequality.Semantic.DeepDerivative(e, a) {
    79  				t.Errorf("Expected %v, got %v", e, a)
    80  			}
    81  			t.Logf("Exited read")
    82  			close(done)
    83  		}()
    84  		<-done
    85  
    86  		done = make(chan struct{})
    87  		go func() {
    88  			_, _, err := decoder.Decode()
    89  			if err == nil {
    90  				t.Errorf("Unexpected nil error")
    91  			}
    92  			close(done)
    93  		}()
    94  		<-done
    95  
    96  		decoder.Close()
    97  	}
    98  }
    99  
   100  func TestDecoder_SourceClose(t *testing.T) {
   101  	out, in := io.Pipe()
   102  	decoder := restclientwatch.NewDecoder(streaming.NewDecoder(out, getDecoder()), getDecoder())
   103  
   104  	done := make(chan struct{})
   105  
   106  	go func() {
   107  		_, _, err := decoder.Decode()
   108  		if err == nil {
   109  			t.Errorf("Unexpected nil error")
   110  		}
   111  		close(done)
   112  	}()
   113  
   114  	in.Close()
   115  
   116  	select {
   117  	case <-done:
   118  		break
   119  	case <-time.After(wait.ForeverTestTimeout):
   120  		t.Error("Timeout")
   121  	}
   122  }