Skip to content

Commit f827be4

Browse files
authored
Merge pull request #399 from Microsoft/uvm_create_opts_test
Cleanup uvm functional tests to use opts
2 parents 3f76611 + e93d509 commit f827be4

File tree

8 files changed

+68
-74
lines changed

8 files changed

+68
-74
lines changed

functional/lcow_test.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@ func TestLCOWUVMNoSCSISingleVPMemVHD(t *testing.T) {
5252

5353
func testLCOWUVMNoSCSISingleVPMem(t *testing.T, opts *uvm.UVMOptions, expected string) {
5454
testutilities.RequiresBuild(t, osversion.RS5)
55-
lcowUVM, err := uvm.Create(opts)
56-
if err != nil {
57-
t.Fatal(err)
58-
}
59-
if err := lcowUVM.Start(); err != nil {
60-
t.Fatal(err)
61-
}
55+
lcowUVM := testutilities.CreateLCOWUVMFromOpts(t, opts)
6256
defer lcowUVM.Close()
6357
out, err := exec.Command(`hcsdiag`, `exec`, `-uvm`, lcowUVM.ID(), `dmesg`).Output() // TODO: Move the CreateProcess.
6458
if err != nil {
@@ -91,13 +85,7 @@ func testLCOWTimeUVMStart(t *testing.T, rfsType uvm.PreferredRootFSType) {
9185
VPMemDeviceCount: &vpmemCount,
9286
PreferredRootFSType: &rfsType,
9387
}
94-
lcowUVM, err := uvm.Create(opts)
95-
if err != nil {
96-
t.Fatal(err)
97-
}
98-
if err := lcowUVM.Start(); err != nil {
99-
t.Fatal(err)
100-
}
88+
lcowUVM := testutilities.CreateLCOWUVMFromOpts(t, opts)
10189
lcowUVM.Close()
10290
}
10391
}
@@ -126,17 +114,7 @@ func TestLCOWSimplePodScenario(t *testing.T) {
126114
defer os.RemoveAll(c2ScratchDir)
127115
c2ScratchFile := filepath.Join(c2ScratchDir, "sandbox.vhdx")
128116

129-
opts := &uvm.UVMOptions{
130-
OperatingSystem: "linux",
131-
ID: "uvm",
132-
}
133-
lcowUVM, err := uvm.Create(opts)
134-
if err != nil {
135-
t.Fatal(err)
136-
}
137-
if err := lcowUVM.Start(); err != nil {
138-
t.Fatal(err)
139-
}
117+
lcowUVM := testutilities.CreateLCOWUVM(t, "uvm")
140118
defer lcowUVM.Close()
141119

142120
// Populate the cache and generate the scratch file for /tmp/scratch

functional/utilities/createuvm.go

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,81 @@
11
package testutilities
22

33
import (
4+
"os"
45
"testing"
56

7+
"github.com/Microsoft/hcsshim/internal/guid"
68
"github.com/Microsoft/hcsshim/internal/uvm"
7-
specs "github.com/opencontainers/runtime-spec/specs-go"
89
)
910

10-
// CreateWCOWUVM creates a WCOW utility VM. Returns the UtilityVM object; folder used as its scratch
11-
func CreateWCOWUVM(t *testing.T, uvmLayers []string, id string, resources *specs.WindowsResources) (*uvm.UtilityVM, string) {
12-
scratchDir := CreateTempDir(t)
11+
// CreateWCOWUVM creates a WCOW utility VM with all default options. Returns the
12+
// UtilityVM object; folder used as its scratch
13+
func CreateWCOWUVM(t *testing.T, id, image string) (*uvm.UtilityVM, []string, string) {
14+
return CreateWCOWUVMFromOptsWithImage(t, &uvm.UVMOptions{ID: id, OperatingSystem: "windows"}, image)
15+
}
1316

14-
opts := &uvm.UVMOptions{
15-
OperatingSystem: "windows",
16-
LayerFolders: append(uvmLayers, scratchDir),
17-
Resources: resources,
17+
// CreateWCOWUVMFromOpts creates a WCOW utility VM with the passed opts.
18+
func CreateWCOWUVMFromOpts(t *testing.T, opts *uvm.UVMOptions) *uvm.UtilityVM {
19+
if opts == nil || len(opts.LayerFolders) < 2 {
20+
t.Fatalf("opts must bet set with LayerFolders")
1821
}
19-
if id != "" {
20-
opts.ID = id
22+
if opts.ID == "" {
23+
opts.ID = guid.New().String()
2124
}
25+
2226
uvm, err := uvm.Create(opts)
2327
if err != nil {
2428
t.Fatal(err)
2529
}
2630
if err := uvm.Start(); err != nil {
31+
uvm.Close()
2732
t.Fatal(err)
2833
}
34+
return uvm
35+
}
36+
37+
// CreateWCOWUVMFromOptsWithImage creates a WCOW utility VM with the passed opts
38+
// builds the LayerFolders based on `image`. Returns the UtilityVM object;
39+
// folder used as its scratch
40+
func CreateWCOWUVMFromOptsWithImage(t *testing.T, opts *uvm.UVMOptions, image string) (*uvm.UtilityVM, []string, string) {
41+
if opts == nil {
42+
t.Fatal("opts must be set")
43+
}
2944

30-
return uvm, scratchDir
45+
uvmLayers := LayerFolders(t, image)
46+
scratchDir := CreateTempDir(t)
47+
defer func() {
48+
if t.Failed() {
49+
os.RemoveAll(scratchDir)
50+
}
51+
}()
52+
53+
opts.LayerFolders = append(opts.LayerFolders, uvmLayers...)
54+
opts.LayerFolders = append(opts.LayerFolders, scratchDir)
55+
56+
return CreateWCOWUVMFromOpts(t, opts), uvmLayers, scratchDir
3157
}
3258

33-
// CreateLCOWUVM creates an LCOW utility VM.
59+
// CreateLCOWUVM with all default options.
3460
func CreateLCOWUVM(t *testing.T, id string) *uvm.UtilityVM {
35-
opts := &uvm.UVMOptions{OperatingSystem: "linux"}
36-
if id != "" {
37-
opts.ID = id
61+
return CreateLCOWUVMFromOpts(t, &uvm.UVMOptions{ID: id, OperatingSystem: "linux"})
62+
}
63+
64+
// CreateLCOWUVMFromOpts creates an LCOW utility VM with the specified options.
65+
func CreateLCOWUVMFromOpts(t *testing.T, opts *uvm.UVMOptions) *uvm.UtilityVM {
66+
if opts == nil {
67+
opts = &uvm.UVMOptions{}
68+
}
69+
if opts.ID == "" {
70+
opts.ID = guid.New().String()
3871
}
72+
3973
uvm, err := uvm.Create(opts)
4074
if err != nil {
4175
t.Fatal(err)
4276
}
4377
if err := uvm.Start(); err != nil {
78+
uvm.Close()
4479
t.Fatal(err)
4580
}
4681
return uvm

functional/uvm_mem_backingtype_test.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,18 @@ import (
1414
"github.com/sirupsen/logrus"
1515
)
1616

17-
func runMemStartTest(t *testing.T, opts *uvm.UVMOptions) {
18-
u, err := uvm.Create(opts)
19-
if err != nil {
20-
t.Fatal(err)
21-
}
22-
defer u.Close()
23-
if err := u.Start(); err != nil {
24-
t.Fatal(err)
25-
}
17+
func runMemStartLCOWTest(t *testing.T, opts *uvm.UVMOptions) {
18+
u := testutilities.CreateLCOWUVMFromOpts(t, opts)
19+
u.Close()
2620
}
2721

2822
func runMemStartWCOWTest(t *testing.T, opts *uvm.UVMOptions) {
29-
imageName := "microsoft/nanoserver"
30-
layers := testutilities.LayerFolders(t, imageName)
31-
scratchDir := testutilities.CreateTempDir(t)
23+
u, _, scratchDir := testutilities.CreateWCOWUVMFromOptsWithImage(t, opts, "microsoft/nanoserver")
3224
defer os.RemoveAll(scratchDir)
33-
34-
opts.LayerFolders = append(layers, scratchDir)
35-
runMemStartTest(t, opts)
25+
u.Close()
3626
}
3727

3828
func runMemTests(t *testing.T, os string) {
39-
4029
type testCase struct {
4130
allowOvercommit *bool
4231
enableDeferredCommit *bool
@@ -55,6 +44,7 @@ func runMemTests(t *testing.T, os string) {
5544
mem := uint64(512 * 1024 * 1024) // 512 MB (OCI in Bytes)
5645
for _, bt := range testCases {
5746
opts := &uvm.UVMOptions{
47+
ID: t.Name(),
5848
OperatingSystem: os,
5949
Resources: &specs.WindowsResources{
6050
Memory: &specs.WindowsMemoryResources{
@@ -68,7 +58,7 @@ func runMemTests(t *testing.T, os string) {
6858
if os == "windows" {
6959
runMemStartWCOWTest(t, opts)
7060
} else {
71-
runMemStartTest(t, opts)
61+
runMemStartLCOWTest(t, opts)
7262
}
7363
}
7464
}
@@ -98,6 +88,7 @@ func runBenchMemStartLcowTest(b *testing.B, allowOverCommit bool, enableDeferred
9888
mem := uint64(512 * 1024 * 1024) // 512 MB (OCI in Bytes)
9989
for i := 0; i < b.N; i++ {
10090
opts := &uvm.UVMOptions{
91+
ID: b.Name(),
10192
OperatingSystem: "linux",
10293
Resources: &specs.WindowsResources{
10394
Memory: &specs.WindowsMemoryResources{

functional/uvm_plannine_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import (
1818
// TODO: This is very basic. Need multiple shares and so-on. Can be iterated on later.
1919
func TestPlan9(t *testing.T) {
2020
testutilities.RequiresBuild(t, osversion.RS5)
21-
//alpineLayers := testutilities.LayerFolders(t, "alpine")
2221

23-
uvm := testutilities.CreateLCOWUVM(t, "TestPlan9")
22+
uvm := testutilities.CreateLCOWUVM(t, t.Name())
2423
defer uvm.Close()
2524

2625
dir := testutilities.CreateTempDir(t)

functional/uvm_properties_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import (
1313

1414
func TestPropertiesGuestConnection_LCOW(t *testing.T) {
1515
testutilities.RequiresBuild(t, osversion.RS5)
16-
tempDir := testutilities.CreateTempDir(t)
17-
defer os.RemoveAll(tempDir)
1816

19-
uvm := testutilities.CreateLCOWUVM(t, "TestCreateLCOWScratch")
17+
uvm := testutilities.CreateLCOWUVM(t, t.Name())
2018
defer uvm.Close()
2119

2220
p, err := uvm.ComputeSystem().Properties(schema1.PropertyTypeGuestConnection)
@@ -33,9 +31,7 @@ func TestPropertiesGuestConnection_LCOW(t *testing.T) {
3331

3432
func TestPropertiesGuestConnection_WCOW(t *testing.T) {
3533
testutilities.RequiresBuild(t, osversion.RS5)
36-
imageName := "microsoft/nanoserver"
37-
layers := testutilities.LayerFolders(t, imageName)
38-
uvm, uvmScratchDir := testutilities.CreateWCOWUVM(t, layers, "", nil)
34+
uvm, _, uvmScratchDir := testutilities.CreateWCOWUVM(t, t.Name(), "microsoft/nanoserver")
3935
defer os.RemoveAll(uvmScratchDir)
4036
defer uvm.Close()
4137

functional/uvm_scsi_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
// negative testing so that a disk can't be attached twice.
2020
func TestSCSIAddRemoveLCOW(t *testing.T) {
2121
testutilities.RequiresBuild(t, osversion.RS5)
22-
u := testutilities.CreateLCOWUVM(t, "TestAddRemoveSCSILCOW")
22+
u := testutilities.CreateLCOWUVM(t, t.Name())
2323
defer u.Close()
2424

2525
testSCSIAddRemove(t, u, `/`, "linux", []string{})
@@ -31,9 +31,7 @@ func TestSCSIAddRemoveLCOW(t *testing.T) {
3131
// negative testing so that a disk can't be attached twice.
3232
func TestSCSIAddRemoveWCOW(t *testing.T) {
3333
testutilities.RequiresBuild(t, osversion.RS5)
34-
imageName := "microsoft/nanoserver"
35-
layers := testutilities.LayerFolders(t, imageName)
36-
u, uvmScratchDir := testutilities.CreateWCOWUVM(t, layers, "", nil)
34+
u, layers, uvmScratchDir := testutilities.CreateWCOWUVM(t, t.Name(), "microsoft/nanoserver")
3735
defer os.RemoveAll(uvmScratchDir)
3836
defer u.Close()
3937

functional/uvm_vpmem_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ func TestVPMEM(t *testing.T) {
1919
testutilities.RequiresBuild(t, osversion.RS5)
2020
alpineLayers := testutilities.LayerFolders(t, "alpine")
2121

22-
id := "TestVPMEM"
23-
u := testutilities.CreateLCOWUVM(t, id)
22+
u := testutilities.CreateLCOWUVM(t, t.Name())
2423
defer u.Close()
2524

2625
var iterations uint32 = uvm.MaxVPMEMCount

functional/uvm_vsmb_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ import (
1414
// TestVSMB tests adding/removing VSMB layers from a v2 Windows utility VM
1515
func TestVSMB(t *testing.T) {
1616
testutilities.RequiresBuild(t, osversion.RS5)
17-
nanoLayers := testutilities.LayerFolders(t, "microsoft/nanoserver")
18-
19-
uvm, uvmScratchDir := testutilities.CreateWCOWUVM(t, nanoLayers, "", nil)
17+
uvm, _, uvmScratchDir := testutilities.CreateWCOWUVM(t, t.Name(), "microsoft/nanoserver")
2018
defer os.RemoveAll(uvmScratchDir)
2119
defer uvm.Close()
2220

0 commit comments

Comments
 (0)