Skip to content

Commit 0b95298

Browse files
authored
Merge pull request #32 from Icinga/test-utils
Test utils/
2 parents 0d0428f + 0f80651 commit 0b95298

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

utils/utils_test.go

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,202 @@
11
package utils
22

33
import (
4+
"context"
5+
"encoding/hex"
6+
"fmt"
7+
"github.com/go-sql-driver/mysql"
8+
"github.com/lib/pq"
49
"github.com/stretchr/testify/assert"
510
"github.com/stretchr/testify/require"
611
"testing"
12+
"time"
713
)
814

15+
func TestBatchSliceOfStrings(t *testing.T) {
16+
subtests := []struct {
17+
name string
18+
keys []string
19+
count int
20+
output [][]string
21+
}{
22+
{"nil", nil, 1, nil},
23+
{"empty", make([]string, 0, 1), 1, nil},
24+
{"a", []string{"a"}, 1, [][]string{{"a"}}},
25+
{"a2", []string{"a"}, 2, [][]string{{"a"}}},
26+
{"a_b", []string{"a", "b"}, 1, [][]string{{"a"}, {"b"}}},
27+
{"ab", []string{"a", "b"}, 2, [][]string{{"a", "b"}}},
28+
{"ab3", []string{"a", "b"}, 3, [][]string{{"a", "b"}}},
29+
{"a_b_c", []string{"a", "b", "c"}, 1, [][]string{{"a"}, {"b"}, {"c"}}},
30+
{"ab_c", []string{"a", "b", "c"}, 2, [][]string{{"a", "b"}, {"c"}}},
31+
{"abc", []string{"a", "b", "c"}, 3, [][]string{{"a", "b", "c"}}},
32+
{"abc4", []string{"a", "b", "c"}, 4, [][]string{{"a", "b", "c"}}},
33+
}
34+
35+
for _, st := range subtests {
36+
t.Run(st.name, func(t *testing.T) {
37+
batches := BatchSliceOfStrings(context.Background(), st.keys, st.count)
38+
require.NotNil(t, batches)
39+
40+
for _, expected := range st.output {
41+
select {
42+
case actual, ok := <-batches:
43+
require.True(t, ok, "receiving should return a value")
44+
require.Equal(t, expected, actual)
45+
case <-time.After(10 * time.Millisecond):
46+
require.Fail(t, "receiving should not block")
47+
}
48+
}
49+
50+
select {
51+
case _, ok := <-batches:
52+
require.False(t, ok, "receiving from channel should not return anything")
53+
case <-time.After(10 * time.Millisecond):
54+
require.Fail(t, "receiving should not block")
55+
}
56+
})
57+
}
58+
59+
for _, i := range []int{0, -1, -2, -30} {
60+
t.Run(fmt.Sprint(i), func(t *testing.T) {
61+
require.Panics(t, func() { BatchSliceOfStrings(context.Background(), nil, i) })
62+
})
63+
}
64+
}
65+
66+
func TestChecksum(t *testing.T) {
67+
subtests := []struct {
68+
name string
69+
input any
70+
output string
71+
}{
72+
{"empty_string", "", "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
73+
{"empty_bytes", []byte(nil), "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
74+
{"space_string", " ", "b858cb282617fb0956d960215c8e84d1ccf909c6"},
75+
{"space_bytes", []byte(" "), "b858cb282617fb0956d960215c8e84d1ccf909c6"},
76+
}
77+
78+
for _, st := range subtests {
79+
t.Run(st.name, func(t *testing.T) {
80+
require.Equal(t, st.output, hex.EncodeToString(Checksum(st.input)))
81+
})
82+
}
83+
84+
unsupported := []struct {
85+
name string
86+
input any
87+
}{
88+
{"nil", nil},
89+
{"bool", false},
90+
{"int", 0},
91+
{"float", 0.0},
92+
{"struct", struct{}{}},
93+
{"slice", []string{}},
94+
{"map", map[string]string{}},
95+
}
96+
97+
for _, st := range unsupported {
98+
t.Run(st.name, func(t *testing.T) {
99+
require.Panics(t, func() { Checksum(st.input) })
100+
})
101+
}
102+
}
103+
104+
func TestIsDeadlock(t *testing.T) {
105+
msg := "Unsuccessful attempt of confusing the tested code."
106+
code := [5]byte{0, 23, 42, 77, 255}
107+
108+
subtests := []struct {
109+
name string
110+
input error
111+
output bool
112+
}{
113+
{"nil", nil, false},
114+
{"deadline", context.DeadlineExceeded, false},
115+
{"mysql1204", &mysql.MySQLError{Number: 1204}, false},
116+
{"mysql1205", &mysql.MySQLError{Number: 1205}, true},
117+
{"mysql1205_with_crap", &mysql.MySQLError{Number: 1205, SQLState: code, Message: msg}, true},
118+
{"mysql1206", &mysql.MySQLError{Number: 1206}, false},
119+
{"mysql1212", &mysql.MySQLError{Number: 1212}, false},
120+
{"mysql1213", &mysql.MySQLError{Number: 1213}, true},
121+
{"mysql1213_with_crap", &mysql.MySQLError{Number: 1213, SQLState: code, Message: msg}, true},
122+
{"mysql1214", &mysql.MySQLError{Number: 1214}, false},
123+
{"postgres40000", &pq.Error{Code: "40000"}, false},
124+
{"postgres40001", &pq.Error{Code: "40001"}, true},
125+
{"postgres40001_with_crap", &pq.Error{Code: "40001", Message: msg}, true},
126+
{"postgres40002", &pq.Error{Code: "40002"}, false},
127+
{"postgres40P01", &pq.Error{Code: "40P01"}, true},
128+
{"postgres40P01_with_crap", &pq.Error{Code: "40P01", Message: msg}, true},
129+
}
130+
131+
for _, st := range subtests {
132+
t.Run(st.name, func(t *testing.T) {
133+
require.Equal(t, st.output, IsDeadlock(st.input))
134+
})
135+
}
136+
}
137+
138+
func TestEllipsize(t *testing.T) {
139+
subtests := []struct {
140+
name string
141+
s string
142+
limit int
143+
output string
144+
}{
145+
{"negative", "", -1, "..."},
146+
{"empty", "", 0, ""},
147+
{"shorter", " ", 2, " "},
148+
{"equal", " ", 1, " "},
149+
{"longer", " ", 0, "..."},
150+
{"unicode", "äöü߀", 4, "ä..."},
151+
}
152+
153+
for _, st := range subtests {
154+
t.Run(st.name, func(t *testing.T) {
155+
require.Equal(t, st.output, Ellipsize(st.s, st.limit))
156+
})
157+
}
158+
}
159+
160+
func TestIsUnixAddr(t *testing.T) {
161+
subtests := []struct {
162+
name string
163+
input string
164+
output bool
165+
}{
166+
{"empty", "", false},
167+
{"slash", "/", true},
168+
{"unix", "/tmp/sock", true},
169+
{"ipv4", "192.0.2.1", false},
170+
{"ipv6", "2001:db8::", false},
171+
}
172+
173+
for _, st := range subtests {
174+
t.Run(st.name, func(t *testing.T) {
175+
require.Equal(t, st.output, IsUnixAddr(st.input))
176+
})
177+
}
178+
}
179+
180+
func TestJoinHostPort(t *testing.T) {
181+
subtests := []struct {
182+
name string
183+
host string
184+
port int
185+
output string
186+
}{
187+
{"empty", "", 0, ":0"},
188+
{"ipv4", "192.0.2.1", 80, "192.0.2.1:80"},
189+
{"ipv6", "2001:db8::", 443, "[2001:db8::]:443"},
190+
{"unix", "/tmp/sock", 5665, "/tmp/sock"},
191+
}
192+
193+
for _, st := range subtests {
194+
t.Run(st.name, func(t *testing.T) {
195+
require.Equal(t, st.output, JoinHostPort(st.host, st.port))
196+
})
197+
}
198+
}
199+
9200
func TestChanFromSlice(t *testing.T) {
10201
t.Run("Nil", func(t *testing.T) {
11202
ch := ChanFromSlice[int](nil)

0 commit comments

Comments
 (0)