github.com/gunjan5/docker@v1.8.2/runconfig/config_test.go (about) 1 package runconfig 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "testing" 9 ) 10 11 func TestEntrypointMarshalJSON(t *testing.T) { 12 entrypoints := map[*Entrypoint]string{ 13 nil: "", 14 &Entrypoint{}: "null", 15 &Entrypoint{[]string{"/bin/sh", "-c", "echo"}}: `["/bin/sh","-c","echo"]`, 16 } 17 18 for entrypoint, expected := range entrypoints { 19 data, err := entrypoint.MarshalJSON() 20 if err != nil { 21 t.Fatal(err) 22 } 23 if string(data) != expected { 24 t.Fatalf("Expected %v, got %v", expected, string(data)) 25 } 26 } 27 } 28 29 func TestEntrypointUnmarshalJSON(t *testing.T) { 30 parts := map[string][]string{ 31 "": {"default", "values"}, 32 "[]": {}, 33 `["/bin/sh","-c","echo"]`: {"/bin/sh", "-c", "echo"}, 34 } 35 for json, expectedParts := range parts { 36 entrypoint := &Entrypoint{ 37 []string{"default", "values"}, 38 } 39 if err := entrypoint.UnmarshalJSON([]byte(json)); err != nil { 40 t.Fatal(err) 41 } 42 43 actualParts := entrypoint.Slice() 44 if len(actualParts) != len(expectedParts) { 45 t.Fatalf("Expected %v parts, got %v (%v)", len(expectedParts), len(actualParts), expectedParts) 46 } 47 for index, part := range actualParts { 48 if part != expectedParts[index] { 49 t.Fatalf("Expected %v, got %v", expectedParts, actualParts) 50 break 51 } 52 } 53 } 54 } 55 56 func TestCommandToString(t *testing.T) { 57 commands := map[*Command]string{ 58 &Command{[]string{""}}: "", 59 &Command{[]string{"one"}}: "one", 60 &Command{[]string{"one", "two"}}: "one two", 61 } 62 for command, expected := range commands { 63 toString := command.ToString() 64 if toString != expected { 65 t.Fatalf("Expected %v, got %v", expected, toString) 66 } 67 } 68 } 69 70 func TestCommandMarshalJSON(t *testing.T) { 71 commands := map[*Command]string{ 72 nil: "", 73 &Command{}: "null", 74 &Command{[]string{"/bin/sh", "-c", "echo"}}: `["/bin/sh","-c","echo"]`, 75 } 76 77 for command, expected := range commands { 78 data, err := command.MarshalJSON() 79 if err != nil { 80 t.Fatal(err) 81 } 82 if string(data) != expected { 83 t.Fatalf("Expected %v, got %v", expected, string(data)) 84 } 85 } 86 } 87 88 func TestCommandUnmarshalJSON(t *testing.T) { 89 parts := map[string][]string{ 90 "": {"default", "values"}, 91 "[]": {}, 92 `["/bin/sh","-c","echo"]`: {"/bin/sh", "-c", "echo"}, 93 } 94 for json, expectedParts := range parts { 95 command := &Command{ 96 []string{"default", "values"}, 97 } 98 if err := command.UnmarshalJSON([]byte(json)); err != nil { 99 t.Fatal(err) 100 } 101 102 actualParts := command.Slice() 103 if len(actualParts) != len(expectedParts) { 104 t.Fatalf("Expected %v parts, got %v (%v)", len(expectedParts), len(actualParts), expectedParts) 105 } 106 for index, part := range actualParts { 107 if part != expectedParts[index] { 108 t.Fatalf("Expected %v, got %v", expectedParts, actualParts) 109 break 110 } 111 } 112 } 113 } 114 115 func TestDecodeContainerConfig(t *testing.T) { 116 fixtures := []struct { 117 file string 118 entrypoint *Entrypoint 119 }{ 120 {"fixtures/container_config_1_14.json", NewEntrypoint()}, 121 {"fixtures/container_config_1_17.json", NewEntrypoint("bash")}, 122 {"fixtures/container_config_1_19.json", NewEntrypoint("bash")}, 123 } 124 125 for _, f := range fixtures { 126 b, err := ioutil.ReadFile(f.file) 127 if err != nil { 128 t.Fatal(err) 129 } 130 131 c, h, err := DecodeContainerConfig(bytes.NewReader(b)) 132 if err != nil { 133 t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err)) 134 } 135 136 if c.Image != "ubuntu" { 137 t.Fatalf("Expected ubuntu image, found %s\n", c.Image) 138 } 139 140 if c.Entrypoint.Len() != f.entrypoint.Len() { 141 t.Fatalf("Expected %v, found %v\n", f.entrypoint, c.Entrypoint) 142 } 143 144 if h.Memory != 1000 { 145 t.Fatalf("Expected memory to be 1000, found %d\n", h.Memory) 146 } 147 } 148 } 149 150 func TestEntrypointUnmarshalString(t *testing.T) { 151 var e *Entrypoint 152 echo, err := json.Marshal("echo") 153 if err != nil { 154 t.Fatal(err) 155 } 156 if err := json.Unmarshal(echo, &e); err != nil { 157 t.Fatal(err) 158 } 159 160 slice := e.Slice() 161 if len(slice) != 1 { 162 t.Fatalf("expected 1 element after unmarshal: %q", slice) 163 } 164 165 if slice[0] != "echo" { 166 t.Fatalf("expected `echo`, got: %q", slice[0]) 167 } 168 } 169 170 func TestEntrypointUnmarshalSlice(t *testing.T) { 171 var e *Entrypoint 172 echo, err := json.Marshal([]string{"echo"}) 173 if err != nil { 174 t.Fatal(err) 175 } 176 if err := json.Unmarshal(echo, &e); err != nil { 177 t.Fatal(err) 178 } 179 180 slice := e.Slice() 181 if len(slice) != 1 { 182 t.Fatalf("expected 1 element after unmarshal: %q", slice) 183 } 184 185 if slice[0] != "echo" { 186 t.Fatalf("expected `echo`, got: %q", slice[0]) 187 } 188 } 189 190 func TestCommandUnmarshalSlice(t *testing.T) { 191 var e *Command 192 echo, err := json.Marshal([]string{"echo"}) 193 if err != nil { 194 t.Fatal(err) 195 } 196 if err := json.Unmarshal(echo, &e); err != nil { 197 t.Fatal(err) 198 } 199 200 slice := e.Slice() 201 if len(slice) != 1 { 202 t.Fatalf("expected 1 element after unmarshal: %q", slice) 203 } 204 205 if slice[0] != "echo" { 206 t.Fatalf("expected `echo`, got: %q", slice[0]) 207 } 208 } 209 210 func TestCommandUnmarshalString(t *testing.T) { 211 var e *Command 212 echo, err := json.Marshal("echo") 213 if err != nil { 214 t.Fatal(err) 215 } 216 if err := json.Unmarshal(echo, &e); err != nil { 217 t.Fatal(err) 218 } 219 220 slice := e.Slice() 221 if len(slice) != 1 { 222 t.Fatalf("expected 1 element after unmarshal: %q", slice) 223 } 224 225 if slice[0] != "echo" { 226 t.Fatalf("expected `echo`, got: %q", slice[0]) 227 } 228 }