| 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 awsmysql |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"gocloud.dev/internal/testing/terraform" |
| 23 |
"gocloud.dev/mysql" |
| 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("awsmysql://%s:%s@%s/%s", username, password, endpoint, databaseName) |
| 47 |
t.Log("Connecting to:", urlstr) |
| 48 |
db, err := mysql.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 TestOpenIAM(t *testing.T) { |
| 61 |
// This test will be skipped unless the project is set up with Terraform. |
| 62 |
// Before running go test, run in this directory: |
| 63 |
// |
| 64 |
// terraform init |
| 65 |
// terraform apply |
| 66 |
|
| 67 |
tfOut, err := terraform.ReadOutput(".") |
| 68 |
if err != nil || len(tfOut) == 0 { |
| 69 |
t.Skipf("Could not obtain harness info: %v", err) |
| 70 |
} |
| 71 |
endpoint, _ := tfOut["endpoint"].Value.(string) |
| 72 |
username, _ := tfOut["iam_db_username"].Value.(string) |
| 73 |
roleARN, _ := tfOut["iam_role_arn"].Value.(string) |
| 74 |
databaseName, _ := tfOut["database"].Value.(string) |
| 75 |
if endpoint == "" || username == "" || databaseName == "" { |
| 76 |
t.Fatalf("Missing one or more required Terraform outputs; got endpoint=%q iam_db_username=%q database=%q", endpoint, username, databaseName) |
| 77 |
} |
| 78 |
ctx := context.Background() |
| 79 |
urlstr := fmt.Sprintf("awsmysql://%s@%s/%s?aws_role_arn=%s", |
| 80 |
username, endpoint, databaseName, roleARN) |
| 81 |
t.Log("Connecting to:", urlstr) |
| 82 |
db, err := mysql.Open(ctx, urlstr) |
| 83 |
if err != nil { |
| 84 |
t.Fatal(err) |
| 85 |
} |
| 86 |
if err := db.Ping(); err != nil { |
| 87 |
t.Error("Ping:", err) |
| 88 |
} |
| 89 |
if err := db.Close(); err != nil { |
| 90 |
t.Error("Close:", err) |
| 91 |
} |
| 92 |
} |