-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathtenus_linux_test.go
235 lines (191 loc) · 4.55 KB
/
tenus_linux_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package tenus
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
type testEnv struct {
createCmds []*exec.Cmd
setupCmds []*exec.Cmd
tearDownCmds []*exec.Cmd
}
func (te *testEnv) create() error {
for _, cmd := range te.createCmds {
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
func (te *testEnv) setup() error {
for _, cmd := range te.setupCmds {
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
func (te *testEnv) teardown() error {
for _, cmd := range te.tearDownCmds {
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
type testLink struct {
testEnv
name string
linkType string
}
func (tl *testLink) prepTestLink(name, linkType string) error {
if os.Getuid() != 0 {
return errors.New("skipping test; must be root")
}
tl.name = name
tl.linkType = linkType
xpath, err := exec.LookPath("ip")
if err != nil {
return err
}
tl.createCmds = append(tl.createCmds, &exec.Cmd{
Path: xpath,
Args: []string{"ip", "link", "add", tl.name, "type", tl.linkType},
})
tl.tearDownCmds = append(tl.tearDownCmds, &exec.Cmd{
Path: xpath,
Args: []string{"ip", "link", "del", tl.name},
})
return nil
}
func (tl *testLink) prepLinkOpts(opts LinkOptions) error {
if os.Getuid() != 0 {
return errors.New("skipping test; must be root")
}
macaddr := opts.MacAddr
mtu := strconv.Itoa(opts.MTU)
flags := opts.Flags
xpath, err := exec.LookPath("ip")
if err != nil {
return err
}
if macaddr != "" {
tl.setupCmds = append(tl.setupCmds, &exec.Cmd{
Path: xpath,
Args: []string{"ip", "link", "set", "dev", tl.name, "address", macaddr},
})
}
if mtu != "" {
tl.setupCmds = append(tl.setupCmds, &exec.Cmd{
Path: xpath,
Args: []string{"ip", "link", "set", "dev", tl.name, "mtu", mtu},
})
}
if (flags & syscall.IFF_UP) == syscall.IFF_UP {
tl.setupCmds = append(tl.setupCmds, &exec.Cmd{
Path: xpath,
Args: []string{"ip", "link", "set", "dev", tl.name, "up"},
})
}
return nil
}
type testDocker struct {
testEnv
name string
command string
}
func (td *testDocker) prepTestDocker(name, command string) error {
if os.Getuid() != 0 {
return errors.New("skipping test; must be root")
}
td.name = name
td.command = command
xpath, err := exec.LookPath("docker")
if err != nil {
return err
}
td.createCmds = append(td.createCmds, &exec.Cmd{
Path: xpath,
Args: []string{"docker", "run", "-t", "-d", "--name", td.name, "ubuntu", td.command},
})
td.tearDownCmds = append(td.tearDownCmds, &exec.Cmd{
Path: xpath,
Args: []string{"docker", "stop", td.name},
})
td.tearDownCmds = append(td.tearDownCmds, &exec.Cmd{
Path: xpath,
Args: []string{"docker", "rm", td.name},
})
return nil
}
type testLinkInfo struct {
linkType string
linkData string
}
var testLinkInfoData = map[string]func([]string) (string, error){
"macvlan": macvlanInfo,
"vlan": vlanInfo,
"macvtap": macvtapInfo,
}
func macvlanInfo(data []string) (string, error) {
if len(data) < 3 {
return "", fmt.Errorf("Unable to parse macvlan result")
}
return data[2], nil
}
func vlanInfo(data []string) (string, error) {
if len(data) < 5 {
return "", fmt.Errorf("Unable to parse vlan result")
}
return data[4], nil
}
func macvtapInfo(data []string) (string, error) {
// At the moment this should work fine
// If we add more tests in the future we will modify this function
return macvlanInfo(data)
}
func linkInfo(name, linkType string) (*testLinkInfo, error) {
ipPath, err := exec.LookPath("ip")
if err != nil {
return nil, fmt.Errorf("Unable to find ip in PATH: %s", err)
}
list := exec.Command(ipPath, "-d", "link", "show", name)
res := exec.Command("tail", "-1")
var pipeErr error
var out bytes.Buffer
res.Stdin, pipeErr = list.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("Unable start UNIX pipe: %s", pipeErr)
}
res.Stdout = &out
if err := res.Start(); err != nil {
return nil, fmt.Errorf("Unable to tail the result: %s", err)
}
if err := list.Run(); err != nil {
return nil, fmt.Errorf("Unable to retrieve interface information: %s", err)
}
if err := res.Wait(); err != nil {
return nil, fmt.Errorf("Could not read UNIX pipe data: %s", err)
}
data := strings.Fields(strings.TrimSpace(out.String()))
linkInfoFunc, ok := testLinkInfoData[linkType]
if !ok {
return &testLinkInfo{
linkType: data[0],
linkData: "",
}, nil
}
linkData, err := linkInfoFunc(data)
if err != nil {
return nil, fmt.Errorf("Could not read Link Info: %s", err)
}
return &testLinkInfo{
linkType: data[0],
linkData: linkData,
}, nil
}