diff --git a/devices/devices.go b/devices/devices.go index beb558cc..539d6f22 100644 --- a/devices/devices.go +++ b/devices/devices.go @@ -17,6 +17,11 @@ var ( ErrNotADeviceNode = errors.New("not a device node") ) +// Testing dependencies +var ( + osLstat = os.Lstat +) + type Device struct { Type rune `json:"type,omitempty"` Path string `json:"path,omitempty"` // It is fine if this is an empty string in the case that you are using Wildcards @@ -42,7 +47,7 @@ func (device *Device) GetCgroupAllowString() string { // Given the path to a device and it's cgroup_permissions(which cannot be easilly queried) look up the information about a linux device and return that information as a Device struct. func GetDevice(path, cgroupPermissions string) (*Device, error) { - fileInfo, err := os.Lstat(path) + fileInfo, err := osLstat(path) if err != nil { return nil, err } diff --git a/devices/devices_test.go b/devices/devices_test.go new file mode 100644 index 00000000..288c765d --- /dev/null +++ b/devices/devices_test.go @@ -0,0 +1,21 @@ +package devices + +import ( + "errors" + "os" + "testing" +) + +func TestGetDeviceLstatFailure(t *testing.T) { + testError := errors.New("test error") + + // Override os.Lstat to inject error. + osLstat = func(path string) (os.FileInfo, error) { + return nil, testError + } + + _, err := GetDevice("", "") + if err != testError { + t.Fatalf("Unexpected error %v, expected %v", err, testError) + } +}