forked from fgeller/kt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_test.go
282 lines (232 loc) · 8.69 KB
/
system_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"sort"
"strings"
"syscall"
"testing"
"time"
"github.com/Shopify/sarama"
"github.com/stretchr/testify/require"
)
type cmd struct {
in string
}
func newCmd() *cmd { return &cmd{} }
func (c *cmd) stdIn(in string) *cmd { c.in = in; return c }
func (c *cmd) run(name string, args ...string) (int, string, string) {
cmd := exec.Command(name, args...)
var stdOut, stdErr bytes.Buffer
cmd.Stdout = &stdOut
cmd.Stderr = &stdErr
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "KT_BROKERS=localhost:9092")
if len(c.in) > 0 {
cmd.Stdin = strings.NewReader(c.in)
}
_ = cmd.Run()
status := cmd.ProcessState.Sys().(syscall.WaitStatus)
strOut := stdOut.String()
strErr := stdErr.String()
return status.ExitStatus(), strOut, strErr
}
func build(t *testing.T) {
var status int
status, _, _ = newCmd().run("make", "build")
require.Zero(t, status)
status, _, _ = newCmd().run("ls", "kt")
require.Zero(t, status)
}
func TestSystem(t *testing.T) {
build(t)
var err error
var status int
var stdOut, stdErr string
//
// kt admin -createtopic
//
topicName := fmt.Sprintf("kt-test-%v", randomString(6))
topicDetail := &sarama.TopicDetail{
NumPartitions: 1,
ReplicationFactor: 1,
}
buf, err := json.Marshal(topicDetail)
require.NoError(t, err)
fnTopicDetail := fmt.Sprintf("topic-detail-%v.json", randomString(6))
err = ioutil.WriteFile(fnTopicDetail, buf, 0666)
require.NoError(t, err)
defer os.RemoveAll(fnTopicDetail)
status, stdOut, stdErr = newCmd().stdIn(string(buf)).run("./kt", "admin", "-createtopic", topicName, "-topicdetail", fnTopicDetail)
fmt.Printf(">> system test kt admin -createtopic %v stdout:\n%s\n", topicName, stdOut)
fmt.Printf(">> system test kt admin -createtopic %v stderr:\n%s\n", topicName, stdErr)
require.Zero(t, status)
require.Empty(t, stdErr)
fmt.Printf(">> ✓\n")
//
// kt produce
//
req := map[string]interface{}{
"value": fmt.Sprintf("hello, %s", randomString(6)),
"key": "boom",
"partition": float64(0),
}
buf, err = json.Marshal(req)
require.NoError(t, err)
status, stdOut, stdErr = newCmd().stdIn(string(buf)).run("./kt", "produce", "-topic", topicName)
fmt.Printf(">> system test kt produce -topic %v stdout:\n%s\n", topicName, stdOut)
fmt.Printf(">> system test kt produce -topic %v stderr:\n%s\n", topicName, stdErr)
require.Zero(t, status)
require.Empty(t, stdErr)
var produceMessage map[string]int
err = json.Unmarshal([]byte(stdOut), &produceMessage)
require.NoError(t, err)
require.Equal(t, 1, produceMessage["count"])
require.Equal(t, 0, produceMessage["partition"])
require.Equal(t, 0, produceMessage["startOffset"])
fmt.Printf(">> ✓\n")
//
// kt consume
//
status, stdOut, stdErr = newCmd().run("./kt", "consume", "-topic", topicName, "-timeout", "500ms", "-group", "hans")
fmt.Printf(">> system test kt consume -topic %v stdout:\n%s\n", topicName, stdOut)
fmt.Printf(">> system test kt consume -topic %v stderr:\n%s\n", topicName, stdErr)
require.Zero(t, status)
lines := strings.Split(stdOut, "\n")
require.True(t, len(lines) > 1)
var lastConsumed map[string]interface{}
err = json.Unmarshal([]byte(lines[len(lines)-2]), &lastConsumed)
require.NoError(t, err)
require.Equal(t, req["value"], lastConsumed["value"])
require.Equal(t, req["key"], lastConsumed["key"])
require.Equal(t, req["partition"], lastConsumed["partition"])
require.NotEmpty(t, lastConsumed["timestamp"])
pt, err := time.Parse(time.RFC3339, lastConsumed["timestamp"].(string))
require.NoError(t, err)
require.True(t, pt.After(time.Now().Add(-2*time.Minute)))
fmt.Printf(">> ✓\n")
//
// kt group
//
status, stdOut, stdErr = newCmd().run("./kt", "group", "-topic", topicName)
fmt.Printf(">> system test kt group -topic %v stdout:\n%s\n", topicName, stdOut)
fmt.Printf(">> system test kt group -topic %v stderr:\n%s\n", topicName, stdErr)
require.Zero(t, status)
require.Contains(t, stdErr, fmt.Sprintf("found partitions=[0] for topic=%v", topicName))
require.Contains(t, stdOut, fmt.Sprintf(`{"name":"hans","topic":"%v","offsets":[{"partition":0,"offset":1,"lag":0}]}`, topicName))
fmt.Printf(">> ✓\n")
//
// kt produce
//
req = map[string]interface{}{
"value": fmt.Sprintf("hello, %s", randomString(6)),
"key": "boom",
"partition": float64(0),
}
buf, err = json.Marshal(req)
require.NoError(t, err)
status, stdOut, stdErr = newCmd().stdIn(string(buf)).run("./kt", "produce", "-topic", topicName)
fmt.Printf(">> system test kt produce -topic %v stdout:\n%s\n", topicName, stdOut)
fmt.Printf(">> system test kt produce -topic %v stderr:\n%s\n", topicName, stdErr)
require.Zero(t, status)
require.Empty(t, stdErr)
err = json.Unmarshal([]byte(stdOut), &produceMessage)
require.NoError(t, err)
require.Equal(t, 1, produceMessage["count"])
require.Equal(t, 0, produceMessage["partition"])
require.Equal(t, 1, produceMessage["startOffset"])
fmt.Printf(">> ✓\n")
//
// kt consume
//
status, stdOut, stdErr = newCmd().run("./kt", "consume", "-topic", topicName, "-offsets", "all=resume", "-timeout", "500ms", "-group", "hans")
fmt.Printf(">> system test kt consume -topic %v -offsets all=resume stdout:\n%s\n", topicName, stdOut)
fmt.Printf(">> system test kt consume -topic %v -offsets all=resume stderr:\n%s\n", topicName, stdErr)
require.Zero(t, status)
lines = strings.Split(stdOut, "\n")
require.True(t, len(lines) == 2) // actual line and an empty one
err = json.Unmarshal([]byte(lines[len(lines)-2]), &lastConsumed)
require.NoError(t, err)
require.Equal(t, req["value"], lastConsumed["value"])
require.Equal(t, req["key"], lastConsumed["key"])
require.Equal(t, req["partition"], lastConsumed["partition"])
require.NotEmpty(t, lastConsumed["timestamp"])
pt, err = time.Parse(time.RFC3339, lastConsumed["timestamp"].(string))
require.NoError(t, err)
require.True(t, pt.After(time.Now().Add(-2*time.Minute)))
fmt.Printf(">> ✓\n")
//
// kt group reset
//
status, stdOut, stdErr = newCmd().run("./kt", "group", "-topic", topicName, "-partitions", "0", "-group", "hans", "-reset", "0")
fmt.Printf(">> system test kt group -topic %v -partitions 0 -group hans -reset 0 stdout:\n%s\n", topicName, stdOut)
fmt.Printf(">> system test kt group -topic %v -partitions 0 -group hans -reset 0 stderr:\n%s\n", topicName, stdErr)
require.Zero(t, status)
lines = strings.Split(stdOut, "\n")
require.True(t, len(lines) > 1)
var groupReset map[string]interface{}
err = json.Unmarshal([]byte(lines[len(lines)-2]), &groupReset)
require.NoError(t, err)
require.Equal(t, groupReset["name"], "hans")
require.Equal(t, groupReset["topic"], topicName)
require.Len(t, groupReset["offsets"], 1)
offsets := groupReset["offsets"].([]interface{})[0].(map[string]interface{})
require.Equal(t, offsets["partition"], float64(0))
require.Equal(t, offsets["offset"], float64(0))
fmt.Printf(">> ✓\n")
//
// kt group
//
status, stdOut, stdErr = newCmd().run("./kt", "group", "-topic", topicName)
fmt.Printf(">> system test kt group -topic %v stdout:\n%s\n", topicName, stdOut)
fmt.Printf(">> system test kt group -topic %v stderr:\n%s\n", topicName, stdErr)
require.Zero(t, status)
require.Contains(t, stdErr, fmt.Sprintf("found partitions=[0] for topic=%v", topicName))
require.Contains(t, stdOut, fmt.Sprintf(`{"name":"hans","topic":"%v","offsets":[{"partition":0,"offset":0,"lag":2}]}`, topicName))
fmt.Printf(">> ✓\n")
//
// kt topic
//
status, stdOut, stdErr = newCmd().run("./kt", "topic", "-filter", topicName)
fmt.Printf(">> system test kt topic stdout:\n%s\n", stdOut)
fmt.Printf(">> system test kt topic stderr:\n%s\n", stdErr)
require.Zero(t, status)
require.Empty(t, stdErr)
lines = strings.Split(stdOut, "\n")
require.True(t, len(lines) > 0)
expectedLines := []string{
fmt.Sprintf(`{"name": "%v"}`, topicName),
}
sort.Strings(lines)
sort.Strings(expectedLines)
for i, l := range lines {
if l == "" { // final newline
continue
}
require.JSONEq(t, expectedLines[i-1], l, fmt.Sprintf("line %d", i-1))
}
fmt.Printf(">> ✓\n")
//
// kt admin -deletetopic
//
status, stdOut, stdErr = newCmd().stdIn(string(buf)).run("./kt", "admin", "-deletetopic", topicName)
fmt.Printf(">> system test kt admin -deletetopic %v stdout:\n%s\n", topicName, stdOut)
fmt.Printf(">> system test kt admin -deletetopic %v stderr:\n%s\n", topicName, stdErr)
require.Zero(t, status)
require.Empty(t, stdErr)
fmt.Printf(">> ✓\n")
//
// kt topic
//
status, stdOut, stdErr = newCmd().run("./kt", "topic", "-filter", topicName)
fmt.Printf(">> system test kt topic stdout:\n%s\n", stdOut)
fmt.Printf(">> system test kt topic stderr:\n%s\n", stdErr)
require.Zero(t, status)
require.Empty(t, stdErr)
require.Empty(t, stdOut)
fmt.Printf(">> ✓\n")
}