runconfig/compare_test.go
d4aec5f0
 package runconfig
 
 import (
 	"testing"
 
907407d0
 	"github.com/docker/engine-api/types/container"
 	"github.com/docker/engine-api/types/strslice"
056e7449
 	"github.com/docker/go-connections/nat"
d4aec5f0
 )
 
12b6083c
 // Just to make life easier
 func newPortNoError(proto, port string) nat.Port {
 	p, _ := nat.NewPort(proto, port)
 	return p
 }
 
d4aec5f0
 func TestCompare(t *testing.T) {
 	ports1 := make(nat.PortSet)
12b6083c
 	ports1[newPortNoError("tcp", "1111")] = struct{}{}
 	ports1[newPortNoError("tcp", "2222")] = struct{}{}
d4aec5f0
 	ports2 := make(nat.PortSet)
12b6083c
 	ports2[newPortNoError("tcp", "3333")] = struct{}{}
 	ports2[newPortNoError("tcp", "4444")] = struct{}{}
d4aec5f0
 	ports3 := make(nat.PortSet)
12b6083c
 	ports3[newPortNoError("tcp", "1111")] = struct{}{}
 	ports3[newPortNoError("tcp", "2222")] = struct{}{}
 	ports3[newPortNoError("tcp", "5555")] = struct{}{}
d4aec5f0
 	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"}
53b0d626
 	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"}
d4aec5f0
 	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"}
 
7ac4232e
 	sameConfigs := map[*container.Config]*container.Config{
d4aec5f0
 		// Empty config
7ac4232e
 		&container.Config{}: {},
d4aec5f0
 		// Does not compare hostname, domainname & image
7ac4232e
 		&container.Config{
d4aec5f0
 			Hostname:   "host1",
 			Domainname: "domain1",
 			Image:      "image1",
 			User:       "user",
 		}: {
 			Hostname:   "host2",
 			Domainname: "domain2",
 			Image:      "image2",
 			User:       "user",
 		},
 		// only OpenStdin
7ac4232e
 		&container.Config{OpenStdin: false}: {OpenStdin: false},
d4aec5f0
 		// only env
7ac4232e
 		&container.Config{Env: envs1}: {Env: envs1},
d4aec5f0
 		// only cmd
7ac4232e
 		&container.Config{Cmd: cmd1}: {Cmd: cmd1},
d4aec5f0
 		// only labels
7ac4232e
 		&container.Config{Labels: labels1}: {Labels: labels1},
d4aec5f0
 		// only exposedPorts
7ac4232e
 		&container.Config{ExposedPorts: ports1}: {ExposedPorts: ports1},
d4aec5f0
 		// only entrypoints
7ac4232e
 		&container.Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint1},
d4aec5f0
 		// only volumes
7ac4232e
 		&container.Config{Volumes: volumes1}: {Volumes: volumes1},
d4aec5f0
 	}
7ac4232e
 	differentConfigs := map[*container.Config]*container.Config{
d4aec5f0
 		nil: nil,
7ac4232e
 		&container.Config{
d4aec5f0
 			Hostname:   "host1",
 			Domainname: "domain1",
 			Image:      "image1",
 			User:       "user1",
 		}: {
 			Hostname:   "host1",
 			Domainname: "domain1",
 			Image:      "image1",
 			User:       "user2",
 		},
 		// only OpenStdin
7ac4232e
 		&container.Config{OpenStdin: false}: {OpenStdin: true},
 		&container.Config{OpenStdin: true}:  {OpenStdin: false},
d4aec5f0
 		// only env
7ac4232e
 		&container.Config{Env: envs1}: {Env: envs2},
d4aec5f0
 		// only cmd
7ac4232e
 		&container.Config{Cmd: cmd1}: {Cmd: cmd2},
d4aec5f0
 		// not the same number of parts
7ac4232e
 		&container.Config{Cmd: cmd1}: {Cmd: cmd3},
d4aec5f0
 		// only labels
7ac4232e
 		&container.Config{Labels: labels1}: {Labels: labels2},
d4aec5f0
 		// not the same number of labels
7ac4232e
 		&container.Config{Labels: labels1}: {Labels: labels3},
d4aec5f0
 		// only exposedPorts
7ac4232e
 		&container.Config{ExposedPorts: ports1}: {ExposedPorts: ports2},
d4aec5f0
 		// not the same number of ports
7ac4232e
 		&container.Config{ExposedPorts: ports1}: {ExposedPorts: ports3},
d4aec5f0
 		// only entrypoints
7ac4232e
 		&container.Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint2},
d4aec5f0
 		// not the same number of parts
7ac4232e
 		&container.Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint3},
d4aec5f0
 		// only volumes
7ac4232e
 		&container.Config{Volumes: volumes1}: {Volumes: volumes2},
d4aec5f0
 		// not the same number of labels
7ac4232e
 		&container.Config{Volumes: volumes1}: {Volumes: volumes3},
d4aec5f0
 	}
 	for config1, config2 := range sameConfigs {
 		if !Compare(config1, config2) {
 			t.Fatalf("Compare should be true for [%v] and [%v]", config1, config2)
 		}
 	}
 	for config1, config2 := range differentConfigs {
 		if Compare(config1, config2) {
 			t.Fatalf("Compare should be false for [%v] and [%v]", config1, config2)
 		}
 	}
 }