-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
100 lines (76 loc) · 2.99 KB
/
main_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
package main
import (
"fmt"
"io"
"net/http"
"testing"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"
)
// The tests in this suite check if the business requirements
// have been satisfied. Each test checks a single requirement.
// Every test in this suite is isolated from all other tests,
// there is no data or container instances shared at all.
// Beware, that by default HTTP client does not set a timeout.
// In tests, however, this isn't an issue because the tests *do*
// have a timeout, which defaults to 10 minutes, but can be reduced
// via a command-line parameter.
// After a container has started, there is some time required before
// the application inside it is ready to service the incoming requests.
// This creates a race condition with the test. To avoid this, the tests
// retry with exponential back-off.
// Requirement 1: The application response must contain a heart ("<3")
func TestRespondsWithLove(t *testing.T) {
pool, err := dockertest.NewPool("")
require.NoError(t, err, "could not connect to Docker")
resource, err := pool.Run("docker-go-server-ping", "latest", []string{})
require.NoError(t, err, "could not start container")
t.Cleanup(func() {
require.NoError(t, pool.Purge(resource), "failed to remove container")
})
var resp *http.Response
err = pool.Retry(func() error {
resp, err = http.Get(fmt.Sprint("http://localhost:", resource.GetPort("8080/tcp"), "/"))
if err != nil {
t.Log("container not ready, waiting...")
return err
}
return nil
})
require.NoError(t, err, "HTTP error")
defer resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode, "HTTP status code")
body, err := io.ReadAll(resp.Body)
fmt.Println("body")
fmt.Println(string(body))
require.NoError(t, err, "failed to read HTTP body")
// Finally, test the business requirement!
require.Contains(t, string(body), "<3", "does not respond with love?")
}
// Requirement 2: The application must include a health-check end-point at /ping,
// responding with JSON document { "Status": "OK" } if everything is well.
func TestHealthCheck(t *testing.T) {
pool, err := dockertest.NewPool("")
require.NoError(t, err, "could not connect to Docker")
resource, err := pool.Run("docker-go-server-ping", "latest", []string{})
require.NoError(t, err, "could not start container")
t.Cleanup(func() {
require.NoError(t, pool.Purge(resource), "failed to remove container")
})
var resp *http.Response
err = pool.Retry(func() error {
resp, err = http.Get(fmt.Sprint("http://localhost:", resource.GetPort("8080/tcp"), "/ping"))
if err != nil {
t.Log("container not ready, waiting...")
return err
}
return nil
})
require.NoError(t, err, "HTTP error")
defer resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode, "HTTP status code")
body, err := io.ReadAll(resp.Body)
require.NoError(t, err, "failed to read HTTP body")
// Finally, test the business requirement!
require.JSONEq(t, `{"Status":"OK"}`, string(body), "does not respond with valid JSON?")
}