Adding some more coverage for unhappy paths and fixing some values to reflect they are no longer floats.

This commit is contained in:
Renan DelValle 2020-01-04 18:51:41 -08:00
parent 64effeef2c
commit 466e307526
No known key found for this signature in database
GPG key ID: 3895800E03F17676
2 changed files with 27 additions and 2 deletions

View file

@ -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

View file

@ -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)
})
}