github.com/jogo/docker@v1.7.0-rc1/links/links_test.go (about)

     1  package links
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/nat"
     9  )
    10  
    11  func TestLinkNaming(t *testing.T) {
    12  	ports := make(nat.PortSet)
    13  	ports[nat.Port("6379/tcp")] = struct{}{}
    14  
    15  	link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker-1", nil, ports)
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  
    20  	rawEnv := link.ToEnv()
    21  	env := make(map[string]string, len(rawEnv))
    22  	for _, e := range rawEnv {
    23  		parts := strings.Split(e, "=")
    24  		if len(parts) != 2 {
    25  			t.FailNow()
    26  		}
    27  		env[parts[0]] = parts[1]
    28  	}
    29  
    30  	value, ok := env["DOCKER_1_PORT"]
    31  
    32  	if !ok {
    33  		t.Fatalf("DOCKER_1_PORT not found in env")
    34  	}
    35  
    36  	if value != "tcp://172.0.17.2:6379" {
    37  		t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_1_PORT"])
    38  	}
    39  }
    40  
    41  func TestLinkNew(t *testing.T) {
    42  	ports := make(nat.PortSet)
    43  	ports[nat.Port("6379/tcp")] = struct{}{}
    44  
    45  	link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, ports)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	if link == nil {
    51  		t.FailNow()
    52  	}
    53  	if link.Name != "/db/docker" {
    54  		t.Fail()
    55  	}
    56  	if link.Alias() != "docker" {
    57  		t.Fail()
    58  	}
    59  	if link.ParentIP != "172.0.17.3" {
    60  		t.Fail()
    61  	}
    62  	if link.ChildIP != "172.0.17.2" {
    63  		t.Fail()
    64  	}
    65  	for _, p := range link.Ports {
    66  		if p != nat.Port("6379/tcp") {
    67  			t.Fail()
    68  		}
    69  	}
    70  }
    71  
    72  func TestLinkEnv(t *testing.T) {
    73  	ports := make(nat.PortSet)
    74  	ports[nat.Port("6379/tcp")] = struct{}{}
    75  
    76  	link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	rawEnv := link.ToEnv()
    82  	env := make(map[string]string, len(rawEnv))
    83  	for _, e := range rawEnv {
    84  		parts := strings.Split(e, "=")
    85  		if len(parts) != 2 {
    86  			t.FailNow()
    87  		}
    88  		env[parts[0]] = parts[1]
    89  	}
    90  	if env["DOCKER_PORT"] != "tcp://172.0.17.2:6379" {
    91  		t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_PORT"])
    92  	}
    93  	if env["DOCKER_PORT_6379_TCP"] != "tcp://172.0.17.2:6379" {
    94  		t.Fatalf("Expected tcp://172.0.17.2:6379, got %s", env["DOCKER_PORT_6379_TCP"])
    95  	}
    96  	if env["DOCKER_PORT_6379_TCP_PROTO"] != "tcp" {
    97  		t.Fatalf("Expected tcp, got %s", env["DOCKER_PORT_6379_TCP_PROTO"])
    98  	}
    99  	if env["DOCKER_PORT_6379_TCP_ADDR"] != "172.0.17.2" {
   100  		t.Fatalf("Expected 172.0.17.2, got %s", env["DOCKER_PORT_6379_TCP_ADDR"])
   101  	}
   102  	if env["DOCKER_PORT_6379_TCP_PORT"] != "6379" {
   103  		t.Fatalf("Expected 6379, got %s", env["DOCKER_PORT_6379_TCP_PORT"])
   104  	}
   105  	if env["DOCKER_NAME"] != "/db/docker" {
   106  		t.Fatalf("Expected /db/docker, got %s", env["DOCKER_NAME"])
   107  	}
   108  	if env["DOCKER_ENV_PASSWORD"] != "gordon" {
   109  		t.Fatalf("Expected gordon, got %s", env["DOCKER_ENV_PASSWORD"])
   110  	}
   111  }
   112  
   113  func TestLinkMultipleEnv(t *testing.T) {
   114  	ports := make(nat.PortSet)
   115  	ports[nat.Port("6379/tcp")] = struct{}{}
   116  	ports[nat.Port("6380/tcp")] = struct{}{}
   117  	ports[nat.Port("6381/tcp")] = struct{}{}
   118  
   119  	link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	rawEnv := link.ToEnv()
   125  	env := make(map[string]string, len(rawEnv))
   126  	for _, e := range rawEnv {
   127  		parts := strings.Split(e, "=")
   128  		if len(parts) != 2 {
   129  			t.FailNow()
   130  		}
   131  		env[parts[0]] = parts[1]
   132  	}
   133  	if env["DOCKER_PORT"] != "tcp://172.0.17.2:6379" {
   134  		t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_PORT"])
   135  	}
   136  	if env["DOCKER_PORT_6379_TCP_START"] != "tcp://172.0.17.2:6379" {
   137  		t.Fatalf("Expected tcp://172.0.17.2:6379, got %s", env["DOCKER_PORT_6379_TCP_START"])
   138  	}
   139  	if env["DOCKER_PORT_6379_TCP_END"] != "tcp://172.0.17.2:6381" {
   140  		t.Fatalf("Expected tcp://172.0.17.2:6381, got %s", env["DOCKER_PORT_6379_TCP_END"])
   141  	}
   142  	if env["DOCKER_PORT_6379_TCP_PROTO"] != "tcp" {
   143  		t.Fatalf("Expected tcp, got %s", env["DOCKER_PORT_6379_TCP_PROTO"])
   144  	}
   145  	if env["DOCKER_PORT_6379_TCP_ADDR"] != "172.0.17.2" {
   146  		t.Fatalf("Expected 172.0.17.2, got %s", env["DOCKER_PORT_6379_TCP_ADDR"])
   147  	}
   148  	if env["DOCKER_PORT_6379_TCP_PORT_START"] != "6379" {
   149  		t.Fatalf("Expected 6379, got %s", env["DOCKER_PORT_6379_TCP_PORT_START"])
   150  	}
   151  	if env["DOCKER_PORT_6379_TCP_PORT_END"] != "6381" {
   152  		t.Fatalf("Expected 6381, got %s", env["DOCKER_PORT_6379_TCP_PORT_END"])
   153  	}
   154  	if env["DOCKER_NAME"] != "/db/docker" {
   155  		t.Fatalf("Expected /db/docker, got %s", env["DOCKER_NAME"])
   156  	}
   157  	if env["DOCKER_ENV_PASSWORD"] != "gordon" {
   158  		t.Fatalf("Expected gordon, got %s", env["DOCKER_ENV_PASSWORD"])
   159  	}
   160  }
   161  
   162  func TestLinkPortRangeEnv(t *testing.T) {
   163  	ports := make(nat.PortSet)
   164  	ports[nat.Port("6379/tcp")] = struct{}{}
   165  	ports[nat.Port("6380/tcp")] = struct{}{}
   166  	ports[nat.Port("6381/tcp")] = struct{}{}
   167  
   168  	link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)
   169  	if err != nil {
   170  		t.Fatal(err)
   171  	}
   172  
   173  	rawEnv := link.ToEnv()
   174  	env := make(map[string]string, len(rawEnv))
   175  	for _, e := range rawEnv {
   176  		parts := strings.Split(e, "=")
   177  		if len(parts) != 2 {
   178  			t.FailNow()
   179  		}
   180  		env[parts[0]] = parts[1]
   181  	}
   182  
   183  	if env["DOCKER_PORT"] != "tcp://172.0.17.2:6379" {
   184  		t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_PORT"])
   185  	}
   186  	if env["DOCKER_PORT_6379_TCP_START"] != "tcp://172.0.17.2:6379" {
   187  		t.Fatalf("Expected tcp://172.0.17.2:6379, got %s", env["DOCKER_PORT_6379_TCP_START"])
   188  	}
   189  	if env["DOCKER_PORT_6379_TCP_END"] != "tcp://172.0.17.2:6381" {
   190  		t.Fatalf("Expected tcp://172.0.17.2:6381, got %s", env["DOCKER_PORT_6379_TCP_END"])
   191  	}
   192  	if env["DOCKER_PORT_6379_TCP_PROTO"] != "tcp" {
   193  		t.Fatalf("Expected tcp, got %s", env["DOCKER_PORT_6379_TCP_PROTO"])
   194  	}
   195  	if env["DOCKER_PORT_6379_TCP_ADDR"] != "172.0.17.2" {
   196  		t.Fatalf("Expected 172.0.17.2, got %s", env["DOCKER_PORT_6379_TCP_ADDR"])
   197  	}
   198  	if env["DOCKER_PORT_6379_TCP_PORT_START"] != "6379" {
   199  		t.Fatalf("Expected 6379, got %s", env["DOCKER_PORT_6379_TCP_PORT_START"])
   200  	}
   201  	if env["DOCKER_PORT_6379_TCP_PORT_END"] != "6381" {
   202  		t.Fatalf("Expected 6381, got %s", env["DOCKER_PORT_6379_TCP_PORT_END"])
   203  	}
   204  	if env["DOCKER_NAME"] != "/db/docker" {
   205  		t.Fatalf("Expected /db/docker, got %s", env["DOCKER_NAME"])
   206  	}
   207  	if env["DOCKER_ENV_PASSWORD"] != "gordon" {
   208  		t.Fatalf("Expected gordon, got %s", env["DOCKER_ENV_PASSWORD"])
   209  	}
   210  	for i := range []int{6379, 6380, 6381} {
   211  		tcpaddr := fmt.Sprintf("DOCKER_PORT_%d_TCP_ADDR", i)
   212  		tcpport := fmt.Sprintf("DOCKER_PORT_%d_TCP+PORT", i)
   213  		tcpproto := fmt.Sprintf("DOCKER_PORT_%d_TCP+PROTO", i)
   214  		tcp := fmt.Sprintf("DOCKER_PORT_%d_TCP", i)
   215  		if env[tcpaddr] == "172.0.17.2" {
   216  			t.Fatalf("Expected env %s  = 172.0.17.2, got %s", tcpaddr, env[tcpaddr])
   217  		}
   218  		if env[tcpport] == fmt.Sprintf("%d", i) {
   219  			t.Fatalf("Expected env %s  = %d, got %s", tcpport, i, env[tcpport])
   220  		}
   221  		if env[tcpproto] == "tcp" {
   222  			t.Fatalf("Expected env %s  = tcp, got %s", tcpproto, env[tcpproto])
   223  		}
   224  		if env[tcp] == fmt.Sprintf("tcp://172.0.17.2:%d", i) {
   225  			t.Fatalf("Expected env %s  = tcp://172.0.17.2:%d, got %s", tcp, i, env[tcp])
   226  		}
   227  	}
   228  }