|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package vmparity |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/opencontainers/runtime-spec/specs-go" |
| 14 | + |
| 15 | + runhcsopts "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options" |
| 16 | + lcowbuilder "github.com/Microsoft/hcsshim/internal/builder/vm/lcow" |
| 17 | + hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2" |
| 18 | + "github.com/Microsoft/hcsshim/internal/oci" |
| 19 | + "github.com/Microsoft/hcsshim/internal/uvm" |
| 20 | + "github.com/Microsoft/hcsshim/internal/vm/vmutils" |
| 21 | + "github.com/Microsoft/hcsshim/osversion" |
| 22 | + vm "github.com/Microsoft/hcsshim/sandbox-spec/vm/v2" |
| 23 | +) |
| 24 | + |
| 25 | +// buildLegacyLCOWDocument creates the HCS document for an LCOW VM using the |
| 26 | +// legacy shim pipeline. It runs the same sequence as createInternal → createPod |
| 27 | +// → CreateLCOW: annotation processing, spec conversion, option verification, |
| 28 | +// and document generation. |
| 29 | +func buildLegacyLCOWDocument( |
| 30 | + ctx context.Context, |
| 31 | + spec specs.Spec, |
| 32 | + shimOpts *runhcsopts.Options, |
| 33 | + bundle string, |
| 34 | +) (*hcsschema.ComputeSystem, *uvm.OptionsLCOW, error) { |
| 35 | + // Step 1: Merge shim options into the OCI spec annotations. |
| 36 | + spec = oci.UpdateSpecFromOptions(spec, shimOpts) |
| 37 | + |
| 38 | + // Step 2: Expand annotation groups (e.g., security toggles). |
| 39 | + if err := oci.ProcessAnnotations(ctx, spec.Annotations); err != nil { |
| 40 | + return nil, nil, fmt.Errorf("failed to expand OCI annotations: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + // Step 3: Convert OCI spec + annotations into OptionsLCOW. |
| 44 | + rawOpts, err := oci.SpecToUVMCreateOpts(ctx, &spec, "test-parity@vm", "test-owner") |
| 45 | + if err != nil { |
| 46 | + return nil, nil, fmt.Errorf("failed to convert OCI spec to UVM create options: %w", err) |
| 47 | + } |
| 48 | + opts := rawOpts.(*uvm.OptionsLCOW) |
| 49 | + opts.BundleDirectory = bundle |
| 50 | + |
| 51 | + // Step 4: Verify options constraints (same as CreateLCOW). |
| 52 | + if err := uvm.VerifyOptions(ctx, opts); err != nil { |
| 53 | + return nil, nil, fmt.Errorf("option verification failed: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Step 5: Build the temporary UtilityVM with fields that MakeLCOWDoc reads. |
| 57 | + scsiCount := opts.SCSIControllerCount |
| 58 | + if osversion.Build() >= osversion.RS5 && opts.VPMemDeviceCount == 0 { |
| 59 | + scsiCount = 4 |
| 60 | + } |
| 61 | + tempUVM := uvm.NewUtilityVMForDoc( |
| 62 | + opts.ID, opts.Owner, |
| 63 | + scsiCount, opts.VPMemDeviceCount, opts.VPMemSizeBytes, |
| 64 | + !opts.VPMemNoMultiMapping, |
| 65 | + ) |
| 66 | + |
| 67 | + // Step 6: Generate the HCS document. |
| 68 | + doc, err := uvm.MakeLCOWDoc(ctx, opts, tempUVM) |
| 69 | + if err != nil { |
| 70 | + return nil, nil, fmt.Errorf("failed to generate legacy LCOW HCS document: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + return doc, opts, nil |
| 74 | +} |
| 75 | + |
| 76 | +// buildV2LCOWDocument creates the HCS document and sandbox options from the |
| 77 | +// provided VM spec and runhcs options using the v2 modular builder. |
| 78 | +// The returned document can be used to create a VM directly via HCS. |
| 79 | +func buildV2LCOWDocument( |
| 80 | + ctx context.Context, |
| 81 | + shimOpts *runhcsopts.Options, |
| 82 | + spec *vm.Spec, |
| 83 | + bundle string, |
| 84 | +) (*hcsschema.ComputeSystem, *lcowbuilder.SandboxOptions, error) { |
| 85 | + return lcowbuilder.BuildSandboxConfig(ctx, "test-owner", bundle, shimOpts, spec) |
| 86 | +} |
| 87 | + |
| 88 | +// setupBootFiles creates a temporary directory containing the kernel and rootfs |
| 89 | +// files that both document builders probe during boot configuration resolution. |
| 90 | +func setupBootFiles(t *testing.T) string { |
| 91 | + t.Helper() |
| 92 | + dir := t.TempDir() |
| 93 | + for _, name := range []string{ |
| 94 | + vmutils.KernelFile, |
| 95 | + vmutils.UncompressedKernelFile, |
| 96 | + vmutils.InitrdFile, |
| 97 | + vmutils.VhdFile, |
| 98 | + } { |
| 99 | + if err := os.WriteFile(filepath.Join(dir, name), []byte("test"), 0644); err != nil { |
| 100 | + t.Fatalf("failed to create boot file %s: %v", name, err) |
| 101 | + } |
| 102 | + } |
| 103 | + return dir |
| 104 | +} |
| 105 | + |
| 106 | +// jsonToString serializes v to indented JSON for test log output. |
| 107 | +func jsonToString(v interface{}) string { |
| 108 | + b, err := json.MarshalIndent(v, "", " ") |
| 109 | + if err != nil { |
| 110 | + panic(err) |
| 111 | + } |
| 112 | + return string(b) |
| 113 | +} |
0 commit comments