From e93d509eb01c5c74008a7739e4bfb6e1f634f865 Mon Sep 17 00:00:00 2001 From: "Justin Terry (VM)" Date: Thu, 29 Nov 2018 14:45:53 -0800 Subject: [PATCH] Cleanup uvm functional tests to use opts Signed-off-by: Justin Terry (VM) --- functional/lcow_test.go | 28 ++--------- functional/utilities/createuvm.go | 65 ++++++++++++++++++++------ functional/uvm_mem_backingtype_test.go | 25 ++++------ functional/uvm_plannine_test.go | 3 +- functional/uvm_properties_test.go | 8 +--- functional/uvm_scsi_test.go | 6 +-- functional/uvm_vpmem_test.go | 3 +- functional/uvm_vsmb_test.go | 4 +- 8 files changed, 68 insertions(+), 74 deletions(-) diff --git a/functional/lcow_test.go b/functional/lcow_test.go index 9b3b0c94df..11fbbb9f0c 100644 --- a/functional/lcow_test.go +++ b/functional/lcow_test.go @@ -52,13 +52,7 @@ func TestLCOWUVMNoSCSISingleVPMemVHD(t *testing.T) { func testLCOWUVMNoSCSISingleVPMem(t *testing.T, opts *uvm.UVMOptions, expected string) { testutilities.RequiresBuild(t, osversion.RS5) - lcowUVM, err := uvm.Create(opts) - if err != nil { - t.Fatal(err) - } - if err := lcowUVM.Start(); err != nil { - t.Fatal(err) - } + lcowUVM := testutilities.CreateLCOWUVMFromOpts(t, opts) defer lcowUVM.Close() out, err := exec.Command(`hcsdiag`, `exec`, `-uvm`, lcowUVM.ID(), `dmesg`).Output() // TODO: Move the CreateProcess. if err != nil { @@ -91,13 +85,7 @@ func testLCOWTimeUVMStart(t *testing.T, rfsType uvm.PreferredRootFSType) { VPMemDeviceCount: &vpmemCount, PreferredRootFSType: &rfsType, } - lcowUVM, err := uvm.Create(opts) - if err != nil { - t.Fatal(err) - } - if err := lcowUVM.Start(); err != nil { - t.Fatal(err) - } + lcowUVM := testutilities.CreateLCOWUVMFromOpts(t, opts) lcowUVM.Close() } } @@ -126,17 +114,7 @@ func TestLCOWSimplePodScenario(t *testing.T) { defer os.RemoveAll(c2ScratchDir) c2ScratchFile := filepath.Join(c2ScratchDir, "sandbox.vhdx") - opts := &uvm.UVMOptions{ - OperatingSystem: "linux", - ID: "uvm", - } - lcowUVM, err := uvm.Create(opts) - if err != nil { - t.Fatal(err) - } - if err := lcowUVM.Start(); err != nil { - t.Fatal(err) - } + lcowUVM := testutilities.CreateLCOWUVM(t, "uvm") defer lcowUVM.Close() // Populate the cache and generate the scratch file for /tmp/scratch diff --git a/functional/utilities/createuvm.go b/functional/utilities/createuvm.go index 55e7e2abc9..1c6165ef5d 100644 --- a/functional/utilities/createuvm.go +++ b/functional/utilities/createuvm.go @@ -1,46 +1,81 @@ package testutilities import ( + "os" "testing" + "github.com/Microsoft/hcsshim/internal/guid" "github.com/Microsoft/hcsshim/internal/uvm" - specs "github.com/opencontainers/runtime-spec/specs-go" ) -// CreateWCOWUVM creates a WCOW utility VM. Returns the UtilityVM object; folder used as its scratch -func CreateWCOWUVM(t *testing.T, uvmLayers []string, id string, resources *specs.WindowsResources) (*uvm.UtilityVM, string) { - scratchDir := CreateTempDir(t) +// CreateWCOWUVM creates a WCOW utility VM with all default options. Returns the +// UtilityVM object; folder used as its scratch +func CreateWCOWUVM(t *testing.T, id, image string) (*uvm.UtilityVM, []string, string) { + return CreateWCOWUVMFromOptsWithImage(t, &uvm.UVMOptions{ID: id, OperatingSystem: "windows"}, image) +} - opts := &uvm.UVMOptions{ - OperatingSystem: "windows", - LayerFolders: append(uvmLayers, scratchDir), - Resources: resources, +// CreateWCOWUVMFromOpts creates a WCOW utility VM with the passed opts. +func CreateWCOWUVMFromOpts(t *testing.T, opts *uvm.UVMOptions) *uvm.UtilityVM { + if opts == nil || len(opts.LayerFolders) < 2 { + t.Fatalf("opts must bet set with LayerFolders") } - if id != "" { - opts.ID = id + if opts.ID == "" { + opts.ID = guid.New().String() } + uvm, err := uvm.Create(opts) if err != nil { t.Fatal(err) } if err := uvm.Start(); err != nil { + uvm.Close() t.Fatal(err) } + return uvm +} + +// CreateWCOWUVMFromOptsWithImage creates a WCOW utility VM with the passed opts +// builds the LayerFolders based on `image`. Returns the UtilityVM object; +// folder used as its scratch +func CreateWCOWUVMFromOptsWithImage(t *testing.T, opts *uvm.UVMOptions, image string) (*uvm.UtilityVM, []string, string) { + if opts == nil { + t.Fatal("opts must be set") + } - return uvm, scratchDir + uvmLayers := LayerFolders(t, image) + scratchDir := CreateTempDir(t) + defer func() { + if t.Failed() { + os.RemoveAll(scratchDir) + } + }() + + opts.LayerFolders = append(opts.LayerFolders, uvmLayers...) + opts.LayerFolders = append(opts.LayerFolders, scratchDir) + + return CreateWCOWUVMFromOpts(t, opts), uvmLayers, scratchDir } -// CreateLCOWUVM creates an LCOW utility VM. +// CreateLCOWUVM with all default options. func CreateLCOWUVM(t *testing.T, id string) *uvm.UtilityVM { - opts := &uvm.UVMOptions{OperatingSystem: "linux"} - if id != "" { - opts.ID = id + return CreateLCOWUVMFromOpts(t, &uvm.UVMOptions{ID: id, OperatingSystem: "linux"}) +} + +// CreateLCOWUVMFromOpts creates an LCOW utility VM with the specified options. +func CreateLCOWUVMFromOpts(t *testing.T, opts *uvm.UVMOptions) *uvm.UtilityVM { + if opts == nil { + opts = &uvm.UVMOptions{} + } + if opts.ID == "" { + opts.ID = guid.New().String() } + uvm, err := uvm.Create(opts) if err != nil { t.Fatal(err) } if err := uvm.Start(); err != nil { + uvm.Close() t.Fatal(err) } return uvm diff --git a/functional/uvm_mem_backingtype_test.go b/functional/uvm_mem_backingtype_test.go index ef231bc2a9..3c2ffb1025 100644 --- a/functional/uvm_mem_backingtype_test.go +++ b/functional/uvm_mem_backingtype_test.go @@ -14,29 +14,18 @@ import ( "github.com/sirupsen/logrus" ) -func runMemStartTest(t *testing.T, opts *uvm.UVMOptions) { - u, err := uvm.Create(opts) - if err != nil { - t.Fatal(err) - } - defer u.Close() - if err := u.Start(); err != nil { - t.Fatal(err) - } +func runMemStartLCOWTest(t *testing.T, opts *uvm.UVMOptions) { + u := testutilities.CreateLCOWUVMFromOpts(t, opts) + u.Close() } func runMemStartWCOWTest(t *testing.T, opts *uvm.UVMOptions) { - imageName := "microsoft/nanoserver" - layers := testutilities.LayerFolders(t, imageName) - scratchDir := testutilities.CreateTempDir(t) + u, _, scratchDir := testutilities.CreateWCOWUVMFromOptsWithImage(t, opts, "microsoft/nanoserver") defer os.RemoveAll(scratchDir) - - opts.LayerFolders = append(layers, scratchDir) - runMemStartTest(t, opts) + u.Close() } func runMemTests(t *testing.T, os string) { - type testCase struct { allowOvercommit *bool enableDeferredCommit *bool @@ -55,6 +44,7 @@ func runMemTests(t *testing.T, os string) { mem := uint64(512 * 1024 * 1024) // 512 MB (OCI in Bytes) for _, bt := range testCases { opts := &uvm.UVMOptions{ + ID: t.Name(), OperatingSystem: os, Resources: &specs.WindowsResources{ Memory: &specs.WindowsMemoryResources{ @@ -68,7 +58,7 @@ func runMemTests(t *testing.T, os string) { if os == "windows" { runMemStartWCOWTest(t, opts) } else { - runMemStartTest(t, opts) + runMemStartLCOWTest(t, opts) } } } @@ -98,6 +88,7 @@ func runBenchMemStartLcowTest(b *testing.B, allowOverCommit bool, enableDeferred mem := uint64(512 * 1024 * 1024) // 512 MB (OCI in Bytes) for i := 0; i < b.N; i++ { opts := &uvm.UVMOptions{ + ID: b.Name(), OperatingSystem: "linux", Resources: &specs.WindowsResources{ Memory: &specs.WindowsMemoryResources{ diff --git a/functional/uvm_plannine_test.go b/functional/uvm_plannine_test.go index 89ba80175b..3a535c1aad 100644 --- a/functional/uvm_plannine_test.go +++ b/functional/uvm_plannine_test.go @@ -18,9 +18,8 @@ import ( // TODO: This is very basic. Need multiple shares and so-on. Can be iterated on later. func TestPlan9(t *testing.T) { testutilities.RequiresBuild(t, osversion.RS5) - //alpineLayers := testutilities.LayerFolders(t, "alpine") - uvm := testutilities.CreateLCOWUVM(t, "TestPlan9") + uvm := testutilities.CreateLCOWUVM(t, t.Name()) defer uvm.Close() dir := testutilities.CreateTempDir(t) diff --git a/functional/uvm_properties_test.go b/functional/uvm_properties_test.go index b1f85b70ff..7ab92a685a 100644 --- a/functional/uvm_properties_test.go +++ b/functional/uvm_properties_test.go @@ -13,10 +13,8 @@ import ( func TestPropertiesGuestConnection_LCOW(t *testing.T) { testutilities.RequiresBuild(t, osversion.RS5) - tempDir := testutilities.CreateTempDir(t) - defer os.RemoveAll(tempDir) - uvm := testutilities.CreateLCOWUVM(t, "TestCreateLCOWScratch") + uvm := testutilities.CreateLCOWUVM(t, t.Name()) defer uvm.Close() p, err := uvm.ComputeSystem().Properties(schema1.PropertyTypeGuestConnection) @@ -33,9 +31,7 @@ func TestPropertiesGuestConnection_LCOW(t *testing.T) { func TestPropertiesGuestConnection_WCOW(t *testing.T) { testutilities.RequiresBuild(t, osversion.RS5) - imageName := "microsoft/nanoserver" - layers := testutilities.LayerFolders(t, imageName) - uvm, uvmScratchDir := testutilities.CreateWCOWUVM(t, layers, "", nil) + uvm, _, uvmScratchDir := testutilities.CreateWCOWUVM(t, t.Name(), "microsoft/nanoserver") defer os.RemoveAll(uvmScratchDir) defer uvm.Close() diff --git a/functional/uvm_scsi_test.go b/functional/uvm_scsi_test.go index 849d52bb54..0f341b29b3 100644 --- a/functional/uvm_scsi_test.go +++ b/functional/uvm_scsi_test.go @@ -19,7 +19,7 @@ import ( // negative testing so that a disk can't be attached twice. func TestSCSIAddRemoveLCOW(t *testing.T) { testutilities.RequiresBuild(t, osversion.RS5) - u := testutilities.CreateLCOWUVM(t, "TestAddRemoveSCSILCOW") + u := testutilities.CreateLCOWUVM(t, t.Name()) defer u.Close() testSCSIAddRemove(t, u, `/`, "linux", []string{}) @@ -31,9 +31,7 @@ func TestSCSIAddRemoveLCOW(t *testing.T) { // negative testing so that a disk can't be attached twice. func TestSCSIAddRemoveWCOW(t *testing.T) { testutilities.RequiresBuild(t, osversion.RS5) - imageName := "microsoft/nanoserver" - layers := testutilities.LayerFolders(t, imageName) - u, uvmScratchDir := testutilities.CreateWCOWUVM(t, layers, "", nil) + u, layers, uvmScratchDir := testutilities.CreateWCOWUVM(t, t.Name(), "microsoft/nanoserver") defer os.RemoveAll(uvmScratchDir) defer u.Close() diff --git a/functional/uvm_vpmem_test.go b/functional/uvm_vpmem_test.go index 7f0f1d2591..43cb512471 100644 --- a/functional/uvm_vpmem_test.go +++ b/functional/uvm_vpmem_test.go @@ -19,8 +19,7 @@ func TestVPMEM(t *testing.T) { testutilities.RequiresBuild(t, osversion.RS5) alpineLayers := testutilities.LayerFolders(t, "alpine") - id := "TestVPMEM" - u := testutilities.CreateLCOWUVM(t, id) + u := testutilities.CreateLCOWUVM(t, t.Name()) defer u.Close() var iterations uint32 = uvm.MaxVPMEMCount diff --git a/functional/uvm_vsmb_test.go b/functional/uvm_vsmb_test.go index 160ca6b8e3..97158c39a7 100644 --- a/functional/uvm_vsmb_test.go +++ b/functional/uvm_vsmb_test.go @@ -14,9 +14,7 @@ import ( // TestVSMB tests adding/removing VSMB layers from a v2 Windows utility VM func TestVSMB(t *testing.T) { testutilities.RequiresBuild(t, osversion.RS5) - nanoLayers := testutilities.LayerFolders(t, "microsoft/nanoserver") - - uvm, uvmScratchDir := testutilities.CreateWCOWUVM(t, nanoLayers, "", nil) + uvm, _, uvmScratchDir := testutilities.CreateWCOWUVM(t, t.Name(), "microsoft/nanoserver") defer os.RemoveAll(uvmScratchDir) defer uvm.Close()