-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver_integration_test.go
323 lines (289 loc) · 11.4 KB
/
driver_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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//go:build integration
// +build integration
package fireboltgosdk
import (
"context"
"database/sql"
"fmt"
"math"
"os"
"reflect"
"runtime/debug"
"strings"
"testing"
"time"
)
var (
dsnMock string
dsnNoDatabaseMock string
dsnSystemEngineWithDatabaseMock string
dsnSystemEngineMock string
clientIdMock string
clientSecretMock string
databaseMock string
engineNameMock string
engineUrlMock string
accountName string
serviceAccountNoUserName string
clientMock *ClientImpl
clientMockWithAccount *ClientImpl
)
const v0Testing = false
// init populates mock variables and client for integration tests
func init() {
clientIdMock = os.Getenv("CLIENT_ID")
clientSecretMock = os.Getenv("CLIENT_SECRET")
databaseMock = os.Getenv("DATABASE_NAME")
engineNameMock = os.Getenv("ENGINE_NAME")
accountName = os.Getenv("ACCOUNT_NAME")
dsnMock = fmt.Sprintf("firebolt:///%s?account_name=%s&engine=%s&client_id=%s&client_secret=%s", databaseMock, accountName, engineNameMock, clientIdMock, clientSecretMock)
dsnNoDatabaseMock = fmt.Sprintf("firebolt://?account_name=%s&engine=%s&client_id=%s&client_secret=%s", accountName, engineNameMock, clientIdMock, clientSecretMock)
dsnSystemEngineWithDatabaseMock = fmt.Sprintf("firebolt:///%s?account_name=%s&client_id=%s&client_secret=%s", databaseMock, accountName, clientIdMock, clientSecretMock)
dsnSystemEngineMock = fmt.Sprintf("firebolt://?account_name=%s&client_id=%s&client_secret=%s", accountName, clientIdMock, clientSecretMock)
var err error
client, err := Authenticate(&fireboltSettings{
clientID: clientIdMock,
clientSecret: clientSecretMock,
accountName: accountName,
engineName: engineNameMock,
database: databaseMock,
newVersion: true,
}, GetHostNameURL())
if err != nil {
panic(fmt.Errorf("Error authenticating with client id %s: %v", clientIdMock, err))
}
clientMock = client.(*ClientImpl)
clientWithAccount, err := Authenticate(&fireboltSettings{
clientID: clientIdMock,
clientSecret: clientSecretMock,
accountName: accountName,
database: databaseMock,
newVersion: true,
}, GetHostNameURL())
if err != nil {
panic(fmt.Sprintf("Authentication error: %v", err))
}
engineUrlMock, _, err = clientMock.GetConnectionParameters(context.TODO(), engineNameMock, databaseMock)
if err != nil {
panic(fmt.Errorf("Error getting connection parameters: %v", err))
}
clientMockWithAccount = clientWithAccount.(*ClientImpl)
clientMockWithAccount.ConnectedToSystemEngine = true
serviceAccountNoUserName = databaseMock + "_sa_no_user"
}
// TestDriverQueryResult tests query happy path, as user would do it
func TestDriverQueryResult(t *testing.T) {
loc, _ := time.LoadLocation("UTC")
db, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf("failed unexpectedly with %v", err)
}
rows, err := db.Query(
"SELECT CAST('2020-01-03 19:08:45' AS DATETIME) as dt, CAST('2020-01-03' AS DATE) as d, CAST(1 AS INT) as i, '-inf'::float as f " +
"UNION " +
"SELECT CAST('2021-01-03 19:38:34' AS DATETIME) as dt, CAST('2000-12-03' AS DATE) as d, CAST(2 AS INT) as i, 'nan'::float as f ORDER BY i")
if err != nil {
t.Errorf("db.Query returned an error: %v", err)
}
var dt, d time.Time
var i int
var f float64
expectedColumns := []string{"dt", "d", "i", "f"}
if columns, err := rows.Columns(); reflect.DeepEqual(expectedColumns, columns) && err != nil {
t.Errorf("columns are not equal (%v != %v) and error is %v", expectedColumns, columns, err)
}
if !rows.Next() {
t.Errorf("Next returned end of output")
}
assert(rows.Scan(&dt, &d, &i, &f), nil, t, "Scan returned an error")
assert(dt, time.Date(2020, 01, 03, 19, 8, 45, 0, loc), t, "results not equal for datetime")
assert(d, time.Date(2020, 01, 03, 0, 0, 0, 0, loc), t, "results not equal for date")
assert(i, 1, t, "results not equal for int")
assert(f, math.Inf(-1), t, "results not equal for float")
if !rows.Next() {
t.Errorf("Next returned end of output")
}
assert(rows.Scan(&dt, &d, &i, &f), nil, t, "Scan returned an error")
assert(dt, time.Date(2021, 01, 03, 19, 38, 34, 0, loc), t, "results not equal for datetime")
assert(d, time.Date(2000, 12, 03, 0, 0, 0, 0, loc), t, "results not equal for date")
assert(i, 2, t, "results not equal for int")
if !math.IsNaN(f) {
t.Log(string(debug.Stack()))
t.Errorf("results not equal for float Expected: NaN Got: %f", f)
}
if rows.Next() {
t.Errorf("Next didn't returned false, although no data is expected")
}
}
// TestDriverInfNanValues tests query with inf and nan values
func TestDriverInfNanValues(t *testing.T) {
db, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf("failed unexpectedly with %v", err)
}
rows, err := db.Query("SELECT '-inf'::double as f, 'inf'::double as f2, 'nan'::double as f3, '-nan'::double as f4")
if err != nil {
t.Errorf("db.Query returned an error: %v", err)
}
var f, f2, f3, f4 float64
if !rows.Next() {
t.Errorf("Next returned end of output")
}
assert(rows.Scan(&f, &f2, &f3, &f4), nil, t, "Scan returned an error")
if !math.IsInf(f, -1) {
t.Errorf("results not equal for float Expected: -Inf Got: %f", f)
}
if !math.IsInf(f2, 1) {
t.Errorf("results not equal for float Expected: Inf Got: %f", f2)
}
if !math.IsNaN(f3) {
t.Errorf("results not equal for float Expected: NaN Got: %f", f3)
}
if !math.IsNaN(f4) {
t.Errorf("results not equal for float Expected: NaN Got: %f", f4)
}
}
// TestDriverOpenConnection checks making a connection on opened connector
func TestDriverOpenConnection(t *testing.T) {
db, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf("failed unexpectedly")
}
ctx := context.TODO()
if _, err = db.Conn(ctx); err != nil {
t.Errorf("connection is not established correctly: %v", err)
}
}
func runTestDriverExecStatement(t *testing.T, dsn string) {
db, err := sql.Open("firebolt", dsn)
if err != nil {
t.Errorf("failed unexpectedly: %s", err)
}
if _, err = db.Exec("SELECT 1"); err != nil {
t.Errorf("connection is not established correctly: %s", err)
}
}
// TestDriverOpenEngineUrl checks opening connector with a default engine
func TestDriverOpenNoDatabase(t *testing.T) {
runTestDriverExecStatement(t, dsnNoDatabaseMock)
}
// TestDriverExecStatement checks exec with full dsn
func TestDriverExecStatement(t *testing.T) {
runTestDriverExecStatement(t, dsnMock)
}
// TestDriverExecStatement checks exec with full dsn
func TestDriverSystemEngineDbContext(t *testing.T) {
db, err := sql.Open("firebolt", dsnSystemEngineWithDatabaseMock)
if err != nil {
t.Errorf("failed unexpectedly")
}
query := "SELECT table_name FROM information_schema.tables WHERE table_type!='VIEW'"
if _, err = db.Exec(query); err != nil {
t.Errorf("System engine with DB context not able to list tables")
}
}
// function that creates a service account and returns its id and secret
func createServiceAccountNoUser(t *testing.T, serviceAccountName string) (string, string) {
serviceAccountDescription := "test_service_account_description"
db, err := sql.Open("firebolt", dsnSystemEngineMock)
if err != nil {
t.Errorf("failed unexpectedly with %v", err)
}
// create service account
createServiceAccountQuery := fmt.Sprintf("CREATE SERVICE ACCOUNT \"%s\" WITH DESCRIPTION = '%s'", serviceAccountName, serviceAccountDescription)
_, err = db.Query(createServiceAccountQuery)
if err != nil {
t.Errorf("The query %s returned an error: %v", createServiceAccountQuery, err)
}
// generate credentials for service account
generateServiceAccountKeyQuery := fmt.Sprintf("CALL fb_GENERATESERVICEACCOUNTKEY('%s')", serviceAccountName)
// get service account id and secret from the result
rows, err := db.Query(generateServiceAccountKeyQuery)
var serviceAccountNameReturned, serviceAccountID, serviceAccountSecret string
for rows.Next() {
if err := rows.Scan(&serviceAccountNameReturned, &serviceAccountID, &serviceAccountSecret); err != nil {
t.Errorf("Failed to retrieve service account id and secret: %v", err)
}
}
// Currently this is bugged so retrieve id via a query if not returned otherwise. FIR-28719
if serviceAccountID == "" {
getServiceAccountIDQuery := fmt.Sprintf("SELECT service_account_id FROM information_schema.service_accounts WHERE service_account_name = '%s'", serviceAccountName)
rows, err := db.Query(getServiceAccountIDQuery)
if err != nil {
t.Errorf("Failed to retrieve service account id: %v", err)
}
for rows.Next() {
if err := rows.Scan(&serviceAccountID); err != nil {
t.Errorf("Failed to retrieve service account id: %v", err)
}
}
}
return serviceAccountID, serviceAccountSecret
}
func deleteServiceAccount(t *testing.T, serviceAccountName string) {
db, err := sql.Open("firebolt", dsnSystemEngineMock)
if err != nil {
t.Errorf("failed unexpectedly with %v", err)
}
// delete service account
deleteServiceAccountQuery := fmt.Sprintf("DROP SERVICE ACCOUNT \"%s\"", serviceAccountName)
_, err = db.Query(deleteServiceAccountQuery)
if err != nil {
t.Errorf("The query %s returned an error: %v", deleteServiceAccountQuery, err)
}
}
// test authentication with service account without a user fails
func TestServiceAccountAuthentication(t *testing.T) {
serviceAccountID, serviceAccountSecret := createServiceAccountNoUser(t, serviceAccountNoUserName)
defer deleteServiceAccount(t, serviceAccountNoUserName) // Delete service account after the test
dsnNoUser := fmt.Sprintf(
"firebolt:///%s?account_name=%s&engine=%s&client_id=%s&client_secret=%s",
databaseMock, accountName, engineNameMock, serviceAccountID, serviceAccountSecret)
_, err := sql.Open("firebolt", dsnNoUser)
if err == nil {
t.Errorf("Authentication didn't return an error, although it should")
t.FailNow()
}
if !strings.Contains(err.Error(), fmt.Sprintf("Database '%s' does not exist or not authorized", databaseMock)) {
t.Errorf("Authentication didn't return an error with correct message, got: %s", err.Error())
}
}
func TestIncorrectQueryThrowingStructuredError(t *testing.T) {
db, err := sql.Open("firebolt", dsnSystemEngineMock)
if err != nil {
t.Errorf("failed unexpectedly with %v", err)
}
_, err = db.Query("SELECT 'blue'::int")
if err == nil {
t.Errorf("Query didn't return an error, although it should")
}
if !strings.HasPrefix(err.Error(), "error during query execution: error during query request:") || !strings.Contains(err.Error(), "Unable to cast text 'blue' to integer") {
t.Errorf("Query didn't return an error with correct message, got: %s", err.Error())
}
}
func TestParametrisedQuery(t *testing.T) {
ctx := context.TODO()
db, err := sql.Open("firebolt", dsnSystemEngineMock)
if err != nil {
t.Errorf("failed unexpectedly with %v", err)
}
query := "SELECT engine_name, status from information_schema.engines WHERE engine_name = ? AND status = ?"
stmt, err := db.PrepareContext(ctx, query)
if err != nil {
t.Errorf("The query %s returned an error: %v", query, err)
}
rows, err := stmt.QueryContext(ctx, engineNameMock, "RUNNING")
if err != nil {
t.Errorf("The query %s returned an error: %v", query, err)
}
if !rows.Next() {
t.Errorf("Next returned end of output")
}
var engineName, status string
if err := rows.Scan(&engineName, &status); err != nil {
t.Errorf("Scan returned an error: %v", err)
}
if engineName != engineNameMock || status != "RUNNING" {
t.Errorf("Results not equal: %s %s", engineName, status)
}
}