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