github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/pkg/cgroups/utils_test.go (about) 1 // Copyright (c) 2020 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 6 package cgroups 7 8 import ( 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "strings" 13 "testing" 14 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestIsSystemdCgroup(t *testing.T) { 19 assert := assert.New(t) 20 21 tests := []struct { 22 path string 23 expected bool 24 }{ 25 {"foo.slice:kata:afhts2e5d4g5s", true}, 26 {"system.slice:kata:afhts2e5d4g5s", true}, 27 {"/kata/afhts2e5d4g5s", false}, 28 {"a:b:c:d", false}, 29 {":::", false}, 30 {"", false}, 31 {":", false}, 32 {"::", false}, 33 {":::", false}, 34 {"a:b", false}, 35 {"a:b:", false}, 36 {":a:b", false}, 37 {"@:@:@", false}, 38 } 39 40 for _, t := range tests { 41 assert.Equal(t.expected, IsSystemdCgroup(t.path), "invalid systemd cgroup path: %v", t.path) 42 } 43 } 44 45 func TestValidCgroupPath(t *testing.T) { 46 assert := assert.New(t) 47 48 for _, t := range []struct { 49 path string 50 systemdCgroup bool 51 error bool 52 }{ 53 // empty paths 54 {"../../../", false, false}, 55 {"../", false, false}, 56 {".", false, false}, 57 {"../../../", false, false}, 58 {"./../", false, false}, 59 60 // valid no-systemd paths 61 {"../../../foo", false, false}, 62 {"/../hi", false, false}, 63 {"/../hi/foo", false, false}, 64 {"o / m /../ g", false, false}, 65 66 // invalid systemd paths 67 {"o / m /../ g", true, true}, 68 {"slice:kata", true, true}, 69 {"/kata/afhts2e5d4g5s", true, true}, 70 {"a:b:c:d", true, true}, 71 {":::", true, true}, 72 {"", true, true}, 73 {":", true, true}, 74 {"::", true, true}, 75 {":::", true, true}, 76 {"a:b", true, true}, 77 {"a:b:", true, true}, 78 {":a:b", true, true}, 79 {"@:@:@", true, true}, 80 81 // valid systemd paths 82 {"x.slice:kata:55555", true, false}, 83 {"system.slice:kata:afhts2e5d4g5s", true, false}, 84 } { 85 path, err := ValidCgroupPath(t.path, t.systemdCgroup) 86 if t.error { 87 assert.Error(err) 88 continue 89 } else { 90 assert.NoError(err) 91 } 92 93 if filepath.IsAbs(t.path) { 94 cleanPath := filepath.Dir(filepath.Clean(t.path)) 95 assert.True(strings.HasPrefix(path, cleanPath), 96 "%v should have prefix %v", cleanPath) 97 } else if t.systemdCgroup { 98 assert.Equal(t.path, path) 99 } else { 100 assert.True(strings.HasPrefix(path, "/"+CgroupKataPrefix) || 101 strings.HasPrefix(path, DefaultCgroupPath), 102 "%v should have prefix /%v or %v", path, CgroupKataPrefix, DefaultCgroupPath) 103 } 104 } 105 106 } 107 108 func TestDeviceToCgroupDevice(t *testing.T) { 109 assert := assert.New(t) 110 111 f, err := ioutil.TempFile("", "device") 112 assert.NoError(err) 113 f.Close() 114 115 // fail: regular file to device 116 dev, err := DeviceToCgroupDevice(f.Name()) 117 assert.Error(err) 118 assert.Nil(dev) 119 120 // fail: no such file 121 os.Remove(f.Name()) 122 dev, err = DeviceToCgroupDevice(f.Name()) 123 assert.Error(err) 124 assert.Nil(dev) 125 126 devPath := "/dev/null" 127 if _, err := os.Stat(devPath); os.IsNotExist(err) { 128 t.Skipf("no such device: %v", devPath) 129 return 130 } 131 dev, err = DeviceToCgroupDevice(devPath) 132 assert.NoError(err) 133 assert.NotNil(dev) 134 assert.Equal(dev.Type, 'c') 135 assert.Equal(dev.Path, devPath) 136 assert.NotZero(dev.Major) 137 assert.NotZero(dev.Minor) 138 assert.NotEmpty(dev.Permissions) 139 assert.NotZero(dev.FileMode) 140 assert.Zero(dev.Uid) 141 assert.Zero(dev.Gid) 142 assert.True(dev.Allow) 143 } 144 145 func TestDeviceToLinuxDevice(t *testing.T) { 146 assert := assert.New(t) 147 148 devPath := "/dev/null" 149 if _, err := os.Stat(devPath); os.IsNotExist(err) { 150 t.Skipf("no such device: %v", devPath) 151 return 152 } 153 dev, err := DeviceToLinuxDevice(devPath) 154 assert.NoError(err) 155 assert.NotNil(dev) 156 assert.Equal(dev.Type, "c") 157 assert.NotNil(dev.Major) 158 assert.NotZero(*dev.Major) 159 assert.NotNil(dev.Minor) 160 assert.NotZero(*dev.Minor) 161 assert.NotEmpty(dev.Access) 162 assert.True(dev.Allow) 163 }