Adding some more coverage for unhappy paths and fixing some values to reflect they are no longer floats.
This commit is contained in:
parent
64effeef2c
commit
466e307526
2 changed files with 27 additions and 2 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -65,12 +66,12 @@ func capNode(base string, percentage int) error {
|
||||||
func maxPower(maxFile string) (uint64, error) {
|
func maxPower(maxFile string) (uint64, error) {
|
||||||
maxPower, err := ioutil.ReadFile(maxFile)
|
maxPower, err := ioutil.ReadFile(maxFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0.0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
maxPoweruW, err := strconv.ParseUint(strings.TrimSpace(string(maxPower)), 10, 64)
|
maxPoweruW, err := strconv.ParseUint(strings.TrimSpace(string(maxPower)), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0.0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return maxPoweruW, nil
|
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
|
// capZone caps a power zone to a specific amount of watts specified by value
|
||||||
func capZone(limitFile string, value uint64) error {
|
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)
|
err := ioutil.WriteFile(limitFile, []byte(strconv.FormatUint(value, 10)), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -52,6 +52,16 @@ func TestMain(m *testing.M) {
|
||||||
func TestCapNode(t *testing.T) {
|
func TestCapNode(t *testing.T) {
|
||||||
err := capNode(raplDir, 95)
|
err := capNode(raplDir, 95)
|
||||||
assert.NoError(t, err)
|
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) {
|
func TestMaxPower(t *testing.T) {
|
||||||
|
@ -60,6 +70,11 @@ func TestMaxPower(t *testing.T) {
|
||||||
maxWatts, err := maxPower(maxFile)
|
maxWatts, err := maxPower(maxFile)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, maxWattage, maxWatts)
|
assert.Equal(t, maxWattage, maxWatts)
|
||||||
|
|
||||||
|
t.Run("nameDoesNotExist", func(t *testing.T) {
|
||||||
|
_, err := maxPower("madeupname")
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCapZone(t *testing.T) {
|
func TestCapZone(t *testing.T) {
|
||||||
|
@ -76,4 +91,9 @@ func TestCapZone(t *testing.T) {
|
||||||
newCap, err := strconv.ParseUint(strings.TrimSpace(string(newCapBytes)), 10, 64)
|
newCap, err := strconv.ParseUint(strings.TrimSpace(string(newCapBytes)), 10, 64)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, powercap, newCap)
|
assert.Equal(t, powercap, newCap)
|
||||||
|
|
||||||
|
t.Run("nameDoesNotExist", func(t *testing.T) {
|
||||||
|
err := capZone("madeupname", powercap)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue