2016-07-15 15:02:40 +08:00
|
|
|
// +build linux
|
2016-02-18 00:33:46 +08:00
|
|
|
|
2016-03-25 23:44:09 +08:00
|
|
|
package specconv
|
2016-02-18 00:33:46 +08:00
|
|
|
|
|
|
|
import (
|
2016-04-23 21:39:42 +08:00
|
|
|
"os"
|
2016-02-18 00:33:46 +08:00
|
|
|
"testing"
|
|
|
|
|
2016-04-23 21:39:42 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs/validate"
|
2016-04-13 04:35:51 +08:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2016-02-18 00:33:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLinuxCgroupsPathSpecified(t *testing.T) {
|
|
|
|
cgroupsPath := "/user/cgroups/path/id"
|
|
|
|
|
2016-03-11 06:18:39 +08:00
|
|
|
spec := &specs.Spec{}
|
2016-09-12 07:30:17 +08:00
|
|
|
spec.Linux = &specs.Linux{
|
2017-03-15 00:36:38 +08:00
|
|
|
CgroupsPath: cgroupsPath,
|
2016-09-12 07:30:17 +08:00
|
|
|
}
|
2016-02-18 00:33:46 +08:00
|
|
|
|
2016-04-23 21:39:42 +08:00
|
|
|
opts := &CreateOpts{
|
|
|
|
CgroupName: "ContainerID",
|
|
|
|
UseSystemdCgroup: false,
|
|
|
|
Spec: spec,
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroup, err := createCgroupConfig(opts)
|
2016-02-18 00:33:46 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Couldn't create Cgroup config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cgroup.Path != cgroupsPath {
|
|
|
|
t.Errorf("Wrong cgroupsPath, expected '%s' got '%s'", cgroupsPath, cgroup.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinuxCgroupsPathNotSpecified(t *testing.T) {
|
2016-03-11 06:18:39 +08:00
|
|
|
spec := &specs.Spec{}
|
2016-04-23 21:39:42 +08:00
|
|
|
opts := &CreateOpts{
|
|
|
|
CgroupName: "ContainerID",
|
|
|
|
UseSystemdCgroup: false,
|
|
|
|
Spec: spec,
|
|
|
|
}
|
2016-02-18 00:33:46 +08:00
|
|
|
|
2016-04-23 21:39:42 +08:00
|
|
|
cgroup, err := createCgroupConfig(opts)
|
2016-02-18 00:33:46 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Couldn't create Cgroup config: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-30 14:12:15 +08:00
|
|
|
if cgroup.Path != "" {
|
|
|
|
t.Errorf("Wrong cgroupsPath, expected it to be empty string, got '%s'", cgroup.Path)
|
2016-02-18 00:33:46 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-27 00:42:22 +08:00
|
|
|
|
2016-04-23 21:39:42 +08:00
|
|
|
func TestSpecconvExampleValidate(t *testing.T) {
|
|
|
|
spec := ExampleSpec()
|
|
|
|
spec.Root.Path = "/"
|
|
|
|
opts := &CreateOpts{
|
|
|
|
CgroupName: "ContainerID",
|
|
|
|
UseSystemdCgroup: false,
|
|
|
|
Spec: spec,
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := CreateLibcontainerConfig(opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Couldn't create libcontainer config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
validator := validate.New()
|
|
|
|
if err := validator.Validate(config); err != nil {
|
|
|
|
t.Errorf("Expected specconv to produce valid container config: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 00:42:22 +08:00
|
|
|
func TestDupNamespaces(t *testing.T) {
|
|
|
|
spec := &specs.Spec{
|
|
|
|
Linux: &specs.Linux{
|
2016-12-17 13:01:53 +08:00
|
|
|
Namespaces: []specs.LinuxNamespace{
|
2016-10-27 00:42:22 +08:00
|
|
|
{
|
|
|
|
Type: "pid",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "pid",
|
|
|
|
Path: "/proc/1/ns/pid",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := CreateLibcontainerConfig(&CreateOpts{
|
|
|
|
Spec: spec,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Duplicated namespaces should be forbidden")
|
|
|
|
}
|
|
|
|
}
|
2016-04-23 21:39:42 +08:00
|
|
|
|
|
|
|
func TestRootlessSpecconvValidate(t *testing.T) {
|
|
|
|
spec := &specs.Spec{
|
|
|
|
Linux: specs.Linux{
|
|
|
|
Namespaces: []specs.Namespace{
|
|
|
|
{
|
|
|
|
Type: specs.UserNamespace,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UIDMappings: []specs.IDMapping{
|
|
|
|
{
|
|
|
|
HostID: uint32(os.Geteuid()),
|
|
|
|
ContainerID: 0,
|
|
|
|
Size: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
GIDMappings: []specs.IDMapping{
|
|
|
|
{
|
|
|
|
HostID: uint32(os.Getegid()),
|
|
|
|
ContainerID: 0,
|
|
|
|
Size: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &CreateOpts{
|
|
|
|
CgroupName: "ContainerID",
|
|
|
|
UseSystemdCgroup: false,
|
|
|
|
Spec: spec,
|
|
|
|
Rootless: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := CreateLibcontainerConfig(opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Couldn't create libcontainer config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
validator := validate.New()
|
|
|
|
if err := validator.Validate(config); err != nil {
|
|
|
|
t.Errorf("Expected specconv to produce valid rootless container config: %v", err)
|
|
|
|
}
|
|
|
|
}
|