1 package kversion
2
3 import (
4 "math"
5 "testing"
6 )
7
8 func TestSetMaxKeyVersion(t *testing.T) {
9 var vs Versions
10 for i := int16(0); i < math.MaxInt16; i++ {
11 vs.SetMaxKeyVersion(i, i)
12 }
13 for i, v := range vs.k2v {
14 if int16(i) != v {
15 t.Errorf("set incorrect: at %d got %d != exp %d", i, v, i)
16 }
17 }
18 }
19
20 func TestVersionGuess(t *testing.T) {
21
22 {
23 v := V0_8_0()
24 if got, exp := v.VersionGuess(), "v0.8.0"; got != exp {
25 t.Errorf("got %s != exp %s without modifications", got, exp)
26 }
27 v.SetMaxKeyVersion(0, -1)
28 if got, exp := v.VersionGuess(), "not even v0.8.0"; got != exp {
29 t.Errorf("got %s != exp %s unsetting produce", got, exp)
30 }
31 v.SetMaxKeyVersion(0, 100)
32 if got, exp := v.VersionGuess(), "unknown custom version at least v0.8.0"; got != exp {
33 t.Errorf("got %s != exp %s maxing produce", got, exp)
34 }
35 v.SetMaxKeyVersion(1, -1)
36 if got, exp := v.VersionGuess(), "unknown custom version"; got != exp {
37 t.Errorf("got %s != exp %s maxing produce and unsetting fetch", got, exp)
38 }
39 }
40
41
42 {
43 v := V0_9_0()
44 if got, exp := v.VersionGuess(), "v0.9.0"; got != exp {
45 t.Errorf("got %s != exp %s without modifications", got, exp)
46 }
47 v.SetMaxKeyVersion(17, 0)
48 if got, exp := v.VersionGuess(), "between v0.9.0 and v0.10.0"; got != exp {
49 t.Errorf("got %s != exp %s setting sasl handshake to 0", got, exp)
50 }
51 v.SetMaxKeyVersion(0, 2)
52 v.SetMaxKeyVersion(1, 2)
53 v.SetMaxKeyVersion(3, 1)
54 v.SetMaxKeyVersion(6, 2)
55 v.SetMaxKeyVersion(18, 0)
56 if got, exp := v.VersionGuess(), "v0.10.0"; got != exp {
57 t.Errorf("got %s != exp %s setting api versions to 0", got, exp)
58 }
59 }
60
61
62 {
63 v := V2_7_0()
64 v.SetMaxKeyVersion(int16(len(v.k2v)+1), -1)
65 if got, exp := v.VersionGuess(), "v2.7"; got != exp {
66 t.Errorf("got %s != exp %s without modifications", got, exp)
67 }
68 }
69
70 {
71 v := V2_7_0()
72 v.SetMaxKeyVersion(4, -1)
73 v.SetMaxKeyVersion(5, -1)
74 v.SetMaxKeyVersion(6, -1)
75 v.SetMaxKeyVersion(7, -1)
76
77 if got, exp := v.VersionGuess(), "v2.7"; got != exp {
78 t.Errorf("got %s != exp %s for v2.7 with 4,5,6,7 unset", got, exp)
79 }
80
81 if got, exp := v.VersionGuess(SkipKeys()), "unknown custom version"; got != exp {
82 t.Errorf("got %s != exp %s for v2.7 with 4,5,6,7 unset without skipping them", got, exp)
83 }
84 }
85 }
86
87 func TestEqual(t *testing.T) {
88 l := V2_7_0()
89 l.SetMaxKeyVersion(int16(len(l.k2v)+1), -1)
90
91 r := V2_7_0()
92
93 if !l.Equal(r) {
94 t.Errorf("unexpectedly not equal")
95 }
96
97 l.SetMaxKeyVersion(0, -1)
98 if l.Equal(r) {
99 t.Errorf("unexpectedly equal after unsetting produce in left")
100 }
101
102 r.SetMaxKeyVersion(0, -1)
103 if !l.Equal(r) {
104 t.Errorf("unexpectedly not equal after unsetting produce in both")
105 }
106
107 l = V0_8_0()
108 r = V0_8_1()
109 if l.Equal(r) {
110 t.Errorf("unexpectedly equal v0.8.0 to v0.8.1")
111 }
112
113 r.SetMaxKeyVersion(8, -1)
114 r.SetMaxKeyVersion(9, -1)
115 if !l.Equal(r) {
116 t.Errorf("unexpectedly not equal after backing v0.8.1 down to v0.8.0")
117 }
118 if !r.Equal(l) {
119 t.Errorf("unexpectedly not equal after backing v0.8.1 down to v0.8.0, opposite direction")
120 }
121 }
122
123 func TestVersionProbeKafka3_1(t *testing.T) {
124 versions := map[int16]int16{
125 0: 9,
126 1: 13,
127 2: 7,
128 3: 12,
129 4: 5,
130 5: 3,
131 6: 7,
132 7: 3,
133 8: 8,
134 9: 8,
135 10: 4,
136 11: 7,
137 12: 4,
138 13: 4,
139 14: 5,
140 15: 5,
141 16: 4,
142 17: 1,
143 18: 3,
144 19: 7,
145 20: 6,
146 21: 2,
147 22: 4,
148 23: 4,
149 24: 3,
150 25: 3,
151 26: 3,
152 27: 1,
153 28: 3,
154 29: 2,
155 30: 2,
156 31: 2,
157 32: 4,
158 33: 2,
159 34: 2,
160 35: 2,
161 36: 2,
162 37: 3,
163 38: 2,
164 39: 2,
165 40: 2,
166 41: 2,
167 42: 2,
168 43: 2,
169 44: 1,
170 45: 0,
171 46: 0,
172 47: 0,
173 48: 1,
174 49: 1,
175 50: 0,
176 51: 0,
177 56: 0,
178 57: 0,
179 60: 0,
180 61: 0,
181 65: 0,
182 66: 0,
183 67: 0,
184 }
185
186 var vs Versions
187 for k, v := range versions {
188 vs.SetMaxKeyVersion(k, v)
189 }
190 if guess := vs.VersionGuess(); guess != "v3.1" {
191 t.Errorf("unexpected version guess, got %s != exp %s", guess, "v3.1")
192 }
193 }
194
View as plain text