Skip to content

Commit b7354bb

Browse files
committed
Tests for Volume Mount Point API wrappers
These are a separate commit, because in order to implement the tests, a bunch of new modules were pulled into go.mod. And the tests only run as Administrator anyway. On the plus side, the tests exercise a few APIs in the vhd package: - CreateVirtualDisk - AttachVirtualDisk - DetachVirtualDisk - GetVirtualDiskPhysicalPath And in future, these tests could also exercise the DecodeReparsePoint API too, since it should be 1:1 with GetVolumeNameForVolumeMountPoint. Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
1 parent ad84bbe commit b7354bb

File tree

3 files changed

+279
-3
lines changed

3 files changed

+279
-3
lines changed

go.mod

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ module github.com/Microsoft/go-winio
33
go 1.12
44

55
require (
6+
github.com/BurntSushi/toml v0.3.1 // indirect
7+
github.com/go-ole/go-ole v1.2.4 // indirect
8+
github.com/hpe-storage/common-host-libs v4.2.0+incompatible
69
github.com/pkg/errors v0.9.1
10+
github.com/satori/go.uuid v1.2.0 // indirect
711
github.com/sirupsen/logrus v1.4.1
8-
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3
12+
github.com/stretchr/testify v1.2.2
13+
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
14+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037
15+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
16+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
17+
gopkg.in/yaml.v2 v2.4.0 // indirect
918
)

go.sum

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
1+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
13
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
24
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
6+
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
7+
github.com/hpe-storage/common-host-libs v4.2.0+incompatible h1:NTOFzSckuKF/1S4muVv/Lgk0TEeqR5Q3XWwqf+uybAA=
8+
github.com/hpe-storage/common-host-libs v4.2.0+incompatible/go.mod h1:qQxvwt4l9C79p2V8bY1P13As1+ylyznKJVp3K2P5bz8=
39
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
410
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
11+
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
12+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
13+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
14+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
15+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
516
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
617
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
718
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
819
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
20+
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
21+
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
922
github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k=
1023
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
1124
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1225
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
1326
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
27+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
28+
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
29+
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
30+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
1431
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
15-
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 h1:7TYNF4UdlohbFwpNH04CoPMp1cHUZgO1Ebq5r2hIjfo=
16-
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
32+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
33+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
34+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
35+
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
36+
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
37+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
38+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
39+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
40+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
41+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
42+
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
43+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
44+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

