-
Notifications
You must be signed in to change notification settings - Fork 30
/
e2e_mysql_test.go
140 lines (129 loc) · 3.9 KB
/
e2e_mysql_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
// Copyright 2022 Google LLC
//
// 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
//
// https://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 cloudsqlconn_test
import (
"context"
"database/sql"
"fmt"
"os"
"testing"
"time"
"cloud.google.com/go/cloudsqlconn"
"cloud.google.com/go/cloudsqlconn/instance"
"cloud.google.com/go/cloudsqlconn/mysql/mysql"
gomysql "github.com/go-sql-driver/mysql"
)
var (
// "Cloud SQL MySQL instance connection name, in the form of 'project:region:instance'.
mysqlConnName = os.Getenv("MYSQL_CONNECTION_NAME")
// "Cloud SQL MySQL instance connection name, in the form of 'project:region:instance'.
mysqlIAMConnName = os.Getenv("MYSQL_IAM_CONNECTION_NAME")
// Name of database user.
mysqlUser = os.Getenv("MYSQL_USER")
// Name of database IAM user.
mysqlIAMUser = os.Getenv("MYSQL_USER_IAM")
// Password for the database user; be careful when entering a password on
// the command line (it may go into your terminal's history).
mysqlPass = os.Getenv("MYSQL_PASS")
// Name of the database to connect to.
mysqlDB = os.Getenv("MYSQL_DB")
)
func requireMySQLVars(t *testing.T) {
switch "" {
case mysqlConnName:
t.Fatal("'MYSQL_CONNECTION_NAME' env var not set")
case mysqlUser:
t.Fatal("'MYSQL_USER' env var not set")
case mysqlPass:
t.Fatal("'MYSQL_PASS' env var not set")
case mysqlDB:
t.Fatal("'MYSQL_DB' env var not set")
}
}
type mockResolver struct {
}
func (r *mockResolver) Resolve(_ context.Context, name string) (instanceName instance.ConnName, err error) {
if name == "mysql.example.com" {
return instance.ParseConnNameWithDomainName(mysqlConnName, "mysql.example.com")
}
return instance.ConnName{}, fmt.Errorf("no resolution for %v", name)
}
func TestMySQLDriver(t *testing.T) {
if testing.Short() {
t.Skip("skipping MySQL integration tests")
}
tcs := []struct {
desc string
driverName string
instanceName string
user string
password string
opts []cloudsqlconn.Option
}{
{
desc: "default options",
driverName: "cloudsql-mysql",
opts: nil,
instanceName: mysqlConnName,
user: mysqlUser,
password: mysqlPass,
},
{
desc: "auto IAM authn",
driverName: "cloudsql-mysql-iam",
opts: []cloudsqlconn.Option{cloudsqlconn.WithIAMAuthN()},
instanceName: mysqlIAMConnName,
user: mysqlIAMUser,
password: "password",
},
{
desc: "with dns",
driverName: "cloudsql-mysql-dns",
opts: []cloudsqlconn.Option{cloudsqlconn.WithResolver(&mockResolver{})},
instanceName: "mysql.example.com",
user: mysqlUser,
password: mysqlPass,
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
testConn := func(db *sql.DB) {
var now time.Time
if err := db.QueryRow("SELECT NOW()").Scan(&now); err != nil {
t.Fatalf("QueryRow failed: %v", err)
}
t.Log(now)
}
cleanup, err := mysql.RegisterDriver(tc.driverName, tc.opts...)
if err != nil {
t.Fatalf("failed to register driver: %v", err)
}
defer cleanup()
cfg := gomysql.NewConfig()
cfg.CheckConnLiveness = true
cfg.User = tc.user
cfg.Passwd = tc.password
cfg.DBName = mysqlDB
cfg.Net = tc.driverName
cfg.Addr = tc.instanceName
cfg.Params = map[string]string{"parseTime": "true"}
db, err := sql.Open("mysql", cfg.FormatDSN())
if err != nil {
t.Fatalf("sql.Open want err = nil, got = %v", err)
}
defer db.Close()
testConn(db)
})
}
}