vitess.io/vitess@v0.16.2/go/vt/topo/k8stopo/file_test.go (about) 1 /* 2 Copyright 2020 The Vitess 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 agreedto 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 k8stopo 18 19 import ( 20 "reflect" 21 "testing" 22 ) 23 24 func Test_packValue(t *testing.T) { 25 tests := []struct { 26 name string 27 value []byte 28 want []byte 29 wantErr bool 30 }{ 31 { 32 // a gzip with an empty payload still has header bytes to identify the stream 33 "empty", 34 []byte{}, 35 []byte{72, 52, 115, 73, 65, 65, 65, 65, 65, 65, 65, 65, 47, 119, 69, 65, 65, 80, 47, 47, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 61}, 36 false, 37 }, 38 { 39 "valid payload", 40 []byte("test payload"), 41 []byte{72, 52, 115, 73, 65, 65, 65, 65, 65, 65, 65, 65, 47, 121, 112, 74, 76, 83, 53, 82, 75, 69, 105, 115, 122, 77, 108, 80, 84, 65, 69, 69, 65, 65, 68, 47, 47, 43, 69, 57, 72, 101, 115, 77, 65, 65, 65, 65}, 42 false, 43 }, 44 } 45 for _, tt := range tests { 46 t.Run(tt.name, func(t *testing.T) { 47 got, err := packValue(tt.value) 48 if (err != nil) != tt.wantErr { 49 t.Errorf("packValue() error = %v, wantErr %v", err, tt.wantErr) 50 return 51 } 52 if !reflect.DeepEqual(got, tt.want) { 53 t.Errorf("packValue() = %v, want %v", got, tt.want) 54 } 55 }) 56 } 57 } 58 59 func Test_unpackValue(t *testing.T) { 60 tests := []struct { 61 name string 62 value []byte 63 want []byte 64 wantErr bool 65 }{ 66 { 67 // a gzip with an empty payload still has header bytes to identify the stream 68 "empty", 69 []byte{72, 52, 115, 73, 65, 65, 65, 65, 65, 65, 65, 65, 47, 119, 69, 65, 65, 80, 47, 47, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 61}, 70 []byte{}, 71 false, 72 }, 73 { 74 "valid payload", 75 []byte{72, 52, 115, 73, 65, 65, 65, 65, 65, 65, 65, 65, 47, 121, 112, 74, 76, 83, 53, 82, 75, 69, 105, 115, 122, 77, 108, 80, 84, 65, 69, 69, 65, 65, 68, 47, 47, 43, 69, 57, 72, 101, 115, 77, 65, 65, 65, 65}, 76 []byte("test payload"), 77 false, 78 }, 79 } 80 for _, tt := range tests { 81 t.Run(tt.name, func(t *testing.T) { 82 got, err := unpackValue(tt.value) 83 if (err != nil) != tt.wantErr { 84 t.Errorf("unpackValue() error = %v, wantErr %v", err, tt.wantErr) 85 return 86 } 87 if !reflect.DeepEqual(got, tt.want) { 88 t.Errorf("unpackValue() = %v, want %v", got, tt.want) 89 } 90 }) 91 } 92 } 93 94 func Test_packUnpackRoundTrip(t *testing.T) { 95 tests := []struct { 96 name string 97 value []byte 98 wantErr bool 99 }{ 100 { 101 "empty", 102 []byte{}, 103 false, 104 }, 105 { 106 "valid payload", 107 []byte("test payload"), 108 false, 109 }, 110 } 111 for _, tt := range tests { 112 t.Run(tt.name, func(t *testing.T) { 113 packed, err := packValue(tt.value) 114 if (err != nil) != tt.wantErr { 115 t.Errorf("packValue() error = %v, wantErr %v", err, tt.wantErr) 116 return 117 } 118 119 unpacked, err := unpackValue(packed) 120 if (err != nil) != tt.wantErr { 121 t.Errorf("packValue() error = %v, wantErr %v", err, tt.wantErr) 122 return 123 } 124 125 if !reflect.DeepEqual(unpacked, tt.value) { 126 t.Errorf("unpacked value != original value original = %v, unpacked %v", tt.value, unpacked) 127 return 128 } 129 }) 130 } 131 }