Skip to content

Commit 5c1ec37

Browse files
committed
ci(coverage): add new tests for increasing coverage rate
1 parent 3300338 commit 5c1ec37

File tree

4 files changed

+145
-10
lines changed

4 files changed

+145
-10
lines changed

crawler/device_test.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package crawler
22

33
import (
4+
"context"
5+
"os"
46
"reflect"
57
"testing"
8+
"time"
69
)
710

811
func TestEventFromUEventData(t *testing.T) {
@@ -48,9 +51,59 @@ DEVNAME=vcs3`,
4851
}
4952

5053
for k, tcase := range testcases {
51-
evt := getEventFromUEventData([]byte(tcase.got))
54+
tmp, err := os.CreateTemp("", "uevent-file")
55+
if err != nil {
56+
t.Fatalf("Test %d failed, unable to create temp file for uevent data", k)
57+
}
58+
defer os.Remove(tmp.Name())
59+
60+
if _, err := tmp.WriteString(tcase.got); err != nil {
61+
t.Fatalf("Test %d failed, unable to append uevent data in temp file", k)
62+
}
63+
64+
evt, err := getEventFromUEventFile(tmp.Name())
65+
if err != nil {
66+
t.Fatalf("Test %d failed, unable to get event dfrom uevent file", k)
67+
}
68+
69+
if !reflect.DeepEqual(evt, getEventFromUEventData([]byte(tcase.got))) {
70+
t.Fatalf("Test %d failed, uevent from file or data must be equals", k)
71+
}
72+
5273
if !reflect.DeepEqual(evt, tcase.expected) {
5374
t.Fatalf("Test %d failed (got: %v, expected: %v)", k, evt, tcase.expected)
5475
}
5576
}
5677
}
78+
79+
func TestExistingDevices(t *testing.T) {
80+
queue := make(chan Device)
81+
errors := make(chan error)
82+
quit := ExistingDevices(queue, errors, nil)
83+
84+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
85+
86+
defer func() {
87+
close(quit)
88+
close(errors)
89+
cancel()
90+
}()
91+
92+
for {
93+
select {
94+
case <-ctx.Done():
95+
t.Fatalf("reach timeout while getting existing devices, err: %v", ctx.Err())
96+
quit <- struct{}{}
97+
case _, more := <-queue:
98+
if !more {
99+
t.Log("Finished processing existing devices")
100+
quit <- struct{}{}
101+
return // without error
102+
}
103+
// t.Logf("Detect device at %s with env %v", device.KObj, device.Env)
104+
case err := <-errors:
105+
t.Fatalf("unable to get existing devices, err: %v", err)
106+
quit <- struct{}{}
107+
}
108+
}
109+
}

netlink/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (c *UEventConn) msgPeek() (int, *[]byte, error) {
6363
buf := make([]byte, os.Getpagesize())
6464
for {
6565
// Just read how many bytes are available in the socket
66-
// Warning: syscall.MSG_PEEK is a blocking call
66+
// WARNING: syscall.MSG_PEEK is a blocking call (no testable from CI)
6767
if n, _, err = syscall.Recvfrom(c.Fd, buf, syscall.MSG_PEEK); err != nil {
6868
return n, &buf, err
6969
}

netlink/conn_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package netlink
22

33
import (
4+
"context"
45
"testing"
6+
"time"
57
)
68

79
func TestConnect(t *testing.T) {
@@ -13,6 +15,7 @@ func TestConnect(t *testing.T) {
1315
_ = conn.Close()
1416
}()
1517

18+
// ensure it is possible to subscribe several times in parallel
1619
conn2 := new(UEventConn)
1720
if err := conn2.Connect(UdevEvent); err != nil {
1821
t.Fatal("unable to subscribe to netlink uevent a second time, err:", err)
@@ -21,3 +24,39 @@ func TestConnect(t *testing.T) {
2124
_ = conn2.Close()
2225
}()
2326
}
27+
28+
func TestMonitor(t *testing.T) {
29+
conn := new(UEventConn)
30+
if err := conn.Connect(UdevEvent); err != nil {
31+
t.Fatal("unable to subscribe to netlink uevent, err:", err)
32+
}
33+
defer func() {
34+
_ = conn.Close()
35+
}()
36+
37+
queue := make(chan UEvent)
38+
errors := make(chan error)
39+
quit := conn.Monitor(queue, errors, nil)
40+
41+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // Monitor 5s then stop for testing
42+
defer func() {
43+
close(quit)
44+
close(errors)
45+
cancel()
46+
}()
47+
48+
loop:
49+
for {
50+
select {
51+
case <-ctx.Done():
52+
t.Log("Reach timeout while monitoring netlink uevent channel, everything is fine.")
53+
quit <- struct{}{}
54+
break loop // stop iteration in case of stop signal received
55+
case uevent := <-queue:
56+
t.Log("Handle", uevent.String())
57+
case err := <-errors:
58+
quit <- struct{}{}
59+
t.Fatalf("unable to get existing devices, err: %v", err)
60+
}
61+
}
62+
}

netlink/uevent_test.go

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package netlink
22

33
import (
4+
"fmt"
45
"runtime"
56
"testing"
67
)
@@ -61,7 +62,6 @@ func TestParseUEvent(testing *testing.T) {
6162

6263
uevent, err := ParseUEvent(raw)
6364
t.FatalfIf(err == nil && uevent != nil, "Event parsed successfully but it should be invalid, err: %s", err.Error())
64-
6565
}
6666

6767
func TestParseUdevEvent(testing *testing.T) {
@@ -124,7 +124,9 @@ func TestParseUdevEvent(testing *testing.T) {
124124
"USEC_INITIALIZED": "75223543693",
125125
"ID_PCI_INTERFACE_FROM_DATABASE": "XHCI",
126126
"ID_SERIAL_SHORT": "AH06W0EQ",
127-
}}}, {
127+
},
128+
},
129+
}, {
128130
Input: []byte("libudev\x00\xfe\xed\xca\xfe(\x00\x00\x00(\x00\x00\x00\xf2\x02\x00\x00\x05w\xc5\xe5'\xf8\xf5\f\x00\x00\x00\x00\x00\x00\x00\x00" +
129131
"ACTION=add\x00DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-2\x00SUBSYSTEM=usb\x00DEVNAME=/dev/bus/usb/001/033\x00DEVTYPE=usb_device\x00" +
130132
"PRODUCT=10c4/ea60/100\x00TYPE=0/0/0\x00BUSNUM=001\x00DEVNUM=033\x00SEQNUM=4410\x00MAJOR=189\x00MINOR=32\x00USEC_INITIALIZED=77155422759\x00" +
@@ -254,43 +256,67 @@ func TestUEventEquality(testing *testing.T) {
254256
},
255257
}
256258

259+
uevent5 := UEvent{
260+
Action: REMOVE,
261+
KObj: "/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.2/0003:04F2:0976.0008/hidraw/hidraw5",
262+
Env: map[string]string{
263+
"ACTION": "add",
264+
"DEVPATH": "/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.2/0003:04F2:0976.0008/hidraw/hidraw4",
265+
"SUBSYSTEM": "hidraw",
266+
"MAJOR": "247",
267+
"MINOR": "4",
268+
"DEVNAME": "hidraw4",
269+
"SEQNUM": "2569",
270+
},
271+
}
272+
257273
// When
258274
testcases := []testcase{
259275
{
260276
object: uevent1,
261277
object2: uevent1,
262278
mustEqual: true,
263279
},
264-
{
280+
{ // wrong env number
265281
object: uevent1,
266282
object2: uevent2,
267283
mustEqual: false,
268284
},
269-
{
285+
{ // wrong env number
270286
object: uevent2,
271287
object2: uevent1,
272288
mustEqual: false,
273289
},
274-
{
290+
{ // wrong env value
275291
object: uevent1,
276292
object2: uevent3,
277293
mustEqual: false,
278294
},
279-
{
295+
{ // wrong env value
280296
object: uevent3,
281297
object2: uevent1,
282298
mustEqual: false,
283299
},
284-
{
300+
{ // wrong action
285301
object: uevent1,
286302
object2: uevent4,
287303
mustEqual: false,
288304
},
289-
{
305+
{ // wrong action
290306
object: uevent4,
291307
object2: uevent1,
292308
mustEqual: false,
293309
},
310+
{ // wrong kobj
311+
object: uevent4,
312+
object2: uevent5,
313+
mustEqual: false,
314+
},
315+
{ // wrong kobj
316+
object: uevent5,
317+
object2: uevent4,
318+
mustEqual: false,
319+
},
294320
}
295321

296322
// Then
@@ -299,3 +325,20 @@ func TestUEventEquality(testing *testing.T) {
299325
t.FatalfIf(tc.mustEqual != res, "not expected result (test n°%d, got: %t, expected: %t), err: %v", i, res, tc.mustEqual, err)
300326
}
301327
}
328+
329+
func TestParseKObjAction(t *testing.T) {
330+
for _, action := range []KObjAction{ADD, REMOVE, CHANGE, MOVE, ONLINE, OFFLINE, BIND, UNBIND} {
331+
a, err := ParseKObjAction(action.String())
332+
if err != nil {
333+
t.Fatalf("Unable to parse KObjAction %s: %v", action.String(), err)
334+
}
335+
if a != action {
336+
t.Fatalf(`action must be equal got: "%s", expected: "%s"`, a.String(), action.String())
337+
}
338+
}
339+
340+
expectedError := fmt.Errorf("unknow kobject action (got: %s)", "wrong")
341+
if _, err := ParseKObjAction("wrong"); err != expectedError {
342+
t.Fatalf("wrong kobj action must be detected, got: %v", err)
343+
}
344+
}

0 commit comments

Comments
 (0)