Skip to content

Commit f720d0e

Browse files
committed
libcontainer/intelrdt: fix unit tests
Signed-off-by: Markus Lehtonen <[email protected]>
1 parent bd1622b commit f720d0e

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

libcontainer/intelrdt/intelrdt.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,19 @@ func getIntelRdtParamString(path, file string) (string, error) {
326326
return string(bytes.TrimSpace(contents)), nil
327327
}
328328

329-
func writeFile(dir, file, data string) error {
330-
if dir == "" {
331-
return fmt.Errorf("no such directory for %s", file)
332-
}
333-
if err := os.WriteFile(filepath.Join(dir, file), []byte(data+"\n"), 0o600); err != nil {
334-
return newLastCmdError(fmt.Errorf("intelrdt: unable to write %v: %w", data, err))
329+
var writeFile = writeFileFunc(os.WriteFile)
330+
331+
// writeFileFunc is a helper to allow mocking os.WriteFile in tests.
332+
func writeFileFunc(f func(string, []byte, os.FileMode) error) func(string, string, string) error {
333+
return func(dir, file, data string) error {
334+
if dir == "" {
335+
return fmt.Errorf("no such directory for %s", file)
336+
}
337+
if err := f(filepath.Join(dir, file), []byte(data+"\n"), 0o600); err != nil {
338+
return newLastCmdError(fmt.Errorf("intelrdt: unable to write %v: %w", data, err))
339+
}
340+
return nil
335341
}
336-
return nil
337342
}
338343

339344
// Get the read-only L3 cache information

libcontainer/intelrdt/intelrdt_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,24 @@ func TestIntelRdtSet(t *testing.T) {
102102
},
103103
}
104104

105+
// override writeFile to validate all writes
106+
writeFile = writeFileFunc(func(name string, data []byte, perm os.FileMode) error {
107+
f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_APPEND, perm)
108+
if err != nil {
109+
return err
110+
}
111+
_, err = f.Write(data)
112+
if errc := f.Close(); errc != nil && err == nil {
113+
err = errc
114+
}
115+
return err
116+
})
117+
105118
for _, tc := range tcs {
106119
t.Run(tc.name, func(t *testing.T) {
107120
helper := NewIntelRdtTestUtil(t)
108121
helper.config.IntelRdt = tc.config
109122

110-
helper.writeFileContents(map[string]string{
111-
/* Common initial value for all test cases */
112-
"schemata": "MB:0=100\nL3:0=ffff\nL2:0=ffffffff\n",
113-
})
114-
115123
intelrdt := newManager(helper.config, "", helper.IntelRdtPath)
116124
if err := intelrdt.Set(helper.config); err != nil {
117125
t.Fatal(err)

libcontainer/intelrdt/util_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,3 @@ func NewIntelRdtTestUtil(t *testing.T) *intelRdtTestUtil {
4040
}
4141
return &intelRdtTestUtil{config: config, IntelRdtPath: testIntelRdtPath, t: t}
4242
}
43-
44-
// Write the specified contents on the mock of the specified Intel RDT "resource control" files
45-
func (c *intelRdtTestUtil) writeFileContents(fileContents map[string]string) {
46-
for file, contents := range fileContents {
47-
err := writeFile(c.IntelRdtPath, file, contents)
48-
if err != nil {
49-
c.t.Fatal(err)
50-
}
51-
}
52-
}

0 commit comments

Comments
 (0)