| 1 |
// Copyright 2019 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 mysql |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"net/url" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"gocloud.dev/internal/testing/terraform" |
| 24 |
) |
| 25 |
|
| 26 |
func TestOpen(t *testing.T) { |
| 27 |
// This test will be skipped unless the project is set up with Terraform. |
| 28 |
// Before running go test, run in this directory: |
| 29 |
// |
| 30 |
// terraform init |
| 31 |
// terraform apply |
| 32 |
|
| 33 |
tfOut, err := terraform.ReadOutput(".") |
| 34 |
if err != nil || len(tfOut) == 0 { |
| 35 |
t.Skipf("Could not obtain harness info: %v", err) |
| 36 |
} |
| 37 |
endpoint, _ := tfOut["endpoint"].Value.(string) |
| 38 |
username, _ := tfOut["username"].Value.(string) |
| 39 |
password, _ := tfOut["password"].Value.(string) |
| 40 |
databaseName, _ := tfOut["database"].Value.(string) |
| 41 |
if endpoint == "" || username == "" || databaseName == "" { |
| 42 |
t.Fatalf("Missing one or more required Terraform outputs; got endpoint=%q username=%q database=%q", endpoint, username, databaseName) |
| 43 |
} |
| 44 |
|
| 45 |
ctx := context.Background() |
| 46 |
urlstr := fmt.Sprintf("%s://%s:%s@%s/%s", Scheme, username, password, endpoint, databaseName) |
| 47 |
t.Log("Connecting to:", urlstr) |
| 48 |
db, err := Open(ctx, urlstr) |
| 49 |
if err != nil { |
| 50 |
t.Fatal(err) |
| 51 |
} |
| 52 |
if err := db.Ping(); err != nil { |
| 53 |
t.Error("Ping:", err) |
| 54 |
} |
| 55 |
if err := db.Close(); err != nil { |
| 56 |
t.Error("Close:", err) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func TestConfigFromURL(t *testing.T) { |
| 61 |
for _, host := range []string{ |
| 62 |
"localhost", |
| 63 |
"tcp(localhost)", |
| 64 |
} { |
| 65 |
urlstr := "mysql://user:password@" + host + "/db?parseTime=true&interpolateParams=true" |
| 66 |
u, err := url.Parse(urlstr) |
| 67 |
if err != nil { |
| 68 |
t.Fatal(err) |
| 69 |
} |
| 70 |
cfg, err := ConfigFromURL(u) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
if got, want := cfg.User, "user"; got != want { |
| 75 |
t.Errorf(`User = %q; want %q`, got, want) |
| 76 |
} |
| 77 |
if got, want := cfg.Passwd, "password"; got != want { |
| 78 |
t.Errorf(`Passwd = %q; want %q`, got, want) |
| 79 |
} |
| 80 |
if got, want := cfg.Net, "tcp"; got != want { |
| 81 |
t.Errorf(`Net = %q; want %q`, got, want) |
| 82 |
} |
| 83 |
if got, want := cfg.Addr, "localhost"; got != want { |
| 84 |
t.Errorf(`Addr = %q; want %q`, got, want) |
| 85 |
} |
| 86 |
if !cfg.ParseTime { |
| 87 |
t.Error("ParseTime = false; want true") |
| 88 |
} |
| 89 |
if !cfg.InterpolateParams { |
| 90 |
t.Error("InterpolateParams = false; want true") |
| 91 |
} |
| 92 |
} |
| 93 |
} |