|
| 1 | +// Copyright 2023 Intel Corporation. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package pluginutils |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "path" |
| 20 | + "path/filepath" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "k8s.io/utils/strings/slices" |
| 24 | +) |
| 25 | + |
| 26 | +func createTestFiles(t *testing.T, root, linkFile string, bypathFiles []string) (string, string) { |
| 27 | + drmPath := path.Join(root, "sys/class/drm/card0") |
| 28 | + devPath := path.Join(root, "sys", linkFile) |
| 29 | + byPath := path.Join(root, "by-path") |
| 30 | + |
| 31 | + if linkFile != "" { |
| 32 | + if err := os.MkdirAll(filepath.Dir(devPath), os.ModePerm); err != nil { |
| 33 | + t.Fatal("Couldn't create test dev dir", err) |
| 34 | + } |
| 35 | + |
| 36 | + if err := os.MkdirAll(filepath.Dir(drmPath), os.ModePerm); err != nil { |
| 37 | + t.Fatal("Couldn't create test drm dir", err) |
| 38 | + } |
| 39 | + |
| 40 | + if err := os.WriteFile(devPath, []byte{0}, os.ModePerm); err != nil { |
| 41 | + t.Fatal("Couldn't create card file", err) |
| 42 | + } |
| 43 | + |
| 44 | + if err := os.Symlink(devPath, drmPath); err != nil { |
| 45 | + t.Fatal("Couldn't create symlink between pci path and sysfs drm path") |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if len(bypathFiles) > 0 { |
| 50 | + if err := os.MkdirAll(byPath, os.ModePerm); err != nil { |
| 51 | + t.Fatal("Mkdir failed:", byPath) |
| 52 | + } |
| 53 | + |
| 54 | + for _, f := range bypathFiles { |
| 55 | + if err := os.WriteFile(path.Join(byPath, f), []byte{1}, os.ModePerm); err != nil { |
| 56 | + t.Fatal("WriteFile failed:", path.Join(byPath, f)) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return drmPath, byPath |
| 62 | +} |
| 63 | + |
| 64 | +func TestBypath(t *testing.T) { |
| 65 | + type testData struct { |
| 66 | + linkpath string |
| 67 | + bypathFiles []string |
| 68 | + mountCount int |
| 69 | + } |
| 70 | + |
| 71 | + tds := []testData{ |
| 72 | + { |
| 73 | + "00.10.2/00.334.302/0.0.1.00/0-1-2-3-3342/drm/card0", |
| 74 | + []string{"pci-0-1-2-3-3342-card", "pci-0-1-2-3-3342-render"}, |
| 75 | + 2, |
| 76 | + }, |
| 77 | + { |
| 78 | + "00.10.2/00.334.302/0.0.1.00/0-1-2-3-4343/drm/card0", |
| 79 | + []string{"pci-0-1-2-3-4444-card", "pci-0-1-2-3-4444-render"}, |
| 80 | + 0, |
| 81 | + }, |
| 82 | + { |
| 83 | + "00.10.2/00.334.302/0.0.1.00/0-1-2-3-3342/drm", |
| 84 | + []string{"pci-0-1-2-3-4444-card", "pci-0-1-2-3-4444-render"}, |
| 85 | + 0, |
| 86 | + }, |
| 87 | + { |
| 88 | + "", |
| 89 | + []string{"pci-0-1-2-3-3342-card", "pci-0-1-2-3-3342-render"}, |
| 90 | + 0, |
| 91 | + }, |
| 92 | + { |
| 93 | + "00.10.2/00.334.302/0.0.1.00/0-1-2-3-3342/drm/card0", |
| 94 | + []string{}, |
| 95 | + 0, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + for _, td := range tds { |
| 100 | + root, err := os.MkdirTemp("", "test_bypath_mounting") |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("can't create temporary directory: %+v", err) |
| 103 | + } |
| 104 | + // dirs/files need to be removed for the next test |
| 105 | + defer os.RemoveAll(root) |
| 106 | + |
| 107 | + drmPath, byPath := createTestFiles(t, root, td.linkpath, td.bypathFiles) |
| 108 | + |
| 109 | + mounts := BypathMountsForPci(ReadPciAddressForCard(drmPath), byPath) |
| 110 | + |
| 111 | + if len(mounts) != td.mountCount { |
| 112 | + t.Errorf("Wrong number of mounts %d vs. %d", len(mounts), td.mountCount) |
| 113 | + } |
| 114 | + |
| 115 | + absPaths := []string{} |
| 116 | + for _, link := range td.bypathFiles { |
| 117 | + absPaths = append(absPaths, path.Join(byPath, link)) |
| 118 | + } |
| 119 | + |
| 120 | + for _, mount := range mounts { |
| 121 | + if !slices.Contains(absPaths, mount.ContainerPath) { |
| 122 | + t.Errorf("containerpath is incorrect: %s", mount.ContainerPath) |
| 123 | + } |
| 124 | + |
| 125 | + if !slices.Contains(absPaths, mount.HostPath) { |
| 126 | + t.Errorf("hostpath is incorrect: %s", mount.HostPath) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments