@@ -2,9 +2,11 @@ package runconfig
22
33import (
44 "bytes"
5+ "encoding/json"
56 "fmt"
67 "io/ioutil"
78 "runtime"
9+ "strings"
810 "testing"
911
1012 "github.com/docker/docker/pkg/stringutils"
@@ -60,3 +62,58 @@ func TestDecodeContainerConfig(t *testing.T) {
6062 }
6163 }
6264}
65+
66+ // TestDecodeContainerConfigIsolation validates the isolation level passed
67+ // to the daemon in the hostConfig structure. Note this is platform specific
68+ // as to what level of container isolation is supported.
69+ func TestDecodeContainerConfigIsolation (t * testing.T ) {
70+
71+ // An invalid isolation level
72+ if _ , _ , err := callDecodeContainerConfigIsolation ("invalid" ); err != nil {
73+ if ! strings .Contains (err .Error (), `invalid --isolation: "invalid"` ) {
74+ t .Fatal (err )
75+ }
76+ }
77+
78+ // Blank isolation level (== default)
79+ if _ , _ , err := callDecodeContainerConfigIsolation ("" ); err != nil {
80+ t .Fatal ("Blank isolation should have succeeded" )
81+ }
82+
83+ // Default isolation level
84+ if _ , _ , err := callDecodeContainerConfigIsolation ("default" ); err != nil {
85+ t .Fatal ("default isolation should have succeeded" )
86+ }
87+
88+ // Hyper-V Containers isolation level (Valid on Windows only)
89+ if runtime .GOOS == "windows" {
90+ if _ , _ , err := callDecodeContainerConfigIsolation ("hyperv" ); err != nil {
91+ t .Fatal ("hyperv isolation should have succeeded" )
92+ }
93+ } else {
94+ if _ , _ , err := callDecodeContainerConfigIsolation ("hyperv" ); err != nil {
95+ if ! strings .Contains (err .Error (), `invalid --isolation: "hyperv"` ) {
96+ t .Fatal (err )
97+ }
98+ }
99+ }
100+ }
101+
102+ // callDecodeContainerConfigIsolation is a utility function to call
103+ // DecodeContainerConfig for validating isolation levels
104+ func callDecodeContainerConfigIsolation (isolation string ) (* Config , * HostConfig , error ) {
105+ var (
106+ b []byte
107+ err error
108+ )
109+ w := ContainerConfigWrapper {
110+ Config : & Config {},
111+ HostConfig : & HostConfig {
112+ NetworkMode : "none" ,
113+ Isolation : IsolationLevel (isolation )},
114+ }
115+ if b , err = json .Marshal (w ); err != nil {
116+ return nil , nil , fmt .Errorf ("Error on marshal %s" , err .Error ())
117+ }
118+ return DecodeContainerConfig (bytes .NewReader (b ))
119+ }
0 commit comments