2014-03-24 03:16:40 -04:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2014-05-05 15:34:21 -04:00
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver/native/template"
|
2014-07-24 18:25:29 -04:00
|
|
|
"github.com/docker/libcontainer/security/capabilities"
|
2014-03-24 03:16:40 -04:00
|
|
|
)
|
|
|
|
|
2014-05-16 20:44:10 -04:00
|
|
|
// Checks whether the expected capability is specified in the capabilities.
|
|
|
|
func hasCapability(expected string, capabilities []string) bool {
|
|
|
|
for _, capability := range capabilities {
|
|
|
|
if capability == expected {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-03-24 03:16:40 -04:00
|
|
|
func TestSetReadonlyRootFs(t *testing.T) {
|
|
|
|
var (
|
|
|
|
container = template.New()
|
|
|
|
opts = []string{
|
|
|
|
"fs.readonly=true",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2014-06-23 19:43:43 -04:00
|
|
|
if container.MountConfig.ReadonlyFs {
|
2014-03-24 03:16:40 -04:00
|
|
|
t.Fatal("container should not have a readonly rootfs by default")
|
|
|
|
}
|
|
|
|
if err := ParseConfiguration(container, nil, opts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-06-23 19:43:43 -04:00
|
|
|
if !container.MountConfig.ReadonlyFs {
|
2014-03-24 03:16:40 -04:00
|
|
|
t.Fatal("container should have a readonly rootfs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurationsDoNotConflict(t *testing.T) {
|
|
|
|
var (
|
|
|
|
container1 = template.New()
|
|
|
|
container2 = template.New()
|
|
|
|
opts = []string{
|
|
|
|
"cap.add=NET_ADMIN",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := ParseConfiguration(container1, nil, opts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-16 20:44:10 -04:00
|
|
|
if !hasCapability("NET_ADMIN", container1.Capabilities) {
|
2014-03-24 03:16:40 -04:00
|
|
|
t.Fatal("container one should have NET_ADMIN enabled")
|
|
|
|
}
|
2014-05-16 20:44:10 -04:00
|
|
|
if hasCapability("NET_ADMIN", container2.Capabilities) {
|
2014-03-24 03:16:40 -04:00
|
|
|
t.Fatal("container two should not have NET_ADMIN enabled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCpusetCpus(t *testing.T) {
|
|
|
|
var (
|
|
|
|
container = template.New()
|
|
|
|
opts = []string{
|
|
|
|
"cgroups.cpuset.cpus=1,2",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := ParseConfiguration(container, nil, opts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected := "1,2"; container.Cgroups.CpusetCpus != expected {
|
|
|
|
t.Fatalf("expected %s got %s for cpuset.cpus", expected, container.Cgroups.CpusetCpus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppArmorProfile(t *testing.T) {
|
|
|
|
var (
|
|
|
|
container = template.New()
|
|
|
|
opts = []string{
|
|
|
|
"apparmor_profile=koye-the-protector",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := ParseConfiguration(container, nil, opts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-06-26 15:23:53 -04:00
|
|
|
|
|
|
|
if expected := "koye-the-protector"; container.AppArmorProfile != expected {
|
|
|
|
t.Fatalf("expected profile %s got %s", expected, container.AppArmorProfile)
|
2014-03-24 03:16:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCpuShares(t *testing.T) {
|
|
|
|
var (
|
|
|
|
container = template.New()
|
|
|
|
opts = []string{
|
|
|
|
"cgroups.cpu_shares=1048",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := ParseConfiguration(container, nil, opts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected := int64(1048); container.Cgroups.CpuShares != expected {
|
|
|
|
t.Fatalf("expected cpu shares %d got %d", expected, container.Cgroups.CpuShares)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-24 01:11:43 -04:00
|
|
|
func TestMemory(t *testing.T) {
|
2014-03-24 03:16:40 -04:00
|
|
|
var (
|
|
|
|
container = template.New()
|
|
|
|
opts = []string{
|
|
|
|
"cgroups.memory=500m",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := ParseConfiguration(container, nil, opts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected := int64(500 * 1024 * 1024); container.Cgroups.Memory != expected {
|
|
|
|
t.Fatalf("expected memory %d got %d", expected, container.Cgroups.Memory)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-24 01:11:43 -04:00
|
|
|
func TestMemoryReservation(t *testing.T) {
|
|
|
|
var (
|
|
|
|
container = template.New()
|
|
|
|
opts = []string{
|
|
|
|
"cgroups.memory_reservation=500m",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := ParseConfiguration(container, nil, opts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected := int64(500 * 1024 * 1024); container.Cgroups.MemoryReservation != expected {
|
|
|
|
t.Fatalf("expected memory reservation %d got %d", expected, container.Cgroups.MemoryReservation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-24 03:16:40 -04:00
|
|
|
func TestAddCap(t *testing.T) {
|
|
|
|
var (
|
|
|
|
container = template.New()
|
|
|
|
opts = []string{
|
|
|
|
"cap.add=MKNOD",
|
|
|
|
"cap.add=SYS_ADMIN",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := ParseConfiguration(container, nil, opts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-16 20:44:10 -04:00
|
|
|
if !hasCapability("MKNOD", container.Capabilities) {
|
2014-03-24 03:16:40 -04:00
|
|
|
t.Fatal("container should have MKNOD enabled")
|
|
|
|
}
|
2014-05-16 20:44:10 -04:00
|
|
|
if !hasCapability("SYS_ADMIN", container.Capabilities) {
|
2014-03-24 03:16:40 -04:00
|
|
|
t.Fatal("container should have SYS_ADMIN enabled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDropCap(t *testing.T) {
|
|
|
|
var (
|
|
|
|
container = template.New()
|
|
|
|
opts = []string{
|
|
|
|
"cap.drop=MKNOD",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
// enabled all caps like in privileged mode
|
2014-06-23 19:43:43 -04:00
|
|
|
container.Capabilities = capabilities.GetAllCapabilities()
|
2014-03-24 03:16:40 -04:00
|
|
|
if err := ParseConfiguration(container, nil, opts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-16 20:44:10 -04:00
|
|
|
if hasCapability("MKNOD", container.Capabilities) {
|
2014-03-24 03:16:40 -04:00
|
|
|
t.Fatal("container should not have MKNOD enabled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDropNamespace(t *testing.T) {
|
|
|
|
var (
|
|
|
|
container = template.New()
|
|
|
|
opts = []string{
|
|
|
|
"ns.drop=NEWNET",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := ParseConfiguration(container, nil, opts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-05-05 15:34:21 -04:00
|
|
|
if container.Namespaces["NEWNET"] {
|
2014-03-24 03:16:40 -04:00
|
|
|
t.Fatal("container should not have NEWNET enabled")
|
|
|
|
}
|
|
|
|
}
|