-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
http_test.go
132 lines (120 loc) · 3.19 KB
/
http_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
package wait_test
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"path"
"testing"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
// https://github.com/testcontainers/testcontainers-go/issues/183
func ExampleHTTPStrategy() {
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "gogs/gogs:0.11.91",
ExposedPorts: []string{"3000/tcp"},
WaitingFor: wait.ForHTTP("/").WithPort("3000/tcp"),
}
gogs, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
panic(err)
}
defer func() {
if err := gogs.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()
// Here you have a running container
}
func TestHTTPStrategyWaitUntilReady(t *testing.T) {
workdir, err := os.Getwd()
if err != nil {
t.Error(err)
return
}
capath := path.Join(workdir, "testdata", "root.pem")
cafile, err := os.ReadFile(capath)
if err != nil {
t.Errorf("can't load ca file: %v", err)
return
}
certpool := x509.NewCertPool()
if !certpool.AppendCertsFromPEM(cafile) {
t.Errorf("the ca file isn't valid")
return
}
tlsconfig := &tls.Config{RootCAs: certpool, ServerName: "testcontainer.go.test"}
var i int
dockerReq := testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Context: path.Join(workdir, "/testdata"),
},
ExposedPorts: []string{"6443/tcp"},
WaitingFor: wait.NewHTTPStrategy("/ping").WithTLS(true, tlsconfig).
WithStartupTimeout(time.Second * 10).WithPort("6443/tcp").
WithResponseMatcher(func(body io.Reader) bool {
data, _ := io.ReadAll(body)
return bytes.Equal(data, []byte("pong"))
}).
WithStatusCodeMatcher(func(status int) bool {
i++ // always fail the first try in order to force the polling loop to be re-run
return i > 1 && status == 200
}).
WithMethod(http.MethodPost).WithBody(bytes.NewReader([]byte("ping"))),
}
ctx := context.Background()
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ContainerRequest: dockerReq, Started: true})
if err != nil {
t.Error(err)
return
}
testcontainers.CleanupContainer(t, ctx, container)
host, err := container.Host(ctx)
if err != nil {
t.Error(err)
return
}
port, err := container.MappedPort(ctx, "6443/tcp")
if err != nil {
t.Error(err)
return
}
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsconfig,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
resp, err := client.Get(fmt.Sprintf("https://%s:%s", host, port.Port()))
if err != nil {
t.Error(err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("status code isn't ok: %s", resp.Status)
return
}
}