runconfig/config_test.go
6393c383
 package runconfig
359a6f49
 
 import (
767df67e
 	"bytes"
15e35c44
 	"encoding/json"
f6e6cf90
 	"fmt"
767df67e
 	"io/ioutil"
a7e686a7
 	"runtime"
15e35c44
 	"strings"
359a6f49
 	"testing"
 
907407d0
 	"github.com/docker/engine-api/types/container"
2bb3fc1b
 	networktypes "github.com/docker/engine-api/types/network"
907407d0
 	"github.com/docker/engine-api/types/strslice"
ea4a0674
 )
767df67e
 
a7e686a7
 type f struct {
 	file       string
53b0d626
 	entrypoint strslice.StrSlice
a7e686a7
 }
 
767df67e
 func TestDecodeContainerConfig(t *testing.T) {
a7e686a7
 
 	var (
 		fixtures []f
 		image    string
 	)
 
 	if runtime.GOOS != "windows" {
 		image = "ubuntu"
 		fixtures = []f{
53b0d626
 			{"fixtures/unix/container_config_1_14.json", strslice.StrSlice{}},
 			{"fixtures/unix/container_config_1_17.json", strslice.StrSlice{"bash"}},
 			{"fixtures/unix/container_config_1_19.json", strslice.StrSlice{"bash"}},
a7e686a7
 		}
 	} else {
 		image = "windows"
 		fixtures = []f{
53b0d626
 			{"fixtures/windows/container_config_1_19.json", strslice.StrSlice{"cmd"}},
a7e686a7
 		}
767df67e
 	}
 
 	for _, f := range fixtures {
 		b, err := ioutil.ReadFile(f.file)
 		if err != nil {
 			t.Fatal(err)
 		}
 
2bb3fc1b
 		c, h, _, err := DecodeContainerConfig(bytes.NewReader(b))
767df67e
 		if err != nil {
 			t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err))
 		}
359a6f49
 
a7e686a7
 		if c.Image != image {
 			t.Fatalf("Expected %s image, found %s\n", image, c.Image)
767df67e
 		}
 
53b0d626
 		if len(c.Entrypoint) != len(f.entrypoint) {
767df67e
 			t.Fatalf("Expected %v, found %v\n", f.entrypoint, c.Entrypoint)
 		}
 
a7e686a7
 		if h != nil && h.Memory != 1000 {
767df67e
 			t.Fatalf("Expected memory to be 1000, found %d\n", h.Memory)
 		}
 	}
359a6f49
 }
15e35c44
 
d4b07324
 // TestDecodeContainerConfigIsolation validates isolation passed
15e35c44
 // 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
2bb3fc1b
 	if _, _, _, err := callDecodeContainerConfigIsolation("invalid"); err != nil {
15e35c44
 		if !strings.Contains(err.Error(), `invalid --isolation: "invalid"`) {
 			t.Fatal(err)
 		}
 	}
 
d4b07324
 	// Blank isolation (== default)
2bb3fc1b
 	if _, _, _, err := callDecodeContainerConfigIsolation(""); err != nil {
15e35c44
 		t.Fatal("Blank isolation should have succeeded")
 	}
 
d4b07324
 	// Default isolation
2bb3fc1b
 	if _, _, _, err := callDecodeContainerConfigIsolation("default"); err != nil {
15e35c44
 		t.Fatal("default isolation should have succeeded")
 	}
 
d4b07324
 	// 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)
15e35c44
 	if runtime.GOOS == "windows" {
2bb3fc1b
 		if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil {
15e35c44
 			t.Fatal("hyperv isolation should have succeeded")
 		}
 	} else {
2bb3fc1b
 		if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil {
15e35c44
 			if !strings.Contains(err.Error(), `invalid --isolation: "hyperv"`) {
 				t.Fatal(err)
 			}
 		}
 	}
 }
 
 // callDecodeContainerConfigIsolation is a utility function to call
d4b07324
 // DecodeContainerConfig for validating isolation
2bb3fc1b
 func callDecodeContainerConfigIsolation(isolation string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
15e35c44
 	var (
 		b   []byte
 		err error
 	)
 	w := ContainerConfigWrapper{
7ac4232e
 		Config: &container.Config{},
 		HostConfig: &container.HostConfig{
15e35c44
 			NetworkMode: "none",
d4b07324
 			Isolation:   container.Isolation(isolation)},
15e35c44
 	}
 	if b, err = json.Marshal(w); err != nil {
2bb3fc1b
 		return nil, nil, nil, fmt.Errorf("Error on marshal %s", err.Error())
15e35c44
 	}
 	return DecodeContainerConfig(bytes.NewReader(b))
 }