mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-12 09:46:25 +08:00
f6a0899b7f
Do this for all errors except one from unix.*.
This fixes a bunch of errorlint warnings, like these
libcontainer/generic_error.go:25:15: type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)
if le, ok := err.(Error); ok {
^
libcontainer/factory_linux_test.go:145:14: type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)
lerr, ok := err.(Error)
^
libcontainer/state_linux_test.go:28:11: type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)
_, ok := err.(*stateTransitionError)
^
libcontainer/seccomp/patchbpf/enosys_linux.go:88:4: switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors (errorlint)
switch err {
^
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
97 lines
2.1 KiB
Go
97 lines
2.1 KiB
Go
// +build !windows
|
|
|
|
package devices
|
|
|
|
import (
|
|
"errors"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func cleanupTest() {
|
|
unixLstat = unix.Lstat
|
|
ioutilReadDir = ioutil.ReadDir
|
|
}
|
|
|
|
func TestDeviceFromPathLstatFailure(t *testing.T) {
|
|
testError := errors.New("test error")
|
|
|
|
// Override unix.Lstat to inject error.
|
|
unixLstat = func(path string, stat *unix.Stat_t) error {
|
|
return testError
|
|
}
|
|
defer cleanupTest()
|
|
|
|
_, err := DeviceFromPath("", "")
|
|
if !errors.Is(err, testError) {
|
|
t.Fatalf("Unexpected error %v, expected %v", err, testError)
|
|
}
|
|
}
|
|
|
|
func TestHostDevicesIoutilReadDirFailure(t *testing.T) {
|
|
testError := errors.New("test error")
|
|
|
|
// Override ioutil.ReadDir to inject error.
|
|
ioutilReadDir = func(dirname string) ([]os.FileInfo, error) {
|
|
return nil, testError
|
|
}
|
|
defer cleanupTest()
|
|
|
|
_, err := HostDevices()
|
|
if !errors.Is(err, testError) {
|
|
t.Fatalf("Unexpected error %v, expected %v", err, testError)
|
|
}
|
|
}
|
|
|
|
func TestHostDevicesIoutilReadDirDeepFailure(t *testing.T) {
|
|
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
|
|
}
|
|
defer cleanupTest()
|
|
|
|
_, err := HostDevices()
|
|
if !errors.Is(err, testError) {
|
|
t.Fatalf("Unexpected error %v, expected %v", err, testError)
|
|
}
|
|
}
|
|
|
|
func TestHostDevicesAllValid(t *testing.T) {
|
|
devices, err := HostDevices()
|
|
if err != nil {
|
|
t.Fatalf("failed to get host devices: %v", err)
|
|
}
|
|
|
|
for _, device := range devices {
|
|
// Devices can't have major number 0.
|
|
if device.Major == 0 {
|
|
t.Errorf("device entry %+v has zero major number", device)
|
|
}
|
|
switch device.Type {
|
|
case BlockDevice, CharDevice:
|
|
case FifoDevice:
|
|
t.Logf("fifo devices shouldn't show up from HostDevices")
|
|
fallthrough
|
|
default:
|
|
t.Errorf("device entry %+v has unexpected type %v", device, device.Type)
|
|
}
|
|
}
|
|
}
|