-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice-utils_test.go
110 lines (102 loc) · 3.21 KB
/
device-utils_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
package deviceutils
import (
"fmt"
"regexp"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestParseNvmeSerial(t *testing.T) {
testCases := []struct {
name string
output string
serial string
expErr bool
}{
{
name: "valid google_nvme_id response",
output: "line 58: warning: command substitution: ignored null byte in input\nID_SERIAL_SHORT=pvc-8ee0cf44-6acd-456e-9f3b-95ccd65065b9\nID_SERIAL=Google_PersistentDisk_pvc-8ee0cf44-6acd-456e-9f3b-95ccd65065b9",
serial: "pvc-8ee0cf44-6acd-456e-9f3b-95ccd65065b9",
expErr: false,
},
{
name: "valid google_nvme_id boot disk response",
output: "line 58: warning: command substitution: ignored null byte in input\nID_SERIAL_SHORT=persistent-disk-0\nID_SERIAL=Google_PersistentDisk_persistent-disk-0",
serial: "persistent-disk-0",
expErr: false,
},
{
name: "invalid google_nvme_id response",
output: "Error: requesting namespace-id from non-block device\nNVMe Status:INVALID_NS: The namespace or the format of that namespace is invalid(b) NSID:0\nxxd: sorry cannot seek.\n[2022-03-12T04:17:17+0000]: NVMe Vendor Extension disk information not present",
serial: "",
expErr: true,
},
}
for _, tc := range testCases {
t.Logf("Running test: %v", tc.name)
actualSerial, err := parseNvmeSerial(tc.output)
if tc.expErr && err == nil {
t.Fatalf("Expected error but didn't get any")
}
if !tc.expErr && err != nil {
t.Fatalf("Got unexpected error: %s", err)
}
if actualSerial != tc.serial {
t.Fatalf("Expected '%s' but got '%s'", tc.serial, actualSerial)
}
}
}
func less(a, b fmt.Stringer) bool {
return a.String() < b.String()
}
// Test that the NVMe Regex matches expected paths
// Note that this only tests the regex, not the actual path finding codepath.
// The real codepath uses filepath.Glob(), which doesn't have an easy way to mock out local
// directory paths (eg: using a subdirectory prefix).
// We could use a recursive child process, setting a root UID, and use Chroot (which requires superuser).
// This is done in upstream golang tests, but this adds additional complexity
// and may prevent our tests from running on all platforms. See the following test for an example:
// https://github.com/golang/go/blob/d33548d178016122726342911f8e15016a691472/src/syscall/exec_linux_test.go#L250
func TestDiskNvmePattern(t *testing.T) {
nvmeDiskRegex := regexp.MustCompile(diskNvmePattern)
testCases := []struct {
paths []string
wantPaths []string
}{
{
paths: []string{
"/dev/nvme0n1p15",
"/dev/nvme0n1p14",
"/dev/nvme0n1p1",
"/dev/nvme0n1",
"/dev/nvme0n2",
"/dev/nvme0",
},
wantPaths: []string{
"/dev/nvme0n1",
"/dev/nvme0n2",
},
},
{
paths: []string{
"/dev/nvme1",
"/dev/nvme0n1p15",
"/dev/nvme0n1p14",
"/dev/nvme0n1p1",
"/dev/nvme2",
},
wantPaths: []string{},
},
}
for _, tc := range testCases {
gotPaths := []string{}
for _, path := range tc.paths {
if nvmeDiskRegex.MatchString(path) {
gotPaths = append(gotPaths, path)
}
}
if diff := cmp.Diff(gotPaths, tc.wantPaths, cmpopts.SortSlices(less)); diff != "" {
t.Errorf("Unexpected NVMe device paths (-got, +want):\n%s", diff)
}
}
}