github.com/enmand/kubernetes@v1.2.0-alpha.0/pkg/watch/json/encoder_test.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors All rights reserved. 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 json 18 19 import ( 20 "bytes" 21 "io/ioutil" 22 "testing" 23 24 "k8s.io/kubernetes/pkg/api" 25 "k8s.io/kubernetes/pkg/api/testapi" 26 "k8s.io/kubernetes/pkg/runtime" 27 "k8s.io/kubernetes/pkg/watch" 28 ) 29 30 func TestEncodeDecodeRoundTrip(t *testing.T) { 31 testCases := []struct { 32 Type watch.EventType 33 Object runtime.Object 34 Codec runtime.Codec 35 }{ 36 { 37 watch.Added, 38 &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, 39 testapi.Default.Codec(), 40 }, 41 { 42 watch.Modified, 43 &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, 44 testapi.Default.Codec(), 45 }, 46 { 47 watch.Deleted, 48 &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, 49 testapi.Default.Codec(), 50 }, 51 } 52 for i, testCase := range testCases { 53 buf := &bytes.Buffer{} 54 55 encoder := NewEncoder(buf, testCase.Codec) 56 if err := encoder.Encode(&watch.Event{Type: testCase.Type, Object: testCase.Object}); err != nil { 57 t.Errorf("%d: unexpected error: %v", i, err) 58 continue 59 } 60 61 decoder := NewDecoder(ioutil.NopCloser(buf), testCase.Codec) 62 event, obj, err := decoder.Decode() 63 if err != nil { 64 t.Errorf("%d: unexpected error: %v", i, err) 65 continue 66 } 67 if !api.Semantic.DeepDerivative(testCase.Object, obj) { 68 t.Errorf("%d: expected %#v, got %#v", i, testCase.Object, obj) 69 } 70 if event != testCase.Type { 71 t.Errorf("%d: unexpected type: %#v", i, event) 72 } 73 } 74 }