github.com/yorinasub17/go-cloud@v0.27.40/mysql/awsmysql/awsmysql_test.go (about)

     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  }