-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_test.go
135 lines (118 loc) · 3.62 KB
/
fetch_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
//
package fetch_test
import (
"context"
"fmt"
"os"
"strings"
"testing"
"cloud.google.com/go/storage"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/googleapis/google-cloudevents-go/cloud/pubsub/v1"
fetch "github.com/nkmr-jp/gcf-fetch"
"github.com/nkmr-jp/zl"
"github.com/stretchr/testify/assert"
"google.golang.org/api/iterator"
)
func TestRun(t *testing.T) {
test := NewTestFetch(t)
t.Run("single url", func(t *testing.T) {
objPath := "api.github.com/users/github"
pubsubData := "https://api.github.com/users/github"
ctx := context.Background()
test.deleteObjects(ctx, "api.github.com")
// Send pubsub message1
if err := fetch.Fetch(ctx, test.event(pubsubData)); err != nil {
assert.Fail(t, err.Error())
}
reader1 := test.getObject(ctx, objPath)
// Send pubsub message2
if err := fetch.Fetch(ctx, test.event(pubsubData)); err != nil {
assert.Fail(t, err.Error())
}
reader2 := test.getObject(ctx, objPath)
// Get generation after send pubsub message.
assert.NotEqual(t, reader1.Attrs.Generation, reader2.Attrs.Generation)
})
t.Run("multi url", func(t *testing.T) {
pubsubData := `
https://api.github.com/users/github
https://api.github.com/users/github/followers
`
ctx := context.Background()
test.deleteObjects(ctx, "api.github.com")
if err := fetch.Fetch(ctx, test.event(pubsubData)); err != nil {
assert.FailNow(t, err.Error())
}
assert.NotNilf(t, test.getObject(ctx, "api.github.com/users/github"), "reader1")
assert.NotNilf(t, test.getObject(ctx, "api.github.com/users/github/followers"), "reader2")
})
t.Run("multi url 2", func(t *testing.T) {
pubsubData := `https://api.github.com/users/github https://api.github.com/users/github/followers`
ctx := context.Background()
test.deleteObjects(ctx, "api.github.com")
if err := fetch.Fetch(ctx, test.event(pubsubData)); err != nil {
assert.FailNow(t, err.Error())
}
assert.NotNilf(t, test.getObject(ctx, "api.github.com/users/github"), "reader1")
assert.NotNilf(t, test.getObject(ctx, "api.github.com/users/github/followers"), "reader2")
})
}
type TestFetch struct {
t *testing.T
bucketName string
}
func NewTestFetch(t *testing.T) *TestFetch {
zl.SetRotateFileName("./test.jsonl")
zl.Init()
bucketName := os.Getenv("BUCKET_NAME")
if !strings.HasSuffix(bucketName, "-test") {
assert.FailNow(t, fmt.Sprintf("%s bucket is not to test", bucketName))
}
return &TestFetch{t, bucketName}
}
func (f *TestFetch) deleteObjects(ctx context.Context, objPath string) {
client, _ := storage.NewClient(ctx)
query := storage.Query{Prefix: objPath, Versions: true}
bucket := client.Bucket(f.bucketName)
it := bucket.Objects(ctx, &query)
for {
objAttrs, err := it.Next()
if err != nil && err != iterator.Done {
assert.Fail(f.t, err.Error())
}
if err == iterator.Done {
break
}
if err := bucket.Object(objAttrs.Name).Generation(objAttrs.Generation).Delete(ctx); err != nil {
assert.Fail(f.t, err.Error())
}
}
}
func (f *TestFetch) getObject(ctx context.Context, objPath string) *storage.Reader {
client, _ := storage.NewClient(ctx)
reader, err := client.Bucket(f.bucketName).Object(objPath).NewReader(ctx)
defer func(rc *storage.Reader) {
err := rc.Close()
if err != nil {
assert.Fail(f.t, err.Error())
}
}(reader)
if err != nil {
assert.Fail(f.t, err.Error())
}
return reader
}
func (f *TestFetch) event(data string) event.Event {
msg := pubsub.MessagePublishedData{
Message: &pubsub.Message{
Data: []byte(data),
},
}
e := event.New()
e.SetDataContentType("application/json")
if err := e.SetData(e.DataContentType(), msg); err != nil {
assert.Fail(f.t, err.Error())
}
return e
}