github.com/toophy/docker@v1.8.2/runconfig/compare_test.go (about)

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