From 78f533fe21b2f5d630a48ffe098a6670055fe7d9 Mon Sep 17 00:00:00 2001 From: Renan DelValle Date: Sat, 4 Jan 2020 17:15:16 -0800 Subject: [PATCH] Adding test for happy path retrieving max power. --- go.sum | 1 + rapl-daemon/util_test.go | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index f2fecd3..411d53c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,6 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= diff --git a/rapl-daemon/util_test.go b/rapl-daemon/util_test.go index afdc411..351677c 100644 --- a/rapl-daemon/util_test.go +++ b/rapl-daemon/util_test.go @@ -1,10 +1,18 @@ package main -import "testing" +import ( + "fmt" + "io/ioutil" + "os" + "strconv" + "testing" + + "github.com/stretchr/testify/assert" +) // TODO(rdelvalle): Add more thourough testing. Generate mock files // that mimic the powercap subsystem and create test to operate on it. -func TestCap(t *testing.T) { +func TestCapNode(t *testing.T) { err := capNode("/sys/devices/virtual/powercap/intel-rapl", 95) @@ -12,3 +20,23 @@ func TestCap(t *testing.T) { t.Fail() } } + +func TestMaxPower(t *testing.T) { + const maxWattage uint64 = 1500000 + + tmpfile, err := ioutil.TempFile("", maxPowerFileShortWindow) + assert.NoError(t, err) + + defer os.Remove(tmpfile.Name()) + + fmt.Println(tmpfile.Name()) + _, err = tmpfile.Write([]byte(strconv.FormatUint(maxWattage, 10))) + assert.NoError(t, err) + + maxWatts, err := maxPower(tmpfile.Name()) + assert.NoError(t, err) + assert.Equal(t, maxWattage, maxWatts) + + err = tmpfile.Close() + assert.NoError(t, err) +}