|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +/* |
| 5 | + * Copyright (C) 2015-2026 Open Containers Initiative Contributors |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +// This code originally comes from runc and was taken from this tree: |
| 21 | +// <https://github.com/opencontainers/runc/tree/v1.4.0/libcontainer/devices>. |
| 22 | + |
| 23 | +package devices |
| 24 | + |
| 25 | +import ( |
| 26 | + "errors" |
| 27 | + "io/fs" |
| 28 | + "os" |
| 29 | + "runtime" |
| 30 | + "testing" |
| 31 | + |
| 32 | + "github.com/opencontainers/cgroups/devices/config" |
| 33 | + "golang.org/x/sys/unix" |
| 34 | +) |
| 35 | + |
| 36 | +func cleanupTest() { |
| 37 | + unixLstat = unix.Lstat |
| 38 | + osReadDir = os.ReadDir |
| 39 | +} |
| 40 | + |
| 41 | +func TestDeviceFromPathLstatFailure(t *testing.T) { |
| 42 | + testError := errors.New("test error") |
| 43 | + |
| 44 | + // Override unix.Lstat to inject error. |
| 45 | + unixLstat = func(path string, stat *unix.Stat_t) error { |
| 46 | + return testError |
| 47 | + } |
| 48 | + defer cleanupTest() |
| 49 | + |
| 50 | + _, err := DeviceFromPath("", "") |
| 51 | + if !errors.Is(err, testError) { |
| 52 | + t.Fatalf("Unexpected error %v, expected %v", err, testError) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestHostDevicesIoutilReadDirFailure(t *testing.T) { |
| 57 | + testError := errors.New("test error") |
| 58 | + |
| 59 | + // Override os.ReadDir to inject error. |
| 60 | + osReadDir = func(dirname string) ([]fs.DirEntry, error) { |
| 61 | + return nil, testError |
| 62 | + } |
| 63 | + defer cleanupTest() |
| 64 | + |
| 65 | + _, err := HostDevices() |
| 66 | + if !errors.Is(err, testError) { |
| 67 | + t.Fatalf("Unexpected error %v, expected %v", err, testError) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestHostDevicesIoutilReadDirDeepFailure(t *testing.T) { |
| 72 | + testError := errors.New("test error") |
| 73 | + called := false |
| 74 | + |
| 75 | + // Override os.ReadDir to inject error after the first call. |
| 76 | + osReadDir = func(dirname string) ([]fs.DirEntry, error) { |
| 77 | + if called { |
| 78 | + return nil, testError |
| 79 | + } |
| 80 | + called = true |
| 81 | + |
| 82 | + // Provoke a second call. |
| 83 | + fi, err := os.Stat("/tmp") |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("Unexpected error %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + return []fs.DirEntry{fs.FileInfoToDirEntry(fi)}, nil |
| 89 | + } |
| 90 | + defer cleanupTest() |
| 91 | + |
| 92 | + _, err := HostDevices() |
| 93 | + if !errors.Is(err, testError) { |
| 94 | + t.Fatalf("Unexpected error %v, expected %v", err, testError) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestHostDevicesAllValid(t *testing.T) { |
| 99 | + devices, err := HostDevices() |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("failed to get host devices: %v", err) |
| 102 | + } |
| 103 | + |
| 104 | + for _, device := range devices { |
| 105 | + // Devices can't have major number 0 on Linux. |
| 106 | + if device.Major == 0 { |
| 107 | + logFn := t.Logf |
| 108 | + if runtime.GOOS == "linux" { |
| 109 | + logFn = t.Errorf |
| 110 | + } |
| 111 | + logFn("device entry %+v has zero major number", device) |
| 112 | + } |
| 113 | + switch device.Type { |
| 114 | + case config.BlockDevice, config.CharDevice: |
| 115 | + case config.FifoDevice: |
| 116 | + t.Logf("fifo devices shouldn't show up from HostDevices") |
| 117 | + fallthrough |
| 118 | + default: |
| 119 | + t.Errorf("device entry %+v has unexpected type %v", device, device.Type) |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments