github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/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/engine-api/types/container" 13 networktypes "github.com/docker/engine-api/types/network" 14 "github.com/docker/engine-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 if runtime.GOOS != "windows" { 30 image = "ubuntu" 31 fixtures = []f{ 32 {"fixtures/unix/container_config_1_14.json", strslice.New()}, 33 {"fixtures/unix/container_config_1_17.json", strslice.New("bash")}, 34 {"fixtures/unix/container_config_1_19.json", strslice.New("bash")}, 35 } 36 } else { 37 image = "windows" 38 fixtures = []f{ 39 {"fixtures/windows/container_config_1_19.json", strslice.New("cmd")}, 40 } 41 } 42 43 for _, f := range fixtures { 44 b, err := ioutil.ReadFile(f.file) 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 c, h, _, err := DecodeContainerConfig(bytes.NewReader(b)) 50 if err != nil { 51 t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err)) 52 } 53 54 if c.Image != image { 55 t.Fatalf("Expected %s image, found %s\n", image, c.Image) 56 } 57 58 if c.Entrypoint.Len() != f.entrypoint.Len() { 59 t.Fatalf("Expected %v, found %v\n", f.entrypoint, c.Entrypoint) 60 } 61 62 if h != nil && h.Memory != 1000 { 63 t.Fatalf("Expected memory to be 1000, found %d\n", h.Memory) 64 } 65 } 66 } 67 68 // TestDecodeContainerConfigIsolation validates the isolation level passed 69 // to the daemon in the hostConfig structure. Note this is platform specific 70 // as to what level of container isolation is supported. 71 func TestDecodeContainerConfigIsolation(t *testing.T) { 72 73 // An invalid isolation level 74 if _, _, _, err := callDecodeContainerConfigIsolation("invalid"); err != nil { 75 if !strings.Contains(err.Error(), `invalid --isolation: "invalid"`) { 76 t.Fatal(err) 77 } 78 } 79 80 // Blank isolation level (== default) 81 if _, _, _, err := callDecodeContainerConfigIsolation(""); err != nil { 82 t.Fatal("Blank isolation should have succeeded") 83 } 84 85 // Default isolation level 86 if _, _, _, err := callDecodeContainerConfigIsolation("default"); err != nil { 87 t.Fatal("default isolation should have succeeded") 88 } 89 90 // Hyper-V Containers isolation level (Valid on Windows only) 91 if runtime.GOOS == "windows" { 92 if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil { 93 t.Fatal("hyperv isolation should have succeeded") 94 } 95 } else { 96 if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil { 97 if !strings.Contains(err.Error(), `invalid --isolation: "hyperv"`) { 98 t.Fatal(err) 99 } 100 } 101 } 102 } 103 104 // callDecodeContainerConfigIsolation is a utility function to call 105 // DecodeContainerConfig for validating isolation levels 106 func callDecodeContainerConfigIsolation(isolation string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) { 107 var ( 108 b []byte 109 err error 110 ) 111 w := ContainerConfigWrapper{ 112 Config: &container.Config{}, 113 HostConfig: &container.HostConfig{ 114 NetworkMode: "none", 115 Isolation: container.IsolationLevel(isolation)}, 116 } 117 if b, err = json.Marshal(w); err != nil { 118 return nil, nil, nil, fmt.Errorf("Error on marshal %s", err.Error()) 119 } 120 return DecodeContainerConfig(bytes.NewReader(b)) 121 }