-
Notifications
You must be signed in to change notification settings - Fork 6
/
uploader_test.go
138 lines (122 loc) · 4.4 KB
/
uploader_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
package main
import (
"context"
"io/ioutil"
"testing"
"time"
"github.com/UnityTech/kafka-archiver/buffer"
"github.com/UnityTech/kafka-archiver/partitioner"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/stretchr/testify/require"
)
func newTestWorker(partitioner partitioner.Partitioner, s3Client *s3.S3) *Worker {
w := NewWorker()
w.s3enabled = true
w.s3Concurrency = 1
w.partitioner = partitioner
w.s3bucket = "test"
w.uploader = s3manager.NewUploaderWithClient(s3Client)
return w
}
func TestUploadWithDefaultPartitioner(t *testing.T) {
s3ClientMock := s3.New(session.New())
s3ClientMock.Handlers.Clear()
s3ClientMock.Handlers.Send.PushBack(func(r *request.Request) {
switch x := r.Params.(type) {
case *s3.PutObjectInput:
bt, err := ioutil.ReadAll(x.Body)
require.NoError(t, err)
require.Equal(t, "{\"message\": 1}{\"message\": 2}{\"message\": 3}", string(bt))
require.Contains(t, *x.Key, "/testTopic1/partition=10/0010.5555.")
require.Equal(t, *x.Bucket, "test")
}
})
// Create new temporary file to upload.
f, err := ioutil.TempFile("", "kafka-archiver-upload-test")
require.NoError(t, err)
f.WriteString(`{"message": 1}`)
f.WriteString(`{"message": 2}`)
f.WriteString(`{"message": 3}`)
require.NoError(t, f.Close())
// Create worker and upload.
w := newTestWorker(partitioner.New(&partitioner.Config{Type: "DefaultPartitioner"}), s3ClientMock)
w.upload(&buffer.Flush{
Topic: "testTopic1",
Path: f.Name(),
Partition: 10,
LastOffset: 5555,
})
}
func TestUploadWithTimeFieldPartitioner(t *testing.T) {
s3ClientMock := s3.New(session.New())
s3ClientMock.Handlers.Clear()
s3ClientMock.Handlers.Send.PushBack(func(r *request.Request) {
switch x := r.Params.(type) {
case *s3.PutObjectInput:
bt, err := ioutil.ReadAll(x.Body)
require.NoError(t, err)
require.Equal(t, "{\"message\": 1, \"ts\": 1444262400}{\"message\": 2, \"ts\": 1444262401}{\"message\": 3, \"ts\": 1444262402}", string(bt))
require.Contains(t, *x.Key, "backup/topic=testTopic2/year=2015/month=10/day=08/hour=00/0010.808.")
require.Equal(t, *x.Bucket, "test")
}
})
// Create new temporary file to upload.
f, err := ioutil.TempFile("", "kafka-archiver-upload-test")
require.NoError(t, err)
f.WriteString(`{"message": 1, "ts": 1444262400}`)
f.WriteString(`{"message": 2, "ts": 1444262401}`)
f.WriteString(`{"message": 3, "ts": 1444262402}`)
require.NoError(t, f.Close())
w := newTestWorker(partitioner.New(&partitioner.Config{
Type: "TimeFieldPartitioner",
FieldKeyName: "ts",
TopicNamePrefix: "topic=",
BaseFolder: "backup",
}), s3ClientMock)
w.upload(&buffer.Flush{
Topic: "testTopic2",
Path: f.Name(),
Partition: 10,
LastOffset: 808,
Ctx: context.WithValue(context.Background(), "hourBucket", time.Unix(1444262400, 0).UTC()),
})
}
func TestUploadWithIsoDateFieldPartitioner(t *testing.T) {
s3ClientMock := s3.New(session.New())
s3ClientMock.Handlers.Clear()
s3ClientMock.Handlers.Send.PushBack(func(r *request.Request) {
switch x := r.Params.(type) {
case *s3.PutObjectInput:
bt, err := ioutil.ReadAll(x.Body)
require.NoError(t, err)
require.Equal(t, "{\"message\": 1, \"ts\": \"2015-10-08T00:00:00Z\"}{\"message\": 2, \"ts\": \"2015-10-08T00:00:01Z\"}{\"message\": 3, \"ts\": \"2015-10-08T00:00:02Z\"}", string(bt))
require.Contains(t, *x.Key, "backup/topic=testTopic2/date=2015-10-08/hour=00/0010.808.")
require.Equal(t, *x.Bucket, "test")
}
})
// Create new temporary file to upload.
f, err := ioutil.TempFile("", "kafka-archiver-upload-test")
require.NoError(t, err)
f.WriteString(`{"message": 1, "ts": "2015-10-08T00:00:00Z"}`)
f.WriteString(`{"message": 2, "ts": "2015-10-08T00:00:01Z"}`)
f.WriteString(`{"message": 3, "ts": "2015-10-08T00:00:02Z"}`)
require.NoError(t, f.Close())
hourBucket, err := time.Parse(time.RFC3339Nano, "2015-10-08T00:00:00Z")
require.NoError(t, err)
w := newTestWorker(partitioner.New(&partitioner.Config{
Type: "IsoDateFieldPartitioner",
FieldKeyName: "ts",
TopicNamePrefix: "topic=",
BaseFolder: "backup",
}), s3ClientMock)
w.upload(&buffer.Flush{
Topic: "testTopic2",
Path: f.Name(),
Partition: 10,
LastOffset: 808,
Ctx: context.WithValue(context.Background(), "hourBucket", hourBucket.UTC()),
})
}