Skip to content

Commit d76bbd7

Browse files
author
Ma Shimiao
committed
runtimetest: add returning validate result
Signed-off-by: Ma Shimiao <[email protected]>
1 parent 6117361 commit d76bbd7

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

cmd/runtimetest/main.go

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func validateLinuxProcess(spec *rspec.Spec) error {
144144
return fmt.Errorf("NoNewPrivileges expected: false, actual: true")
145145
}
146146

147-
return nil
147+
return validateSucceed("container process validation passed.")
148148
}
149149

150150
func validateCapabilities(spec *rspec.Spec) error {
@@ -182,7 +182,7 @@ func validateCapabilities(spec *rspec.Spec) error {
182182
}
183183
}
184184

185-
return nil
185+
return validateSucceed("capabilities validation passed.")
186186
}
187187

188188
func validateHostname(spec *rspec.Spec) error {
@@ -194,7 +194,7 @@ func validateHostname(spec *rspec.Spec) error {
194194
if spec.Hostname != "" && hostname != spec.Hostname {
195195
return fmt.Errorf("Hostname expected: %v, actual: %v", spec.Hostname, hostname)
196196
}
197-
return nil
197+
return validateSucceed("hostname validation passed.")
198198
}
199199

200200
func validateRlimits(spec *rspec.Spec) error {
@@ -217,7 +217,7 @@ func validateRlimits(spec *rspec.Spec) error {
217217
return fmt.Errorf("%v rlimit hard expected: %v, actual: %v", r.Type, r.Hard, rlimit.Max)
218218
}
219219
}
220-
return nil
220+
return validateSucceed("rlimits validation passed.")
221221
}
222222

223223
func validateSysctls(spec *rspec.Spec) error {
@@ -233,7 +233,7 @@ func validateSysctls(spec *rspec.Spec) error {
233233
return fmt.Errorf("Sysctl %v value expected: %v, actual: %v", k, v, value)
234234
}
235235
}
236-
return nil
236+
return validateSucceed("sysctls validation passed.")
237237
}
238238

239239
func testWriteAccess(path string) error {
@@ -257,7 +257,7 @@ func validateRootFS(spec *rspec.Spec) error {
257257
}
258258
}
259259

260-
return nil
260+
return validateSucceed("root filesystem validation passed.")
261261
}
262262

263263
func validateDefaultFS(spec *rspec.Spec) error {
@@ -279,7 +279,7 @@ func validateDefaultFS(spec *rspec.Spec) error {
279279
}
280280
}
281281

282-
return nil
282+
return validateSucceed("linkx default filesystem validation passed.")
283283
}
284284

285285
func validateLinuxDevices(spec *rspec.Spec) error {
@@ -338,7 +338,7 @@ func validateLinuxDevices(spec *rspec.Spec) error {
338338
}
339339
}
340340

341-
return nil
341+
return validateSucceed("linux devices validation passed.")
342342
}
343343

344344
func validateDefaultDevices(spec *rspec.Spec) error {
@@ -357,7 +357,7 @@ func validateDefaultDevices(spec *rspec.Spec) error {
357357
}
358358
}
359359

360-
return nil
360+
return validateSucceed("linux default devices validation passed.")
361361
}
362362

363363
func validateMaskedPaths(spec *rspec.Spec) error {
@@ -374,7 +374,7 @@ func validateMaskedPaths(spec *rspec.Spec) error {
374374
return fmt.Errorf("%v should not be readable", maskedPath)
375375
}
376376
}
377-
return nil
377+
return validateSucceed("maskedPaths validation passed.")
378378
}
379379

380380
func validateROPaths(spec *rspec.Spec) error {
@@ -386,7 +386,7 @@ func validateROPaths(spec *rspec.Spec) error {
386386
}
387387
}
388388

389-
return nil
389+
return validateSucceed("readonlyPaths validation passed.")
390390
}
391391

392392
func validateOOMScoreAdj(spec *rspec.Spec) error {
@@ -415,7 +415,7 @@ func validateOOMScoreAdj(spec *rspec.Spec) error {
415415
}
416416
}
417417

