...

Source file src/github.com/Microsoft/hcsshim/internal/oci/uvm_test.go

Documentation: github.com/Microsoft/hcsshim/internal/oci

     1  //go:build windows
     2  
     3  package oci
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"testing"
     9  
    10  	runhcsopts "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
    11  	"github.com/Microsoft/hcsshim/internal/uvm"
    12  	"github.com/Microsoft/hcsshim/pkg/annotations"
    13  	"github.com/google/go-cmp/cmp"
    14  	"github.com/opencontainers/runtime-spec/specs-go"
    15  )
    16  
    17  func Test_SpecUpdate_MemorySize_WithAnnotation_WithOpts(t *testing.T) {
    18  	opts := &runhcsopts.Options{
    19  		VmMemorySizeInMb: 3072,
    20  	}
    21  	s := &specs.Spec{
    22  		Linux: &specs.Linux{},
    23  		Annotations: map[string]string{
    24  			annotations.MemorySizeInMB: "2048",
    25  		},
    26  	}
    27  	updatedSpec := UpdateSpecFromOptions(*s, opts)
    28  
    29  	if updatedSpec.Annotations[annotations.MemorySizeInMB] != "2048" {
    30  		t.Fatal("should not have updated annotation to default when annotation is provided in the spec")
    31  	}
    32  }
    33  
    34  func Test_SpecUpdate_MemorySize_NoAnnotation_WithOpts(t *testing.T) {
    35  	opts := &runhcsopts.Options{
    36  		VmMemorySizeInMb: 3072,
    37  	}
    38  	s := &specs.Spec{
    39  		Linux:       &specs.Linux{},
    40  		Annotations: map[string]string{},
    41  	}
    42  	updatedSpec := UpdateSpecFromOptions(*s, opts)
    43  
    44  	if updatedSpec.Annotations[annotations.MemorySizeInMB] != "3072" {
    45  		t.Fatal("should have updated annotation to default when annotation is not provided in the spec")
    46  	}
    47  }
    48  
    49  func Test_SpecUpdate_ProcessorCount_WithAnnotation_WithOpts(t *testing.T) {
    50  	opts := &runhcsopts.Options{
    51  		VmProcessorCount: 4,
    52  	}
    53  	s := &specs.Spec{
    54  		Linux: &specs.Linux{},
    55  		Annotations: map[string]string{
    56  			annotations.ProcessorCount: "8",
    57  		},
    58  	}
    59  	updatedSpec := UpdateSpecFromOptions(*s, opts)
    60  
    61  	if updatedSpec.Annotations[annotations.ProcessorCount] != "8" {
    62  		t.Fatal("should not have updated annotation to default when annotation is provided in the spec")
    63  	}
    64  }
    65  
    66  func Test_SpecUpdate_ProcessorCount_NoAnnotation_WithOpts(t *testing.T) {
    67  	opts := &runhcsopts.Options{
    68  		VmProcessorCount: 4,
    69  	}
    70  	s := &specs.Spec{
    71  		Linux:       &specs.Linux{},
    72  		Annotations: map[string]string{},
    73  	}
    74  	updatedSpec := UpdateSpecFromOptions(*s, opts)
    75  
    76  	if updatedSpec.Annotations[annotations.ProcessorCount] != "4" {
    77  		t.Fatal("should have updated annotation to default when annotation is not provided in the spec")
    78  	}
    79  }
    80  
    81  func Test_SpecToUVMCreateOptions_Default_LCOW(t *testing.T) {
    82  	s := &specs.Spec{
    83  		Linux:       &specs.Linux{},
    84  		Annotations: make(map[string]string),
    85  	}
    86  
    87  	opts, err := SpecToUVMCreateOpts(context.Background(), s, t.Name(), "")
    88  	if err != nil {
    89  		t.Fatalf("could not generate creation options from spec: %v", err)
    90  	}
    91  
    92  	lopts := (opts).(*uvm.OptionsLCOW)
    93  	dopts := uvm.NewDefaultOptionsLCOW(t.Name(), "")
    94  
    95  	// output handler equality is always false, so set to nil
    96  	lopts.OutputHandler = nil
    97  	dopts.OutputHandler = nil
    98  
    99  	if !cmp.Equal(*lopts, *dopts) {
   100  		t.Fatalf("should not have updated create options from default when no annotation are provided:\n%s", cmp.Diff(lopts, dopts))
   101  	}
   102  }
   103  
   104  func Test_SpecToUVMCreateOptions_Default_WCOW(t *testing.T) {
   105  	s := &specs.Spec{
   106  		Windows: &specs.Windows{
   107  			HyperV: &specs.WindowsHyperV{},
   108  		},
   109  		Annotations: make(map[string]string),
   110  	}
   111  
   112  	opts, err := SpecToUVMCreateOpts(context.Background(), s, t.Name(), "")
   113  	if err != nil {
   114  		t.Fatalf("could not generate creation options from spec: %v", err)
   115  	}
   116  
   117  	wopts := (opts).(*uvm.OptionsWCOW)
   118  	dopts := uvm.NewDefaultOptionsWCOW(t.Name(), "")
   119  
   120  	if !cmp.Equal(*wopts, *dopts) {
   121  		t.Fatalf("should not have updated create options from default when no annotation are provided:\n%s", cmp.Diff(wopts, dopts))
   122  	}
   123  }
   124  
   125  func Test_SpecToUVMCreateOptions_Common(t *testing.T) {
   126  	cpugroupid := "1"
   127  	lowmmiogap := 1024
   128  	as := map[string]string{
   129  		annotations.ProcessorCount:            "8",
   130  		annotations.CPUGroupID:                cpugroupid,
   131  		annotations.DisableWritableFileShares: "true",
   132  		annotations.MemoryLowMMIOGapInMB:      fmt.Sprint(lowmmiogap),
   133  	}
   134  
   135  	tests := []struct {
   136  		name    string
   137  		spec    specs.Spec
   138  		extract func(interface{}) *uvm.Options
   139  	}{
   140  		{
   141  			name: "lcow",
   142  			spec: specs.Spec{
   143  				Linux: &specs.Linux{},
   144  			},
   145  			// generics would be nice ...
   146  			extract: func(i interface{}) *uvm.Options {
   147  				o := (i).(*uvm.OptionsLCOW)
   148  				return o.Options
   149  			},
   150  		},
   151  		{
   152  			name: "wcow-hypervisor",
   153  			spec: specs.Spec{
   154  				Windows: &specs.Windows{
   155  					HyperV: &specs.WindowsHyperV{},
   156  				},
   157  			},
   158  			extract: func(i interface{}) *uvm.Options {
   159  				o := (i).(*uvm.OptionsWCOW)
   160  				return o.Options
   161  			},
   162  		},
   163  	}
   164  
   165  	for _, tt := range tests {
   166  		t.Run(tt.name, func(t *testing.T) {
   167  			tt.spec.Annotations = as
   168  			opts, err := SpecToUVMCreateOpts(context.Background(), &tt.spec, t.Name(), "")
   169  			if err != nil {
   170  				t.Fatalf("could not generate creation options from spec: %v", err)
   171  			}
   172  
   173  			// get the underlying uvm.Options from uvm.Options[LW]COW
   174  			copts := tt.extract(opts)
   175  			if copts.LowMMIOGapInMB != uint64(lowmmiogap) {
   176  				t.Fatalf("should have updated creation options low MMIO Gap when annotation is provided: %v != %v", copts.LowMMIOGapInMB, lowmmiogap)
   177  			}
   178  			if copts.ProcessorCount != 8 {
   179  				t.Fatalf("should have updated creation options processor count when annotation is provided: %v != 8", copts.ProcessorCount)
   180  			}
   181  			if copts.CPUGroupID != cpugroupid {
   182  				t.Fatalf("should have updated creation options CPU group Id when annotation is provided: %v != %v", copts.CPUGroupID, cpugroupid)
   183  			}
   184  			if !copts.NoWritableFileShares {
   185  				t.Fatal("should have disabled writable in shares creation when annotation is provided")
   186  			}
   187  		})
   188  	}
   189  }
   190  

View as plain text