2014-02-11 23:04:39 -05:00
|
|
|
package runconfig
|
2013-11-14 01:08:08 -05:00
|
|
|
|
|
|
|
import (
|
2015-04-10 20:05:21 -04:00
|
|
|
"bytes"
|
2015-09-18 21:21:57 -04:00
|
|
|
"encoding/json"
|
2014-06-25 18:46:41 -04:00
|
|
|
"fmt"
|
2015-04-10 20:05:21 -04:00
|
|
|
"io/ioutil"
|
2015-09-09 22:23:06 -04:00
|
|
|
"runtime"
|
2015-09-18 21:21:57 -04:00
|
|
|
"strings"
|
2013-11-14 01:08:08 -05:00
|
|
|
"testing"
|
|
|
|
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types/container"
|
2016-01-07 19:18:34 -05:00
|
|
|
networktypes "github.com/docker/engine-api/types/network"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types/strslice"
|
2015-08-28 11:29:10 -04:00
|
|
|
)
|
2015-04-10 20:05:21 -04:00
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
type f struct {
|
|
|
|
file string
|
2015-12-17 17:22:18 -05:00
|
|
|
entrypoint *strslice.StrSlice
|
2015-09-09 22:23:06 -04:00
|
|
|
}
|
|
|
|
|
2015-04-10 20:05:21 -04:00
|
|
|
func TestDecodeContainerConfig(t *testing.T) {
|
2015-09-09 22:23:06 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
fixtures []f
|
|
|
|
image string
|
|
|
|
)
|
|
|
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
image = "ubuntu"
|
|
|
|
fixtures = []f{
|
2015-12-17 17:22:18 -05:00
|
|
|
{"fixtures/unix/container_config_1_14.json", strslice.New()},
|
|
|
|
{"fixtures/unix/container_config_1_17.json", strslice.New("bash")},
|
|
|
|
{"fixtures/unix/container_config_1_19.json", strslice.New("bash")},
|
2015-09-09 22:23:06 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
image = "windows"
|
|
|
|
fixtures = []f{
|
2015-12-17 17:22:18 -05:00
|
|
|
{"fixtures/windows/container_config_1_19.json", strslice.New("cmd")},
|
2015-09-09 22:23:06 -04:00
|
|
|
}
|
2015-04-10 20:05:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range fixtures {
|
|
|
|
b, err := ioutil.ReadFile(f.file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:18:34 -05:00
|
|
|
c, h, _, err := DecodeContainerConfig(bytes.NewReader(b))
|
2015-04-10 20:05:21 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err))
|
|
|
|
}
|
2013-11-14 01:08:08 -05:00
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
if c.Image != image {
|
|
|
|
t.Fatalf("Expected %s image, found %s\n", image, c.Image)
|
2015-04-10 20:05:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Entrypoint.Len() != f.entrypoint.Len() {
|
|
|
|
t.Fatalf("Expected %v, found %v\n", f.entrypoint, c.Entrypoint)
|
|
|
|
}
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
if h != nil && h.Memory != 1000 {
|
2015-04-10 20:05:21 -04:00
|
|
|
t.Fatalf("Expected memory to be 1000, found %d\n", h.Memory)
|
|
|
|
}
|
|
|
|
}
|
2013-11-14 01:08:08 -05:00
|
|
|
}
|
2015-09-18 21:21:57 -04:00
|
|
|
|
2016-02-03 15:07:00 -05:00
|
|
|
// TestDecodeContainerConfigIsolation validates isolation passed
|
2015-09-18 21:21:57 -04:00
|
|
|
// to the daemon in the hostConfig structure. Note this is platform specific
|
|
|
|
// as to what level of container isolation is supported.
|
|
|
|
func TestDecodeContainerConfigIsolation(t *testing.T) {
|
|
|
|
|
|
|
|
// An invalid isolation level
|
2016-01-07 19:18:34 -05:00
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation("invalid"); err != nil {
|
2015-09-18 21:21:57 -04:00
|
|
|
if !strings.Contains(err.Error(), `invalid --isolation: "invalid"`) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 15:07:00 -05:00
|
|
|
// Blank isolation (== default)
|
2016-01-07 19:18:34 -05:00
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation(""); err != nil {
|
2015-09-18 21:21:57 -04:00
|
|
|
t.Fatal("Blank isolation should have succeeded")
|
|
|
|
}
|
|
|
|
|
2016-02-03 15:07:00 -05:00
|
|
|
// Default isolation
|
2016-01-07 19:18:34 -05:00
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation("default"); err != nil {
|
2015-09-18 21:21:57 -04:00
|
|
|
t.Fatal("default isolation should have succeeded")
|
|
|
|
}
|
|
|
|
|
2016-02-03 15:07:00 -05:00
|
|
|
// Process isolation (Valid on Windows only)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation("process"); err != nil {
|
|
|
|
t.Fatal("process isolation should have succeeded")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation("process"); err != nil {
|
|
|
|
if !strings.Contains(err.Error(), `invalid --isolation: "process"`) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hyper-V Containers isolation (Valid on Windows only)
|
2015-09-18 21:21:57 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
2016-01-07 19:18:34 -05:00
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil {
|
2015-09-18 21:21:57 -04:00
|
|
|
t.Fatal("hyperv isolation should have succeeded")
|
|
|
|
}
|
|
|
|
} else {
|
2016-01-07 19:18:34 -05:00
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil {
|
2015-09-18 21:21:57 -04:00
|
|
|
if !strings.Contains(err.Error(), `invalid --isolation: "hyperv"`) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// callDecodeContainerConfigIsolation is a utility function to call
|
2016-02-03 15:07:00 -05:00
|
|
|
// DecodeContainerConfig for validating isolation
|
2016-01-07 19:18:34 -05:00
|
|
|
func callDecodeContainerConfigIsolation(isolation string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
|
2015-09-18 21:21:57 -04:00
|
|
|
var (
|
|
|
|
b []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
w := ContainerConfigWrapper{
|
2015-12-18 13:36:17 -05:00
|
|
|
Config: &container.Config{},
|
|
|
|
HostConfig: &container.HostConfig{
|
2015-09-18 21:21:57 -04:00
|
|
|
NetworkMode: "none",
|
2016-02-03 15:07:00 -05:00
|
|
|
Isolation: container.Isolation(isolation)},
|
2015-09-18 21:21:57 -04:00
|
|
|
}
|
|
|
|
if b, err = json.Marshal(w); err != nil {
|
2016-01-07 19:18:34 -05:00
|
|
|
return nil, nil, nil, fmt.Errorf("Error on marshal %s", err.Error())
|
2015-09-18 21:21:57 -04:00
|
|
|
}
|
|
|
|
return DecodeContainerConfig(bytes.NewReader(b))
|
|
|
|
}
|