Skip to content

Commit 0cf91bc

Browse files
committed
libcontainer/intelrdt: rewrite writeFile()
Stop using os.WriteFile() so that the writes are unit-testable. Also remove now stale helper function from tests. Signed-off-by: Markus Lehtonen <[email protected]>
1 parent 44c5acc commit 0cf91bc

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

libcontainer/intelrdt/intelrdt.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,16 @@ func writeFile(dir, file, data string) error {
330330
if dir == "" {
331331
return fmt.Errorf("no such directory for %s", file)
332332
}
333-
if err := os.WriteFile(filepath.Join(dir, file), []byte(data+"\n"), 0o600); err != nil {
333+
334+
f, err := os.OpenFile(filepath.Join(dir, file), os.O_WRONLY|os.O_APPEND, 0o600)
335+
if err != nil {
336+
return fmt.Errorf("failed to open file %s: %w", file, err)
337+
}
338+
_, err = f.Write([]byte(data + "\n"))
339+
if errc := f.Close(); errc != nil && err == nil {
340+
err = errc
341+
}
342+
if err != nil {
334343
return newLastCmdError(fmt.Errorf("intelrdt: unable to write %v: %w", data, err))
335344
}
336345
return nil

libcontainer/intelrdt/intelrdt_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,10 @@ func TestIntelRdtSet(t *testing.T) {
106106
t.Run(tc.name, func(t *testing.T) {
107107
helper := NewIntelRdtTestUtil(t)
108108
helper.config.IntelRdt = tc.config
109-
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-
})
109+
// Pre-create an empty schemata file
110+
if err := os.WriteFile(filepath.Join(helper.IntelRdtPath, "schemata"), []byte{}, 0o600); err != nil {
111+
t.Fatal(err)
112+
}
114113

115114
intelrdt := newManager(helper.config, "", helper.IntelRdtPath)
116115
if err := intelrdt.Set(helper.config); err != nil {

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)