forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_forwarding_test.go
156 lines (128 loc) · 4.01 KB
/
port_forwarding_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package testcontainers_test
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
tcexec "github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/network"
)
const (
expectedResponse = "Hello, World!"
)
func TestExposeHostPorts(t *testing.T) {
hostPorts := make([]int, 3)
for i := range hostPorts {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, expectedResponse)
}))
hostPorts[i] = server.Listener.Addr().(*net.TCPAddr).Port
t.Cleanup(server.Close)
}
singlePort := hostPorts[0:1]
t.Run("single-port", func(t *testing.T) {
testExposeHostPorts(t, singlePort, false, false)
})
t.Run("single-port-network", func(t *testing.T) {
testExposeHostPorts(t, singlePort, true, false)
})
t.Run("single-port-host-access", func(t *testing.T) {
testExposeHostPorts(t, singlePort, false, true)
})
t.Run("single-port-network-host-access", func(t *testing.T) {
testExposeHostPorts(t, singlePort, true, true)
})
t.Run("multi-port", func(t *testing.T) {
testExposeHostPorts(t, hostPorts, false, false)
})
t.Run("multi-port-network", func(t *testing.T) {
testExposeHostPorts(t, hostPorts, true, false)
})
t.Run("multi-port-host-access", func(t *testing.T) {
testExposeHostPorts(t, hostPorts, false, true)
})
t.Run("multi-port-network-host-access", func(t *testing.T) {
testExposeHostPorts(t, hostPorts, true, true)
})
}
func testExposeHostPorts(t *testing.T, hostPorts []int, hasNetwork, hasHostAccess bool) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
var hostAccessPorts []int
if hasHostAccess {
hostAccessPorts = hostPorts
}
req := testcontainers.GenericContainerRequest{
// hostAccessPorts {
ContainerRequest: testcontainers.ContainerRequest{
Image: "alpine:3.17",
HostAccessPorts: hostAccessPorts,
Cmd: []string{"top"},
},
// }
Started: true,
}
if hasNetwork {
nw, err := network.New(ctx)
require.NoError(t, err)
testcontainers.CleanupNetwork(t, nw)
req.Networks = []string{nw.Name}
req.NetworkAliases = map[string][]string{nw.Name: {"myalpine"}}
}
c, err := testcontainers.GenericContainer(ctx, req)
testcontainers.CleanupContainer(t, c)
require.NoError(t, err)
if hasHostAccess {
// Verify that the container can access the host ports.
containerHasHostAccess(t, c, hostPorts...)
return
}
// Verify that the container cannot access the host ports.
containerHasNoHostAccess(t, c, hostPorts...)
}
// httpRequest sends an HTTP request from the container to the host port via
// [testcontainers.HostInternal] address.
func httpRequest(t *testing.T, c testcontainers.Container, port int) (int, string) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// wgetHostInternal {
code, reader, err := c.Exec(
ctx,
[]string{"wget", "-q", "-O", "-", "-T", "2", fmt.Sprintf("http://%s:%d", testcontainers.HostInternal, port)},
tcexec.Multiplexed(),
)
// }
require.NoError(t, err)
// read the response
bs, err := io.ReadAll(reader)
require.NoError(t, err)
return code, string(bs)
}
// containerHasHostAccess verifies that the container can access the host ports
// via [testcontainers.HostInternal] address.
func containerHasHostAccess(t *testing.T, c testcontainers.Container, ports ...int) {
t.Helper()
for _, port := range ports {
code, response := httpRequest(t, c, port)
require.Zero(t, code)
require.Equal(t, expectedResponse, response)
}
}
// containerHasNoHostAccess verifies that the container cannot access the host ports
// via [testcontainers.HostInternal] address.
func containerHasNoHostAccess(t *testing.T, c testcontainers.Container, ports ...int) {
t.Helper()
for _, port := range ports {
code, response := httpRequest(t, c, port)
require.NotZero(t, code)
require.Contains(t, response, "bad address")
}
}