You've already forked runc
mirror of
https://github.com/opencontainers/runc.git
synced 2025-08-08 12:42:06 +03:00
Add tests for GetHugePageSize
Add tests to avoid regressions Signed-off-by: Odin Ugedal <odin@ugedal.com>
This commit is contained in:
@@ -409,14 +409,22 @@ func RemovePaths(paths map[string]string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetHugePageSize() ([]string, error) {
|
func GetHugePageSize() ([]string, error) {
|
||||||
var pageSizes []string
|
|
||||||
sizeList := []string{"B", "KB", "MB", "GB", "TB", "PB"}
|
|
||||||
files, err := ioutil.ReadDir("/sys/kernel/mm/hugepages")
|
files, err := ioutil.ReadDir("/sys/kernel/mm/hugepages")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return pageSizes, err
|
return []string{}, err
|
||||||
}
|
}
|
||||||
|
var fileNames []string
|
||||||
for _, st := range files {
|
for _, st := range files {
|
||||||
nameArray := strings.Split(st.Name(), "-")
|
fileNames = append(fileNames, st.Name())
|
||||||
|
}
|
||||||
|
return getHugePageSizeFromFilenames(fileNames)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHugePageSizeFromFilenames(fileNames []string) ([]string, error) {
|
||||||
|
var pageSizes []string
|
||||||
|
sizeList := []string{"B", "KB", "MB", "GB", "TB", "PB"}
|
||||||
|
for _, fileName := range fileNames {
|
||||||
|
nameArray := strings.Split(fileName, "-")
|
||||||
pageSize, err := units.RAMInBytes(nameArray[1])
|
pageSize, err := units.RAMInBytes(nameArray[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}, err
|
return []string{}, err
|
||||||
|
@@ -4,6 +4,7 @@ package cgroups
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -421,3 +422,38 @@ func TestFindCgroupMountpointAndRoot(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetHugePageSizeImpl(t *testing.T) {
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
inputFiles []string
|
||||||
|
outputPageSizes []string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
inputFiles: []string{"hugepages-1048576kB", "hugepages-2048kB", "hugepages-32768kB", "hugepages-64kB"},
|
||||||
|
outputPageSizes: []string{"1GB", "2MB", "32MB", "64KB"},
|
||||||
|
err: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inputFiles: []string{},
|
||||||
|
outputPageSizes: []string{},
|
||||||
|
err: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inputFiles: []string{"hugepages-a"},
|
||||||
|
outputPageSizes: []string{},
|
||||||
|
err: errors.New("invalid size: 'a'"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range testCases {
|
||||||
|
pageSizes, err := getHugePageSizeFromFilenames(c.inputFiles)
|
||||||
|
if len(pageSizes) != 0 && len(c.outputPageSizes) != 0 && !reflect.DeepEqual(pageSizes, c.outputPageSizes) {
|
||||||
|
t.Errorf("expected %s, got %s", c.outputPageSizes, pageSizes)
|
||||||
|
}
|
||||||
|
if err != nil && err.Error() != c.err.Error() {
|
||||||
|
t.Errorf("expected error %s, got %s", c.err, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user