github.com/camronlevanger/libcompose@v0.4.1-0.20180423130544-6bb86d53fa21/config/merge_test.go (about) 1 package config 2 3 import ( 4 "io/ioutil" 5 "testing" 6 ) 7 8 type NullLookup struct { 9 } 10 11 func (n *NullLookup) Lookup(file, relativeTo string) ([]byte, string, error) { 12 return nil, "", nil 13 } 14 15 func (n *NullLookup) ResolvePath(path, inFile string) string { 16 return "" 17 } 18 19 type FileLookup struct { 20 } 21 22 func (f *FileLookup) Lookup(file, relativeTo string) ([]byte, string, error) { 23 bytes, err := ioutil.ReadFile(file) 24 return bytes, file, err 25 } 26 27 func (f *FileLookup) ResolvePath(path, inFile string) string { 28 return "" 29 } 30 31 func TestExtendsInheritImage(t *testing.T) { 32 _, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 33 parent: 34 image: foo 35 child: 36 extends: 37 service: parent 38 `), nil) 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 _, configV2, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 44 version: '2' 45 services: 46 parent: 47 image: foo 48 child: 49 extends: 50 service: parent 51 `), nil) 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 for _, config := range []map[string]*ServiceConfig{configV1, configV2} { 57 parent := config["parent"] 58 child := config["child"] 59 60 if parent.Image != "foo" { 61 t.Fatal("Invalid parent image", parent.Image) 62 } 63 64 t.Logf("%#v", config["child"]) 65 if child.Build.Context != "" { 66 t.Fatalf("Invalid build %#v", child.Build) 67 } 68 69 if child.Image != "foo" { 70 t.Fatal("Invalid child image", child.Image) 71 } 72 } 73 } 74 75 func TestExtendsInheritBuild(t *testing.T) { 76 _, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 77 parent: 78 build: . 79 child: 80 extends: 81 service: parent 82 `), nil) 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 _, configV2, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 88 version: '2' 89 services: 90 parent: 91 build: 92 context: . 93 child: 94 extends: 95 service: parent 96 `), nil) 97 if err != nil { 98 t.Fatal(err) 99 } 100 101 for _, config := range []map[string]*ServiceConfig{configV1, configV2} { 102 parent := config["parent"] 103 child := config["child"] 104 105 if parent.Build.Context != "." { 106 t.Fatal("Invalid build", parent.Build) 107 } 108 109 if child.Build.Context != "." { 110 t.Fatal("Invalid build", child.Build) 111 } 112 113 if child.Image != "" { 114 t.Fatal("Invalid image", child.Image) 115 } 116 } 117 } 118 119 func TestExtendBuildOverImageV1(t *testing.T) { 120 _, config, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 121 parent: 122 image: foo 123 child: 124 build: . 125 extends: 126 service: parent 127 `), nil) 128 if err != nil { 129 t.Fatal(err) 130 } 131 132 parent := config["parent"] 133 child := config["child"] 134 135 if parent.Image != "foo" { 136 t.Fatal("Invalid image", parent.Image) 137 } 138 139 if child.Build.Context != "." { 140 t.Fatal("Invalid build", child.Build) 141 } 142 143 if child.Image != "" { 144 t.Fatal("Invalid image", child.Image) 145 } 146 } 147 148 func TestExtendBuildOverImageV2(t *testing.T) { 149 _, config, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 150 version: '2' 151 services: 152 parent: 153 image: foo 154 child: 155 build: 156 context: . 157 extends: 158 service: parent 159 `), nil) 160 if err != nil { 161 t.Fatal(err) 162 } 163 164 parent := config["parent"] 165 child := config["child"] 166 167 if parent.Image != "foo" { 168 t.Fatal("Invalid image", parent.Image) 169 } 170 171 if child.Build.Context != "." { 172 t.Fatal("Invalid build", child.Build) 173 } 174 175 if child.Image != "foo" { 176 t.Fatal("Invalid image", child.Image) 177 } 178 } 179 180 func TestExtendImageOverBuildV1(t *testing.T) { 181 _, config, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 182 parent: 183 build: . 184 child: 185 image: foo 186 extends: 187 service: parent 188 `), nil) 189 if err != nil { 190 t.Fatal(err) 191 } 192 193 parent := config["parent"] 194 child := config["child"] 195 196 if parent.Image != "" { 197 t.Fatal("Invalid image", parent.Image) 198 } 199 200 if parent.Build.Context != "." { 201 t.Fatal("Invalid build", parent.Build) 202 } 203 204 if child.Build.Context != "" { 205 t.Fatal("Invalid build", child.Build) 206 } 207 208 if child.Image != "foo" { 209 t.Fatal("Invalid image", child.Image) 210 } 211 212 } 213 214 func TestExtendImageOverBuildV2(t *testing.T) { 215 _, config, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 216 version: '2' 217 services: 218 parent: 219 build: 220 context: . 221 child: 222 image: foo 223 extends: 224 service: parent 225 `), nil) 226 if err != nil { 227 t.Fatal(err) 228 } 229 230 parent := config["parent"] 231 child := config["child"] 232 233 if parent.Image != "" { 234 t.Fatal("Invalid image", parent.Image) 235 } 236 237 if parent.Build.Context != "." { 238 t.Fatal("Invalid build", parent.Build) 239 } 240 241 if child.Build.Context != "." { 242 t.Fatal("Invalid build", child.Build) 243 } 244 245 if child.Image != "foo" { 246 t.Fatal("Invalid image", child.Image) 247 } 248 } 249 250 func TestMergesEnvFile(t *testing.T) { 251 _, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &FileLookup{}, "", []byte(` 252 test: 253 image: foo 254 env_file: 255 - testdata/.env 256 `), nil) 257 if err != nil { 258 t.Fatal(err) 259 } 260 261 _, configV2, _, _, err := Merge(NewServiceConfigs(), nil, &FileLookup{}, "", []byte(` 262 version: '2' 263 services: 264 test: 265 image: foo 266 env_file: 267 - testdata/.env 268 `), nil) 269 if err != nil { 270 t.Fatal(err) 271 } 272 273 for _, config := range []map[string]*ServiceConfig{configV1, configV2} { 274 test := config["test"] 275 276 if len(test.Environment) != 2 { 277 t.Fatal("env_file is not merged", test.Environment) 278 } 279 280 for _, environment := range test.Environment { 281 if (environment != "FOO=foo") && (environment != "BAR=bar") { 282 t.Fatal("Empty line and comment should be excluded", environment) 283 } 284 } 285 } 286 } 287 288 func TestRestartNo(t *testing.T) { 289 _, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 290 test: 291 restart: "no" 292 image: foo 293 `), nil) 294 if err != nil { 295 t.Fatal(err) 296 } 297 298 _, configV2, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 299 version: '2' 300 services: 301 test: 302 restart: "no" 303 image: foo 304 `), nil) 305 if err != nil { 306 t.Fatal(err) 307 } 308 309 for _, config := range []map[string]*ServiceConfig{configV1, configV2} { 310 test := config["test"] 311 312 if test.Restart != "no" { 313 t.Fatal("Invalid restart policy", test.Restart) 314 } 315 } 316 } 317 318 func TestRestartAlways(t *testing.T) { 319 _, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 320 test: 321 restart: always 322 image: foo 323 `), nil) 324 if err != nil { 325 t.Fatal(err) 326 } 327 328 _, configV2, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 329 version: '2' 330 services: 331 test: 332 restart: always 333 image: foo 334 `), nil) 335 if err != nil { 336 t.Fatal(err) 337 } 338 339 for _, config := range []map[string]*ServiceConfig{configV1, configV2} { 340 test := config["test"] 341 342 if test.Restart != "always" { 343 t.Fatal("Invalid restart policy", test.Restart) 344 } 345 } 346 } 347 348 func TestIsValidRemote(t *testing.T) { 349 gitUrls := []string{ 350 "git://github.com/docker/docker", 351 "git@github.com:docker/docker.git", 352 "git@bitbucket.org:atlassianlabs/atlassian-docker.git", 353 "https://github.com/docker/docker.git", 354 "http://github.com/docker/docker.git", 355 "http://github.com/docker/docker.git#branch", 356 "http://github.com/docker/docker.git#:dir", 357 } 358 incompleteGitUrls := []string{ 359 "github.com/docker/docker", 360 } 361 invalidGitUrls := []string{ 362 "http://github.com/docker/docker.git:#branch", 363 } 364 for _, url := range gitUrls { 365 if !IsValidRemote(url) { 366 t.Fatalf("%q should have been a valid remote", url) 367 } 368 } 369 for _, url := range incompleteGitUrls { 370 if !IsValidRemote(url) { 371 t.Fatalf("%q should have been a valid remote", url) 372 } 373 } 374 for _, url := range invalidGitUrls { 375 if !IsValidRemote(url) { 376 t.Fatalf("%q should have been a valid remote", url) 377 } 378 } 379 } 380 381 func preprocess(services RawServiceMap) (RawServiceMap, error) { 382 for name := range services { 383 services[name]["image"] = "foo2" 384 } 385 return services, nil 386 } 387 388 func postprocess(services map[string]*ServiceConfig) (map[string]*ServiceConfig, error) { 389 for name := range services { 390 services[name].ContainerName = "cname" 391 } 392 return services, nil 393 } 394 395 func TestParseOptions(t *testing.T) { 396 parseOptions := ParseOptions{ 397 Interpolate: false, 398 Validate: false, 399 Preprocess: preprocess, 400 Postprocess: postprocess, 401 } 402 403 _, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 404 test: 405 image: foo 406 labels: 407 x: $X 408 test2: 409 invalid_key: true 410 `), &parseOptions) 411 if err != nil { 412 t.Fatal(err) 413 } 414 415 _, configV2, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(` 416 version: '2' 417 services: 418 test: 419 image: foo 420 labels: 421 x: $X 422 test2: 423 invalid_key: true 424 `), &parseOptions) 425 if err != nil { 426 t.Fatal(err) 427 } 428 429 for _, config := range []map[string]*ServiceConfig{configV1, configV2} { 430 test := config["test"] 431 432 if test.Image != "foo2" { 433 t.Fatal("Preprocess failed to change image", test.Image) 434 } 435 if test.ContainerName != "cname" { 436 t.Fatal("Postprocess failed to change container name", test.ContainerName) 437 } 438 if test.Labels["x"] != "$X" { 439 t.Fatal("Failed to disable interpolation") 440 } 441 } 442 }