2018-02-05 21:05:59 +00:00
|
|
|
package runconfig // import "github.com/docker/docker/runconfig"
|
2013-11-14 06:08:08 +00:00
|
|
|
|
|
|
|
import (
|
2015-04-11 00:05:21 +00:00
|
|
|
"bytes"
|
2015-09-19 01:21:57 +00:00
|
|
|
"encoding/json"
|
2014-06-25 22:46:41 +00:00
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"os"
|
2015-09-10 02:23:06 +00:00
|
|
|
"runtime"
|
2015-09-19 01:21:57 +00:00
|
|
|
"strings"
|
2013-11-14 06:08:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/docker/docker/api/types/strslice"
|
2020-03-10 12:09:25 +00:00
|
|
|
"github.com/docker/docker/pkg/sysinfo"
|
2015-08-28 15:29:10 +00:00
|
|
|
)
|
2015-04-11 00:05:21 +00:00
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
type f struct {
|
|
|
|
file string
|
2016-02-29 11:28:37 +00:00
|
|
|
entrypoint strslice.StrSlice
|
2015-09-10 02:23:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-11 00:05:21 +00:00
|
|
|
func TestDecodeContainerConfig(t *testing.T) {
|
2015-09-10 02:23:06 +00:00
|
|
|
var (
|
|
|
|
fixtures []f
|
|
|
|
image string
|
|
|
|
)
|
|
|
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
image = "ubuntu"
|
|
|
|
fixtures = []f{
|
2016-02-29 11:28:37 +00:00
|
|
|
{"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"}},
|
2015-09-10 02:23:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
image = "windows"
|
|
|
|
fixtures = []f{
|
2016-02-29 11:28:37 +00:00
|
|
|
{"fixtures/windows/container_config_1_19.json", strslice.StrSlice{"cmd"}},
|
2015-09-10 02:23:06 +00:00
|
|
|
}
|
2015-04-11 00:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range fixtures {
|
2022-04-08 21:27:50 +00:00
|
|
|
f := f
|
|
|
|
t.Run(f.file, func(t *testing.T) {
|
|
|
|
b, err := os.ReadFile(f.file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-11 00:05:21 +00:00
|
|
|
|
2022-04-08 21:27:50 +00:00
|
|
|
c, h, _, err := decodeContainerConfig(bytes.NewReader(b), sysinfo.New())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 06:08:08 +00:00
|
|
|
|
2022-04-08 21:27:50 +00:00
|
|
|
if c.Image != image {
|
|
|
|
t.Fatalf("Expected %s image, found %s", image, c.Image)
|
|
|
|
}
|
2015-04-11 00:05:21 +00:00
|
|
|
|
2022-04-08 21:27:50 +00:00
|
|
|
if len(c.Entrypoint) != len(f.entrypoint) {
|
|
|
|
t.Fatalf("Expected %v, found %v", f.entrypoint, c.Entrypoint)
|
|
|
|
}
|
2015-04-11 00:05:21 +00:00
|
|
|
|
2022-04-08 21:27:50 +00:00
|
|
|
if h != nil && h.Memory != 1000 {
|
|
|
|
t.Fatalf("Expected memory to be 1000, found %d", h.Memory)
|
|
|
|
}
|
|
|
|
})
|
2015-04-11 00:05:21 +00:00
|
|
|
}
|
2013-11-14 06:08:08 +00:00
|
|
|
}
|
2015-09-19 01:21:57 +00:00
|
|
|
|
2016-02-03 20:07:00 +00:00
|
|
|
// TestDecodeContainerConfigIsolation validates isolation passed
|
2015-09-19 01:21:57 +00: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) {
|
2017-05-21 17:50:55 +00:00
|
|
|
// An Invalid isolation level
|
2016-01-08 00:18:34 +00:00
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation("invalid"); err != nil {
|
2017-05-21 17:50:55 +00:00
|
|
|
if !strings.Contains(err.Error(), `Invalid isolation: "invalid"`) {
|
2015-09-19 01:21:57 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 20:07:00 +00:00
|
|
|
// Blank isolation (== default)
|
2016-01-08 00:18:34 +00:00
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation(""); err != nil {
|
2015-09-19 01:21:57 +00:00
|
|
|
t.Fatal("Blank isolation should have succeeded")
|
|
|
|
}
|
|
|
|
|
2016-02-03 20:07:00 +00:00
|
|
|
// Default isolation
|
2016-01-08 00:18:34 +00:00
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation("default"); err != nil {
|
2015-09-19 01:21:57 +00:00
|
|
|
t.Fatal("default isolation should have succeeded")
|
|
|
|
}
|
|
|
|
|
2016-02-03 20:07:00 +00: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 {
|
2017-05-21 17:50:55 +00:00
|
|
|
if !strings.Contains(err.Error(), `Invalid isolation: "process"`) {
|
2016-02-03 20:07:00 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hyper-V Containers isolation (Valid on Windows only)
|
2015-09-19 01:21:57 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2016-01-08 00:18:34 +00:00
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil {
|
2015-09-19 01:21:57 +00:00
|
|
|
t.Fatal("hyperv isolation should have succeeded")
|
|
|
|
}
|
|
|
|
} else {
|
2016-01-08 00:18:34 +00:00
|
|
|
if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil {
|
2017-05-21 17:50:55 +00:00
|
|
|
if !strings.Contains(err.Error(), `Invalid isolation: "hyperv"`) {
|
2015-09-19 01:21:57 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// callDecodeContainerConfigIsolation is a utility function to call
|
2016-02-03 20:07:00 +00:00
|
|
|
// DecodeContainerConfig for validating isolation
|
2016-01-08 00:18:34 +00:00
|
|
|
func callDecodeContainerConfigIsolation(isolation string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
|
2015-09-19 01:21:57 +00:00
|
|
|
var (
|
|
|
|
b []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
w := ContainerConfigWrapper{
|
2015-12-18 18:36:17 +00:00
|
|
|
Config: &container.Config{},
|
|
|
|
HostConfig: &container.HostConfig{
|
2015-09-19 01:21:57 +00:00
|
|
|
NetworkMode: "none",
|
2022-01-20 12:56:48 +00:00
|
|
|
Isolation: container.Isolation(isolation),
|
|
|
|
},
|
2015-09-19 01:21:57 +00:00
|
|
|
}
|
|
|
|
if b, err = json.Marshal(w); err != nil {
|
2016-01-08 00:18:34 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("Error on marshal %s", err.Error())
|
2015-09-19 01:21:57 +00:00
|
|
|
}
|
2021-07-14 14:45:02 +00:00
|
|
|
return decodeContainerConfig(bytes.NewReader(b), sysinfo.New())
|
2015-09-19 01:21:57 +00:00
|
|
|
}
|