forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kata_agent_linux_test.go
73 lines (58 loc) · 1.79 KB
/
kata_agent_linux_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"fmt"
"os"
"path"
"strings"
"syscall"
"testing"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
"code.cloudfoundry.org/bytefmt"
)
func TestHandleHugepages(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("Test disabled as requires root user")
}
assert := assert.New(t)
dir := t.TempDir()
k := kataAgent{}
var formattedSizes []string
var mounts []specs.Mount
var hugepageLimits []specs.LinuxHugepageLimit
// Hugepage sizes must be set at boot time and cannot be created ad hoc.
// Use any that are present.
dirs, err := os.ReadDir(sysHugepagesDir)
assert.Nil(err)
for _, dir := range dirs {
formattedSizes = append(formattedSizes, strings.TrimPrefix(dir.Name(), "hugepages-"))
}
for _, formattedSize := range formattedSizes {
bytes, err := bytefmt.ToBytes(formattedSize)
assert.Nil(err)
hugepageLimits = append(hugepageLimits, specs.LinuxHugepageLimit{
Pagesize: formattedSize,
Limit: 1_000_000 * bytes,
})
target := path.Join(dir, fmt.Sprintf("hugepages-%s", formattedSize))
err = os.MkdirAll(target, 0777)
assert.NoError(err, "Unable to create dir %s", target)
err = syscall.Mount("nodev", target, "hugetlbfs", uintptr(0), fmt.Sprintf("pagesize=%s", formattedSize))
assert.NoError(err, "Unable to mount %s", target)
defer syscall.Unmount(target, 0)
defer os.RemoveAll(target)
mount := specs.Mount{
Type: KataLocalDevType,
Source: target,
}
mounts = append(mounts, mount)
}
hugepages, err := k.handleHugepages(mounts, hugepageLimits)
assert.NoError(err, "Unable to handle hugepages %v", hugepageLimits)
assert.NotNil(hugepages)
assert.Equal(len(hugepages), len(formattedSizes))
}