This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
forked from mdlayher/devlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_linux_test.go
178 lines (158 loc) · 3.52 KB
/
client_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
//+build linux
package devlink
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mdlayher/genetlink"
"github.com/mdlayher/genetlink/genltest"
"github.com/mdlayher/netlink"
"github.com/mdlayher/netlink/nlenc"
"github.com/mdlayher/netlink/nltest"
"golang.org/x/sys/unix"
)
func TestLinuxClientEmptyResponse(t *testing.T) {
tests := []struct {
name string
fn func(t *testing.T, c *client)
msgs []genetlink.Message
}{
{
name: "devices",
fn: func(t *testing.T, c *client) {
devices, err := c.Devices()
if err != nil {
t.Fatalf("failed to get devices: %v", err)
}
if diff := cmp.Diff(0, len(devices)); diff != "" {
t.Fatalf("unexpected number of devices (-want +got):\n%s", diff)
}
},
},
{
name: "ports",
fn: func(t *testing.T, c *client) {
ports, err := c.Ports()
if err != nil {
t.Fatalf("failed to get ports: %v", err)
}
if diff := cmp.Diff(0, len(ports)); diff != "" {
t.Fatalf("unexpected number of ports (-want +got):\n%s", diff)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := testClient(t, func(_ genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) {
return tt.msgs, nil
})
defer c.Close()
tt.fn(t, c)
})
}
}
func TestLinuxClientDevicesOK(t *testing.T) {
const (
bus = "pci"
deviceA = "0000:01:00.0"
deviceB = "0000:02:00.0"
)
tests := []struct {
name string
msgs []genetlink.Message
devices []*Device
}{
{
name: "one",
msgs: []genetlink.Message{{
Data: nltest.MustMarshalAttributes([]netlink.Attribute{
{
Type: unix.DEVLINK_ATTR_BUS_NAME,
Data: nlenc.Bytes(bus),
},
{
Type: unix.DEVLINK_ATTR_DEV_NAME,
Data: nlenc.Bytes(deviceA),
},
}),
}},
devices: []*Device{{
Bus: bus,
Device: deviceA,
}},
},
{
name: "multiple",
msgs: []genetlink.Message{
{
Data: nltest.MustMarshalAttributes([]netlink.Attribute{
{
Type: unix.DEVLINK_ATTR_BUS_NAME,
Data: nlenc.Bytes(bus),
},
{
Type: unix.DEVLINK_ATTR_DEV_NAME,
Data: nlenc.Bytes(deviceA),
},
}),
},
{
Data: nltest.MustMarshalAttributes([]netlink.Attribute{
{
Type: unix.DEVLINK_ATTR_BUS_NAME,
Data: nlenc.Bytes(bus),
},
{
Type: unix.DEVLINK_ATTR_DEV_NAME,
Data: nlenc.Bytes(deviceB),
},
}),
},
},
devices: []*Device{
{
Bus: bus,
Device: deviceA,
},
{
Bus: bus,
Device: deviceB,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
const (
cmd = unix.DEVLINK_CMD_GET
flags = netlink.Request | netlink.Dump
)
fn := func(_ genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) {
return tt.msgs, nil
}
c := testClient(t, genltest.CheckRequest(familyID, cmd, flags, fn))
defer c.Close()
devices, err := c.Devices()
if err != nil {
t.Fatalf("failed to get devices: %v", err)
}
if diff := cmp.Diff(tt.devices, devices); diff != "" {
t.Fatalf("unexpected devices (-want +got):\n%s", diff)
}
})
}
}
const familyID = 20
func testClient(t *testing.T, fn genltest.Func) *client {
family := genetlink.Family{
ID: familyID,
Version: unix.DEVLINK_GENL_VERSION,
Name: unix.DEVLINK_GENL_NAME,
}
conn := genltest.Dial(genltest.ServeFamily(family, fn))
c, err := initClient(conn)
if err != nil {
t.Fatalf("failed to open client: %v", err)
}
return c
}