github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/mysql/azuremysql/azuremysql_test.go (about)

     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  // Read the documentation on Azure Database for MySql for more information.
    16  // See https://docs.microsoft.com/en-us/azure/mysql/howto-configure-ssl.
    17  // To run this test, create a MySQL instance using Azure Portal or Terraform.
    18  // For Azure Portal, see https://docs.microsoft.com/en-us/azure/mysql/quickstart-create-mysql-server-database-using-azure-portal.
    19  // For Terraform, see https://www.terraform.io/docs/providers/azurerm/r/mysql_database.html.
    20  package azuremysql
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  	"testing"
    26  
    27  	"gocloud.dev/internal/testing/terraform"
    28  	"gocloud.dev/mysql"
    29  )
    30  
    31  func TestURLOpener(t *testing.T) {
    32  	// This test will be skipped unless the project is set up with Terraform.
    33  	// Before running go test:
    34  	//
    35  	// 1. Install Azure CLI (az) (https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux)
    36  	// 2. Run "az login"
    37  	// 3. terraform init
    38  	// 4. terraform apply
    39  
    40  	tfOut, err := terraform.ReadOutput(".")
    41  	if err != nil || len(tfOut) == 0 {
    42  		t.Skipf("Could not obtain harness info: %v", err)
    43  	}
    44  
    45  	serverName, _ := tfOut["servername"].Value.(string)
    46  	username, _ := tfOut["username"].Value.(string)
    47  	password, _ := tfOut["password"].Value.(string)
    48  	databaseName, _ := tfOut["database"].Value.(string)
    49  
    50  	ctx := context.Background()
    51  	db, err := mysql.Open(ctx, fmt.Sprintf("azuremysql://%s:%s@%s/%s", username, password, serverName, databaseName))
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if err := db.Ping(); err != nil {
    56  		t.Error("Ping: ", err)
    57  	}
    58  	if _, err = db.ExecContext(ctx, "CREATE TABLE tblTester (id INT NOT NULL, PRIMARY KEY(id))"); err != nil {
    59  		t.Error("ExecContext: ", err)
    60  	}
    61  	if _, err = db.ExecContext(ctx, "DROP TABLE tblTester"); err != nil {
    62  		t.Error("ExecContext: ", err)
    63  	}
    64  	if err := db.Close(); err != nil {
    65  		t.Error("Close: ", err)
    66  	}
    67  }