From 466e30752627dd1d869892b049afcb54edc6272e Mon Sep 17 00:00:00 2001 From: Renan DelValle Date: Sat, 4 Jan 2020 18:51:41 -0800 Subject: [PATCH] Adding some more coverage for unhappy paths and fixing some values to reflect they are no longer floats. --- rapl-daemon/util.go | 9 +++++++-- rapl-daemon/util_test.go | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/rapl-daemon/util.go b/rapl-daemon/util.go index 5deee2c..8646b20 100644 --- a/rapl-daemon/util.go +++ b/rapl-daemon/util.go @@ -4,6 +4,7 @@ import ( "fmt" "io/ioutil" "math" + "os" "path/filepath" "strconv" "strings" @@ -65,12 +66,12 @@ func capNode(base string, percentage int) error { func maxPower(maxFile string) (uint64, error) { maxPower, err := ioutil.ReadFile(maxFile) if err != nil { - return 0.0, err + return 0, err } maxPoweruW, err := strconv.ParseUint(strings.TrimSpace(string(maxPower)), 10, 64) if err != nil { - return 0.0, err + return 0, err } return maxPoweruW, nil @@ -78,6 +79,10 @@ func maxPower(maxFile string) (uint64, error) { // capZone caps a power zone to a specific amount of watts specified by value func capZone(limitFile string, value uint64) error { + if _, err := os.Stat(limitFile); os.IsNotExist(err) { + return err + } + err := ioutil.WriteFile(limitFile, []byte(strconv.FormatUint(value, 10)), 0644) if err != nil { return err diff --git a/rapl-daemon/util_test.go b/rapl-daemon/util_test.go index e3a601e..ffada72 100644 --- a/rapl-daemon/util_test.go +++ b/rapl-daemon/util_test.go @@ -52,6 +52,16 @@ func TestMain(m *testing.M) { func TestCapNode(t *testing.T) { err := capNode(raplDir, 95) assert.NoError(t, err) + + t.Run("badPercentage", func(t *testing.T) { + err := capNode(raplDir, 1000) + assert.Error(t, err) + }) + + t.Run("zeroPercent", func(t *testing.T) { + err := capNode(raplDir, 0) + assert.Error(t, err) + }) } func TestMaxPower(t *testing.T) { @@ -60,6 +70,11 @@ func TestMaxPower(t *testing.T) { maxWatts, err := maxPower(maxFile) assert.NoError(t, err) assert.Equal(t, maxWattage, maxWatts) + + t.Run("nameDoesNotExist", func(t *testing.T) { + _, err := maxPower("madeupname") + assert.Error(t, err) + }) } func TestCapZone(t *testing.T) { @@ -76,4 +91,9 @@ func TestCapZone(t *testing.T) { newCap, err := strconv.ParseUint(strings.TrimSpace(string(newCapBytes)), 10, 64) assert.NoError(t, err) assert.Equal(t, powercap, newCap) + + t.Run("nameDoesNotExist", func(t *testing.T) { + err := capZone("madeupname", powercap) + assert.Error(t, err) + }) }