|
1 | 1 | package testutilities |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
4 | 5 | "testing" |
5 | 6 |
|
| 7 | + "github.com/Microsoft/hcsshim/internal/guid" |
6 | 8 | "github.com/Microsoft/hcsshim/internal/uvm" |
7 | | - specs "github.com/opencontainers/runtime-spec/specs-go" |
8 | 9 | ) |
9 | 10 |
|
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 | +} |
13 | 16 |
|
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") |
18 | 21 | } |
19 | | - if id != "" { |
20 | | - opts.ID = id |
| 22 | + if opts.ID == "" { |
| 23 | + opts.ID = guid.New().String() |
21 | 24 | } |
| 25 | + |
22 | 26 | uvm, err := uvm.Create(opts) |
23 | 27 | if err != nil { |
24 | 28 | t.Fatal(err) |
25 | 29 | } |
26 | 30 | if err := uvm.Start(); err != nil { |
| 31 | + uvm.Close() |
27 | 32 | t.Fatal(err) |
28 | 33 | } |
| 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 | + } |
29 | 44 |
|
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 |
31 | 57 | } |
32 | 58 |
|
33 | | -// CreateLCOWUVM creates an LCOW utility VM. |
| 59 | +// CreateLCOWUVM with all default options. |
34 | 60 | 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() |
38 | 71 | } |
| 72 | + |
39 | 73 | uvm, err := uvm.Create(opts) |
40 | 74 | if err != nil { |
41 | 75 | t.Fatal(err) |
42 | 76 | } |
43 | 77 | if err := uvm.Start(); err != nil { |
| 78 | + uvm.Close() |
44 | 79 | t.Fatal(err) |
45 | 80 | } |
46 | 81 | return uvm |
|
0 commit comments