pkg/volmount/volmount_test.go

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
package volmount
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"regexp"
8+
"strings"
9+
"syscall"
10+
"testing"
11+
12+
"github.com/Microsoft/go-winio/vhd"
13+
"github.com/hpe-storage/common-host-libs/windows/wmi"
14+
"github.com/pkg/errors"
15+
"github.com/stretchr/testify/require"
16+
)
17+
18+
// fullObjectIDFromObjectID turns the given class and objectID into an objectID suitable for calling
19+
// wmi.ExecWmiMethod on.
20+
// There should be an __PATH property on the object for this, according to wbemtest.exe, but I cannot see
21+
// how to get it from the objects exposed by wmi.
22+
func fullObjectIDFromObjectID(class, objectID string) string {
23+
return class + `.ObjectId="` + strings.ReplaceAll(strings.ReplaceAll(objectID, `\`, `\\`), `"`, `\"`) + `"`
24+
}
25+
26+
// turnRawDiskIntoAVolume will take the given PhysicalDisk path, **wipe its partition table** and
27+
// create a single new partition, returning the Volume path needed by the mount-point APIs.
28+
func turnRawDiskIntoAVolume(physPath string) (string, error) {
29+
// This is all implemented via WMI because that turned out to be easier.
30+
const wmiNamespace = "Root\\Microsoft\\Windows\\Storage"
31+
32+
// If go-winio ever grows support for DRIVE_LAYOUT_INFORMATION_EX and PARTITION_INFORMATION_EX,
33+
// this could be done using DeviceIoControl instead, to exercise those APIs.
34+
35+
physicalDiskRE := regexp.MustCompile(`\\\\.\\PhysicalDrive(.*)`)
36+
matches := physicalDiskRE.FindStringSubmatch(physPath)
37+
if len(matches) != 2 {
38+
return "", errors.Errorf("Bad drive physical path %s", physPath)
39+
}
40+
driveNumber := matches[1]
41+
42+
disks, err := wmi.GetMSFTDisk("Number = " + driveNumber)
43+
if err != nil {
44+
return "", errors.Wrapf(err, "wmi.GetMSFTDIsk(%s) failed", "Number = "+driveNumber)
45+
}
46+
47+
if len(disks) != 1 {
48+
return "", errors.Errorf("wmi.GetMSFTDIsk returned unexpected disk list: %v", disks)
49+
}
50+
51+
disk := disks[0]
52+
53+
const diskClass = "MSFT_Disk"
54+
55+
fullDiskObjectID := fullObjectIDFromObjectID(diskClass, disk.ObjectId)
56+
57+
// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/initialize-msft-disk
58+
result, err := wmi.ExecWmiMethod(fullDiskObjectID, "Initialize", wmiNamespace, 1)
59+
if err != nil {
60+
return "", errors.Wrapf(err, "wmi.ExecWmiMethod(%s, Initialize) failed", fullDiskObjectID)
61+
}
62+
if result.Val != 0 {
63+
return "", errors.Errorf("Unexpected %s::Initialize Result: %d", diskClass, result.Val)
64+
}
65+
66+
// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/createpartition-msft-disk
67+
// In theory the next arg is an output UTF-16 string to save me enumerating partitions below, but
68+
// I don't know how to make this work with out-params.
69+
result, err = wmi.ExecWmiMethod(fullDiskObjectID, "CreatePartition", wmiNamespace, nil, true, nil, nil, nil, false, 7, nil, false, false)
70+
if err != nil {
71+
return "", errors.Wrapf(err, "ExecWmiMethod(%v, CreatePartition) failed", fullDiskObjectID)
72+
}
73+
if result.Val != 0 {
74+
return "", errors.Errorf("Unexpected %s::CreatePartition return value: %d", diskClass, result.Val)
75+
}
76+
77+
if err = wmi.RescanDisks(); err != nil {
78+
return "", errors.Wrap(err, "RescanDisks failed")
79+
}
80+
81+
// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-partition
82+
partitions, err := wmi.GetMSFTPartitionForDiskNumber(disk.Number)
83+
if err != nil {
84+
return "", errors.Wrapf(err, "GetMSFTPartitionForDiskNumber(%d) failed", disk.Number)
85+
}
86+
87+
if len(partitions) != 1 {
88+
return "", errors.Errorf("GetMSFTPartitionForDiskNumber failed to find our new partition: %v", partitions)
89+
}
90+
91+
partition := partitions[0]
92+
93+
if len(partition.AccessPaths) != 1 {
94+
return "", errors.Errorf("New partition has unexpectedly access paths (expecting only one): %v", partition.AccessPaths)
95+
}
96+
97+
volumePath := partition.AccessPaths[0]
98+
99+
volumeRE := regexp.MustCompile(`\\\\\?\\Volume{.*}\\`)
100+
if !volumeRE.MatchString(volumePath) {
101+
return "", errors.Errorf("New partition access path not a volume path: %s", volumePath)
102+
}
103+
104+
// Apparently, we don't need to format this volume, SetVolumeMountPoint doesn't care.
105+
return volumePath, nil
106+
}
107+
108+
func mountAtAndCheck(t *testing.T, volumePath, mountPoint string) {
109+
err := os.MkdirAll(mountPoint, 0)
110+
if err != nil {
111+
t.Fatal(err)
112+
}
113+
114+
err = SetVolumeMountPoint(mountPoint, volumePath)
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
defer func() {
119+
if t.Failed() {
120+
deleteErr := DeleteVolumeMountPoint(mountPoint)
121+
if deleteErr != nil {
122+
// Assuming if we're already failing, failing more isn't wrong.
123+
t.Fatal(deleteErr)
124+
}
125+
}
126+
}()
127+
128+
mountPointVolumePath, err := GetVolumeNameForVolumeMountPoint(mountPoint)
129+
if err != nil {
130+
t.Fatal(err)
131+
}
132+
133+
if mountPointVolumePath != volumePath {
134+
t.Fatalf("Mount read-back incorrectly, expected %s; got %s", volumePath, mountPointVolumePath)
135+
}
136+
}
137+
138+
// TestVolumeMountAPIs creates and attaches a small VHD, and then exercises the
139+
// various mount-point APIs against it.
140+
func TestVolumeMountSuccess(t *testing.T) {
141+
142+
dir, err := ioutil.TempDir("", "volmountapis")
143+
if err != nil {
144+
t.Fatal(err)
145+
}
146+
defer os.RemoveAll(dir)
147+
148+
vhdPath := filepath.Join(dir, "small.vhdx")
149+
150+
createParams := vhd.CreateVirtualDiskParameters{
151+
Version: 2,
152+
Version2: vhd.CreateVersion2{
153+
MaximumSize: 5 * 1024 * 1024,
154+
BlockSizeInBytes: 1024 * 1024,
155+
},
156+
}
157+
158+
vhdHandle, err := vhd.CreateVirtualDisk(vhdPath, vhd.VirtualDiskAccessNone, vhd.CreateVirtualDiskFlagNone, &createParams)
159+
if err != nil {
160+
t.Fatal(err)
161+
}
162+
defer os.Remove(vhdPath)
163+
defer syscall.CloseHandle(vhdHandle)
164+
165+
// This needs to be done elevated, or with some specific permission anyway.
166+
attachParams := vhd.AttachVirtualDiskParameters{Version: 2}
167+
err = vhd.AttachVirtualDisk(vhdHandle, vhd.AttachVirtualDiskFlagNoDriveLetter, &attachParams)
168+
if err != nil {
169+
if errno, ok := errors.Cause(err).(syscall.Errno); ok && errno == syscall.ERROR_PRIVILEGE_NOT_HELD {
170+
t.Skip(err)
171+
}
172+
t.Fatal(err)
173+
}
174+
defer func() {
175+
detachErr := vhd.DetachVirtualDisk(vhdHandle)
176+
if detachErr != nil {
177+
// assuming if we're already failing, failing more isn't wrong
178+
t.Fatal(detachErr)
179+
}
180+
}()
181+
182+
physPath, err := vhd.GetVirtualDiskPhysicalPath(vhdHandle)
183+
if err != nil {
184+
t.Fatal(err)
185+
}
186+
187+
volumePath, err := turnRawDiskIntoAVolume(physPath)
188+
if err != nil {
189+
t.Fatal(err)
190+
}
191+
192+
mountPoints, err := GetMountPathsFromVolumeName(volumePath)
193+
if err != nil {
194+
t.Fatal(err)
195+
}
196+
197+
if len(mountPoints) != 0 {
198+
t.Fatalf("Brand new volume was unexpectedly mounted at: %v", mountPoints)
199+
}
200+
201+
mountPoint0 := filepath.Join(dir, "mount0")
202+
mountAtAndCheck(t, volumePath, mountPoint0)
203+
defer func() {
204+
deleteErr := DeleteVolumeMountPoint(mountPoint0)
205+
if deleteErr != nil {
206+
// Assuming if we're already failing, failing more isn't wrong.
207+
t.Fatal(deleteErr)
208+
}
209+
}()
210+
211+
mountPoints, err = GetMountPathsFromVolumeName(volumePath)
212+
if err != nil {
213+
t.Fatal(err)
214+
}
215+
216+
expectedMountPoints := []string{mountPoint0 + string(filepath.Separator)}
217+
require.ElementsMatch(t, expectedMountPoints, mountPoints, "Mount apparently failed, expected %v (order irrelevant): got %v", expectedMountPoints, mountPoints)
218+
219+
mountPoint1 := filepath.Join(dir, "mount1")
220+
mountAtAndCheck(t, volumePath, mountPoint1)
221+
defer func() {
222+
deleteErr := DeleteVolumeMountPoint(mountPoint1)
223+
if deleteErr != nil {
224+
// Assuming if we're already failing, failing more isn't wrong.
225+
t.Fatal(deleteErr)
226+
}
227+
}()
228+
229+
mountPoints, err = GetMountPathsFromVolumeName(volumePath)
230+
if err != nil {
231+
t.Fatal(err)
232+
}
233+
234+
// No order guarantee on the mounts
235+
expectedMountPoints = []string{mountPoint0 + string(filepath.Separator), mountPoint1 + string(filepath.Separator)}
236+
require.ElementsMatch(t, expectedMountPoints, mountPoints, "Mount apparently failed, expected %v (order irrelevant): got %v", expectedMountPoints, mountPoints)
237+
238+
// Note: DeleteVolumeMountPoint is tested in the defers above.
239+
}

0 commit comments

Comments
 (0)