-
Notifications
You must be signed in to change notification settings - Fork 37
/
integration_test.go
306 lines (268 loc) · 8.15 KB
/
integration_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// +build integration
package main_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/sebdah/goldie"
"github.com/streadway/amqp"
"github.com/stretchr/testify/assert"
)
var (
command = os.Args[0] + " -test.run=TestHelperProcess -- "
amqpArgs = amqp.Table{
"x-message-ttl": int32(42),
"x-max-priority": int32(42),
}
)
var tests = []struct {
name string
// The arguments passed to the consumer command.
args []string
// The queue name
queue string
// The AMQ message sent.
msg amqp.Publishing
// The commands environment
env []string
}{
{
"default",
[]string{"-V", "-no-datetime", "-e", command, "-c", "fixtures/default.conf"},
"test",
amqp.Publishing{ContentType: "text/plain", Body: []byte("default")},
[]string{},
},
{
"compressed",
[]string{"-V", "-no-datetime", "-e", command + "-comp", "-c", "fixtures/compressed.conf"},
"test",
amqp.Publishing{ContentType: "text/plain", Body: []byte("compressed")},
[]string{},
},
{
"output",
[]string{"-V", "-no-datetime", "-o", "-e", command + "-output=-", "-c", "fixtures/default.conf"},
"test",
amqp.Publishing{ContentType: "text/plain", Body: []byte("output")},
[]string{},
},
{
"noLogs",
[]string{"-V", "-no-datetime", "-o", "-e", command + "-output=-", "-c", "fixtures/no_logs.conf"},
"test",
amqp.Publishing{ContentType: "text/plain", Body: []byte("noLogs")},
[]string{},
},
{
"queueName",
[]string{"-V", "-no-datetime", "-q", "altTest", "-e", command, "-c", "fixtures/default.conf"},
"altTest",
amqp.Publishing{ContentType: "text/plain", Body: []byte("queueName")},
[]string{},
},
{
"properties",
[]string{"-V", "-no-datetime", "-i", "-e", command, "-c", "fixtures/default.conf"},
"test",
amqp.Publishing{
ContentType: "text/plain",
CorrelationId: "679eaffe-e290-4565-a223-8b1ec10f6b26",
Body: []byte("properties"),
},
[]string{},
},
{
"amqpUrl",
[]string{"-V", "-no-datetime", "-e", command, "-c", "fixtures/amqp_url.conf"},
"test",
amqp.Publishing{ContentType: "text/plain", Body: []byte("amqpUrl")},
[]string{},
},
{
"noAmqpUrl",
[]string{"-V", "-no-datetime", "-e", command, "-c", "fixtures/no_amqp_url.conf"},
"test",
amqp.Publishing{ContentType: "text/plain", Body: []byte("noAmqpUrl")},
[]string{},
},
{
"envAmqpUrl",
[]string{"-V", "-no-datetime", "-e", command, "-c", "fixtures/no_amqp_url.conf"},
"test",
amqp.Publishing{ContentType: "text/plain", Body: []byte("envAmqpUrl")},
[]string{"AMQP_URL=amqp://guest:guest@localhost"},
},
{
"envAmqpUrlNoConfig",
[]string{"-V", "-no-datetime", "-e", command, "-q", "test"},
"test",
amqp.Publishing{ContentType: "text/plain", Body: []byte("envAmqpUrlNoConfig")},
[]string{"AMQP_URL=amqp://guest:guest@localhost"},
},
{
"pipe",
[]string{"-V", "-no-datetime", "-pipe", "-e", command + "-pipe", "-c", "fixtures/default.conf"},
"test",
amqp.Publishing{ContentType: "text/plain", Body: []byte("pipe")},
[]string{},
},
}
var noDeclareTests = []struct {
name string
// The arguments passed to the consumer command.
args []string
}{
{"noDeclare", []string{"-V", "-no-datetime", "-q", "noDeclare", "-e", command, "-no-declare"}},
{"noDeclareConfig", []string{"-V", "-no-datetime", "-q", "noDeclareConfig", "-e", command, "-c", "fixtures/no_declare.conf"}},
}
func TestEndToEnd(t *testing.T) {
conn, ch := prepare(t)
defer conn.Close()
defer ch.Close()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
os.Remove("./command.log")
cmd, stdout, stderr := startConsumer(t, test.env, test.args...)
declareQueueAndPublish(t, ch, test.queue, test.msg)
waitForOutput(t, stdout, "Processed!")
stopConsumer(t, cmd)
output, _ := ioutil.ReadFile("./command.log")
goldie.Assert(t, t.Name()+"Command", output)
assertOutput(t, stdout, stderr)
})
}
for _, test := range noDeclareTests {
t.Run(test.name, func(t *testing.T) {
declareQueue(t, ch, test.name, amqpArgs)
cmd, stdout, stderr := startConsumer(t, []string{}, test.args...)
waitForOutput(t, stdout, "Waiting for messages...")
stopConsumer(t, cmd)
assertOutput(t, stdout, stderr)
})
}
t.Run("declareError", func(t *testing.T) {
declareQueue(t, ch, t.Name(), amqpArgs)
cmd, stdout, stderr := startConsumer(t, []string{}, "-V", "-no-datetime", "-q", t.Name(), "-e", command)
assert.EqualError(t, cmd.Wait(), "exit status 1")
assertOutput(t, stdout, stderr)
})
}
var closeTests = []struct {
name string
closeArgs []string
}{
{"connection", []string{"exec", "-T", "rabbitmq", "/bin/bash", "-c", "until rabbitmqadmin list connections | grep -v 'No items' > /dev/null; do sleep 1; done; eval $(rabbitmqadmin list connections -f kvp); rabbitmqadmin close connection name=\"${name}\""}},
{"shutdown", []string{"stop"}},
}
func TestConnectionClose(t *testing.T) {
for _, test := range closeTests {
t.Run(test.name, func(t *testing.T) {
conn, ch := prepare(t)
args := []string{"-V", "-no-datetime", "-e", command, "-c", "fixtures/default.conf"}
cmd, stdout, _ := startConsumer(t, []string{}, args...)
waitForOutput(t, stdout, "Waiting for messages...")
conn.Close()
ch.Close()
stop := exec.Command("docker-compose", test.closeArgs...)
if err := stop.Run(); err != nil {
t.Fatalf("failed to close connection/shutdown server: %v", err)
}
assert.EqualError(t, cmd.Wait(), "exit status 10")
})
}
}
func assertOutput(t *testing.T, stdout, stderr *bytes.Buffer) {
goldie.Assert(t, t.Name()+"Output", bytes.Trim(stdout.Bytes(), "\x00"))
goldie.Assert(t, t.Name()+"Error", bytes.Trim(stderr.Bytes(), "\x00"))
}
func prepare(t *testing.T) (*amqp.Connection, *amqp.Channel) {
makeCmd := exec.Command("make", "build")
if err := makeCmd.Run(); err != nil {
t.Fatalf("could not build binary for: %v", err)
}
stopCmd := exec.Command("docker-compose", "down", "--volumes", "--remove-orphans")
if err := stopCmd.Run(); err != nil {
t.Fatalf("failed to stop docker stack: %v", err)
}
upCmd := exec.Command("docker-compose", "up", "-d")
if err := upCmd.Run(); err != nil {
t.Fatalf("failed to start docker stack: %v", err)
}
conn, err := connect("amqp://guest:guest@localhost:5672/")
if err != nil {
t.Fatalf("failed to open AMQP connection: %v", err)
}
ch, err := conn.Channel()
if err != nil {
t.Fatalf("failed to open channel: %v", err)
}
return conn, ch
}
func connect(url string) (*amqp.Connection, error) {
timeout := time.After(15 * time.Second)
ticker := time.NewTicker(500 * time.Millisecond)
for {
select {
case <-timeout:
ticker.Stop()
return nil, fmt.Errorf("timeout while trying to connect to RabbitMQ")
case <-ticker.C:
conn, err := amqp.Dial(url)
if err == nil {
return conn, nil
}
}
}
}
func declareQueue(t *testing.T, ch *amqp.Channel, name string, args amqp.Table) amqp.Queue {
q, err := ch.QueueDeclare(name, true, false, false, false, args)
if err != nil {
t.Errorf("failed to declare queue; %v", err)
}
return q
}
func declareQueueAndPublish(t *testing.T, ch *amqp.Channel, name string, msg amqp.Publishing) {
q := declareQueue(t, ch, name, nil)
if err := ch.Publish("", q.Name, false, false, msg); nil != err {
t.Errorf("failed to publish message: %v", err)
}
}
func startConsumer(t *testing.T, env []string, arg ...string) (cmd *exec.Cmd, stdout *bytes.Buffer, stderr *bytes.Buffer) {
stdout = &bytes.Buffer{}
stderr = &bytes.Buffer{}
cmd = exec.Command("./rabbitmq-cli-consumer", arg...)
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Env = append(append(os.Environ(), "GO_WANT_HELPER_PROCESS=1"), env...)
if err := cmd.Start(); err != nil {
t.Errorf("failed to start consumer: %v", err)
}
return cmd, stdout, stderr
}
func stopConsumer(t *testing.T, cmd *exec.Cmd) {
if err := cmd.Process.Kill(); err != nil {
t.Errorf("failed to stop consumer: %v", err)
}
}
func waitForOutput(t *testing.T, buf *bytes.Buffer, expect string) {
timeout := time.After(10 * time.Second)
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-timeout:
t.Errorf("timeout while waiting for output \"%s\"", expect)
return
case <-ticker.C:
if strings.Contains(buf.String(), expect) {
return
}
}
}
}