-
Notifications
You must be signed in to change notification settings - Fork 6
/
caller_test.go
182 lines (165 loc) · 4.62 KB
/
caller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"context"
"log"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/m-lab/tcp-info/eventsocket"
)
type strFlag struct {
flag string
value string
}
var (
testDir string
sockPath string
)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func TestMain(m *testing.M) {
var err error
// testing.M does not have a TempDir() method.
testDir, err = os.MkdirTemp("", "test-directory")
if err != nil {
log.Fatalf("failed to create test directory (error: %v)", err)
}
defer os.RemoveAll(testDir)
sockPath = filepath.Join(testDir, "events.sock")
os.Exit(m.Run())
}
// TestMainFunc tests that main() succeeds to create a triggertrace
// handler and establish a connection with the eventsocket server.
func TestMainFunc(t *testing.T) {
saveOSArgs := os.Args
logFatal = func(args ...interface{}) { panic(args[0]) }
defer func() {
if r := recover(); r != nil {
t.Errorf("main() = %v, want nil", r)
}
logFatal = log.Fatal
os.Args = saveOSArgs
}()
srv := eventsocket.New(sockPath)
if err := srv.Listen(); err != nil {
t.Fatalf("failed to start the empty server (error: %v)", err)
}
ctx, cancel = context.WithCancel(context.Background())
srvDone := make(chan struct{})
go func(t *testing.T) {
if err := srv.Serve(ctx); err != nil && ctx.Err() != context.Canceled {
t.Logf("failed to start eventsocket server (error: %v, ctx.Err(): %v)", err, ctx.Err())
}
close(srvDone)
}(t)
// Cancel the server's context after one second.
go func() {
time.Sleep(1 * time.Second)
cancel()
}()
for _, arg := range []strFlag{
{"-scamper.bin", "/bin/echo"},
{"-scamper.trace-type", "mda"},
{"-scamper.waitprobe", "15"},
{"-prometheusx.listen-address", ":0"},
{"-tcpinfo.eventsocket", sockPath},
{"-traceroute-output", testDir},
{"-hopannotation-output", testDir},
} {
os.Args = append(os.Args, arg.flag, arg.value)
}
main()
select {
case <-srvDone:
// Since the server's context was cancelled after one second,
// two seconds should be long enough for the server to have stopped.
case <-time.After(2 * time.Second):
t.Errorf("eventsocket server goroutine still running")
}
}
// TestMainEventSocket tests that main() fails when a path to tcp-info's
// event socket is not provided.
func TestMainEventSocket(t *testing.T) {
saveOSArgs := os.Args
logFatal = func(args ...interface{}) { panic(args[0]) }
defer func() {
r := recover()
checkError(t, r, errEventSocket)
logFatal = log.Fatal
os.Args = saveOSArgs
}()
ctx, cancel = context.WithCancel(context.Background())
for _, arg := range []strFlag{
{"-scamper.bin", "/bin/echo"},
{"-scamper.trace-type", "mda"},
{"-scamper.waitprobe", "15"},
{"-prometheusx.listen-address", ":0"},
{"-tcpinfo.eventsocket", ""}, // should cause failure
{"-traceroute-output", testDir},
{"-hopannotation-output", testDir},
} {
os.Args = append(os.Args, arg.flag, arg.value)
}
main()
}
// TestMainScamper tests that main() fails when scamper configuration
// isn't valid.
func TestMainScamper(t *testing.T) {
saveOSArgs := os.Args
logFatal = func(args ...interface{}) { panic(args[0]) }
defer func() {
r := recover()
checkError(t, r, errScamper)
logFatal = log.Fatal
os.Args = saveOSArgs
}()
ctx, cancel = context.WithCancel(context.Background())
for _, arg := range []strFlag{
{"-scamper.bin", "/bin/echo"},
{"-scamper.waitprobe", "10"}, // should cause failure (15 <= valid <= 200)
{"-prometheusx.listen-address", ":0"},
{"-tcpinfo.eventsocket", sockPath},
{"-traceroute-output", testDir},
{"-hopannotation-output", testDir},
} {
os.Args = append(os.Args, arg.flag, arg.value)
}
main()
}
// TestMainNewHandler tests that main() fails when hop annotation
// configuration for creating a new handler is invalid.
func TestMainNewHandler(t *testing.T) {
saveOSArgs := os.Args
logFatal = func(args ...interface{}) { panic(args[0]) }
defer func() {
r := recover()
checkError(t, r, errNewHandler)
logFatal = log.Fatal
os.Args = saveOSArgs
}()
ctx, cancel = context.WithCancel(context.Background())
for _, arg := range []strFlag{
{"-scamper.bin", "/bin/echo"},
{"-scamper.trace-type", "mda"},
{"-scamper.waitprobe", "15"},
{"-prometheusx.listen-address", ":0"},
{"-tcpinfo.eventsocket", sockPath},
{"-traceroute-output", testDir},
{"-hopannotation-output", ""}, // should cause failure
} {
os.Args = append(os.Args, arg.flag, arg.value)
}
main()
}
func checkError(t *testing.T, r interface{}, want error) {
t.Helper()
if r == nil {
t.Errorf("main() = nil, want %v", want)
}
if got := r.(error); !strings.Contains(got.Error(), want.Error()) {
t.Errorf("main() = %v, want %v", got, want)
}
}