github.com/reds/docker@v1.11.2-rc1/runconfig/compare_test.go (about)

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