2015-10-24 04:50:24 +08:00
|
|
|
// +build linux freebsd
|
|
|
|
|
2014-09-05 07:34:34 +08:00
|
|
|
package devices
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2017-07-21 22:54:47 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2014-09-05 07:34:34 +08:00
|
|
|
)
|
|
|
|
|
2015-02-03 20:27:21 +08:00
|
|
|
func TestDeviceFromPathLstatFailure(t *testing.T) {
|
2014-09-05 07:34:34 +08:00
|
|
|
testError := errors.New("test error")
|
|
|
|
|
2017-07-21 22:54:47 +08:00
|
|
|
// Override unix.Lstat to inject error.
|
|
|
|
unixLstat = func(path string, stat *unix.Stat_t) error {
|
|
|
|
return testError
|
2014-09-05 07:34:34 +08:00
|
|
|
}
|
|
|
|
|
2015-02-03 20:27:21 +08:00
|
|
|
_, err := DeviceFromPath("", "")
|
2014-09-05 07:34:34 +08:00
|
|
|
if err != testError {
|
|
|
|
t.Fatalf("Unexpected error %v, expected %v", err, testError)
|
|
|
|
}
|
|
|
|
}
|
2014-09-05 08:00:09 +08:00
|
|
|
|
2015-02-03 20:27:21 +08:00
|
|
|
func TestHostDevicesIoutilReadDirFailure(t *testing.T) {
|
2014-09-05 08:00:09 +08:00
|
|
|
testError := errors.New("test error")
|
|
|
|
|
|
|
|
// Override ioutil.ReadDir to inject error.
|
|
|
|
ioutilReadDir = func(dirname string) ([]os.FileInfo, error) {
|
|
|
|
return nil, testError
|
|
|
|
}
|
|
|
|
|
2015-02-03 20:27:21 +08:00
|
|
|
_, err := HostDevices()
|
2014-09-05 08:00:09 +08:00
|
|
|
if err != testError {
|
|
|
|
t.Fatalf("Unexpected error %v, expected %v", err, testError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 20:27:21 +08:00
|
|
|
func TestHostDevicesIoutilReadDirDeepFailure(t *testing.T) {
|
2014-09-05 08:00:09 +08:00
|
|
|
testError := errors.New("test error")
|
|
|
|
called := false
|
|
|
|
|
|
|
|
// Override ioutil.ReadDir to inject error after the first call.
|
|
|
|
ioutilReadDir = func(dirname string) ([]os.FileInfo, error) {
|
|
|
|
if called {
|
|
|
|
return nil, testError
|
|
|
|
}
|
|
|
|
called = true
|
|
|
|
|
|
|
|
// Provoke a second call.
|
|
|
|
fi, err := os.Lstat("/tmp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []os.FileInfo{fi}, nil
|
|
|
|
}
|
|
|
|
|
2015-02-03 20:27:21 +08:00
|
|
|
_, err := HostDevices()
|
2014-09-05 08:00:09 +08:00
|
|
|
if err != testError {
|
|
|
|
t.Fatalf("Unexpected error %v, expected %v", err, testError)
|
|
|
|
}
|
|
|
|
}
|