2014-04-25 08:17:45 +08:00
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2014-06-24 06:28:43 +08:00
|
|
|
|
|
|
|
"github.com/docker/libcontainer/devices"
|
2014-04-25 08:17:45 +08:00
|
|
|
)
|
|
|
|
|
2014-05-17 08:44:10 +08:00
|
|
|
// Checks whether the expected capability is specified in the capabilities.
|
2014-05-20 08:13:00 +08:00
|
|
|
func contains(expected string, values []string) bool {
|
|
|
|
for _, v := range values {
|
|
|
|
if v == expected {
|
2014-05-17 08:44:10 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-06-24 06:28:43 +08:00
|
|
|
func containsDevice(expected *devices.Device, values []*devices.Device) bool {
|
|
|
|
for _, d := range values {
|
|
|
|
if d.Path == expected.Path &&
|
|
|
|
d.CgroupPermissions == expected.CgroupPermissions &&
|
|
|
|
d.FileMode == expected.FileMode &&
|
|
|
|
d.MajorNumber == expected.MajorNumber &&
|
|
|
|
d.MinorNumber == expected.MinorNumber &&
|
|
|
|
d.Type == expected.Type {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-06-24 07:54:35 +08:00
|
|
|
func TestConfigJsonFormat(t *testing.T) {
|
2014-06-21 01:39:50 +08:00
|
|
|
f, err := os.Open("sample_configs/attach_to_bridge.json")
|
2014-04-25 08:17:45 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to open container.json")
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2014-06-24 07:54:35 +08:00
|
|
|
var container *Config
|
2014-04-25 08:17:45 +08:00
|
|
|
if err := json.NewDecoder(f).Decode(&container); err != nil {
|
2014-05-06 03:34:21 +08:00
|
|
|
t.Fatalf("failed to decode container config: %s", err)
|
2014-04-25 08:17:45 +08:00
|
|
|
}
|
|
|
|
if container.Hostname != "koye" {
|
|
|
|
t.Log("hostname is not set")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !container.Tty {
|
|
|
|
t.Log("tty should be set to true")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2014-05-06 03:34:21 +08:00
|
|
|
if !container.Namespaces["NEWNET"] {
|
2014-04-25 08:17:45 +08:00
|
|
|
t.Log("namespaces should contain NEWNET")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2014-05-06 03:34:21 +08:00
|
|
|
if container.Namespaces["NEWUSER"] {
|
2014-04-25 08:17:45 +08:00
|
|
|
t.Log("namespaces should not contain NEWUSER")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2014-05-20 08:13:00 +08:00
|
|
|
if contains("SYS_ADMIN", container.Capabilities) {
|
2014-04-25 14:02:30 +08:00
|
|
|
t.Log("SYS_ADMIN should not be enabled in capabilities mask")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2014-05-20 08:13:00 +08:00
|
|
|
if !contains("MKNOD", container.Capabilities) {
|
2014-04-25 14:02:30 +08:00
|
|
|
t.Log("MKNOD should be enabled in capabilities mask")
|
2014-04-25 08:17:45 +08:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2014-06-18 07:08:01 +08:00
|
|
|
if !contains("SYS_CHROOT", container.Capabilities) {
|
|
|
|
t.Log("capabilities mask should contain SYS_CHROOT")
|
2014-04-25 08:17:45 +08:00
|
|
|
t.Fail()
|
|
|
|
}
|
2014-06-24 06:19:14 +08:00
|
|
|
|
|
|
|
for _, n := range container.Networks {
|
|
|
|
if n.Type == "veth" {
|
|
|
|
if n.Bridge != "docker0" {
|
|
|
|
t.Logf("veth bridge should be docker0 but received %q", n.Bridge)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Address != "172.17.0.101/16" {
|
|
|
|
t.Logf("veth address should be 172.17.0.101/61 but received %q", n.Address)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.VethPrefix != "veth" {
|
|
|
|
t.Logf("veth prefix should be veth but received %q", n.VethPrefix)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Gateway != "172.17.42.1" {
|
|
|
|
t.Logf("veth gateway should be 172.17.42.1 but received %q", n.Gateway)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Mtu != 1500 {
|
|
|
|
t.Logf("veth mtu should be 1500 but received %d", n.Mtu)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-06-24 06:28:43 +08:00
|
|
|
|
|
|
|
for _, d := range devices.DefaultSimpleDevices {
|
|
|
|
if !containsDevice(d, container.MountConfig.DeviceNodes) {
|
|
|
|
t.Logf("expected defice configuration for %s", d.Path)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 08:17:45 +08:00
|
|
|
}
|