|
| 1 | +/* |
| 2 | + Copyright © 2021 The CDI Authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cdi |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestNewCache(t *testing.T) { |
| 28 | + type testCase struct { |
| 29 | + name string |
| 30 | + etc map[string]string |
| 31 | + run map[string]string |
| 32 | + sources map[string]string |
| 33 | + errors map[string]struct{} |
| 34 | + } |
| 35 | + for _, tc := range []*testCase{ |
| 36 | + { |
| 37 | + name: "no spec dirs", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "no spec files", |
| 41 | + etc: map[string]string{}, |
| 42 | + run: map[string]string{}, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "one spec file", |
| 46 | + etc: map[string]string{ |
| 47 | + "vendor1.yaml": ` |
| 48 | +cdiVersion: "0.2.0" |
| 49 | +kind: "vendor1.com/device" |
| 50 | +devices: |
| 51 | + - name: "dev1" |
| 52 | + containerEdits: |
| 53 | + deviceNodes: |
| 54 | + - path: "/dev/vendor1-dev1" |
| 55 | +`, |
| 56 | + }, |
| 57 | + sources: map[string]string{ |
| 58 | + "vendor1.com/device=dev1": "etc/vendor1.yaml", |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "multiple spec files with override", |
| 63 | + etc: map[string]string{ |
| 64 | + "vendor1.yaml": ` |
| 65 | +cdiVersion: "0.2.0" |
| 66 | +kind: "vendor1.com/device" |
| 67 | +devices: |
| 68 | + - name: "dev1" |
| 69 | + containerEdits: |
| 70 | + deviceNodes: |
| 71 | + - path: "/dev/vendor1-dev1" |
| 72 | + - name: "dev2" |
| 73 | + containerEdits: |
| 74 | + deviceNodes: |
| 75 | + - path: "/dev/vendor1-dev2" |
| 76 | +`, |
| 77 | + }, |
| 78 | + run: map[string]string{ |
| 79 | + "vendor1.yaml": ` |
| 80 | +cdiVersion: "0.2.0" |
| 81 | +kind: "vendor1.com/device" |
| 82 | +devices: |
| 83 | + - name: "dev1" |
| 84 | + containerEdits: |
| 85 | + deviceNodes: |
| 86 | + - path: "/dev/vendor1-dev1" |
| 87 | +`, |
| 88 | + }, |
| 89 | + sources: map[string]string{ |
| 90 | + "vendor1.com/device=dev1": "run/vendor1.yaml", |
| 91 | + "vendor1.com/device=dev2": "etc/vendor1.yaml", |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "multiple spec files, with conflicts", |
| 96 | + run: map[string]string{ |
| 97 | + "vendor1.yaml": ` |
| 98 | +cdiVersion: "0.2.0" |
| 99 | +kind: "vendor1.com/device" |
| 100 | +devices: |
| 101 | + - name: "dev1" |
| 102 | + containerEdits: |
| 103 | + deviceNodes: |
| 104 | + - path: "/dev/vendor1-dev1" |
| 105 | + - name: "dev2" |
| 106 | + containerEdits: |
| 107 | + deviceNodes: |
| 108 | + - path: "/dev/vendor1-dev2" |
| 109 | +`, |
| 110 | + "vendor1-other.yaml": ` |
| 111 | +cdiVersion: "0.2.0" |
| 112 | +kind: "vendor1.com/device" |
| 113 | +devices: |
| 114 | + - name: "dev1" |
| 115 | + containerEdits: |
| 116 | + deviceNodes: |
| 117 | + - path: "/dev/vendor1-dev1" |
| 118 | +`, |
| 119 | + }, |
| 120 | + sources: map[string]string{ |
| 121 | + "vendor1.com/device=dev2": "run/vendor1.yaml", |
| 122 | + }, |
| 123 | + errors: map[string]struct{}{ |
| 124 | + "run/vendor1.yaml": {}, |
| 125 | + "run/vendor1-other.yaml": {}, |
| 126 | + }, |
| 127 | + }, |
| 128 | + } { |
| 129 | + t.Run(tc.name, func(t *testing.T) { |
| 130 | + var ( |
| 131 | + dir string |
| 132 | + err error |
| 133 | + cache *Cache |
| 134 | + ) |
| 135 | + if tc.etc != nil || tc.run != nil { |
| 136 | + dir, err = createSpecDirs(t, tc.etc, tc.run) |
| 137 | + if err != nil { |
| 138 | + t.Errorf("failed to create test directory: %v", err) |
| 139 | + return |
| 140 | + } |
| 141 | + } |
| 142 | + cache, err = NewCache(WithSpecDirs( |
| 143 | + filepath.Join(dir, "etc"), |
| 144 | + filepath.Join(dir, "run")), |
| 145 | + ) |
| 146 | + |
| 147 | + if len(tc.errors) == 0 { |
| 148 | + require.Nil(t, err) |
| 149 | + } |
| 150 | + require.NotNil(t, cache) |
| 151 | + |
| 152 | + for name, dev := range cache.devices { |
| 153 | + require.Equal(t, filepath.Join(dir, tc.sources[name]), |
| 154 | + dev.GetSpec().GetPath()) |
| 155 | + } |
| 156 | + for name, path := range tc.sources { |
| 157 | + dev := cache.devices[name] |
| 158 | + require.NotNil(t, dev) |
| 159 | + require.Equal(t, filepath.Join(dir, path), |
| 160 | + dev.GetSpec().GetPath()) |
| 161 | + } |
| 162 | + |
| 163 | + for path := range tc.errors { |
| 164 | + fullPath := filepath.Join(dir, path) |
| 165 | + _, ok := cache.errors[fullPath] |
| 166 | + require.True(t, ok) |
| 167 | + } |
| 168 | + for fullPath := range cache.errors { |
| 169 | + path, err := filepath.Rel(dir, fullPath) |
| 170 | + require.Nil(t, err) |
| 171 | + _, ok := tc.errors[path] |
| 172 | + require.True(t, ok) |
| 173 | + } |
| 174 | + }) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +// Create and populate automatically cleaned up spec directories. |
| 179 | +func createSpecDirs(t *testing.T, etc, run map[string]string) (string, error) { |
| 180 | + return mkTestDir(t, map[string]map[string]string{ |
| 181 | + "etc": etc, |
| 182 | + "run": run, |
| 183 | + }) |
| 184 | +} |
| 185 | + |
| 186 | +// Update spec directories with new data. |
| 187 | +func updateSpecDirs(t *testing.T, dir string, etc, run map[string]string) error { |
| 188 | + updates := map[string]map[string]string{ |
| 189 | + "etc": {}, |
| 190 | + "run": {}, |
| 191 | + } |
| 192 | + for sub, entries := range map[string]map[string]string{ |
| 193 | + "etc": etc, |
| 194 | + "run": run, |
| 195 | + } { |
| 196 | + path := filepath.Join(dir, sub) |
| 197 | + for name, data := range entries { |
| 198 | + if data == "remove" { |
| 199 | + os.Remove(filepath.Join(path, name)) |
| 200 | + } else { |
| 201 | + updates[sub][name] = data |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + return updateTestDir(t, dir, updates) |
| 206 | +} |
0 commit comments