forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_linux_test.go
195 lines (185 loc) · 4.3 KB
/
block_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
// +build linux
package ghw
import (
"os"
"reflect"
"testing"
)
func TestParseMtabEntry(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok {
t.Skip("Skipping block tests.")
}
tests := []struct {
line string
expected *mtabEntry
}{
{
line: "/dev/sda6 / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0",
expected: &mtabEntry{
Partition: "/dev/sda6",
Mountpoint: "/",
FilesystemType: "ext4",
Options: []string{
"rw",
"relatime",
"errors=remount-ro",
"data=ordered",
},
},
},
{
line: "/dev/sda8 /home/Name\\040with\\040spaces ext4 ro 0 0",
expected: &mtabEntry{
Partition: "/dev/sda8",
Mountpoint: "/home/Name with spaces",
FilesystemType: "ext4",
Options: []string{
"ro",
},
},
},
{
// Whoever might do this in real life should be quarantined and
// placed in administrative segregation
line: "/dev/sda8 /home/Name\\011with\\012tab&newline ext4 ro 0 0",
expected: &mtabEntry{
Partition: "/dev/sda8",
Mountpoint: "/home/Name\twith\ntab&newline",
FilesystemType: "ext4",
Options: []string{
"ro",
},
},
},
{
line: "/dev/sda1 /home/Name\\\\withslash ext4 ro 0 0",
expected: &mtabEntry{
Partition: "/dev/sda1",
Mountpoint: "/home/Name\\withslash",
FilesystemType: "ext4",
Options: []string{
"ro",
},
},
},
{
line: "Indy, bad dates",
expected: nil,
},
}
for x, test := range tests {
actual := parseMtabEntry(test.line)
if test.expected == nil {
if actual != nil {
t.Fatalf("Expected nil, but got %v", actual)
}
} else if !reflect.DeepEqual(test.expected, actual) {
t.Fatalf("In test %d, expected %v == %v", x, test.expected, actual)
}
}
}
func TestDiskTypes(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok {
t.Skip("Skipping block tests.")
}
type entry struct {
driveType DriveType
storageController StorageController
busType BusType
}
tests := []struct {
line string
expected entry
}{
{
line: "sda6",
expected: entry{
driveType: DRIVE_TYPE_HDD,
storageController: STORAGE_CONTROLLER_SCSI,
busType: BUS_TYPE_SCSI,
},
},
{
line: "nvme0n1",
expected: entry{
driveType: DRIVE_TYPE_SSD,
storageController: STORAGE_CONTROLLER_NVME,
busType: BUS_TYPE_NVME,
},
},
{
line: "vda1",
expected: entry{
driveType: DRIVE_TYPE_HDD,
storageController: STORAGE_CONTROLLER_VIRTIO,
busType: BUS_TYPE_VIRTIO,
},
},
{
line: "xvda1",
expected: entry{
driveType: DRIVE_TYPE_HDD,
storageController: STORAGE_CONTROLLER_SCSI,
busType: BUS_TYPE_SCSI,
},
},
{
line: "fda1",
expected: entry{
driveType: DRIVE_TYPE_FDD,
storageController: STORAGE_CONTROLLER_UNKNOWN,
busType: BUS_TYPE_UNKNOWN,
},
},
{
line: "sr0",
expected: entry{
driveType: DRIVE_TYPE_ODD,
storageController: STORAGE_CONTROLLER_SCSI,
busType: BUS_TYPE_SCSI,
},
},
{
line: "mmcblk0",
expected: entry{
driveType: DRIVE_TYPE_SSD,
storageController: STORAGE_CONTROLLER_MMC,
busType: BUS_TYPE_UNKNOWN,
},
},
{
line: "Indy, bad dates",
expected: entry{
driveType: DRIVE_TYPE_UNKNOWN,
storageController: STORAGE_CONTROLLER_UNKNOWN,
busType: BUS_TYPE_UNKNOWN,
},
},
}
for _, test := range tests {
gotDriveType, gotStorageController, gotBusType := diskTypes(test.line)
if test.expected.driveType != gotDriveType {
t.Fatalf(
"For %s, expected drive type %s, but got %s",
test.line, test.expected.driveType, gotDriveType,
)
}
if test.expected.storageController != gotStorageController {
t.Fatalf(
"For %s, expected storage controller %s, but got %s",
test.line, test.expected.storageController, gotStorageController,
)
}
if test.expected.busType != gotBusType {
t.Fatalf(
"For %s, expected bus type %s, but got %s",
test.line, test.expected.busType, gotBusType,
)
}
}
}