Debian Codegraph @

rest/watch/decoder_test.go

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