-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
206 lines (182 loc) · 4.4 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
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
204
205
206
package eskeeper
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"testing"
"github.com/elastic/go-elasticsearch"
"github.com/ory/dockertest"
)
var url string
func TestMain(m *testing.M) {
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
resource, err := pool.Run(
"docker.elastic.co/elasticsearch/elasticsearch",
"7.11.1",
[]string{
"ES_JAVA_OPTS=-Xms512m -Xmx512m",
"discovery.type=single-node",
"node.name=es01",
},
)
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}
port := resource.GetPort("9200/tcp")
url = fmt.Sprintf("http://localhost:%s", port)
cfg := elasticsearch.Config{
Addresses: []string{url},
}
es, err := elasticsearch.NewClient(cfg)
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
if err := pool.Retry(func() error {
res, err := es.Info()
if err != nil {
log.Println("waiting to be ready...")
return err
}
defer res.Body.Close()
return nil
}); err != nil {
log.Fatalf("could not retry to connect : %s\n", err)
}
code := m.Run()
// You can't defer this because os.Exit doesn't care for defer
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
os.Exit(code)
}
func createTmpIndexHelper(tb testing.TB, name string) {
tb.Helper()
conf := elasticsearch.Config{
Addresses: []string{url},
}
es, err := elasticsearch.NewClient(conf)
if err != nil {
tb.Fatal(err)
}
f, err := os.Open("testdata/test.json")
if err != nil {
tb.Fatal(err)
}
res, err := es.Indices.Create(
name,
es.Indices.Create.WithBody(f),
)
if err != nil {
tb.Fatal(err)
}
if res.StatusCode != 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
tb.Fatal(err)
}
tb.Fatalf("failed to create index [index=%v, statusCode=%v, res=%v]", name, res.StatusCode, string(body))
}
}
var testAliasQuery = `
{
"actions" : [
{ "add" : { "index" : "%v", "alias" : "%v" } }
]
}`
func createTmpAliasHelper(tb testing.TB, name string, index string) {
tb.Helper()
conf := elasticsearch.Config{
Addresses: []string{url},
}
es, err := elasticsearch.NewClient(conf)
if err != nil {
tb.Fatal(err)
}
res, err := es.Indices.UpdateAliases(
strings.NewReader(fmt.Sprintf(testAliasQuery, index, name)),
)
if err != nil {
tb.Fatal(err)
}
if res.StatusCode != 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
tb.Fatal(err)
}
tb.Fatalf("failed to create alias [index= %v, statusCode=%v, res=%v]", name, res.StatusCode, string(body))
}
}
func deleteIndexHelper(tb testing.TB, indices []string) {
conf := elasticsearch.Config{
Addresses: []string{url},
}
es, err := elasticsearch.NewClient(conf)
if err != nil {
tb.Fatal(err)
}
d := es.Indices.Delete
res, err := d(indices)
if err != nil {
tb.Fatalf("close index: %v", err)
}
if res.StatusCode != 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
tb.Fatalf("failed to close index [statusCode=%v]", res.StatusCode)
}
tb.Fatalf("failed to close index [statusCode=%v, res=%v]", res.StatusCode, string(body))
}
}
func closeIndexHelper(tb testing.TB, index string) {
conf := elasticsearch.Config{
Addresses: []string{url},
}
es, err := elasticsearch.NewClient(conf)
if err != nil {
tb.Fatal(err)
}
close := es.Indices.Close
res, err := close([]string{index})
if err != nil {
tb.Fatalf("close index: %v", err)
}
if res.StatusCode != 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
tb.Fatalf("failed to close index [index= %v, statusCode=%v]", index, res.StatusCode)
}
tb.Fatalf("failed to close index [index= %v, statusCode=%v, res=%v]", index, res.StatusCode, string(body))
}
}
func postDocHelper(tb testing.TB, index string) {
tb.Helper()
conf := elasticsearch.Config{
Addresses: []string{url},
}
es, err := elasticsearch.NewClient(conf)
if err != nil {
tb.Fatal(err)
}
body := strings.NewReader(`{"title":"this is title","body":"this is body"}`)
res, err := es.Index(
index,
body,
es.Index.WithRefresh("true"),
)
if err != nil {
tb.Fatal(err)
}
if res.StatusCode != 201 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
tb.Fatal(err)
}
tb.Fatalf("failed to post document [index=%v, statusCode=%v, res=%v]", index, res.StatusCode, string(body))
}
}