-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacceptance_test.go
193 lines (174 loc) · 5.74 KB
/
acceptance_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
// Copyright © 2022 Meroxa, Inc. & Gophers Lab Technologies Pvt. Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package googlesheets
import (
"context"
"fmt"
"math/rand"
"os"
"strings"
"testing"
"time"
"github.com/conduitio-labs/conduit-connector-google-sheets/config"
"github.com/conduitio/conduit-commons/opencdc"
sdk "github.com/conduitio/conduit-connector-sdk"
"go.uber.org/goleak"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
var (
offset int
sheetName string
credFilePath string
tokenFilePath string
sheetURL string
spreadsheetID string
sheetID int64
)
func TestAcceptance(t *testing.T) {
credJSON := strings.TrimSpace(os.Getenv("CONDUIT_GOOGLE_CREDENTIAL_JSON"))
if credJSON != "" {
credFile, err := os.CreateTemp("", "cred*.json")
if err != nil {
t.Fatal(err)
}
defer os.Remove(credFilePath)
if _, err = credFile.WriteString(credJSON); err != nil {
t.Error("error writing cred file", err)
}
credFilePath = credFile.Name()
} else {
t.Skip("credentials not set in env CONDUIT_GOOGLE_CREDENTIAL_JSON")
}
tokenJSON := strings.TrimSpace(os.Getenv("CONDUIT_GOOGLE_TOKEN_JSON"))
if tokenJSON != "" {
tokenFile, err := os.CreateTemp("", "token*.json")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tokenFilePath)
if _, err = tokenFile.WriteString(tokenJSON); err != nil {
t.Error("error writing token file", err)
}
tokenFilePath = tokenFile.Name()
} else {
t.Error("token not set in env CONDUIT_GOOGLE_TOKEN_JSON")
t.FailNow()
}
sheetURL = strings.TrimSpace(os.Getenv("CONDUIT_GOOGLE_SHEET_URL"))
if sheetURL == "" {
t.Error("sheetURL not set in env CONDUIT_GOOGLE_SHEET_URL")
t.Skip()
}
sheetName = strings.TrimSpace(os.Getenv("CONDUIT_GOOGLE_SHEET_NAME"))
if sheetName == "" {
t.Error("sheetName not set in env CONDUIT_GOOGLE_SHEET_NAME")
t.FailNow()
}
sourceConfig := map[string]string{
"credentialsFile": credFilePath,
"tokensFile": tokenFilePath,
"sheetsURL": sheetURL,
"pollingPeriod": "1s", // Configurable polling period
}
destConfig := map[string]string{
"credentialsFile": credFilePath,
"tokensFile": tokenFilePath,
"sheetsURL": sheetURL,
"sheetName": sheetName,
"valueInputOption": "USER_ENTERED",
"bufferSize": "10",
}
ctx := context.Background()
conf, err := config.Parse(sourceConfig)
if err != nil {
t.Fatal(err)
}
spreadsheetID = conf.GoogleSpreadsheetID
sheetID = conf.GoogleSheetID
client := conf.OAuthConfig.Client(ctx, conf.OAuthToken)
sheetService, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
t.Fatal(err)
}
clearSheet := func(t *testing.T) {
_, err := sheetService.Spreadsheets.Values.Clear(conf.GoogleSpreadsheetID, "1:1000", &sheets.ClearValuesRequest{}).Do()
if err != nil {
t.Errorf("error cleaning the sheet: %v", err.Error())
}
}
// clear sheet before starting the tests
clearSheet(t)
sdk.AcceptanceTest(t, AcceptanceTestDriver{
rand: rand.New(rand.NewSource(time.Now().UnixNano())), //nolint: gosec // only used for testing
ConfigurableAcceptanceTestDriver: sdk.ConfigurableAcceptanceTestDriver{
Config: sdk.ConfigurableAcceptanceTestDriverConfig{
Connector: Connector,
SourceConfig: sourceConfig,
DestinationConfig: destConfig,
BeforeTest: func(*testing.T) {},
GoleakOptions: []goleak.Option{goleak.IgnoreCurrent()},
AfterTest: func(t *testing.T) {
// clear sheet after every test to ensure clean sheet for next test
offset = 0
clearSheet(t)
},
},
},
})
}
type AcceptanceTestDriver struct {
rand *rand.Rand
sdk.ConfigurableAcceptanceTestDriver
}
// GenerateRecord overrides the pre-defined generate record function to generate the
// records in required google sheets compatible format.
func (d AcceptanceTestDriver) GenerateRecord(*testing.T, opencdc.Operation) opencdc.Record {
payload := fmt.Sprintf(`["%s","%s","%s","%s"]`, d.randString(32), d.randString(32),
d.randString(32), d.randString(32))
offset++
return opencdc.Record{
Operation: opencdc.OperationSnapshot,
Position: opencdc.Position(fmt.Sprintf(`{"row_offset":%v, "spreadsheet_id":%v, "sheet_id":%v}`, offset,
spreadsheetID, sheetID)),
Metadata: nil,
Key: opencdc.RawData(fmt.Sprintf("%v", offset)),
Payload: opencdc.Change{After: opencdc.RawData(payload)},
}
}
// randString generates a random string of length n.
// (source: https://stackoverflow.com/a/31832326)
func (d AcceptanceTestDriver) randString(n int) string {
const letterBytes = `0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
sb := strings.Builder{}
sb.Grow(n)
// src.Int63() generates 63 random bits, enough for letterIdxMax characters
for i, cache, remain := n-1, d.rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = d.rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
sb.WriteByte(letterBytes[idx])
i--
}
cache >>= letterIdxBits
remain--
}
return sb.String()
}