| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package gcpmysql |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"net/url" |
| 21 |
"reflect" |
| 22 |
"testing" |
| 23 |
|
| 24 |
drvr "github.com/go-sql-driver/mysql" |
| 25 |
"gocloud.dev/internal/testing/terraform" |
| 26 |
"gocloud.dev/mysql" |
| 27 |
) |
| 28 |
|
| 29 |
func TestOpen(t *testing.T) { |
| 30 |
// This test will be skipped unless the project is set up with Terraform. |
| 31 |
// Before running go test, run in this directory: |
| 32 |
// |
| 33 |
// terraform init |
| 34 |
// terraform apply |
| 35 |
|
| 36 |
tfOut, err := terraform.ReadOutput(".") |
| 37 |
if err != nil || len(tfOut) == 0 { |
| 38 |
t.Skipf("Could not obtain harness info: %v", err) |
| 39 |
} |
| 40 |
project, _ := tfOut["project"].Value.(string) |
| 41 |
region, _ := tfOut["region"].Value.(string) |
| 42 |
instance, _ := tfOut["instance"].Value.(string) |
| 43 |
username, _ := tfOut["username"].Value.(string) |
| 44 |
password, _ := tfOut["password"].Value.(string) |
| 45 |
databaseName, _ := tfOut["database"].Value.(string) |
| 46 |
if project == "" || region == "" || instance == "" || username == "" || databaseName == "" { |
| 47 |
t.Fatalf("Missing one or more required Terraform outputs; got project=%q region=%q instance=%q username=%q database=%q", project, region, instance, username, databaseName) |
| 48 |
} |
| 49 |
|
| 50 |
ctx := context.Background() |
| 51 |
urlstr := fmt.Sprintf("gcpmysql://%s:%s@%s/%s/%s/%s", username, password, project, region, instance, databaseName) |
| 52 |
t.Log("Connecting to", urlstr) |
| 53 |
db, err := mysql.Open(ctx, urlstr) |
| 54 |
if err != nil { |
| 55 |
t.Fatal(err) |
| 56 |
} |
| 57 |
if err := db.Ping(); err != nil { |
| 58 |
t.Error("Ping:", err) |
| 59 |
} |
| 60 |
if err := db.Close(); err != nil { |
| 61 |
t.Error("Close:", err) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestInstanceFromURL(t *testing.T) { |
| 66 |
tests := []struct { |
| 67 |
name string |
| 68 |
urlString string |
| 69 |
wantInstance string |
| 70 |
wantDatabase string |
| 71 |
wantErr bool |
| 72 |
}{ |
| 73 |
{ |
| 74 |
name: "AllValuesSpecified", |
| 75 |
urlString: "gcpmysql://username:password@my-project-id/us-central1/my-instance-id/my-db?foo=bar&baz=quux", |
| 76 |
wantInstance: "my-project-id:us-central1:my-instance-id", |
| 77 |
wantDatabase: "my-db", |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "OptionalValuesOmitted", |
| 81 |
urlString: "gcpmysql://my-project-id/us-central1/my-instance-id/my-db", |
| 82 |
wantInstance: "my-project-id:us-central1:my-instance-id", |
| 83 |
wantDatabase: "my-db", |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "DatabaseNameEmpty", |
| 87 |
urlString: "gcpmysql://my-project-id/us-central1/my-instance-id/", |
| 88 |
wantErr: true, |
| 89 |
}, |
| 90 |
{ |
| 91 |
name: "InstanceEmpty", |
| 92 |
urlString: "gcpmysql://my-project-id/us-central1//my-db", |
| 93 |
wantErr: true, |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "RegionEmpty", |
| 97 |
urlString: "gcpmysql://my-project-id//my-instance-id/my-db", |
| 98 |
wantErr: true, |
| 99 |
}, |
| 100 |
{ |
| 101 |
name: "ProjectEmpty", |
| 102 |
urlString: "gcpmysql:///us-central1/my-instance-id/my-db", |
| 103 |
wantErr: true, |
| 104 |
}, |
| 105 |
{ |
| 106 |
name: "DatabaseNameWithSlashes", |
| 107 |
urlString: "gcpmysql://my-project-id/us-central1/my-instance-id/foo/bar/baz", |
| 108 |
wantInstance: "my-project-id:us-central1:my-instance-id", |
| 109 |
wantDatabase: "foo/bar/baz", |
| 110 |
}, |
| 111 |
} |
| 112 |
for _, test := range tests { |
| 113 |
t.Run(test.name, func(t *testing.T) { |
| 114 |
u, err := url.Parse(test.urlString) |
| 115 |
if err != nil { |
| 116 |
t.Fatalf("failed to parse URL %q: %v", test.urlString, err) |
| 117 |
} |
| 118 |
instance, database, err := instanceFromURL(u) |
| 119 |
if err != nil { |
| 120 |
t.Logf("instanceFromURL(url.Parse(%q)): %v", u, err) |
| 121 |
if !test.wantErr { |
| 122 |
t.Fail() |
| 123 |
} |
| 124 |
return |
| 125 |
} |
| 126 |
if test.wantErr { |
| 127 |
t.Fatalf("instanceFromURL(url.Parse(%q)) = %q, %q, <nil>; want error", test.urlString, instance, database) |
| 128 |
} |
| 129 |
if instance != test.wantInstance || database != test.wantDatabase { |
| 130 |
t.Errorf("instanceFromURL(url.Parse(%q)) = %q, %q, <nil>; want %q, %q, <nil>", test.urlString, instance, database, test.wantInstance, test.wantDatabase) |
| 131 |
} |
| 132 |
}) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func Test_configFromURL(t *testing.T) { |
| 137 |
type args struct { |
| 138 |
urlStr string |
| 139 |
} |
| 140 |
tests := []struct { |
| 141 |
name string |
| 142 |
args args |
| 143 |
want *drvr.Config |
| 144 |
wantErr bool |
| 145 |
}{ |
| 146 |
{ |
| 147 |
name: "ConfigWithNoOptions", |
| 148 |
args: args{ |
| 149 |
urlStr: "gcpmysql://user:password@my-project-id/us-central1/my-instance-id/my-db", |
| 150 |
}, |
| 151 |
want: func() *drvr.Config { |
| 152 |
cfg := drvr.NewConfig() |
| 153 |
cfg.AllowNativePasswords = true |
| 154 |
cfg.Net = "tcp" |
| 155 |
cfg.Addr = "my-project-id:us-central1:my-instance-id" |
| 156 |
cfg.User = "user" |
| 157 |
cfg.Passwd = "password" |
| 158 |
cfg.DBName = "my-db" |
| 159 |
return cfg |
| 160 |
}(), |
| 161 |
wantErr: false, |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "ConfigWithSignalOptions", |
| 165 |
args: args{ |
| 166 |
urlStr: "gcpmysql://user:password@my-project-id/us-central1/my-instance-id/my-db?parseTime=true", |
| 167 |
}, |
| 168 |
want: func() *drvr.Config { |
| 169 |
cfg := drvr.NewConfig() |
| 170 |
cfg.AllowNativePasswords = true |
| 171 |
cfg.ParseTime = true |
| 172 |
cfg.Net = "tcp" |
| 173 |
cfg.Addr = "my-project-id:us-central1:my-instance-id" |
| 174 |
cfg.User = "user" |
| 175 |
cfg.Passwd = "password" |
| 176 |
cfg.DBName = "my-db" |
| 177 |
return cfg |
| 178 |
}(), |
| 179 |
wantErr: false, |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "ConfigWithMultipleOptions", |
| 183 |
args: args{ |
| 184 |
urlStr: "gcpmysql://user:password@my-project-id/us-central1/my-instance-id/my-db?columnsWithAlias=true&parseTime=true", |
| 185 |
}, |
| 186 |
want: func() *drvr.Config { |
| 187 |
cfg := drvr.NewConfig() |
| 188 |
cfg.AllowNativePasswords = true |
| 189 |
cfg.ColumnsWithAlias = true |
| 190 |
cfg.ParseTime = true |
| 191 |
cfg.Net = "tcp" |
| 192 |
cfg.Addr = "my-project-id:us-central1:my-instance-id" |
| 193 |
cfg.User = "user" |
| 194 |
cfg.Passwd = "password" |
| 195 |
cfg.DBName = "my-db" |
| 196 |
return cfg |
| 197 |
}(), |
| 198 |
wantErr: false, |
| 199 |
}, |
| 200 |
{ |
| 201 |
name: "InstanceFromURLError", |
| 202 |
args: args{ |
| 203 |
urlStr: "gcpmysql://user:password@my-project-id/us-central1/my-db", |
| 204 |
}, |
| 205 |
want: nil, |
| 206 |
wantErr: true, |
| 207 |
}, |
| 208 |
{ |
| 209 |
name: "DNSParseError", |
| 210 |
args: args{ |
| 211 |
urlStr: "gcpmysql://user:password@my-project-id/us-central1/my-instance-id/my-db?parseTime=nope", |
| 212 |
}, |
| 213 |
want: nil, |
| 214 |
wantErr: true, |
| 215 |
}, |
| 216 |
} |
| 217 |
for _, tt := range tests { |
| 218 |
t.Run(tt.name, func(t *testing.T) { |
| 219 |
u, err := url.Parse(tt.args.urlStr) |
| 220 |
if err != nil { |
| 221 |
t.Fatalf("failed to parse URL %q: %v", tt.args.urlStr, err) |
| 222 |
} |
| 223 |
got, err := configFromURL(u) |
| 224 |
if (err != nil) != tt.wantErr { |
| 225 |
t.Errorf("configFromURL() error = %v, wantErr %v", err, tt.wantErr) |
| 226 |
return |
| 227 |
} |
| 228 |
if !reflect.DeepEqual(got, tt.want) { |
| 229 |
t.Errorf("configFromURL() = %v, want %v", got, tt.want) |
| 230 |
} |
| 231 |
}) |
| 232 |
} |
| 233 |
} |