Skip to content

Commit

Permalink
Cleanup uvm functional tests to use opts
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
  • Loading branch information
jterry75 committed Nov 29, 2018
1 parent 3f76611 commit e93d509
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 74 deletions.
28 changes: 3 additions & 25 deletions functional/lcow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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
Expand Down
65 changes: 50 additions & 15 deletions functional/utilities/createuvm.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 8 additions & 17 deletions functional/uvm_mem_backingtype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -68,7 +58,7 @@ func runMemTests(t *testing.T, os string) {
if os == "windows" {
runMemStartWCOWTest(t, opts)
} else {
runMemStartTest(t, opts)
runMemStartLCOWTest(t, opts)
}
}
}
Expand Down Expand Up @@ -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{
Expand Down
3 changes: 1 addition & 2 deletions functional/uvm_plannine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions functional/uvm_properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand Down
6 changes: 2 additions & 4 deletions functional/uvm_scsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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()

Expand Down
3 changes: 1 addition & 2 deletions functional/uvm_vpmem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions functional/uvm_vsmb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit e93d509

Please sign in to comment.