-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
119 lines (101 loc) · 3.15 KB
/
main_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
package main
import (
"context"
"fmt"
"io/ioutil"
"net"
"os"
"syscall"
"testing"
"time"
"github.com/m-lab/go/flagx"
"github.com/m-lab/go/prometheusx"
"github.com/google/gopacket/pcap"
"github.com/m-lab/go/rtx"
"github.com/m-lab/tcp-info/eventsocket"
)
func fakePcapOpenLive(device string, snaplen int32, promisc bool, timeout time.Duration) (*pcap.Handle, error) {
return pcap.OpenOffline("testdata/v6.pcap")
}
func TestProcessFlags(t *testing.T) {
interfaces = flagx.StringArray{}
netInterfaces = func() ([]net.Interface, error) {
return nil, fmt.Errorf("Fake interfaces error")
}
defer func() {
// Reset function pointer.
netInterfaces = net.Interfaces
interfaces = flagx.StringArray{}
}()
_, err := processFlags()
if err == nil {
t.Fatalf("processFlags() return wrong error; got nil, want %q", err)
}
interfaces = flagx.StringArray{"lo"}
ifaces, err := processFlags()
if err != nil || len(ifaces) != 1 {
t.Fatalf("processFlags() did not get the loopback: %s, %+v", err, ifaces)
}
interfaces = flagx.StringArray{"doesnotexist"}
_, err = processFlags()
if err == nil {
t.Fatalf("processFlags() return wrong error; got nil")
}
// Artificially set uuid wait duration to be longer than capture duration.
*uuidWaitDuration = 2 * *captureDuration
defer func() {
*uuidWaitDuration = *captureDuration / 2
}()
_, err = processFlags()
if err == nil {
t.Fatalf("processFlags() return wrong error; got nil, want %q", err)
}
}
func TestMainSmokeTest(t *testing.T) {
mainCtx, mainCancel = context.WithCancel(context.Background())
*prometheusx.ListenAddress = ":0"
dir, err := ioutil.TempDir("", "TestMainSmokeTest")
rtx.Must(err, "Could not create temp dir")
defer os.RemoveAll(dir)
// Set up a tcpinfo service. Don't use mainCtx for it because if the socket
// goes away while main() is running then main() (correctly) crashes. We
// don't want the exit of main() to race with the termination of this
// server.
tcpiCtx, tcpiCancel := context.WithCancel(context.Background())
defer tcpiCancel()
*eventsocket.Filename = dir + "/tcpevents.sock"
tcpi := eventsocket.New(*eventsocket.Filename)
tcpi.Listen()
go tcpi.Serve(tcpiCtx)
// Wait until the eventsocket appears.
for _, err := os.Stat(*eventsocket.Filename); err != nil; _, err = os.Stat(*eventsocket.Filename) {
}
// Tests are unlikely to have enough privileges to open packet captures, so
// use a fake version that reads from one of our testfiles.
pcapOpenLive = fakePcapOpenLive
go func() {
time.Sleep(1)
mainCancel()
}()
// Listen on any port for metrics.
*prometheusx.ListenAddress = ":0"
main()
// No crash and successful termination == success
}
func TestSigtermHandlerOnCancel(t *testing.T) {
mainCtx, mainCancel = context.WithCancel(context.Background())
mainCancel()
catch(syscall.SIGUSR1)
// No freeze == success
}
func TestSigtermHandlerOnSignal(t *testing.T) {
// Test signal handling with the "window size change" signal.
mainCtx, mainCancel = context.WithCancel(context.Background())
defer mainCancel()
go func() {
time.Sleep(100 * time.Millisecond)
syscall.Kill(syscall.Getpid(), syscall.SIGWINCH)
}()
catch(syscall.SIGWINCH)
<-mainCtx.Done()
}