-
Notifications
You must be signed in to change notification settings - Fork 14
/
main_test.go
87 lines (81 loc) · 1.45 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
package main
import (
"bytes"
"net"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/nakabonne/gosivy/diagnoser"
)
func TestRun(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
tests := []struct {
name string
cli cli
args []string
want int
}{
{
name: "emit version",
cli: cli{
version: true,
},
want: 0,
},
{
name: "invalid option",
cli: cli{
scrapeInterval: time.Microsecond,
},
want: 1,
},
{
name: "run with remote addr",
cli: cli{
scrapeInterval: time.Second,
},
args: []string{"localhost:8080"},
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := new(bytes.Buffer)
tt.cli.stderr = b
tt.cli.stdout = b
m := diagnoser.NewMockDiagnoser(ctrl)
m.EXPECT().Run().AnyTimes()
tt.cli.diagnoser = m
got := tt.cli.run(tt.args)
assert.Equal(t, tt.want, got)
})
}
}
func TestTargetToAddr(t *testing.T) {
tests := []struct {
name string
target string
want *net.TCPAddr
wantErr bool
}{
{
name: "remote mode",
target: "localhost:8080",
want: &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 8080,
Zone: "",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := targetToAddr(tt.target)
assert.Equal(t, tt.wantErr, err != nil)
assert.Equal(t, tt.want, got)
})
}
}