...

Source file src/github.com/Microsoft/hcsshim/internal/guest/runtime/hcsv2/uvm_state_test.go

Documentation: github.com/Microsoft/hcsshim/internal/guest/runtime/hcsv2

     1  //go:build linux
     2  // +build linux
     3  
     4  package hcsv2
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func Test_Add_Remove_RWDevice(t *testing.T) {
    11  	hm := newHostMounts()
    12  	mountPath := "/run/gcs/c/abcd"
    13  	sourcePath := "/dev/sda"
    14  
    15  	if err := hm.AddRWDevice(mountPath, sourcePath, false); err != nil {
    16  		t.Fatalf("unexpected error adding RW device: %s", err)
    17  	}
    18  	if err := hm.RemoveRWDevice(mountPath, sourcePath); err != nil {
    19  		t.Fatalf("unexpected error removing RW device: %s", err)
    20  	}
    21  }
    22  
    23  func Test_Cannot_AddRWDevice_Twice(t *testing.T) {
    24  	hm := newHostMounts()
    25  	mountPath := "/run/gcs/c/abc"
    26  	sourcePath := "/dev/sda"
    27  
    28  	if err := hm.AddRWDevice(mountPath, sourcePath, false); err != nil {
    29  		t.Fatalf("unexpected error: %s", err)
    30  	}
    31  	if err := hm.AddRWDevice(mountPath, sourcePath, false); err == nil {
    32  		t.Fatalf("expected error adding %q for the second time", mountPath)
    33  	}
    34  }
    35  
    36  func Test_Cannot_RemoveRWDevice_Wrong_Source(t *testing.T) {
    37  	hm := newHostMounts()
    38  	mountPath := "/run/gcs/c/abcd"
    39  	sourcePath := "/dev/sda"
    40  	wrongSource := "/dev/sdb"
    41  	if err := hm.AddRWDevice(mountPath, sourcePath, false); err != nil {
    42  		t.Fatalf("unexpected error: %s", err)
    43  	}
    44  	if err := hm.RemoveRWDevice(mountPath, wrongSource); err == nil {
    45  		t.Fatalf("expected error removing wrong source %s", wrongSource)
    46  	}
    47  }
    48  
    49  func Test_HostMounts_IsEncrypted(t *testing.T) {
    50  	hm := newHostMounts()
    51  	encryptedPath := "/run/gcs/c/encrypted"
    52  	encryptedSource := "/dev/sda"
    53  	if err := hm.AddRWDevice(encryptedPath, encryptedSource, true); err != nil {
    54  		t.Fatalf("unexpected error adding RW device: %s", err)
    55  	}
    56  	nestedUnencrypted := "/run/gcs/c/encrypted/unencrypted"
    57  	unencryptedSource := "/dev/sdb"
    58  	if err := hm.AddRWDevice(nestedUnencrypted, unencryptedSource, false); err != nil {
    59  		t.Fatalf("unexpected error adding RW device: %s", err)
    60  	}
    61  
    62  	for _, tc := range []struct {
    63  		name     string
    64  		testPath string
    65  		expected bool
    66  	}{
    67  		{
    68  			name:     "ValidSubPath1",
    69  			testPath: "/run/gcs/c/encrypted/nested",
    70  			expected: true,
    71  		},
    72  		{
    73  			name:     "ValidSubPath2",
    74  			testPath: "/run/gcs/c/encrypted/../encrypted/nested",
    75  			expected: true,
    76  		},
    77  		{
    78  			name:     "NotSubPath1",
    79  			testPath: "/run/gcs/c/abcdef",
    80  			expected: false,
    81  		},
    82  		{
    83  			name:     "NotSubPath2",
    84  			testPath: "/run/gcs/c",
    85  			expected: false,
    86  		},
    87  		{
    88  			name:     "NotSubPath3",
    89  			testPath: "/run/gcs/c/../abcd",
    90  			expected: false,
    91  		},
    92  		{
    93  			name:     "NestedUnencrypted",
    94  			testPath: "/run/gcs/c/encrypted/unencrypted/foo",
    95  			expected: false,
    96  		},
    97  		{
    98  			name:     "SamePathEncrypted",
    99  			testPath: "/run/gcs/c/encrypted",
   100  			expected: true,
   101  		},
   102  	} {
   103  		t.Run(tc.name, func(t *testing.T) {
   104  			encrypted := hm.IsEncrypted(tc.testPath)
   105  			if encrypted != tc.expected {
   106  				t.Fatalf("expected encrypted %t, got %t", tc.expected, encrypted)
   107  			}
   108  		})
   109  	}
   110  }
   111  

View as plain text