Skip to content

Commit 3d6677a

Browse files
authored
Merge pull request #1488 from pwschuurman/automated-cherry-pick-of-#1485-upstream-release-1.11
[release-1.11] Automated cherry pick of #1485: Update nvme /dev/ device search path to only identify
2 parents 4688015 + 8a31f9e commit 3d6677a

File tree

9 files changed

+800
-2
lines changed

9 files changed

+800
-2
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
cloud.google.com/go/kms v1.12.1
88
github.com/GoogleCloudPlatform/k8s-cloud-provider v1.24.0
99
github.com/container-storage-interface/spec v1.6.0
10+
github.com/google/go-cmp v0.5.9
1011
github.com/google/uuid v1.3.0
1112
github.com/kubernetes-csi/csi-proxy/client v1.1.1
1213
github.com/kubernetes-csi/csi-test/v4 v4.4.0
@@ -50,7 +51,6 @@ require (
5051
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5152
github.com/golang/protobuf v1.5.3 // indirect
5253
github.com/google/gnostic v0.5.7-v3refs // indirect
53-
github.com/google/go-cmp v0.5.9 // indirect
5454
github.com/google/gofuzz v1.2.1-0.20210504230335-f78f29fc09ea // indirect
5555
github.com/google/s2a-go v0.1.4 // indirect
5656
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect

pkg/deviceutils/device-utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const (
3838
diskSDPath = "/dev/sd"
3939
diskSDPattern = "/dev/sd*"
4040
diskNvmePath = "/dev/nvme"
41-
diskNvmePattern = "/dev/nvme*"
41+
diskNvmePattern = "^/dev/nvme[0-9]+n[0-9]+$"
4242
// How many times to retry for a consistent read of /proc/mounts.
4343
maxListTries = 3
4444
// Number of fields per line in /proc/mounts as per the fstab man page.

pkg/deviceutils/device-utils_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package deviceutils
22

33
import (
4+
"fmt"
5+
"regexp"
46
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/google/go-cmp/cmp/cmpopts"
510
)
611

712
func TestParseNvmeSerial(t *testing.T) {
@@ -45,3 +50,61 @@ func TestParseNvmeSerial(t *testing.T) {
4550
}
4651
}
4752
}
53+
54+
func less(a, b fmt.Stringer) bool {
55+
return a.String() < b.String()
56+
}
57+
58+
// Test that the NVMe Regex matches expected paths
59+
// Note that this only tests the regex, not the actual path finding codepath.
60+
// The real codepath uses filepath.Glob(), which doesn't have an easy way to mock out local
61+
// directory paths (eg: using a subdirectory prefix).
62+
// We could use a recursive child process, setting a root UID, and use Chroot (which requires superuser).
63+
// This is done in upstream golang tests, but this adds additional complexity
64+
// and may prevent our tests from running on all platforms. See the following test for an example:
65+
// https://github.com/golang/go/blob/d33548d178016122726342911f8e15016a691472/src/syscall/exec_linux_test.go#L250
66+
func TestDiskNvmePattern(t *testing.T) {
67+
nvmeDiskRegex := regexp.MustCompile(diskNvmePattern)
68+
69+
testCases := []struct {
70+
paths []string
71+
wantPaths []string
72+
}{
73+
{
74+
paths: []string{
75+
"/dev/nvme0n1p15",
76+
"/dev/nvme0n1p14",
77+
"/dev/nvme0n1p1",
78+
"/dev/nvme0n1",
79+
"/dev/nvme0n2",
80+
"/dev/nvme0",
81+
},
82+
wantPaths: []string{
83+
"/dev/nvme0n1",
84+
"/dev/nvme0n2",
85+
},
86+
},
87+
{
88+
paths: []string{
89+
"/dev/nvme1",
90+
"/dev/nvme0n1p15",
91+
"/dev/nvme0n1p14",
92+
"/dev/nvme0n1p1",
93+
"/dev/nvme2",
94+
},
95+
wantPaths: []string{},
96+
},
97+
}
98+
99+
for _, tc := range testCases {
100+
gotPaths := []string{}
101+
for _, path := range tc.paths {
102+
if nvmeDiskRegex.MatchString(path) {
103+
gotPaths = append(gotPaths, path)
104+
}
105+
}
106+
if diff := cmp.Diff(gotPaths, tc.wantPaths, cmpopts.SortSlices(less)); diff != "" {
107+
t.Errorf("Unexpected NVMe device paths (-got, +want):\n%s", diff)
108+
}
109+
}
110+
}

vendor/github.com/google/go-cmp/cmp/cmpopts/equate.go

+156
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)