|
4 | 4 | "os" |
5 | 5 | "path/filepath" |
6 | 6 | "slices" |
| 7 | + "strconv" |
7 | 8 | "strings" |
8 | 9 | "testing" |
9 | 10 |
|
@@ -126,31 +127,176 @@ func TestIntelRdtSet(t *testing.T) { |
126 | 127 | } |
127 | 128 |
|
128 | 129 | func TestApply(t *testing.T) { |
129 | | - helper := NewIntelRdtTestUtil(t) |
| 130 | + const pid = 1234 |
| 131 | + tests := []struct { |
| 132 | + name string |
| 133 | + config configs.IntelRdt |
| 134 | + precreateClos bool |
| 135 | + isError bool |
| 136 | + postApplyAssert func(*Manager) |
| 137 | + }{ |
| 138 | + { |
| 139 | + name: "failure because non-pre-existing CLOS", |
| 140 | + config: configs.IntelRdt{ |
| 141 | + ClosID: "non-existing-clos", |
| 142 | + }, |
| 143 | + isError: true, |
| 144 | + postApplyAssert: func(m *Manager) { |
| 145 | + if _, err := os.Stat(m.path); err == nil { |
| 146 | + t.Fatal("closid dir should not exist") |
| 147 | + } |
| 148 | + }, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "CLOS dir should be created if some schema has been specified", |
| 152 | + config: configs.IntelRdt{ |
| 153 | + ClosID: "clos-to-be-created", |
| 154 | + L3CacheSchema: "L3:0=f", |
| 155 | + }, |
| 156 | + postApplyAssert: func(m *Manager) { |
| 157 | + pids, err := getIntelRdtParamString(m.path, "tasks") |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("failed to read tasks file: %v", err) |
| 160 | + } |
| 161 | + if pids != strconv.Itoa(pid) { |
| 162 | + t.Fatalf("unexpected tasks file, expected '%d', got %q", pid, pids) |
| 163 | + } |
| 164 | + }, |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "clos and monitoring group should be created if EnableMonitoring is true", |
| 168 | + config: configs.IntelRdt{ |
| 169 | + EnableMonitoring: true, |
| 170 | + }, |
| 171 | + precreateClos: true, |
| 172 | + postApplyAssert: func(m *Manager) { |
| 173 | + pids, err := getIntelRdtParamString(m.path, "tasks") |
| 174 | + if err != nil { |
| 175 | + t.Fatalf("failed to read tasks file: %v", err) |
| 176 | + } |
| 177 | + if pids != strconv.Itoa(pid) { |
| 178 | + t.Fatalf("unexpected tasks file, expected '%d', got %q", pid, pids) |
| 179 | + } |
| 180 | + }, |
| 181 | + }, |
| 182 | + } |
130 | 183 |
|
131 | | - const closID = "test-clos" |
132 | | - closPath := filepath.Join(helper.IntelRdtPath, closID) |
| 184 | + for _, tt := range tests { |
| 185 | + t.Run(tt.name, func(t *testing.T) { |
| 186 | + NewIntelRdtTestUtil(t) |
| 187 | + id := "abcd-1234" |
| 188 | + closPath := filepath.Join(intelRdtRoot, id) |
| 189 | + if tt.config.ClosID != "" { |
| 190 | + closPath = filepath.Join(intelRdtRoot, tt.config.ClosID) |
| 191 | + } |
133 | 192 |
|
134 | | - helper.config.IntelRdt.ClosID = closID |
135 | | - intelrdt := newManager(helper.config, "container-1", closPath) |
136 | | - if err := intelrdt.Apply(1234); err == nil { |
137 | | - t.Fatal("unexpected success when applying pid") |
138 | | - } |
139 | | - if _, err := os.Stat(closPath); err == nil { |
140 | | - t.Fatal("closid dir should not exist") |
| 193 | + if tt.precreateClos { |
| 194 | + if err := os.MkdirAll(filepath.Join(closPath, "mon_groups"), 0o755); err != nil { |
| 195 | + t.Fatal(err) |
| 196 | + } |
| 197 | + } |
| 198 | + m := newManager(&configs.Config{IntelRdt: &tt.config}, id, closPath) |
| 199 | + err := m.Apply(pid) |
| 200 | + if tt.isError && err == nil { |
| 201 | + t.Fatal("expected error, got nil") |
| 202 | + } else if !tt.isError && err != nil { |
| 203 | + t.Fatalf("unexpected error: %v", err) |
| 204 | + } |
| 205 | + tt.postApplyAssert(m) |
| 206 | + }) |
141 | 207 | } |
| 208 | +} |
142 | 209 |
|
143 | | - // Dir should be created if some schema has been specified |
144 | | - intelrdt.config.IntelRdt.L3CacheSchema = "L3:0=f" |
145 | | - if err := intelrdt.Apply(1235); err != nil { |
146 | | - t.Fatalf("Apply() failed: %v", err) |
147 | | - } |
| 210 | +func TestDestroy(t *testing.T) { |
| 211 | + tests := []struct { |
| 212 | + name string |
| 213 | + config configs.IntelRdt |
| 214 | + testFunc func(*Manager) |
| 215 | + }{ |
| 216 | + { |
| 217 | + name: "per-container CLOS dir should be removed", |
| 218 | + testFunc: func(m *Manager) { |
| 219 | + closPath := m.path |
| 220 | + if _, err := os.Stat(closPath); err != nil { |
| 221 | + t.Fatal("CLOS dir should exist") |
| 222 | + } |
| 223 | + // Need to delete the tasks file so that the dir is empty |
| 224 | + if err := os.Remove(filepath.Join(closPath, "tasks")); err != nil { |
| 225 | + t.Fatalf("failed to remove tasks file: %v", err) |
| 226 | + } |
| 227 | + if err := m.Destroy(); err != nil { |
| 228 | + t.Fatalf("Destroy() failed: %v", err) |
| 229 | + } |
| 230 | + if _, err := os.Stat(closPath); err == nil { |
| 231 | + t.Fatal("CLOS dir should not exist") |
| 232 | + } |
| 233 | + }, |
| 234 | + }, |
| 235 | + { |
| 236 | + name: "pre-existing CLOS should not be removed", |
| 237 | + config: configs.IntelRdt{ |
| 238 | + ClosID: "pre-existing-clos", |
| 239 | + }, |
| 240 | + testFunc: func(m *Manager) { |
| 241 | + closPath := m.path |
148 | 242 |
|
149 | | - pids, err := getIntelRdtParamString(intelrdt.GetPath(), "tasks") |
150 | | - if err != nil { |
151 | | - t.Fatalf("failed to read tasks file: %v", err) |
| 243 | + if _, err := os.Stat(closPath); err != nil { |
| 244 | + t.Fatal("CLOS dir should exist") |
| 245 | + } |
| 246 | + if err := m.Destroy(); err != nil { |
| 247 | + t.Fatalf("Destroy() failed: %v", err) |
| 248 | + } |
| 249 | + if _, err := os.Stat(closPath); err != nil { |
| 250 | + t.Fatal("CLOS dir should exist") |
| 251 | + } |
| 252 | + }, |
| 253 | + }, |
| 254 | + { |
| 255 | + name: "per-container MON dir in pre-existing CLOS should be removed", |
| 256 | + config: configs.IntelRdt{ |
| 257 | + ClosID: "pre-existing-clos", |
| 258 | + EnableMonitoring: true, |
| 259 | + }, |
| 260 | + testFunc: func(m *Manager) { |
| 261 | + closPath := m.path |
| 262 | + |
| 263 | + monPath := filepath.Join(closPath, "mon_groups", m.id) |
| 264 | + if _, err := os.Stat(monPath); err != nil { |
| 265 | + t.Fatal("MON dir should exist") |
| 266 | + } |
| 267 | + // Need to delete the tasks file so that the dir is empty |
| 268 | + os.Remove(filepath.Join(monPath, "tasks")) |
| 269 | + if err := m.Destroy(); err != nil { |
| 270 | + t.Fatalf("Destroy() failed: %v", err) |
| 271 | + } |
| 272 | + if _, err := os.Stat(closPath); err != nil { |
| 273 | + t.Fatalf("CLOS dir should exist: %f", err) |
| 274 | + } |
| 275 | + if _, err := os.Stat(monPath); err == nil { |
| 276 | + t.Fatal("MON dir should not exist") |
| 277 | + } |
| 278 | + }, |
| 279 | + }, |
152 | 280 | } |
153 | | - if pids != "1235" { |
154 | | - t.Fatalf("unexpected tasks file, expected '1235', got %q", pids) |
| 281 | + |
| 282 | + for _, tt := range tests { |
| 283 | + t.Run(tt.name, func(t *testing.T) { |
| 284 | + NewIntelRdtTestUtil(t) |
| 285 | + |
| 286 | + id := "abcd-1234" |
| 287 | + closPath := filepath.Join(intelRdtRoot, id) |
| 288 | + if tt.config.ClosID != "" { |
| 289 | + closPath = filepath.Join(intelRdtRoot, tt.config.ClosID) |
| 290 | + // Pre-create the CLOS directory |
| 291 | + if err := os.MkdirAll(filepath.Join(closPath, "mon_groups"), 0o755); err != nil { |
| 292 | + t.Fatal(err) |
| 293 | + } |
| 294 | + } |
| 295 | + m := newManager(&configs.Config{IntelRdt: &tt.config}, id, closPath) |
| 296 | + if err := m.Apply(1234); err != nil { |
| 297 | + t.Fatalf("Apply() failed: %v", err) |
| 298 | + } |
| 299 | + tt.testFunc(m) |
| 300 | + }) |
155 | 301 | } |
156 | 302 | } |
0 commit comments