-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathvlan_linux_test.go
152 lines (124 loc) · 3.72 KB
/
vlan_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
package tenus
import (
"net"
"strconv"
"testing"
"time"
)
type vlnTest struct {
masterDev string
id uint16
}
var vlnTests = []vlnTest{
{"master01", 10},
{"master02", 20},
}
func Test_NewVlanLink(t *testing.T) {
for _, tt := range vlnTests {
tl := &testLink{}
if err := tl.prepTestLink(tt.masterDev, "dummy"); err != nil {
t.Skipf("NewVlanLink test requries external command: %v", err)
}
if err := tl.create(); err != nil {
t.Fatalf("testLink.create failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}
vln, err := NewVlanLink(tt.masterDev, tt.id)
if err != nil {
t.Fatalf("NewVlanLink(%s, %d) failed to run: %s", tt.masterDev, tt.id, err)
}
vlnName := vln.NetInterface().Name
if _, err := net.InterfaceByName(vlnName); err != nil {
tl.teardown()
t.Fatalf("Could not find %s on the host: %s", vlnName, err)
}
testRes, err := linkInfo(vlnName, "vlan")
if err != nil {
tl.teardown()
t.Fatalf("Failed to list %s operation mode: %s", vlnName, err)
}
if testRes.linkType != "vlan" {
tl.teardown()
t.Fatalf("NewVlanLink(%s, %d) failed: expected vlan, returned %s",
tt.masterDev, tt.id, testRes.linkType)
}
id, err := strconv.Atoi(testRes.linkData)
if err != nil {
tl.teardown()
t.Fatalf("Failed to convert link data %s : %s", testRes.linkData, err)
}
if uint16(id) != tt.id {
tl.teardown()
t.Fatalf("NewVlanLink(%s, %d) failed: expected %d, returned %d",
tt.masterDev, tt.id, tt.id, id)
}
if err := tl.teardown(); err != nil {
t.Fatalf("testLink.teardown failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}
}
}
type vlnWithOptionsTest struct {
masterDev string
opts *VlanOptions
}
var vlnWithOptionsTests = []vlnWithOptionsTest{
{"master01", &VlanOptions{Dev: "test", MacAddr: "aa:aa:aa:aa:aa:aa", Id: 10}},
{"master02", &VlanOptions{Dev: "test", MacAddr: "aa:aa:aa:aa:aa:aa", Id: 20}},
}
func Test_NewVlanLinkWithOptions(t *testing.T) {
for _, tt := range vlnWithOptionsTests {
var iface *net.Interface
tl := &testLink{}
if err := tl.prepTestLink(tt.masterDev, "dummy"); err != nil {
t.Skipf("NewVlanLinkWithOptions test requries external command: %v", err)
}
if err := tl.create(); err != nil {
t.Fatalf("testLink.create failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}
vln, err := NewVlanLinkWithOptions(tt.masterDev, *tt.opts)
if err != nil {
t.Fatalf("NewVlanLinkWithOptions(%s, %v) failed to run: %s", tt.masterDev, *tt.opts, err)
}
iface = vln.NetInterface()
if iface.HardwareAddr.String() != tt.opts.MacAddr {
tl.teardown()
t.Fatalf("NewVlanLinkWithOptions(%s, %v) failed: expected %s, returned %s",
tt.masterDev, *tt.opts, tt.opts.MacAddr, iface.HardwareAddr.String())
}
vlnName := vln.NetInterface().Name
if _, err := net.InterfaceByName(vlnName); err != nil {
tl.teardown()
t.Fatalf("Could not find %s on the host: %s", vlnName, err)
}
testRes, err := linkInfo(vlnName, "vlan")
if err != nil {
tl.teardown()
t.Fatalf("Failed to list %s operation mode: %s", vlnName, err)
}
if testRes.linkType != "vlan" {
tl.teardown()
t.Fatalf("NewVlanLinkWithOptions(%s, %v) failed: expected vlan, returned %s",
tt.masterDev, *tt.opts, testRes.linkType)
}
id, err := strconv.Atoi(testRes.linkData)
if err != nil {
tl.teardown()
t.Fatalf("Failed to convert link data %s : %s", testRes.linkData, err)
}
if uint16(id) != tt.opts.Id {
tl.teardown()
t.Fatalf("NewVlanLinkWithOptions(%s, %v) failed: expected %d, returned %d",
tt.masterDev, *tt.opts, tt.opts.Id, id)
}
if err := tl.teardown(); err != nil {
t.Fatalf("testLink.teardown failed: %v", err)
} else {
time.Sleep(10 * time.Millisecond)
}
}
}