2018-02-05 21:05:59 +00:00
|
|
|
package cache // import "github.com/docker/docker/image/cache"
|
2015-07-01 08:16:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/strslice"
|
2015-12-18 17:58:48 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2015-07-01 08:16:36 +00:00
|
|
|
)
|
|
|
|
|
2015-07-16 03:45:48 +00:00
|
|
|
// Just to make life easier
|
|
|
|
func newPortNoError(proto, port string) nat.Port {
|
|
|
|
p, _ := nat.NewPort(proto, port)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2015-07-01 08:16:36 +00:00
|
|
|
func TestCompare(t *testing.T) {
|
|
|
|
ports1 := make(nat.PortSet)
|
2015-07-16 03:45:48 +00:00
|
|
|
ports1[newPortNoError("tcp", "1111")] = struct{}{}
|
|
|
|
ports1[newPortNoError("tcp", "2222")] = struct{}{}
|
2015-07-01 08:16:36 +00:00
|
|
|
ports2 := make(nat.PortSet)
|
2015-07-16 03:45:48 +00:00
|
|
|
ports2[newPortNoError("tcp", "3333")] = struct{}{}
|
|
|
|
ports2[newPortNoError("tcp", "4444")] = struct{}{}
|
2015-07-01 08:16:36 +00:00
|
|
|
ports3 := make(nat.PortSet)
|
2015-07-16 03:45:48 +00:00
|
|
|
ports3[newPortNoError("tcp", "1111")] = struct{}{}
|
|
|
|
ports3[newPortNoError("tcp", "2222")] = struct{}{}
|
|
|
|
ports3[newPortNoError("tcp", "5555")] = struct{}{}
|
2015-07-01 08:16:36 +00:00
|
|
|
volumes1 := make(map[string]struct{})
|
|
|
|
volumes1["/test1"] = struct{}{}
|
|
|
|
volumes2 := make(map[string]struct{})
|
|
|
|
volumes2["/test2"] = struct{}{}
|
|
|
|
volumes3 := make(map[string]struct{})
|
|
|
|
volumes3["/test1"] = struct{}{}
|
|
|
|
volumes3["/test3"] = struct{}{}
|
|
|
|
envs1 := []string{"ENV1=value1", "ENV2=value2"}
|
|
|
|
envs2 := []string{"ENV1=value1", "ENV3=value3"}
|
2016-02-29 11:28:37 +00:00
|
|
|
entrypoint1 := strslice.StrSlice{"/bin/sh", "-c"}
|
|
|
|
entrypoint2 := strslice.StrSlice{"/bin/sh", "-d"}
|
|
|
|
entrypoint3 := strslice.StrSlice{"/bin/sh", "-c", "echo"}
|
|
|
|
cmd1 := strslice.StrSlice{"/bin/sh", "-c"}
|
|
|
|
cmd2 := strslice.StrSlice{"/bin/sh", "-d"}
|
|
|
|
cmd3 := strslice.StrSlice{"/bin/sh", "-c", "echo"}
|
2015-07-01 08:16:36 +00:00
|
|
|
labels1 := map[string]string{"LABEL1": "value1", "LABEL2": "value2"}
|
|
|
|
labels2 := map[string]string{"LABEL1": "value1", "LABEL2": "value3"}
|
|
|
|
labels3 := map[string]string{"LABEL1": "value1", "LABEL2": "value2", "LABEL3": "value3"}
|
|
|
|
|
2015-12-18 18:36:17 +00:00
|
|
|
sameConfigs := map[*container.Config]*container.Config{
|
2015-07-01 08:16:36 +00:00
|
|
|
// Empty config
|
2017-05-26 06:25:58 +00:00
|
|
|
{}: {},
|
2015-07-01 08:16:36 +00:00
|
|
|
// Does not compare hostname, domainname & image
|
2017-05-26 06:25:58 +00:00
|
|
|
{
|
2015-07-01 08:16:36 +00:00
|
|
|
Hostname: "host1",
|
|
|
|
Domainname: "domain1",
|
|
|
|
Image: "image1",
|
|
|
|
User: "user",
|
|
|
|
}: {
|
|
|
|
Hostname: "host2",
|
|
|
|
Domainname: "domain2",
|
|
|
|
Image: "image2",
|
|
|
|
User: "user",
|
|
|
|
},
|
|
|
|
// only OpenStdin
|
2017-05-26 06:25:58 +00:00
|
|
|
{OpenStdin: false}: {OpenStdin: false},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only env
|
2017-05-26 06:25:58 +00:00
|
|
|
{Env: envs1}: {Env: envs1},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only cmd
|
2017-05-26 06:25:58 +00:00
|
|
|
{Cmd: cmd1}: {Cmd: cmd1},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only labels
|
2017-05-26 06:25:58 +00:00
|
|
|
{Labels: labels1}: {Labels: labels1},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only exposedPorts
|
2017-05-26 06:25:58 +00:00
|
|
|
{ExposedPorts: ports1}: {ExposedPorts: ports1},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only entrypoints
|
2017-05-26 06:25:58 +00:00
|
|
|
{Entrypoint: entrypoint1}: {Entrypoint: entrypoint1},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only volumes
|
2017-05-26 06:25:58 +00:00
|
|
|
{Volumes: volumes1}: {Volumes: volumes1},
|
2015-07-01 08:16:36 +00:00
|
|
|
}
|
2015-12-18 18:36:17 +00:00
|
|
|
differentConfigs := map[*container.Config]*container.Config{
|
2015-07-01 08:16:36 +00:00
|
|
|
nil: nil,
|
2017-05-26 06:25:58 +00:00
|
|
|
{
|
2015-07-01 08:16:36 +00:00
|
|
|
Hostname: "host1",
|
|
|
|
Domainname: "domain1",
|
|
|
|
Image: "image1",
|
|
|
|
User: "user1",
|
|
|
|
}: {
|
|
|
|
Hostname: "host1",
|
|
|
|
Domainname: "domain1",
|
|
|
|
Image: "image1",
|
|
|
|
User: "user2",
|
|
|
|
},
|
|
|
|
// only OpenStdin
|
2017-05-26 06:25:58 +00:00
|
|
|
{OpenStdin: false}: {OpenStdin: true},
|
|
|
|
{OpenStdin: true}: {OpenStdin: false},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only env
|
2017-05-26 06:25:58 +00:00
|
|
|
{Env: envs1}: {Env: envs2},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only cmd
|
2017-05-26 06:25:58 +00:00
|
|
|
{Cmd: cmd1}: {Cmd: cmd2},
|
2015-07-01 08:16:36 +00:00
|
|
|
// not the same number of parts
|
2017-05-26 06:25:58 +00:00
|
|
|
{Cmd: cmd1}: {Cmd: cmd3},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only labels
|
2017-05-26 06:25:58 +00:00
|
|
|
{Labels: labels1}: {Labels: labels2},
|
2015-07-01 08:16:36 +00:00
|
|
|
// not the same number of labels
|
2017-05-26 06:25:58 +00:00
|
|
|
{Labels: labels1}: {Labels: labels3},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only exposedPorts
|
2017-05-26 06:25:58 +00:00
|
|
|
{ExposedPorts: ports1}: {ExposedPorts: ports2},
|
2015-07-01 08:16:36 +00:00
|
|
|
// not the same number of ports
|
2017-05-26 06:25:58 +00:00
|
|
|
{ExposedPorts: ports1}: {ExposedPorts: ports3},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only entrypoints
|
2017-05-26 06:25:58 +00:00
|
|
|
{Entrypoint: entrypoint1}: {Entrypoint: entrypoint2},
|
2015-07-01 08:16:36 +00:00
|
|
|
// not the same number of parts
|
2017-05-26 06:25:58 +00:00
|
|
|
{Entrypoint: entrypoint1}: {Entrypoint: entrypoint3},
|
2015-07-01 08:16:36 +00:00
|
|
|
// only volumes
|
2017-05-26 06:25:58 +00:00
|
|
|
{Volumes: volumes1}: {Volumes: volumes2},
|
2015-07-01 08:16:36 +00:00
|
|
|
// not the same number of labels
|
2017-05-26 06:25:58 +00:00
|
|
|
{Volumes: volumes1}: {Volumes: volumes3},
|
2015-07-01 08:16:36 +00:00
|
|
|
}
|
|
|
|
for config1, config2 := range sameConfigs {
|
2017-01-03 18:19:27 +00:00
|
|
|
if !compare(config1, config2) {
|
2015-07-01 08:16:36 +00:00
|
|
|
t.Fatalf("Compare should be true for [%v] and [%v]", config1, config2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for config1, config2 := range differentConfigs {
|
2017-01-03 18:19:27 +00:00
|
|
|
if compare(config1, config2) {
|
2015-07-01 08:16:36 +00:00
|
|
|
t.Fatalf("Compare should be false for [%v] and [%v]", config1, config2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|