-
Notifications
You must be signed in to change notification settings - Fork 61
/
link_linux_test.go
146 lines (123 loc) · 3.62 KB
/
link_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
package tenus
import (
"net"
"testing"
"time"
)
func Test_NewLink(t *testing.T) {
testLinks := []string{"ifc01", "ifc02", "ifc03"}
for _, tt := range testLinks {
tl := &testLink{}
if err := tl.prepTestLink(tt, "dummy"); err != nil {
t.Skipf("NewLink test requries external command: %v", err)
}
_, err := NewLink(tt)
if err != nil {
t.Fatalf("NewLink(%s) failed to run: %s", tt, err)
}
if _, err := net.InterfaceByName(tt); err != nil {
tl.teardown()
t.Fatalf("Could not find %s on the host: %s", tt, err)
}
if err := tl.teardown(); err != nil {
t.Fatalf("testLink.teardown failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}
}
}
type ifcLinkOptsTest struct {
testLink
opts LinkOptions
expected *net.Interface
}
var hwaddr, _ = net.ParseMAC("22:ce:e0:99:63:6f")
var ifcLinkOptsTests = []ifcLinkOptsTest{
{testLink{name: "ifc01"},
LinkOptions{MacAddr: "22:ce:e0:99:63:6f", MTU: 1400, Flags: net.FlagUp},
&net.Interface{Name: "ifc01", MTU: 1400, Flags: net.FlagUp, HardwareAddr: hwaddr}},
{testLink{name: "ifc02"},
LinkOptions{},
&net.Interface{Name: "ifc01"}},
{testLink{name: "ifc03"},
LinkOptions{MTU: -100},
nil},
}
func Test_NewLinkWithOptions(t *testing.T) {
for _, tt := range ifcLinkOptsTests {
tl := &testLink{}
if err := tl.prepTestLink(tt.name, "dummy"); err != nil {
t.Skipf("NewLinkWithOptions test requries external command: %v", err)
}
_, err := NewLinkWithOptions(tt.name, tt.opts)
if err != nil && tt.expected != nil {
t.Fatalf("NewLinkWithOptions(%s, %v) failed to run: %s", tt.name, tt.opts, err)
}
ifc, err := net.InterfaceByName(tt.name)
if err != nil && tt.expected != nil {
tl.teardown()
t.Fatalf("Could not find %s on the host: %s", tt.name, err)
}
if tt.expected != nil {
if ifc.Name != tt.name {
tl.teardown()
t.Fatalf("NewLinkWithOptions(%s, %v) failed: expected %s, returned: %s",
tt.name, tt.opts, tt.name, ifc.Name)
}
if tt.opts.MacAddr != "" {
if ifc.HardwareAddr.String() != tt.opts.MacAddr {
tl.teardown()
t.Fatalf("NewLinkWithOptions(%s, %v) failed: expected %s, returned: %s",
tt.name, tt.opts, tt.opts.MacAddr, ifc.HardwareAddr.String())
}
}
if tt.opts.MTU != 0 {
if ifc.MTU != tt.opts.MTU {
tl.teardown()
t.Fatalf("NewLinkWithOptions(%s, %v) failed: expected %d, returned: %d",
tt.name, tt.opts, tt.opts.MTU, ifc.MTU)
}
}
if tt.opts.Flags != 0 {
if (ifc.Flags & tt.opts.Flags) != tt.opts.Flags {
tl.teardown()
t.Fatalf("NewLinkWithOptions(%s, %v) failed: expected %v, returned: %v",
tt.name, tt.opts, tt.opts.Flags, ifc.Flags)
}
}
if err := tl.teardown(); err != nil {
t.Fatalf("testIfcLink.teardown failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}
}
if tt.expected == nil && ifc != nil {
tl.teardown()
t.Fatalf("NewLinkWithOptions(%s, %v) failed. Expected: %v, Returned: %v",
tt.name, tt.opts, tt.expected, ifc)
}
}
}
func Test_DeleteLink(t *testing.T) {
testLinks := []string{"ifc01", "ifc02", "ifc03"}
for _, tt := range testLinks {
tl := &testLink{}
if err := tl.prepTestLink(tt, "dummy"); err != nil {
t.Skipf("DeleteLink test requries external command: %v", err)
}
if err := tl.create(); err != nil {
t.Fatalf("testLink.setup failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}
if err := DeleteLink(tt); err != nil {
tl.teardown()
t.Fatalf("Failed to delete %s interface: %s", tt, err)
}
i, _ := net.InterfaceByName(tt)
if i != nil {
tl.teardown()
t.Fatalf("DeleteLink(%s) expected: nil, returned: %v", tt, i)
}
}
}