Skip to content

Commit bdbdb90

Browse files
author
Ma Shimiao
authored
Merge pull request #510 from q384566678/user-check-runtiemtest
runtimetest: add posixValidations
2 parents 1a24909 + 8765570 commit bdbdb90

1 file changed

Lines changed: 59 additions & 39 deletions

File tree

cmd/runtimetest/main.go

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -83,38 +83,11 @@ func loadSpecConfig(path string) (spec *rspec.Spec, err error) {
8383
return spec, nil
8484
}
8585

86-
// should be included by other platform specified process validation
87-
func validateGeneralProcess(spec *rspec.Spec) error {
88-
if spec.Process.Cwd != "" {
89-
cwd, err := os.Getwd()
90-
if err != nil {
91-
return err
92-
}
93-
if cwd != spec.Process.Cwd {
94-
return fmt.Errorf("Cwd expected: %v, actual: %v", spec.Process.Cwd, cwd)
95-
}
96-
}
97-
98-
for _, env := range spec.Process.Env {
99-
parts := strings.Split(env, "=")
100-
key := parts[0]
101-
expectedValue := parts[1]
102-
actualValue := os.Getenv(key)
103-
if actualValue != expectedValue {
104-
return fmt.Errorf("Env %v expected: %v, actual: %v", key, expectedValue, actualValue)
105-
}
106-
}
107-
108-
return nil
109-
}
110-
111-
func validateLinuxProcess(spec *rspec.Spec) error {
86+
func validatePosixUser(spec *rspec.Spec) error {
11287
if spec.Process == nil {
11388
return nil
11489
}
11590

116-
validateGeneralProcess(spec)
117-
11891
uid := os.Getuid()
11992
if uint32(uid) != spec.Process.User.UID {
12093
return fmt.Errorf("UID expected: %v, actual: %v", spec.Process.User.UID, uid)
@@ -140,6 +113,38 @@ func validateLinuxProcess(spec *rspec.Spec) error {
140113
}
141114
}
142115

116+
return nil
117+
}
118+
119+
func validateProcess(spec *rspec.Spec) error {
120+
if spec.Process.Cwd != "" {
121+
cwd, err := os.Getwd()
122+
if err != nil {
123+
return err
124+
}
125+
if cwd != spec.Process.Cwd {
126+
return fmt.Errorf("Cwd expected: %v, actual: %v", spec.Process.Cwd, cwd)
127+
}
128+
}
129+
130+
for _, env := range spec.Process.Env {
131+
parts := strings.Split(env, "=")
132+
key := parts[0]
133+
expectedValue := parts[1]
134+
actualValue := os.Getenv(key)
135+
if actualValue != expectedValue {
136+
return fmt.Errorf("Env %v expected: %v, actual: %v", key, expectedValue, actualValue)
137+
}
138+
}
139+
140+
return nil
141+
}
142+
143+
func validateLinuxProcess(spec *rspec.Spec) error {
144+
if spec.Process == nil {
145+
return nil
146+
}
147+
143148
cmdlineBytes, err := ioutil.ReadFile("/proc/self/cmdline")
144149
if err != nil {
145150
return err
@@ -269,10 +274,6 @@ func validateHostname(spec *rspec.Spec) error {
269274
}
270275

271276
func validateRlimits(spec *rspec.Spec) error {
272-
if runtime.GOOS == "windows" {
273-
return nil
274-
}
275-
276277
if spec.Process == nil {
277278
return nil
278279
}
@@ -702,12 +703,7 @@ func mountMatch(configMount rspec.Mount, sysMount *mount.Info) error {
702703
return nil
703704
}
704705

705-
func validateMounts(spec *rspec.Spec) error {
706-
if runtime.GOOS == "windows" {
707-
logrus.Warnf("mounts validation not yet implemented for OS %q", runtime.GOOS)
708-
return nil
709-
}
710-
706+
func validatePosixMounts(spec *rspec.Spec) error {
711707
mountInfos, err := mount.GetMounts()
712708
if err != nil {
713709
return err
@@ -800,9 +796,20 @@ func run(context *cli.Context) error {
800796
description: "hostname",
801797
},
802798
{
803-
test: validateMounts,
799+
test: validateProcess,
800+
description: "process",
801+
},
802+
}
803+
804+
posixValidations := []validation{
805+
{
806+
test: validatePosixMounts,
804807
description: "mounts",
805808
},
809+
{
810+
test: validatePosixUser,
811+
description: "user",
812+
},
806813
{
807814
test: validateRlimits,
808815
description: "rlimits",
@@ -885,6 +892,19 @@ func run(context *cli.Context) error {
885892
}
886893
}
887894

895+
if platform == "linux" || platform == "solaris" {
896+
for _, v := range posixValidations {
897+
err := v.test(spec)
898+
t.Ok(err == nil, v.description)
899+
if err != nil {
900+
if e, ok := err.(*specerror.Error); ok && e.Err.Level < complianceLevel {
901+
continue
902+
}
903+
validationErrors = multierror.Append(validationErrors, err)
904+
}
905+
}
906+
}
907+
888908
if platform == "linux" {
889909
for _, v := range linuxValidations {
890910
err := v.test(spec)

0 commit comments

Comments
 (0)