forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_forwarding_test.go
203 lines (177 loc) · 4.41 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package testcontainers_test
import (
"context"
"fmt"
"io"
"net"
"net/http"
"testing"
"time"
"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) {
tests := []struct {
name string
numberOfPorts int
hasNetwork bool
hasHostAccess bool
}{
{
name: "single port",
numberOfPorts: 1,
hasHostAccess: true,
},
{
name: "single port using a network",
numberOfPorts: 1,
hasNetwork: true,
hasHostAccess: true,
},
{
name: "multiple ports",
numberOfPorts: 3,
hasHostAccess: true,
},
{
name: "single port with cancellation",
numberOfPorts: 1,
hasHostAccess: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(tt *testing.T) {
freePorts := make([]int, tc.numberOfPorts)
for i := range freePorts {
freePort, err := getFreePort()
if err != nil {
tt.Fatal(err)
}
freePorts[i] = freePort
// create an http server for each port
server, err := createHttpServer(freePort)
if err != nil {
tt.Fatal(err)
}
go func() {
_ = server.ListenAndServe()
}()
tt.Cleanup(func() {
server.Close()
})
}
req := testcontainers.GenericContainerRequest{
// hostAccessPorts {
ContainerRequest: testcontainers.ContainerRequest{
Image: "alpine:3.17",
HostAccessPorts: freePorts,
Cmd: []string{"top"},
},
// }
Started: true,
}
var nw *testcontainers.DockerNetwork
if tc.hasNetwork {
var err error
nw, err = network.New(context.Background())
if err != nil {
tt.Fatal(err)
}
tt.Cleanup(func() {
if err := nw.Remove(context.Background()); err != nil {
tt.Fatal(err)
}
})
req.Networks = []string{nw.Name}
req.NetworkAliases = map[string][]string{nw.Name: {"myalpine"}}
}
ctx := context.Background()
if !tc.hasHostAccess {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
defer cancel()
}
c, err := testcontainers.GenericContainer(ctx, req)
if err != nil {
tt.Fatal(err)
}
tt.Cleanup(func() {
if err := c.Terminate(context.Background()); err != nil {
tt.Fatal(err)
}
})
if tc.hasHostAccess {
// create a container that has host access, which will
// automatically forward the port to the container
assertContainerHasHostAccess(tt, c, freePorts...)
} else {
// force cancellation because of timeout
time.Sleep(11 * time.Second)
assertContainerHasNoHostAccess(tt, c, freePorts...)
}
})
}
}
func httpRequest(t *testing.T, c testcontainers.Container, port int) (int, string) {
// wgetHostInternal {
code, reader, err := c.Exec(
context.Background(),
[]string{"wget", "-q", "-O", "-", fmt.Sprintf("http://%s:%d", testcontainers.HostInternal, port)},
tcexec.Multiplexed(),
)
// }
if err != nil {
t.Fatal(err)
}
// read the response
bs, err := io.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
return code, string(bs)
}
func assertContainerHasHostAccess(t *testing.T, c testcontainers.Container, ports ...int) {
for _, port := range ports {
code, response := httpRequest(t, c, port)
if code != 0 {
t.Fatalf("expected status code [%d] but got [%d]", 0, code)
}
if response != expectedResponse {
t.Fatalf("expected [%s] but got [%s]", expectedResponse, response)
}
}
}
func assertContainerHasNoHostAccess(t *testing.T, c testcontainers.Container, ports ...int) {
for _, port := range ports {
_, response := httpRequest(t, c, port)
if response == expectedResponse {
t.Fatalf("expected not to get [%s] but got [%s]", expectedResponse, response)
}
}
}
func createHttpServer(port int) (*http.Server, error) {
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
}
server.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, expectedResponse)
})
return server, nil
}
// getFreePort asks the kernel for a free open port that is ready to use.
func getFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}