|
| 1 | +package context |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + docker "github.com/fsouza/go-dockerclient" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +type FakePortBinding struct{} |
| 11 | + |
| 12 | +var httpPort = docker.Port("80/tcp") |
| 13 | +var httpPortBinding = docker.PortBinding{ |
| 14 | + HostIP: "100.100.100.100", |
| 15 | + HostPort: "8080", |
| 16 | +} |
| 17 | + |
| 18 | +var httpsPort = docker.Port("443/tcp") |
| 19 | + |
| 20 | +var httpTestPort = docker.Port("8080/tcp") |
| 21 | +var httpsTestPort = docker.Port("8443/tcp") |
| 22 | + |
| 23 | +func TestGenerateContainerAddresses(t *testing.T) { |
| 24 | + testContainer := &docker.Container{ |
| 25 | + Config: &docker.Config{ |
| 26 | + ExposedPorts: map[docker.Port]struct{}{}, |
| 27 | + }, |
| 28 | + NetworkSettings: &docker.NetworkSettings{ |
| 29 | + IPAddress: "10.0.0.10", |
| 30 | + LinkLocalIPv6Address: "24", |
| 31 | + GlobalIPv6Address: "10.0.0.1", |
| 32 | + Ports: map[docker.Port][]docker.PortBinding{}, |
| 33 | + }, |
| 34 | + } |
| 35 | + testContainer.NetworkSettings.Ports[httpPort] = []docker.PortBinding{httpPortBinding} |
| 36 | + testContainer.NetworkSettings.Ports[httpsPort] = []docker.PortBinding{} |
| 37 | + |
| 38 | + addresses := GetContainerAddresses(testContainer) |
| 39 | + assert.Len(t, addresses, len(testContainer.NetworkSettings.Ports)) |
| 40 | + assert.Contains(t, addresses, Address{ |
| 41 | + IP: "10.0.0.10", |
| 42 | + IP6LinkLocal: "24", |
| 43 | + IP6Global: "10.0.0.1", |
| 44 | + Port: "80", |
| 45 | + Proto: "tcp", |
| 46 | + HostIP: "100.100.100.100", |
| 47 | + HostPort: "8080", |
| 48 | + }) |
| 49 | + assert.Contains(t, addresses, Address{ |
| 50 | + IP: "10.0.0.10", |
| 51 | + IP6LinkLocal: "24", |
| 52 | + IP6Global: "10.0.0.1", |
| 53 | + Port: "443", |
| 54 | + Proto: "tcp", |
| 55 | + HostIP: "", |
| 56 | + HostPort: "", |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +func TestGenerateContainerAddressesWithExposedPorts(t *testing.T) { |
| 61 | + testContainer := &docker.Container{ |
| 62 | + Config: &docker.Config{ |
| 63 | + ExposedPorts: map[docker.Port]struct{}{}, |
| 64 | + }, |
| 65 | + NetworkSettings: &docker.NetworkSettings{ |
| 66 | + IPAddress: "10.0.0.10", |
| 67 | + LinkLocalIPv6Address: "24", |
| 68 | + GlobalIPv6Address: "10.0.0.1", |
| 69 | + Ports: map[docker.Port][]docker.PortBinding{}, |
| 70 | + }, |
| 71 | + } |
| 72 | + testContainer.NetworkSettings.Ports[httpPort] = []docker.PortBinding{} |
| 73 | + testContainer.NetworkSettings.Ports[httpsPort] = []docker.PortBinding{} |
| 74 | + testContainer.Config.ExposedPorts[httpPort] = struct{}{} |
| 75 | + testContainer.Config.ExposedPorts[httpsPort] = struct{}{} |
| 76 | + testContainer.Config.ExposedPorts[httpTestPort] = struct{}{} |
| 77 | + |
| 78 | + assert.Len(t, GetContainerAddresses(testContainer), 2) |
| 79 | +} |
| 80 | + |
| 81 | +func TestGenerateContainerAddressesWithNoPorts(t *testing.T) { |
| 82 | + testContainer := &docker.Container{ |
| 83 | + Config: &docker.Config{ |
| 84 | + ExposedPorts: map[docker.Port]struct{}{}, |
| 85 | + }, |
| 86 | + NetworkSettings: &docker.NetworkSettings{ |
| 87 | + IPAddress: "10.0.0.10", |
| 88 | + LinkLocalIPv6Address: "24", |
| 89 | + GlobalIPv6Address: "10.0.0.1", |
| 90 | + Ports: map[docker.Port][]docker.PortBinding{}, |
| 91 | + }, |
| 92 | + } |
| 93 | + testContainer.Config.ExposedPorts[httpTestPort] = FakePortBinding{} |
| 94 | + testContainer.Config.ExposedPorts[httpsTestPort] = FakePortBinding{} |
| 95 | + |
| 96 | + addresses := GetContainerAddresses(testContainer) |
| 97 | + assert.Len(t, addresses, len(testContainer.Config.ExposedPorts)) |
| 98 | + assert.Contains(t, addresses, Address{ |
| 99 | + IP: "10.0.0.10", |
| 100 | + IP6LinkLocal: "24", |
| 101 | + IP6Global: "10.0.0.1", |
| 102 | + Port: "8080", |
| 103 | + Proto: "tcp", |
| 104 | + HostIP: "", |
| 105 | + HostPort: "", |
| 106 | + }) |
| 107 | + assert.Contains(t, addresses, Address{ |
| 108 | + IP: "10.0.0.10", |
| 109 | + IP6LinkLocal: "24", |
| 110 | + IP6Global: "10.0.0.1", |
| 111 | + Port: "8443", |
| 112 | + Proto: "tcp", |
| 113 | + HostIP: "", |
| 114 | + HostPort: "", |
| 115 | + }) |
| 116 | +} |
0 commit comments