github.com/gondor/docker@v1.9.0-rc1/runconfig/compare_test.go (about)

     1  package runconfig
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/pkg/nat"
     7  	"github.com/docker/docker/pkg/stringutils"
     8  )
     9  
    10  // Just to make life easier
    11  func newPortNoError(proto, port string) nat.Port {
    12  	p, _ := nat.NewPort(proto, port)
    13  	return p
    14  }
    15  
    16  func TestCompare(t *testing.T) {
    17  	ports1 := make(nat.PortSet)
    18  	ports1[newPortNoError("tcp", "1111")] = struct{}{}
    19  	ports1[newPortNoError("tcp", "2222")] = struct{}{}
    20  	ports2 := make(nat.PortSet)
    21  	ports2[newPortNoError("tcp", "3333")] = struct{}{}
    22  	ports2[newPortNoError("tcp", "4444")] = struct{}{}
    23  	ports3 := make(nat.PortSet)
    24  	ports3[newPortNoError("tcp", "1111")] = struct{}{}
    25  	ports3[newPortNoError("tcp", "2222")] = struct{}{}
    26  	ports3[newPortNoError("tcp", "5555")] = struct{}{}
    27  	volumes1 := make(map[string]struct{})
    28  	volumes1["/test1"] = struct{}{}
    29  	volumes2 := make(map[string]struct{})
    30  	volumes2["/test2"] = struct{}{}
    31  	volumes3 := make(map[string]struct{})
    32  	volumes3["/test1"] = struct{}{}
    33  	volumes3["/test3"] = struct{}{}
    34  	envs1 := []string{"ENV1=value1", "ENV2=value2"}
    35  	envs2 := []string{"ENV1=value1", "ENV3=value3"}
    36  	entrypoint1 := stringutils.NewStrSlice("/bin/sh", "-c")
    37  	entrypoint2 := stringutils.NewStrSlice("/bin/sh", "-d")
    38  	entrypoint3 := stringutils.NewStrSlice("/bin/sh", "-c", "echo")
    39  	cmd1 := stringutils.NewStrSlice("/bin/sh", "-c")
    40  	cmd2 := stringutils.NewStrSlice("/bin/sh", "-d")
    41  	cmd3 := stringutils.NewStrSlice("/bin/sh", "-c", "echo")
    42  	labels1 := map[string]string{"LABEL1": "value1", "LABEL2": "value2"}
    43  	labels2 := map[string]string{"LABEL1": "value1", "LABEL2": "value3"}
    44  	labels3 := map[string]string{"LABEL1": "value1", "LABEL2": "value2", "LABEL3": "value3"}
    45  
    46  	sameConfigs := map[*Config]*Config{
    47  		// Empty config
    48  		&Config{}: {},
    49  		// Does not compare hostname, domainname & image
    50  		&Config{
    51  			Hostname:   "host1",
    52  			Domainname: "domain1",
    53  			Image:      "image1",
    54  			User:       "user",
    55  		}: {
    56  			Hostname:   "host2",
    57  			Domainname: "domain2",
    58  			Image:      "image2",
    59  			User:       "user",
    60  		},
    61  		// only OpenStdin
    62  		&Config{OpenStdin: false}: {OpenStdin: false},
    63  		// only env
    64  		&Config{Env: envs1}: {Env: envs1},
    65  		// only cmd
    66  		&Config{Cmd: cmd1}: {Cmd: cmd1},
    67  		// only labels
    68  		&Config{Labels: labels1}: {Labels: labels1},
    69  		// only exposedPorts
    70  		&Config{ExposedPorts: ports1}: {ExposedPorts: ports1},
    71  		// only entrypoints
    72  		&Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint1},
    73  		// only volumes
    74  		&Config{Volumes: volumes1}: {Volumes: volumes1},
    75  	}
    76  	differentConfigs := map[*Config]*Config{
    77  		nil: nil,
    78  		&Config{
    79  			Hostname:   "host1",
    80  			Domainname: "domain1",
    81  			Image:      "image1",
    82  			User:       "user1",
    83  		}: {
    84  			Hostname:   "host1",
    85  			Domainname: "domain1",
    86  			Image:      "image1",
    87  			User:       "user2",
    88  		},
    89  		// only OpenStdin
    90  		&Config{OpenStdin: false}: {OpenStdin: true},
    91  		&Config{OpenStdin: true}:  {OpenStdin: false},
    92  		// only env
    93  		&Config{Env: envs1}: {Env: envs2},
    94  		// only cmd
    95  		&Config{Cmd: cmd1}: {Cmd: cmd2},
    96  		// not the same number of parts
    97  		&Config{Cmd: cmd1}: {Cmd: cmd3},
    98  		// only labels
    99  		&Config{Labels: labels1}: {Labels: labels2},
   100  		// not the same number of labels
   101  		&Config{Labels: labels1}: {Labels: labels3},
   102  		// only exposedPorts
   103  		&Config{ExposedPorts: ports1}: {ExposedPorts: ports2},
   104  		// not the same number of ports
   105  		&Config{ExposedPorts: ports1}: {ExposedPorts: ports3},
   106  		// only entrypoints
   107  		&Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint2},
   108  		// not the same number of parts
   109  		&Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint3},
   110  		// only volumes
   111  		&Config{Volumes: volumes1}: {Volumes: volumes2},
   112  		// not the same number of labels
   113  		&Config{Volumes: volumes1}: {Volumes: volumes3},
   114  	}
   115  	for config1, config2 := range sameConfigs {
   116  		if !Compare(config1, config2) {
   117  			t.Fatalf("Compare should be true for [%v] and [%v]", config1, config2)
   118  		}
   119  	}
   120  	for config1, config2 := range differentConfigs {
   121  		if Compare(config1, config2) {
   122  			t.Fatalf("Compare should be false for [%v] and [%v]", config1, config2)
   123  		}
   124  	}
   125  }