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