github.com/csfrancis/docker@v1.8.0-rc2/runconfig/merge_test.go (about) 1 package runconfig 2 3 import ( 4 "testing" 5 6 "github.com/docker/docker/pkg/nat" 7 ) 8 9 func TestMerge(t *testing.T) { 10 volumesImage := make(map[string]struct{}) 11 volumesImage["/test1"] = struct{}{} 12 volumesImage["/test2"] = struct{}{} 13 portsImage := make(nat.PortSet) 14 portsImage[newPortNoError("tcp", "1111")] = struct{}{} 15 portsImage[newPortNoError("tcp", "2222")] = struct{}{} 16 configImage := &Config{ 17 ExposedPorts: portsImage, 18 Env: []string{"VAR1=1", "VAR2=2"}, 19 Volumes: volumesImage, 20 } 21 22 portsUser := make(nat.PortSet) 23 portsUser[newPortNoError("tcp", "2222")] = struct{}{} 24 portsUser[newPortNoError("tcp", "3333")] = struct{}{} 25 volumesUser := make(map[string]struct{}) 26 volumesUser["/test3"] = struct{}{} 27 configUser := &Config{ 28 ExposedPorts: portsUser, 29 Env: []string{"VAR2=3", "VAR3=3"}, 30 Volumes: volumesUser, 31 } 32 33 if err := Merge(configUser, configImage); err != nil { 34 t.Error(err) 35 } 36 37 if len(configUser.ExposedPorts) != 3 { 38 t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts)) 39 } 40 for portSpecs := range configUser.ExposedPorts { 41 if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" { 42 t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs) 43 } 44 } 45 if len(configUser.Env) != 3 { 46 t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env)) 47 } 48 for _, env := range configUser.Env { 49 if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" { 50 t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env) 51 } 52 } 53 54 if len(configUser.Volumes) != 3 { 55 t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes)) 56 } 57 for v := range configUser.Volumes { 58 if v != "/test1" && v != "/test2" && v != "/test3" { 59 t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v) 60 } 61 } 62 63 ports, _, err := nat.ParsePortSpecs([]string{"0000"}) 64 if err != nil { 65 t.Error(err) 66 } 67 configImage2 := &Config{ 68 ExposedPorts: ports, 69 } 70 71 if err := Merge(configUser, configImage2); err != nil { 72 t.Error(err) 73 } 74 75 if len(configUser.ExposedPorts) != 4 { 76 t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts)) 77 } 78 for portSpecs := range configUser.ExposedPorts { 79 if portSpecs.Port() != "0" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" { 80 t.Fatalf("Expected %q or %q or %q or %q, found %s", 0, 1111, 2222, 3333, portSpecs) 81 } 82 } 83 }