-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
runtimetest: add validation of cgroups #93
Merged
zhouhao3
merged 10 commits into
opencontainers:master
from
Mashimiao:runtime-test-cgroup-path-validation
Dec 13, 2017
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9a56096
add cgroups package
07118a8
add runtimeOutsideValidate
4a57b0f
add cgroup memory test for runtime
432615a
add cgroup hugetlb test for runtime
f1e02ff
add cgroup blkio test for runtime
b712995
add cgroup cpus test for runtime
72e67e5
add cgroup pids test for runtime
6351044
add cgroup network test for runtime
1aebc09
update to support relative cgrouppath test
7c09b4c
add relative cgroupath test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package cgroups | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
rspec "github.com/opencontainers/runtime-spec/specs-go" | ||
) | ||
|
||
var ( | ||
// AbsCgroupPath is absolute path for container's cgroup mount | ||
AbsCgroupPath = "/cgrouptest" | ||
// RelCgroupPath is relative path for container's cgroup mount | ||
RelCgroupPath = "testdir/cgrouptest/container" | ||
) | ||
|
||
// Cgroup represents interfaces for cgroup validation | ||
type Cgroup interface { | ||
GetBlockIOData(pid int, cgPath string) (*rspec.LinuxBlockIO, error) | ||
GetCPUData(pid int, cgPath string) (*rspec.LinuxCPU, error) | ||
GetDevicesData(pid int, cgPath string) ([]rspec.LinuxDeviceCgroup, error) | ||
GetHugepageLimitData(pid int, cgPath string) ([]rspec.LinuxHugepageLimit, error) | ||
GetMemoryData(pid int, cgPath string) (*rspec.LinuxMemory, error) | ||
GetNetworkData(pid int, cgPath string) (*rspec.LinuxNetwork, error) | ||
GetPidsData(pid int, cgPath string) (*rspec.LinuxPids, error) | ||
} | ||
|
||
// FindCgroup gets cgroup root mountpoint | ||
func FindCgroup() (Cgroup, error) { | ||
f, err := os.Open("/proc/self/mountinfo") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
cgroupv2 := false | ||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
text := scanner.Text() | ||
fields := strings.Split(text, " ") | ||
// Safe as mountinfo encodes mountpoints with spaces as \040. | ||
index := strings.Index(text, " - ") | ||
postSeparatorFields := strings.Fields(text[index+3:]) | ||
numPostFields := len(postSeparatorFields) | ||
|
||
// This is an error as we can't detect if the mount is for "cgroup" | ||
if numPostFields == 0 { | ||
return nil, fmt.Errorf("Found no fields post '-' in %q", text) | ||
} | ||
|
||
if postSeparatorFields[0] == "cgroup" { | ||
// Check that the mount is properly formated. | ||
if numPostFields < 3 { | ||
return nil, fmt.Errorf("Error found less than 3 fields post '-' in %q", text) | ||
} | ||
|
||
cg := &CgroupV1{ | ||
MountPath: filepath.Dir(fields[4]), | ||
} | ||
return cg, nil | ||
} else if postSeparatorFields[0] == "cgroup2" { | ||
cgroupv2 = true | ||
continue | ||
//TODO cgroupv2 unimplemented | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
if cgroupv2 { | ||
return nil, fmt.Errorf("cgroupv2 is not supported yet") | ||
} | ||
return nil, fmt.Errorf("cgroup is not found") | ||
} | ||
|
||
// GetSubsystemPath gets path of subsystem | ||
func GetSubsystemPath(pid int, subsystem string) (string, error) { | ||
contents, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cgroup", pid)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
parts := strings.Split(strings.TrimSpace(string(contents)), "\n") | ||
for _, part := range parts { | ||
elem := strings.SplitN(part, ":", 3) | ||
if len(elem) < 3 { | ||
continue | ||
} | ||
subelems := strings.Split(elem[1], ",") | ||
for _, subelem := range subelems { | ||
if subelem == subsystem { | ||
return elem[2], nil | ||
} | ||
} | ||
} | ||
|
||
return "", fmt.Errorf("subsystem %s not found", subsystem) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to put these two variables into the cgroups pkg?
Beside these, the PR looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can change it later if there is a better place to leave these.