-
Notifications
You must be signed in to change notification settings - Fork 23
/
enumerate_test.go
118 lines (103 loc) · 2.19 KB
/
enumerate_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
// +build linux
package udev
import (
"fmt"
"runtime"
"testing"
)
func ExampleEnumerate_DeviceSyspaths() {
// Create Udev and Enumerate
u := Udev{}
e := u.NewEnumerate()
// Enumerate all device syspaths
dsp, _ := e.DeviceSyspaths()
for s := range dsp {
fmt.Println(s)
}
}
func TestEnumerateDeviceSyspaths(t *testing.T) {
u := Udev{}
e := u.NewEnumerate()
dsp, err := e.DeviceSyspaths()
if err != nil {
t.Fail()
}
if len(dsp) <= 0 {
t.Fail()
}
}
func ExampleEnumerate_SubsystemSyspaths() {
// Create Udev and Enumerate
u := Udev{}
e := u.NewEnumerate()
// Enumerate all subsystem syspaths
dsp, _ := e.SubsystemSyspaths()
for s := range dsp {
fmt.Println(s)
}
}
func TestEnumerateSubsystemSyspaths(t *testing.T) {
u := Udev{}
e := u.NewEnumerate()
ssp, err := e.SubsystemSyspaths()
if err != nil {
t.Fail()
}
if len(ssp) == 0 {
t.Fail()
}
}
func ExampleEnumerate_Devices() {
// Create Udev and Enumerate
u := Udev{}
e := u.NewEnumerate()
// Add some FilterAddMatchSubsystemDevtype
e.AddMatchSubsystem("block")
e.AddMatchIsInitialized()
devices, _ := e.Devices()
for i := range devices {
device := devices[i]
fmt.Println(device.Syspath())
}
}
func TestEnumerateDevicesWithFilter(t *testing.T) {
u := Udev{}
e := u.NewEnumerate()
e.AddMatchSubsystem("block")
e.AddMatchIsInitialized()
e.AddNomatchSubsystem("mem")
e.AddMatchProperty("ID_TYPE", "disk")
e.AddMatchSysattr("partition", "1")
e.AddMatchTag("systemd")
// e.AddMatchProperty("DEVTYPE", "partition")
ds, err := e.Devices()
if err != nil || len(ds) == 0 {
fmt.Println(len(ds))
t.Fail()
}
for i := range ds {
if ds[i].Subsystem() != "block" {
t.Error("Wrong subsystem")
}
if !ds[i].IsInitialized() {
t.Error("Not initialized")
}
if ds[i].PropertyValue("ID_TYPE") != "disk" {
t.Error("Wrong ID_TYPE")
}
if ds[i].SysattrValue("partition") != "1" {
t.Error("Wrong partition")
}
if !ds[i].HasTag("systemd") {
t.Error("Not tagged")
}
parent := ds[i].Parent()
parent2 := ds[i].ParentWithSubsystemDevtype("block", "disk")
if parent.Syspath() != parent2.Syspath() {
t.Error("Parent syspaths don't match")
}
}
}
func TestEnumerateGC(t *testing.T) {
runtime.GC()
}