github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/links/links_test.go (about)

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