|
5 | 5 | "path/filepath"
|
6 | 6 | "strings"
|
7 | 7 | "testing"
|
| 8 | + |
| 9 | + "github.com/opencontainers/runc/libcontainer/configs" |
8 | 10 | )
|
9 | 11 |
|
10 | 12 | func TestIntelRdtSetL3CacheSchema(t *testing.T) {
|
@@ -98,31 +100,173 @@ func TestIntelRdtSetMemBwScSchema(t *testing.T) {
|
98 | 100 | }
|
99 | 101 |
|
100 | 102 | func TestApply(t *testing.T) {
|
101 |
| - helper := NewIntelRdtTestUtil(t) |
102 |
| - |
103 | 103 | const closID = "test-clos"
|
104 |
| - closPath := filepath.Join(helper.IntelRdtPath, closID) |
105 | 104 |
|
106 |
| - helper.config.IntelRdt.ClosID = closID |
107 |
| - intelrdt := newManager(helper.config, "container-1", closPath) |
108 |
| - if err := intelrdt.Apply(1234); err == nil { |
109 |
| - t.Fatal("unexpected success when applying pid") |
| 105 | + tests := []struct { |
| 106 | + name string |
| 107 | + testFunc func(*intelRdtTestUtil) |
| 108 | + }{ |
| 109 | + { |
| 110 | + name: "failure because non-pre-existing CLOS", |
| 111 | + testFunc: func(helper *intelRdtTestUtil) { |
| 112 | + helper.config.IntelRdt = &configs.IntelRdt{ |
| 113 | + ClosID: closID, |
| 114 | + } |
| 115 | + |
| 116 | + closPath := filepath.Join(intelRdtRoot, closID) |
| 117 | + intelrdt := newManager(helper.config, "", closPath) |
| 118 | + if err := intelrdt.Apply(1234); err == nil { |
| 119 | + t.Fatal("unexpected success when applying pid") |
| 120 | + } |
| 121 | + if _, err := os.Stat(closPath); err == nil { |
| 122 | + t.Fatal("closid dir should not exist") |
| 123 | + } |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "CLOS dir should be created if some schema has been specified", |
| 128 | + testFunc: func(helper *intelRdtTestUtil) { |
| 129 | + helper.config.IntelRdt = &configs.IntelRdt{ |
| 130 | + ClosID: closID, |
| 131 | + L3CacheSchema: "L3:0=f", |
| 132 | + } |
| 133 | + |
| 134 | + closPath := filepath.Join(intelRdtRoot, closID) |
| 135 | + intelrdt := newManager(helper.config, "", closPath) |
| 136 | + if err := intelrdt.Apply(1235); err != nil { |
| 137 | + t.Fatalf("Apply() failed: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + pids, err := getIntelRdtParamString(closPath, "tasks") |
| 141 | + if err != nil { |
| 142 | + t.Fatalf("failed to read tasks file: %v", err) |
| 143 | + } |
| 144 | + if pids != "1235" { |
| 145 | + t.Fatalf("unexpected tasks file, expected '1235', got %q", pids) |
| 146 | + } |
| 147 | + }, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "clos and monitoring group should be created if EnableMonitoring is true", |
| 151 | + testFunc: func(helper *intelRdtTestUtil) { |
| 152 | + helper.config.IntelRdt = &configs.IntelRdt{ |
| 153 | + EnableMonitoring: true, |
| 154 | + } |
| 155 | + id := "aaaa-bbbb" |
| 156 | + |
| 157 | + closPath := filepath.Join(intelRdtRoot, id) |
| 158 | + intelrdt := newManager(helper.config, id, closPath) |
| 159 | + // We need to pre-create the CLOS/mon_groups directory |
| 160 | + if err := os.MkdirAll(filepath.Join(closPath, "mon_groups"), 0o755); err != nil { |
| 161 | + t.Fatal(err) |
| 162 | + } |
| 163 | + |
| 164 | + if err := intelrdt.Apply(1236); err != nil { |
| 165 | + t.Fatalf("Apply() failed: %v", err) |
| 166 | + } |
| 167 | + |
| 168 | + pids, err := getIntelRdtParamString(closPath, "tasks") |
| 169 | + if err != nil { |
| 170 | + t.Fatalf("failed to read tasks file: %v", err) |
| 171 | + } |
| 172 | + if pids != "1236" { |
| 173 | + t.Fatalf("unexpected tasks file, expected '1236', got %q", pids) |
| 174 | + } |
| 175 | + }, |
| 176 | + }, |
110 | 177 | }
|
111 |
| - if _, err := os.Stat(closPath); err == nil { |
112 |
| - t.Fatal("closid dir should not exist") |
| 178 | + |
| 179 | + for _, tt := range tests { |
| 180 | + t.Run(tt.name, func(t *testing.T) { |
| 181 | + helper := NewIntelRdtTestUtil(t) |
| 182 | + tt.testFunc(helper) |
| 183 | + }) |
113 | 184 | }
|
| 185 | +} |
| 186 | + |
| 187 | +func TestDestroy(t *testing.T) { |
| 188 | + const closID = "test-clos" |
| 189 | + |
| 190 | + // TC-1: per-container CLOS dir should be removed |
| 191 | + { |
| 192 | + helper := NewIntelRdtTestUtil(t) |
| 193 | + id := "abcd-efgh" |
114 | 194 |
|
115 |
| - // Dir should be created if some schema has been specified |
116 |
| - intelrdt.config.IntelRdt.L3CacheSchema = "L3:0=f" |
117 |
| - if err := intelrdt.Apply(1235); err != nil { |
118 |
| - t.Fatalf("Apply() failed: %v", err) |
| 195 | + closPath := filepath.Join(intelRdtRoot, id) |
| 196 | + intelrdt := newManager(helper.config, id, closPath) |
| 197 | + if err := intelrdt.Apply(1234); err != nil { |
| 198 | + t.Fatalf("Apply() failed: %v", err) |
| 199 | + } |
| 200 | + if _, err := os.Stat(closPath); err != nil { |
| 201 | + t.Fatal("CLOS dir should exist") |
| 202 | + } |
| 203 | + // Need to delete the tasks file so that the dir is empty |
| 204 | + os.Remove(filepath.Join(closPath, "tasks")) |
| 205 | + if err := intelrdt.Destroy(); err != nil { |
| 206 | + t.Fatalf("Destroy() failed: %v", err) |
| 207 | + } |
| 208 | + if _, err := os.Stat(closPath); err == nil { |
| 209 | + t.Fatal("CLOS dir should not exist") |
| 210 | + } |
119 | 211 | }
|
| 212 | + // TC-2: pre-existing CLOS should not be removed |
| 213 | + { |
| 214 | + helper := NewIntelRdtTestUtil(t) |
| 215 | + helper.config.IntelRdt = &configs.IntelRdt{ |
| 216 | + ClosID: closID, |
| 217 | + } |
120 | 218 |
|
121 |
| - pids, err := getIntelRdtParamString(intelrdt.GetPath(), "tasks") |
122 |
| - if err != nil { |
123 |
| - t.Fatalf("failed to read tasks file: %v", err) |
| 219 | + closPath := filepath.Join(intelRdtRoot, closID) |
| 220 | + if err := os.MkdirAll(closPath, 0o755); err != nil { |
| 221 | + t.Fatal(err) |
| 222 | + } |
| 223 | + |
| 224 | + intelrdt := newManager(helper.config, "container-1", closPath) |
| 225 | + if err := intelrdt.Apply(1234); err != nil { |
| 226 | + t.Fatalf("Apply() failed: %v", err) |
| 227 | + } |
| 228 | + if _, err := os.Stat(closPath); err != nil { |
| 229 | + t.Fatal("CLOS dir should exist") |
| 230 | + } |
| 231 | + if err := intelrdt.Destroy(); err != nil { |
| 232 | + t.Fatalf("Destroy() failed: %v", err) |
| 233 | + } |
| 234 | + if _, err := os.Stat(closPath); err != nil { |
| 235 | + t.Fatal("CLOS dir should exist") |
| 236 | + } |
124 | 237 | }
|
125 |
| - if pids != "1235" { |
126 |
| - t.Fatalf("unexpected tasks file, expected '1235', got %q", pids) |
| 238 | + // TC-3: per-container MON dir in pre-existing CLOS should be removed |
| 239 | + { |
| 240 | + helper := NewIntelRdtTestUtil(t) |
| 241 | + helper.config.IntelRdt = &configs.IntelRdt{ |
| 242 | + ClosID: closID, |
| 243 | + EnableMonitoring: true, |
| 244 | + } |
| 245 | + id := "abcd-efgh" |
| 246 | + |
| 247 | + closPath := filepath.Join(intelRdtRoot, closID) |
| 248 | + if err := os.MkdirAll(filepath.Join(closPath, "mon_groups"), 0o755); err != nil { |
| 249 | + t.Fatal(err) |
| 250 | + } |
| 251 | + |
| 252 | + intelrdt := newManager(helper.config, id, closPath) |
| 253 | + if err := intelrdt.Apply(1234); err != nil { |
| 254 | + t.Fatalf("Apply() failed: %v", err) |
| 255 | + } |
| 256 | + monPath := filepath.Join(closPath, "mon_groups", id) |
| 257 | + if _, err := os.Stat(monPath); err != nil { |
| 258 | + t.Fatal("MON dir should exist") |
| 259 | + } |
| 260 | + // Need to delete the tasks file so that the dir is empty |
| 261 | + os.Remove(filepath.Join(monPath, "tasks")) |
| 262 | + if err := intelrdt.Destroy(); err != nil { |
| 263 | + t.Fatalf("Destroy() failed: %v", err) |
| 264 | + } |
| 265 | + if _, err := os.Stat(closPath); err != nil { |
| 266 | + t.Fatalf("CLOS dir should exist: %f", err) |
| 267 | + } |
| 268 | + if _, err := os.Stat(monPath); err == nil { |
| 269 | + t.Fatal("MON dir should not exist") |
| 270 | + } |
127 | 271 | }
|
128 | 272 | }
|
0 commit comments