runconfig/config_test.go
4f0d95fa
 package runconfig // import "github.com/docker/docker/runconfig"
359a6f49
 
 import (
767df67e
 	"bytes"
15e35c44
 	"encoding/json"
f6e6cf90
 	"fmt"
767df67e
 	"io/ioutil"
a7e686a7
 	"runtime"
15e35c44
 	"strings"
359a6f49
 	"testing"
 
91e197d6
 	"github.com/docker/docker/api/types/container"
 	networktypes "github.com/docker/docker/api/types/network"
 	"github.com/docker/docker/api/types/strslice"
38457285
 	"gotest.tools/assert"
 	is "gotest.tools/assert/cmp"
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)
 		}
 
ebcb7d6b
 		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) {
 
92291a73
 	// An Invalid isolation level
2bb3fc1b
 	if _, _, _, err := callDecodeContainerConfigIsolation("invalid"); err != nil {
92291a73
 		if !strings.Contains(err.Error(), `Invalid isolation: "invalid"`) {
15e35c44
 			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 {
92291a73
 			if !strings.Contains(err.Error(), `Invalid isolation: "process"`) {
d4b07324
 				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 {
92291a73
 			if !strings.Contains(err.Error(), `Invalid isolation: "hyperv"`) {
15e35c44
 				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
 	}
ebcb7d6b
 	return decodeContainerConfig(bytes.NewReader(b))
15e35c44
 }
06ecc041
 
 type decodeConfigTestcase struct {
 	doc                string
 	wrapper            ContainerConfigWrapper
 	expectedErr        string
 	expectedConfig     *container.Config
 	expectedHostConfig *container.HostConfig
 	goos               string
 }
 
 func runDecodeContainerConfigTestCase(testcase decodeConfigTestcase) func(t *testing.T) {
 	return func(t *testing.T) {
 		raw := marshal(t, testcase.wrapper, testcase.doc)
 		config, hostConfig, _, err := decodeContainerConfig(bytes.NewReader(raw))
 		if testcase.expectedErr != "" {
6be0f709
 			if !assert.Check(t, is.ErrorContains(err, "")) {
06ecc041
 				return
 			}
6be0f709
 			assert.Check(t, is.Contains(err.Error(), testcase.expectedErr))
06ecc041
 			return
 		}
6be0f709
 		assert.Check(t, err)
 		assert.Check(t, is.DeepEqual(testcase.expectedConfig, config))
 		assert.Check(t, is.DeepEqual(testcase.expectedHostConfig, hostConfig))
06ecc041
 	}
 }
 
 func marshal(t *testing.T, w ContainerConfigWrapper, doc string) []byte {
 	b, err := json.Marshal(w)
6be0f709
 	assert.NilError(t, err, "%s: failed to encode config wrapper", doc)
06ecc041
 	return b
 }
 
 func containerWrapperWithVolume(volume string) ContainerConfigWrapper {
 	return ContainerConfigWrapper{
 		Config: &container.Config{
 			Volumes: map[string]struct{}{
 				volume: {},
 			},
 		},
 		HostConfig: &container.HostConfig{},
 	}
 }
 
 func containerWrapperWithBind(bind string) ContainerConfigWrapper {
 	return ContainerConfigWrapper{
 		Config: &container.Config{
 			Volumes: map[string]struct{}{},
 		},
 		HostConfig: &container.HostConfig{
 			Binds: []string{bind},
 		},
 	}
 }