github.com/getong/docker@v1.13.1/runconfig/config_test.go (about) 1 package runconfig 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "runtime" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types/container" 13 networktypes "github.com/docker/docker/api/types/network" 14 "github.com/docker/docker/api/types/strslice" 15 ) 16 17 type f struct { 18 file string 19 entrypoint strslice.StrSlice 20 } 21 22 func TestDecodeContainerConfig(t *testing.T) { 23 24 var ( 25 fixtures []f 26 image string 27 ) 28 29 //TODO: Should run for Solaris 30 if runtime.GOOS == "solaris" { 31 t.Skip() 32 } 33 34 if runtime.GOOS != "windows" { 35 image = "ubuntu" 36 fixtures = []f{ 37 {"fixtures/unix/container_config_1_14.json", strslice.StrSlice{}}, 38 {"fixtures/unix/container_config_1_17.json", strslice.StrSlice{"bash"}}, 39 {"fixtures/unix/container_config_1_19.json", strslice.StrSlice{"bash"}}, 40 } 41 } else { 42 image = "windows" 43 fixtures = []f{ 44 {"fixtures/windows/container_config_1_19.json", strslice.StrSlice{"cmd"}}, 45 } 46 } 47 48 for _, f := range fixtures { 49 b, err := ioutil.ReadFile(f.file) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 c, h, _, err := DecodeContainerConfig(bytes.NewReader(b)) 55 if err != nil { 56 t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err)) 57 } 58 59 if c.Image != image { 60 t.Fatalf("Expected %s image, found %s\n", image, c.Image) 61 } 62 63 if len(c.Entrypoint) != len(f.entrypoint) { 64 t.Fatalf("Expected %v, found %v\n", f.entrypoint, c.Entrypoint) 65 } 66 67 if h != nil && h.Memory != 1000 { 68 t.Fatalf("Expected memory to be 1000, found %d\n", h.Memory) 69 } 70 } 71 } 72 73 // TestDecodeContainerConfigIsolation validates isolation passed 74 // to the daemon in the hostConfig structure. Note this is platform specific 75 // as to what level of container isolation is supported. 76 func TestDecodeContainerConfigIsolation(t *testing.T) { 77 78 // An invalid isolation level 79 if _, _, _, err := callDecodeContainerConfigIsolation("invalid"); err != nil { 80 if !strings.Contains(err.Error(), `invalid --isolation: "invalid"`) { 81 t.Fatal(err) 82 } 83 } 84 85 // Blank isolation (== default) 86 if _, _, _, err := callDecodeContainerConfigIsolation(""); err != nil { 87 t.Fatal("Blank isolation should have succeeded") 88 } 89 90 // Default isolation 91 if _, _, _, err := callDecodeContainerConfigIsolation("default"); err != nil { 92 t.Fatal("default isolation should have succeeded") 93 } 94 95 // Process isolation (Valid on Windows only) 96 if runtime.GOOS == "windows" { 97 if _, _, _, err := callDecodeContainerConfigIsolation("process"); err != nil { 98 t.Fatal("process isolation should have succeeded") 99 } 100 } else { 101 if _, _, _, err := callDecodeContainerConfigIsolation("process"); err != nil { 102 if !strings.Contains(err.Error(), `invalid --isolation: "process"`) { 103 t.Fatal(err) 104 } 105 } 106 } 107 108 // Hyper-V Containers isolation (Valid on Windows only) 109 if runtime.GOOS == "windows" { 110 if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil { 111 t.Fatal("hyperv isolation should have succeeded") 112 } 113 } else { 114 if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil { 115 if !strings.Contains(err.Error(), `invalid --isolation: "hyperv"`) { 116 t.Fatal(err) 117 } 118 } 119 } 120 } 121 122 // callDecodeContainerConfigIsolation is a utility function to call 123 // DecodeContainerConfig for validating isolation 124 func callDecodeContainerConfigIsolation(isolation string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) { 125 var ( 126 b []byte 127 err error 128 ) 129 w := ContainerConfigWrapper{ 130 Config: &container.Config{}, 131 HostConfig: &container.HostConfig{ 132 NetworkMode: "none", 133 Isolation: container.Isolation(isolation)}, 134 } 135 if b, err = json.Marshal(w); err != nil { 136 return nil, nil, nil, fmt.Errorf("Error on marshal %s", err.Error()) 137 } 138 return DecodeContainerConfig(bytes.NewReader(b)) 139 }