-
Notifications
You must be signed in to change notification settings - Fork 9
/
tester_test.go
156 lines (142 loc) · 2.87 KB
/
tester_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
package ipset
import (
"fmt"
"os"
"os/exec"
"testing"
)
var (
needError bool
flag = struct{}{}
)
func fakeExecCommand(command string, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
if needError {
cmd.Env = append(cmd.Env, "GO_WANT_HELPER_NEED_ERR=1")
}
return cmd
}
func TestHelperProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
args = args[1:]
break
}
args = args[1:]
}
if len(args) == 0 {
_, _ = fmt.Fprintf(os.Stderr, "No command")
os.Exit(2)
}
if os.Getenv("GO_WANT_HELPER_NEED_ERR") == "1" {
_, _ = fmt.Fprintf(os.Stderr, "fake error")
os.Exit(1)
}
if len(args) > 1 {
switch args[1] {
case _version:
if args[0] == "non-supported" {
_, _ = fmt.Fprintf(os.Stdout, invalidVersion)
} else {
_, _ = fmt.Fprintf(os.Stdout, validVersion)
}
case _list:
if findOption(args, "-resolve") {
_, _ = fmt.Fprintf(os.Stdout, listInfoResolved)
} else {
_, _ = fmt.Fprintf(os.Stdout, listInfo)
}
case _save:
if findOption(args, "-resolve") {
_, _ = fmt.Fprintf(os.Stdout, saveInfoResolved)
} else {
_, _ = fmt.Fprintf(os.Stdout, saveInfo)
}
case _test:
if len(args) > 3 && args[3] == testNotExistIp {
_, _ = fmt.Fprintf(os.Stderr, "1.1.1.2 is NOT in set foo.")
os.Exit(1)
}
}
}
os.Exit(0)
}
func findOption(args []string, target string) bool {
for _, arg := range args {
if arg == target {
return true
}
}
return false
}
func setupCmd(flag ...struct{}) {
execCommand = fakeExecCommand
if len(flag) > 0 {
needError = true
}
}
func teardownCmd() {
execCommand = exec.Command
needError = false
}
func setupLookPath(filename ...string) {
fn := ""
if len(filename) > 0 {
fn = filename[0]
}
execLookPath = func(f string) (s string, err error) {
switch fn {
case "error":
err = fmt.Errorf("path not exist")
case "":
s = f
default:
s = fn
}
return
}
}
func teardownLookPath() {
execLookPath = exec.LookPath
ipsetPath = ""
}
const (
validVersion = "ipset v6.29, protocol version: 6"
invalidVersion = "ipset v5.1, protocol version: 5"
listInfo = `
Name: foo
Type: hash:ip
Revision: 4
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 168
References: 0
Number of entries: 1
Members:
1.1.1.1`
listInfoResolved = `
Name: foo
Type: hash:ip
Revision: 4
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 168
References: 0
Number of entries: 1
Members:
one.one.one.one`
saveInfo = `
create foo hash:ip family inet hashsize 1024 maxelem 65536
add foo 1.1.1.1
`
saveInfoResolved = `
create foo hash:ip family inet hashsize 1024 maxelem 65536
add foo one.one.one.one
`
testNotExistIp = "1.1.1.2"
)