418-
return nil
418+
return validateSucceed("oomScoreAdj validation passed.")
419419
}
420420

421421
func getIDMappings(path string) ([]rspec.IDMapping, error) {
@@ -482,13 +482,21 @@ func validateIDMappings(mappings []rspec.IDMapping, path string, property string
482482
func validateUIDMappings(spec *rspec.Spec) error {
483483
logrus.Debugf("validating uidMappings")
484484

485-
return validateIDMappings(spec.Linux.UIDMappings, "/proc/self/uid_map", "linux.uidMappings")
485+
err := validateIDMappings(spec.Linux.UIDMappings, "/proc/self/uid_map", "linux.uidMappings")
486+
if err != nil {
487+
return err
488+
}
489+
return validateSucceed("uidMappings validation passed.")
486490
}
487491

488492
func validateGIDMappings(spec *rspec.Spec) error {
489493
logrus.Debugf("validating gidMappings")
490494

491-
return validateIDMappings(spec.Linux.GIDMappings, "/proc/self/gid_map", "linux.gidMappings")
495+
err := validateIDMappings(spec.Linux.GIDMappings, "/proc/self/gid_map", "linux.gidMappings")
496+
if err != nil {
497+
return err
498+
}
499+
return validateSucceed("gidMappings validation passed.")
492500
}
493501

494502
func mountMatch(specMount rspec.Mount, sysMount rspec.Mount) error {
@@ -538,20 +546,22 @@ func validateMountsExist(spec *rspec.Spec) error {
538546
}
539547
}
540548

549+
return validateSucceed("mounts exist validation passed.")
550+
}
551+
552+
func validateFailed(err error) error {
553+
return fmt.Errorf("-----------------------------------------------------------------------------------\nRuntime validation failed:\nError: %s", err.Error())
554+
}
555+
556+
func validateSucceed(msg string) error {
557+
fmt.Println(msg)
541558
return nil
542559
}
543560

544561
func validate(context *cli.Context) error {
545-
logLevelString := context.String("log-level")
546-
logLevel, err := logrus.ParseLevel(logLevelString)
547-
if err != nil {
548-
return err
549-
}
550-
logrus.SetLevel(logLevel)
551-
552562
spec, err := loadSpecConfig()
553563
if err != nil {
554-
return err
564+
return validateFailed(err)
555565
}
556566

557567
defaultValidations := []validation{
@@ -577,19 +587,19 @@ func validate(context *cli.Context) error {
577587

578588
for _, v := range defaultValidations {
579589
if err := v(spec); err != nil {
580-
return err
590+
return validateFailed(err)
581591
}
582592
}
583593

584594
if spec.Platform.OS == "linux" {
585595
for _, v := range linuxValidations {
586596
if err := v(spec); err != nil {
587-
return err
597+
return validateFailed(err)
588598
}
589599
}
590600
}
591601

592-
return nil
602+
return validateSucceed("Runtime validation succeeded.")
593603
}
594604

595605
func main() {
@@ -599,6 +609,7 @@ func main() {
599609
app.Usage = "Compare the environment with an OCI configuration"
600610
app.Description = "runtimetest compares its current environment with an OCI runtime configuration read from config.json in its current working directory. The tests are fairly generic and cover most configurations used by the runtime validation suite, but there are corner cases where a container launched by a valid runtime would not satisfy runtimetest."
601611
app.UsageText = "runtimetest [options]"
612+
app.Before = before
602613
app.Flags = []cli.Flag{
603614
cli.StringFlag{
604615
Name: "log-level",
@@ -612,3 +623,14 @@ func main() {
612623
logrus.Fatal(err)
613624
}
614625
}
626+
627+
func before(context *cli.Context) error {
628+
logLevelString := context.GlobalString("log-level")
629+
logLevel, err := logrus.ParseLevel(logLevelString)
630+
if err != nil {
631+
logrus.Fatalf(err.Error())
632+
}
633+
logrus.SetLevel(logLevel)
634+
635+
return nil
636+
}

0 commit comments

Comments
 (0)