mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-19 07:14:22 +01:00
pkg/cgroups: implement functions to get information from a host device
Add functions to convert a host device to a cgroup device or linux device, the first one is used to update the device cgroup and the second one to update the resources in the OCI spec. Signed-off-by: Julio Montes <julio.montes@intel.com> Signed-off-by: Peng Tao <bergwolf@hyper.sh>
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
package cgroups
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -102,3 +104,60 @@ func TestValidCgroupPath(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDeviceToCgroupDevice(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
f, err := ioutil.TempFile("", "device")
|
||||
assert.NoError(err)
|
||||
f.Close()
|
||||
|
||||
// fail: regular file to device
|
||||
dev, err := DeviceToCgroupDevice(f.Name())
|
||||
assert.Error(err)
|
||||
assert.Nil(dev)
|
||||
|
||||
// fail: no such file
|
||||
os.Remove(f.Name())
|
||||
dev, err = DeviceToCgroupDevice(f.Name())
|
||||
assert.Error(err)
|
||||
assert.Nil(dev)
|
||||
|
||||
devPath := "/dev/null"
|
||||
if _, err := os.Stat(devPath); os.IsNotExist(err) {
|
||||
t.Skipf("no such device: %v", devPath)
|
||||
return
|
||||
}
|
||||
dev, err = DeviceToCgroupDevice(devPath)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(dev)
|
||||
assert.Equal(dev.Type, 'c')
|
||||
assert.Equal(dev.Path, devPath)
|
||||
assert.NotZero(dev.Major)
|
||||
assert.NotZero(dev.Minor)
|
||||
assert.NotEmpty(dev.Permissions)
|
||||
assert.NotZero(dev.FileMode)
|
||||
assert.Zero(dev.Uid)
|
||||
assert.Zero(dev.Gid)
|
||||
assert.True(dev.Allow)
|
||||
}
|
||||
|
||||
func TestDeviceToLinuxDevice(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
devPath := "/dev/null"
|
||||
if _, err := os.Stat(devPath); os.IsNotExist(err) {
|
||||
t.Skipf("no such device: %v", devPath)
|
||||
return
|
||||
}
|
||||
dev, err := DeviceToLinuxDevice(devPath)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(dev)
|
||||
assert.Equal(dev.Type, "c")
|
||||
assert.NotNil(dev.Major)
|
||||
assert.NotZero(*dev.Major)
|
||||
assert.NotNil(dev.Minor)
|
||||
assert.NotZero(*dev.Minor)
|
||||
assert.NotEmpty(dev.Access)
|
||||
assert.True(dev.Allow)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user