...

Source file src/github.com/Microsoft/hcsshim/internal/uvm/vpmem_mapped_test.go

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

     1  //go:build windows
     2  
     3  package uvm
     4  
     5  import (
     6  	"context"
     7  	"testing"
     8  
     9  	"github.com/Microsoft/hcsshim/internal/memory"
    10  )
    11  
    12  func setupNewVPMemScenario(ctx context.Context, t *testing.T, size uint64, hostPath, uvmPath string) (*vPMemInfoMulti, *mappedDeviceInfo) {
    13  	t.Helper()
    14  	pmem := newPackedVPMemDevice()
    15  	memReg, err := pmem.Allocate(size)
    16  	if err != nil {
    17  		t.Fatalf("failed to setup multi-mapping VPMem Scenario: %s", err)
    18  	}
    19  	mappedDev := newVPMemMappedDevice(hostPath, uvmPath, size, memReg)
    20  
    21  	if err := pmem.mapVHDLayer(ctx, mappedDev); err != nil {
    22  		t.Errorf("unexpected error: %s", err)
    23  	}
    24  
    25  	// do some basic checks
    26  	md, ok := pmem.mappings[hostPath]
    27  	if !ok {
    28  		t.Fatalf("mapping '%s' not added", hostPath)
    29  	}
    30  	if md.hostPath != hostPath {
    31  		t.Fatalf("expected hostPath=%s, got hostPath=%s", hostPath, md.hostPath)
    32  	}
    33  	if md.uvmPath != uvmPath {
    34  		t.Fatalf("expected uvmPath=%s, got uvmPath=%s", uvmPath, md.uvmPath)
    35  	}
    36  	if md.refCount != 1 {
    37  		t.Fatalf("expected refCount=1, got refCount=%d", md.refCount)
    38  	}
    39  
    40  	return pmem, md
    41  }
    42  
    43  func Test_VPMem_MapDevice_New(t *testing.T) {
    44  	// basic scenario already validated in the helper function
    45  	setupNewVPMemScenario(context.TODO(), t, memory.MiB, "foo", "bar")
    46  }
    47  
    48  func Test_VPMem_UnmapDevice_With_Removal(t *testing.T) {
    49  	pmem, _ := setupNewVPMemScenario(context.TODO(), t, memory.MiB, "foo", "bar")
    50  
    51  	err := pmem.unmapVHDLayer(context.TODO(), "foo")
    52  	if err != nil {
    53  		t.Fatalf("unexpected error: %s", err)
    54  	}
    55  	if m, ok := pmem.mappings["foo"]; ok {
    56  		t.Fatalf("mapping should've been removed: %v", m)
    57  	}
    58  }
    59  
    60  func Test_VPMem_UnmapDevice_Without_Removal(t *testing.T) {
    61  	pmem, mappedDevice := setupNewVPMemScenario(context.TODO(), t, memory.MiB, "foo", "bar")
    62  	err := pmem.mapVHDLayer(context.TODO(), mappedDevice)
    63  	if err != nil {
    64  		t.Fatalf("unexpected error when mapping device: %s", err)
    65  	}
    66  	m, ok := pmem.mappings["foo"]
    67  	if !ok {
    68  		t.Fatalf("mapping not found")
    69  	}
    70  	if m.refCount != 2 {
    71  		t.Fatalf("expected refCount=2, got refCount=%d", m.refCount)
    72  	}
    73  
    74  	err = pmem.unmapVHDLayer(context.TODO(), "foo")
    75  	if err != nil {
    76  		t.Fatalf("error unmapping device: %s", err)
    77  	}
    78  	m, ok = pmem.mappings["foo"]
    79  	if !ok {
    80  		t.Fatalf("mapping should still be present")
    81  	}
    82  	if m.refCount != 1 {
    83  		t.Fatalf("expected refCount=1, got refCount=%d", m.refCount)
    84  	}
    85  }
    86  

View as plain text