2014-12-17 17:12:23 +08:00
|
|
|
package configs
|
2014-04-25 08:17:45 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-03-04 17:13:45 +08:00
|
|
|
"fmt"
|
2014-04-25 08:17:45 +08:00
|
|
|
"os"
|
2014-06-25 09:11:52 +08:00
|
|
|
"path/filepath"
|
2014-04-25 08:17:45 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-03 20:27:21 +08:00
|
|
|
func containsDevice(expected *Device, values []*Device) bool {
|
2014-06-24 06:28:43 +08:00
|
|
|
for _, d := range values {
|
|
|
|
if d.Path == expected.Path &&
|
2015-02-03 20:27:21 +08:00
|
|
|
d.Permissions == expected.Permissions &&
|
2014-06-24 06:28:43 +08:00
|
|
|
d.FileMode == expected.FileMode &&
|
2015-02-03 20:27:21 +08:00
|
|
|
d.Major == expected.Major &&
|
|
|
|
d.Minor == expected.Minor &&
|
2014-06-24 06:28:43 +08:00
|
|
|
d.Type == expected.Type {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-06-25 09:11:52 +08:00
|
|
|
func loadConfig(name string) (*Config, error) {
|
2014-12-17 17:12:23 +08:00
|
|
|
f, err := os.Open(filepath.Join("../sample_configs", name))
|
2014-04-25 08:17:45 +08:00
|
|
|
if err != nil {
|
2014-06-25 09:11:52 +08:00
|
|
|
return nil, err
|
2014-04-25 08:17:45 +08:00
|
|
|
}
|
|
|
|
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-06-25 09:11:52 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-03-04 17:13:45 +08:00
|
|
|
// Check that a config doesn't contain extra fields
|
|
|
|
var configMap, abstractMap map[string]interface{}
|
|
|
|
|
|
|
|
if _, err := f.Seek(0, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(f).Decode(&abstractMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := json.Marshal(&container)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(configData, &configMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range configMap {
|
|
|
|
delete(abstractMap, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(abstractMap) != 0 {
|
|
|
|
return nil, fmt.Errorf("unknown fields: %s", abstractMap)
|
|
|
|
}
|
|
|
|
|
2014-06-25 09:11:52 +08:00
|
|
|
return container, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigJsonFormat(t *testing.T) {
|
|
|
|
container, err := loadConfig("attach_to_bridge.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-04-25 08:17:45 +08:00
|
|
|
}
|
2014-06-25 09:11:52 +08:00
|
|
|
|
2014-04-25 08:17:45 +08:00
|
|
|
if container.Hostname != "koye" {
|
|
|
|
t.Log("hostname is not set")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2015-01-27 20:54:19 +08:00
|
|
|
if !container.Namespaces.Contains(NEWNET) {
|
2014-04-25 08:17:45 +08:00
|
|
|
t.Log("namespaces should contain NEWNET")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2015-01-27 20:54:19 +08:00
|
|
|
if container.Namespaces.Contains(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.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
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 20:27:21 +08:00
|
|
|
for _, d := range DefaultSimpleDevices {
|
2015-02-04 09:44:58 +08:00
|
|
|
if !containsDevice(d, container.Devices) {
|
2014-06-25 09:11:52 +08:00
|
|
|
t.Logf("expected device configuration for %s", d.Path)
|
2014-06-24 06:28:43 +08:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2014-06-25 09:11:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestApparmorProfile(t *testing.T) {
|
|
|
|
container, err := loadConfig("apparmor.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.AppArmorProfile != "docker-default" {
|
|
|
|
t.Fatalf("expected apparmor profile to be docker-default but received %q", container.AppArmorProfile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelinuxLabels(t *testing.T) {
|
|
|
|
container, err := loadConfig("selinux.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
label := "system_u:system_r:svirt_lxc_net_t:s0:c164,c475"
|
|
|
|
|
|
|
|
if container.ProcessLabel != label {
|
|
|
|
t.Fatalf("expected process label %q but received %q", label, container.ProcessLabel)
|
|
|
|
}
|
2015-02-03 20:27:21 +08:00
|
|
|
if container.MountLabel != label {
|
|
|
|
t.Fatalf("expected mount label %q but received %q", label, container.MountLabel)
|
2014-06-25 09:11:52 +08:00
|
|
|
}
|
2014-04-25 08:17:45 +08:00
|
|
|
}
|
2014-11-25 06:39:32 +08:00
|
|
|
|
2015-01-27 20:54:19 +08:00
|
|
|
func TestRemoveNamespace(t *testing.T) {
|
|
|
|
ns := Namespaces{
|
|
|
|
{Type: NEWNET},
|
|
|
|
}
|
|
|
|
if !ns.Remove(NEWNET) {
|
|
|
|
t.Fatal("NEWNET was not removed")
|
|
|
|
}
|
|
|
|
if len(ns) != 0 {
|
|
|
|
t.Fatalf("namespaces should have 0 items but reports %d", len(ns))
|
2014-11-25 06:39:32 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-04 09:44:58 +08:00
|
|
|
|
|
|
|
func TestHostUIDNoUSERNS(t *testing.T) {
|
|
|
|
config := &Config{
|
|
|
|
Namespaces: Namespaces{},
|
|
|
|
}
|
|
|
|
uid, err := config.HostUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if uid != 0 {
|
2015-02-17 05:09:00 +08:00
|
|
|
t.Fatalf("expected uid 0 with no USERNS but received %d", uid)
|
2015-02-04 09:44:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostUIDWithUSERNS(t *testing.T) {
|
|
|
|
config := &Config{
|
|
|
|
Namespaces: Namespaces{{Type: NEWUSER}},
|
|
|
|
UidMappings: []IDMap{
|
|
|
|
{
|
|
|
|
ContainerID: 0,
|
|
|
|
HostID: 1000,
|
|
|
|
Size: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
uid, err := config.HostUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if uid != 1000 {
|
2015-02-17 05:09:00 +08:00
|
|
|
t.Fatalf("expected uid 1000 with no USERNS but received %d", uid)
|
2015-02-04 09:44:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostGIDNoUSERNS(t *testing.T) {
|
|
|
|
config := &Config{
|
|
|
|
Namespaces: Namespaces{},
|
|
|
|
}
|
|
|
|
uid, err := config.HostGID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if uid != 0 {
|
2015-02-17 05:09:00 +08:00
|
|
|
t.Fatalf("expected gid 0 with no USERNS but received %d", uid)
|
2015-02-04 09:44:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostGIDWithUSERNS(t *testing.T) {
|
|
|
|
config := &Config{
|
|
|
|
Namespaces: Namespaces{{Type: NEWUSER}},
|
|
|
|
GidMappings: []IDMap{
|
|
|
|
{
|
|
|
|
ContainerID: 0,
|
|
|
|
HostID: 1000,
|
|
|
|
Size: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
uid, err := config.HostGID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if uid != 1000 {
|
2015-02-17 05:09:00 +08:00
|
|
|
t.Fatalf("expected gid 1000 with no USERNS but received %d", uid)
|
2015-02-04 09:44:58 +08:00
|
|
|
}
|
|
|
|
}
|