-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
120 lines (111 loc) · 3.36 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
120
package main
import (
"context"
"net"
"os"
"reflect"
"testing"
"time"
"github.com/m-lab/go/rtx"
"github.com/m-lab/tcp-info/eventsocket"
"github.com/m-lab/uuid-annotator/ipservice"
)
func TestMainSmokeTest(t *testing.T) {
tests := []struct {
name string
value string
}{
{
name: "hostname-literal",
value: "mlab1-lga03.mlab-sandbox.measurement-lab.org",
},
{
name: "hostname-file",
value: "@./testdata/hostname",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set up the local HD.
dir, err := os.MkdirTemp("", "TestMain")
rtx.Must(err, "Could not create tempdir")
defer os.RemoveAll(dir)
// We need two contexts, one for the test and one for main. We need the test
// context to outlive main's context, because we don't want the cancellation
// of main to cause the cancellation of the tcpinfo.eventsocket, because
// then the shutdown of main() will race the shutdown of the local
// tcpinfo.eventsocket.Server, and when the tcpinfo.eventsocket.Server shuts
// down first then main (correctly) exits with an error and the test fails.
testCtx, testCancel := context.WithCancel(context.Background())
defer testCancel()
// Set up global variables.
mainCtx, mainCancel = context.WithCancel(testCtx)
mainRunning = make(chan struct{}, 1)
*eventsocket.Filename = dir + "/eventsocket.sock"
*ipservice.SocketFilename = dir + "/ipannotator.sock"
rtx.Must(maxmindurl.Set("file:./testdata/fake.tar.gz"), "Failed to set maxmind url for testing")
rtx.Must(routeviewv4.Set("file:./testdata/RouteViewIPv4.tiny.gz"), "Failed to set routeview v4 url for testing")
rtx.Must(routeviewv6.Set("file:./testdata/RouteViewIPv6.tiny.gz"), "Failed to set routeview v6 url for testing")
rtx.Must(asnameurl.Set("file:./data/asnames.ipinfo.csv"), "Failed to set ipinfo ASName url for testing")
rtx.Must(siteinfo.Set("file:./testdata/annotations.json"), "Failed to set siteinfo annotations url for testing")
os.Setenv("HOSTNAME", tt.value)
// Now start up a fake eventsocket.
srv := eventsocket.New(*eventsocket.Filename)
rtx.Must(srv.Listen(), "Could not listen")
go srv.Serve(testCtx)
// Cancel main after main has been running all its goroutines for half a
// second.
go func() {
<-mainRunning
// Give all of main's goroutines a little time to start now that we know
// main() has started them all.
time.Sleep(500 * time.Millisecond)
mainCancel()
}()
// Run main. Full coverage, no crash, and return == success!
main()
})
}
}
func Test_findLocalIPs(t *testing.T) {
tests := []struct {
name string
local []net.Addr
want []net.IP
}{
{
name: "success",
local: []net.Addr{
&net.IPNet{
IP: net.ParseIP("127.0.0.1"),
Mask: net.CIDRMask(24, 32),
},
&net.IPNet{
IP: net.ParseIP("2001:1900:2100:2d::75"),
Mask: net.CIDRMask(64, 128),
},
},
want: []net.IP{
net.ParseIP("127.0.0.1"),
net.ParseIP("2001:1900:2100:2d::75"),
},
},
{
name: "skip-all-return-empty",
local: []net.Addr{
&net.UnixAddr{
Name: "fake-unix",
Net: "unix",
},
},
want: []net.IP{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := findLocalIPs(tt.local); !reflect.DeepEqual(got, tt.want) {
t.Errorf("findLocalIPs() = %v, want %v", got, tt.want)
}
})
}
}