Skip to content

Commit 7e2d60f

Browse files
author
Mrunal Patel
authored
Merge pull request #742 from kolyshkin/fix-fix
Followup fixes to validator
2 parents 8ac076b + 30cecc1 commit 7e2d60f

File tree

5 files changed

+52
-43
lines changed

5 files changed

+52
-43
lines changed

cmd/runtimetest/main.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,22 @@ func (c *complianceTester) validateRootfsPropagation(spec *rspec.Spec) error {
548548
}
549549
defer os.RemoveAll(targetDir)
550550

551+
mountErr := unix.Mount("/", targetDir, "", unix.MS_BIND|unix.MS_REC, "")
552+
if mountErr == unix.EPERM { //nolint:errcheck // unix errors are bare
553+
// This test needs CAP_SYS_ADMIN to perform mounts.
554+
// EPERM most probably means it was not granted.
555+
c.harness.Skip(1, "unable to perform mount (test requires CAP_SYS_ADMIN)")
556+
return nil
557+
}
558+
if err == nil {
559+
defer unix.Unmount(targetDir, unix.MNT_DETACH) //nolint:errcheck
560+
}
561+
551562
switch spec.Linux.RootfsPropagation {
552563
case "shared", "slave", "private":
564+
if mountErr != nil {
565+
return fmt.Errorf("bind-mount / %s: %w", targetDir, err)
566+
}
553567
mountDir, err := ioutil.TempDir("/", "mount")
554568
if err != nil {
555569
return err
@@ -568,12 +582,8 @@ func (c *complianceTester) validateRootfsPropagation(spec *rspec.Spec) error {
568582
}
569583
defer os.Remove(tmpfile.Name())
570584

571-
if err := unix.Mount("/", targetDir, "", unix.MS_BIND|unix.MS_REC, ""); err != nil {
572-
return err
573-
}
574-
defer unix.Unmount(targetDir, unix.MNT_DETACH) //nolint:errcheck
575585
if err := unix.Mount(testDir, mountDir, "", unix.MS_BIND|unix.MS_REC, ""); err != nil {
576-
return err
586+
return fmt.Errorf("bind-mount %s %s: %w", testDir, mountDir, err)
577587
}
578588
defer unix.Unmount(mountDir, unix.MNT_DETACH) //nolint:errcheck
579589
targetFile := filepath.Join(targetDir, filepath.Join(mountDir, filepath.Base(tmpfile.Name())))
@@ -595,14 +605,12 @@ func (c *complianceTester) validateRootfsPropagation(spec *rspec.Spec) error {
595605
)
596606
}
597607
case "unbindable":
598-
err = unix.Mount("/", targetDir, "", unix.MS_BIND|unix.MS_REC, "")
599-
if err == syscall.EINVAL {
608+
if mountErr == syscall.EINVAL {
600609
c.harness.Pass("root propagation is unbindable")
601610
return nil
602-
} else if err != nil {
603-
return err
611+
} else if mountErr != nil {
612+
return fmt.Errorf("bind-mount / %s: %w", targetDir, err)
604613
}
605-
defer unix.Unmount(targetDir, unix.MNT_DETACH) //nolint:errcheck
606614
c.harness.Fail("root propagation is unbindable")
607615
return nil
608616
default:

validation/create/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"os/exec"
66
"runtime"
77

8+
"github.com/google/uuid"
89
"github.com/mndrix/tap-go"
910
rspecs "github.com/opencontainers/runtime-spec/specs-go"
1011
"github.com/opencontainers/runtime-tools/generate"
1112
"github.com/opencontainers/runtime-tools/specerror"
1213
"github.com/opencontainers/runtime-tools/validation/util"
13-
"github.com/google/uuid"
1414
)
1515

1616
func main() {
@@ -33,7 +33,7 @@ func main() {
3333
if err != nil {
3434
util.Fatal(err)
3535
}
36-
defer r.Clean(true, true)
36+
defer r.Clean()
3737

3838
err = r.SetConfig(&g)
3939
if err != nil {

validation/linux_rootfs_propagation/linux_rootfs_propagation.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ func testLinuxRootPropagation(t *tap.T, propMode string) error {
1010
if err != nil {
1111
util.Fatal(err)
1212
}
13-
g.SetupPrivileged(true)
13+
// Test case validateRootfsPropagation needs CAP_SYS_ADMIN to perform mounts.
14+
g.AddProcessCapability("CAP_SYS_ADMIN")
15+
// The generated seccomp profile does not enable mount/umount/umount2 syscalls.
16+
g.Config.Linux.Seccomp = nil
17+
1418
g.SetLinuxRootPropagation(propMode)
15-
g.AddAnnotation("TestName", "check root propagation")
19+
g.AddAnnotation("TestName", "check root propagation: "+propMode)
1620
return util.RuntimeInsideValidate(g, t, nil)
1721
}
1822

validation/util/container.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"os/exec"
1010
"path/filepath"
11-
"time"
1211

1312
"github.com/google/uuid"
1413
rspecs "github.com/opencontainers/runtime-spec/specs-go"
@@ -175,10 +174,22 @@ func (r *Runtime) Kill(sig string) (err error) {
175174
return execWithStderrFallbackToStdout(cmd)
176175
}
177176

178-
// Delete a container
179-
func (r *Runtime) Delete() (err error) {
177+
// Delete removes a (stopped) container.
178+
func (r *Runtime) Delete() error {
179+
return r.del(false)
180+
}
181+
182+
// ForceDelete removes a container (killing it if necessary).
183+
func (r *Runtime) ForceDelete() error {
184+
return r.del(true)
185+
}
186+
187+
func (r *Runtime) del(force bool) (err error) {
180188
var args []string
181189
args = append(args, "delete")
190+
if force {
191+
args = append(args, "--force")
192+
}
182193
if r.ID != "" {
183194
args = append(args, r.ID)
184195
}
@@ -187,28 +198,16 @@ func (r *Runtime) Delete() (err error) {
187198
return execWithStderrFallbackToStdout(cmd)
188199
}
189200

190-
// Clean deletes the container. If removeBundle is set, the bundle
191-
// directory is removed after the container is deleted successfully or, if
192-
// forceRemoveBundle is true, after the deletion attempt regardless of
193-
// whether it was successful or not.
194-
func (r *Runtime) Clean(removeBundle bool, forceRemoveBundle bool) {
195-
if err := r.Kill("KILL"); err != nil {
196-
fmt.Fprintf(os.Stderr, "Clean: Kill: %v", err)
197-
}
198-
if err := WaitingForStatus(*r, LifecycleStatusStopped, time.Second*10, time.Second/10); err != nil {
199-
fmt.Fprintf(os.Stderr, "Clean: %v", err)
200-
}
201-
202-
err := r.Delete()
201+
// Clean kills and removes the container and its bundle directory.
202+
func (r *Runtime) Clean() {
203+
err := r.ForceDelete()
203204
if err != nil {
204-
fmt.Fprintf(os.Stderr, "Clean: Delete: %v", err)
205+
fmt.Fprintln(os.Stderr, "Clean: Delete: ", err)
205206
}
206207

207-
if removeBundle && (err == nil || forceRemoveBundle) {
208-
err := os.RemoveAll(r.bundleDir())
209-
if err != nil {
210-
fmt.Fprintf(os.Stderr, "Clean: %v", err)
211-
}
208+
err = os.RemoveAll(r.bundleDir())
209+
if err != nil {
210+
fmt.Fprintln(os.Stderr, "Clean: ", err)
212211
}
213212
}
214213

validation/util/test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@ import (
1111
"strings"
1212
"time"
1313

14+
"github.com/google/uuid"
1415
"github.com/mndrix/tap-go"
1516
"github.com/mrunalp/fileutils"
1617
rspec "github.com/opencontainers/runtime-spec/specs-go"
1718
"github.com/opencontainers/runtime-tools/generate"
1819
"github.com/opencontainers/runtime-tools/specerror"
19-
"github.com/google/uuid"
2020
)
2121

22-
var (
23-
// RuntimeCommand is the default runtime command.
24-
RuntimeCommand = "runc"
25-
)
22+
// RuntimeCommand is the default runtime command.
23+
var RuntimeCommand = "runc"
2624

2725
// LifecycleAction defines the phases will be called.
2826
type LifecycleAction int
@@ -194,7 +192,7 @@ func RuntimeInsideValidate(g *generate.Generator, t *tap.T, f PreFunc) (err erro
194192
os.RemoveAll(bundleDir)
195193
return err
196194
}
197-
defer r.Clean(true, true)
195+
defer r.Clean()
198196
err = r.SetConfig(g)
199197
if err != nil {
200198
return err
@@ -271,7 +269,7 @@ func RuntimeOutsideValidate(g *generate.Generator, t *tap.T, f AfterFunc) error
271269
os.RemoveAll(bundleDir)
272270
return err
273271
}
274-
defer r.Clean(true, true)
272+
defer r.Clean()
275273
err = r.SetConfig(g)
276274
if err != nil {
277275
return err

0 commit comments

Comments
 (0)