Skip to content

Commit 03d5d45

Browse files
committed
libcontainer/intelrdt: drop writeFile function
Drop writeFile() as it only had one caller. Stop using os.WriteFile() for writing to the schemata file so that the writes are unit-testable. Also, adapt and simplify the testutil helper writeFile() function. Signed-off-by: Markus Lehtonen <[email protected]>
1 parent b25c3fe commit 03d5d45

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

libcontainer/intelrdt/intelrdt.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,6 @@ 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))
335-
}
336-
return nil
337-
}
338-
339329
// Get the read-only L3 cache information
340330
func getL3CacheInfo() (*L3CacheInfo, error) {
341331
l3CacheInfo := &L3CacheInfo{}
@@ -648,7 +638,15 @@ func (m *Manager) Set(container *configs.Config) error {
648638
path := filepath.Join(m.GetPath(), "schemata")
649639
for _, line := range append([]string{r.L3CacheSchema, r.MemBwSchema}, r.Schemata...) {
650640
if line != "" {
651-
if err := os.WriteFile(path, []byte(line+"\n"), 0o600); err != nil {
641+
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0o600)
642+
if err != nil {
643+
return err
644+
}
645+
_, err = f.Write([]byte(line + "\n"))
646+
if errc := f.Close(); errc != nil && err == nil {
647+
err = errc
648+
}
649+
if err != nil {
652650
return newLastCmdError(fmt.Errorf("intelrdt: unable to write %v: %w", line, err))
653651
}
654652
}

libcontainer/intelrdt/intelrdt_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,8 @@ 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+
helper.writeFile("schemata", "")
114111

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

libcontainer/intelrdt/util_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ func NewIntelRdtTestUtil(t *testing.T) *intelRdtTestUtil {
4242
}
4343

4444
// 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-
}
45+
func (c *intelRdtTestUtil) writeFile(file, contents string) {
46+
err := os.WriteFile(filepath.Join(c.IntelRdtPath, file), []byte(contents), 0o600)
47+
if err != nil {
48+
c.t.Fatal(err)
5149
}
5250
}

0 commit comments

Comments
 (0)