Changing test structure to only set up mock subsystem once and allowing functions to test on it later.
This commit is contained in:
parent
d4075271cd
commit
64effeef2c
1 changed files with 43 additions and 40 deletions
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -12,65 +13,67 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var raplDir string
|
||||
|
||||
const maxWattage uint64 = 1500000
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
raplDir, err = ioutil.TempDir("", raplPrefixCPU)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer os.RemoveAll(raplDir)
|
||||
|
||||
// Create temporary directory that mocks powercap subsytem
|
||||
zonePath := filepath.Join(raplDir, raplPrefixCPU+":0")
|
||||
err = os.Mkdir(zonePath, 755)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
initialWatts := strconv.FormatUint(maxWattage, 10)
|
||||
|
||||
err = ioutil.WriteFile(filepath.Join(zonePath, maxPowerFileShortWindow), []byte(initialWatts), 0444)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(filepath.Join(zonePath, powerLimitFileShortWindow), []byte(initialWatts), 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
// TODO(rdelvalle): Create filesystem only once and allow tests to use it
|
||||
func TestCapNode(t *testing.T) {
|
||||
|
||||
RAPLdir, err := ioutil.TempDir("", "intel-rapl")
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(RAPLdir)
|
||||
|
||||
zonePath := filepath.Join(RAPLdir, raplPrefixCPU+":0")
|
||||
err = os.Mkdir(zonePath, 755)
|
||||
assert.NoError(t, err)
|
||||
err = ioutil.WriteFile(filepath.Join(zonePath, maxPowerFileShortWindow), []byte("1500000"), 0444)
|
||||
assert.NoError(t, err)
|
||||
err = ioutil.WriteFile(filepath.Join(zonePath, powerLimitFileShortWindow), []byte("1500000"), 0644)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = capNode(RAPLdir, 95)
|
||||
err := capNode(raplDir, 95)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestMaxPower(t *testing.T) {
|
||||
const maxWattage uint64 = 1500000
|
||||
maxFile := filepath.Join(raplDir, raplPrefixCPU+":0", maxPowerFileShortWindow)
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", maxPowerFileShortWindow)
|
||||
assert.NoError(t, err)
|
||||
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
_, err = tmpfile.Write([]byte(strconv.FormatUint(maxWattage, 10)))
|
||||
assert.NoError(t, err)
|
||||
|
||||
maxWatts, err := maxPower(tmpfile.Name())
|
||||
maxWatts, err := maxPower(maxFile)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, maxWattage, maxWatts)
|
||||
|
||||
err = tmpfile.Close()
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestCapZone(t *testing.T) {
|
||||
const maxPower float64 = 1500000
|
||||
const percentage float64 = .50
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", powerLimitFileShortWindow)
|
||||
powercap := uint64(math.Ceil(float64(maxWattage) * percentage))
|
||||
limitFile := filepath.Join(raplDir, raplPrefixCPU+":0", powerLimitFileShortWindow)
|
||||
err := capZone(limitFile, powercap)
|
||||
assert.NoError(t, err)
|
||||
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
powercap := uint64(math.Ceil(maxPower * percentage))
|
||||
|
||||
err = capZone(tmpfile.Name(), powercap)
|
||||
assert.NoError(t, err)
|
||||
|
||||
newCapBytes, err := ioutil.ReadFile(tmpfile.Name())
|
||||
newCapBytes, err := ioutil.ReadFile(limitFile)
|
||||
assert.NoError(t, err)
|
||||
|
||||
newCap, err := strconv.ParseUint(strings.TrimSpace(string(newCapBytes)), 10, 64)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, powercap, newCap)
|
||||
|
||||
err = tmpfile.Close()
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
Reference in a new issue