exec_linux_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // +build linux
  2. package daemon
  3. import (
  4. "testing"
  5. containertypes "github.com/docker/docker/api/types/container"
  6. "github.com/docker/docker/container"
  7. "github.com/docker/docker/daemon/exec"
  8. "github.com/gotestyourself/gotestyourself/assert"
  9. "github.com/opencontainers/runc/libcontainer/apparmor"
  10. "github.com/opencontainers/runtime-spec/specs-go"
  11. )
  12. func TestExecSetPlatformOpt(t *testing.T) {
  13. if !apparmor.IsEnabled() {
  14. t.Skip("requires AppArmor to be enabled")
  15. }
  16. d := &Daemon{}
  17. c := &container.Container{AppArmorProfile: "my-custom-profile"}
  18. ec := &exec.Config{}
  19. p := &specs.Process{}
  20. err := d.execSetPlatformOpt(c, ec, p)
  21. assert.NilError(t, err)
  22. assert.Equal(t, "my-custom-profile", p.ApparmorProfile)
  23. }
  24. // TestExecSetPlatformOptPrivileged verifies that `docker exec --privileged`
  25. // does not disable AppArmor profiles. Exec currently inherits the `Privileged`
  26. // configuration of the container. See https://github.com/moby/moby/pull/31773#discussion_r105586900
  27. //
  28. // This behavior may change in future, but test for the behavior to prevent it
  29. // from being changed accidentally.
  30. func TestExecSetPlatformOptPrivileged(t *testing.T) {
  31. if !apparmor.IsEnabled() {
  32. t.Skip("requires AppArmor to be enabled")
  33. }
  34. d := &Daemon{}
  35. c := &container.Container{AppArmorProfile: "my-custom-profile"}
  36. ec := &exec.Config{Privileged: true}
  37. p := &specs.Process{}
  38. err := d.execSetPlatformOpt(c, ec, p)
  39. assert.NilError(t, err)
  40. assert.Equal(t, "my-custom-profile", p.ApparmorProfile)
  41. c.HostConfig = &containertypes.HostConfig{Privileged: true}
  42. err = d.execSetPlatformOpt(c, ec, p)
  43. assert.NilError(t, err)
  44. assert.Equal(t, "unconfined", p.ApparmorProfile)
  45. }