...

Source file src/github.com/GoogleCloudPlatform/cloudsql-proxy/tests/mysql_test.go

Documentation: github.com/GoogleCloudPlatform/cloudsql-proxy/tests

     1  // Copyright 2020 Google LLC
     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  //      http://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  // mysql_test runs various tests against a MySQL flavored Cloud SQL instance.
    16  package tests
    17  
    18  import (
    19  	"flag"
    20  	"io/ioutil"
    21  	"log"
    22  	"os"
    23  	"path"
    24  	"runtime"
    25  	"testing"
    26  
    27  	mysql "github.com/go-sql-driver/mysql"
    28  )
    29  
    30  var (
    31  	mysqlConnName = flag.String("mysql_conn_name", os.Getenv("MYSQL_CONNECTION_NAME"), "Cloud SQL MYSQL instance connection name, in the form of 'project:region:instance'.")
    32  	mysqlUser     = flag.String("mysql_user", os.Getenv("MYSQL_USER"), "Name of database user.")
    33  	mysqlPass     = flag.String("mysql_pass", os.Getenv("MYSQL_PASS"), "Password for the database user; be careful when entering a password on the command line (it may go into your terminal's history).")
    34  	mysqlDb       = flag.String("mysql_db", os.Getenv("MYSQL_DB"), "Name of the database to connect to.")
    35  
    36  	mysqlPort = 3306
    37  )
    38  
    39  func requireMysqlVars(t *testing.T) {
    40  	switch "" {
    41  	case *mysqlConnName:
    42  		t.Fatal("'mysql_conn_name' not set")
    43  	case *mysqlUser:
    44  		t.Fatal("'mysql_user' not set")
    45  	case *mysqlPass:
    46  		t.Fatal("'mysql_pass' not set")
    47  	case *mysqlDb:
    48  		t.Fatal("'mysql_db' not set")
    49  	}
    50  }
    51  
    52  func TestMysqlTcp(t *testing.T) {
    53  	if testing.Short() {
    54  		t.Skip("skipping MySQL integration tests")
    55  	}
    56  	requireMysqlVars(t)
    57  	cfg := mysql.Config{
    58  		User:                 *mysqlUser,
    59  		Passwd:               *mysqlPass,
    60  		DBName:               *mysqlDb,
    61  		AllowNativePasswords: true,
    62  	}
    63  	proxyConnTest(t, *mysqlConnName, "mysql", cfg.FormatDSN(), mysqlPort, "")
    64  }
    65  
    66  func TestMysqlSocket(t *testing.T) {
    67  	if testing.Short() {
    68  		t.Skip("skipping MySQL integration tests")
    69  	}
    70  	if runtime.GOOS == "windows" {
    71  		t.Skip("Skipped Unix socket test on Windows")
    72  	}
    73  	requireMysqlVars(t)
    74  
    75  	dir, err := ioutil.TempDir("", "csql-proxy-tests")
    76  	if err != nil {
    77  		log.Fatalf("unable to create tmp dir: %s", err)
    78  	}
    79  	defer os.RemoveAll(dir)
    80  
    81  	cfg := mysql.Config{
    82  		User:                 *mysqlUser,
    83  		Passwd:               *mysqlPass,
    84  		Net:                  "unix",
    85  		Addr:                 path.Join(dir, *mysqlConnName),
    86  		DBName:               *mysqlDb,
    87  		AllowNativePasswords: true,
    88  	}
    89  	proxyConnTest(t, *mysqlConnName, "mysql", cfg.FormatDSN(), 0, dir)
    90  }
    91  
    92  func TestMysqlConnLimit(t *testing.T) {
    93  	if testing.Short() {
    94  		t.Skip("skipping MySQL integration tests")
    95  	}
    96  	requireMysqlVars(t)
    97  	cfg := mysql.Config{
    98  		User:                 *mysqlUser,
    99  		Passwd:               *mysqlPass,
   100  		DBName:               *mysqlDb,
   101  		AllowNativePasswords: true,
   102  	}
   103  	proxyConnLimitTest(t, *mysqlConnName, "mysql", cfg.FormatDSN(), mysqlPort)
   104  }
   105  
   106  // Test to verify that when a proxy client serves one mysql instance that can be
   107  // dialed successfully, the health check readiness endpoint serves http.StatusOK.
   108  func TestMysqlDial(t *testing.T) {
   109  	if testing.Short() {
   110  		t.Skip("skipping MySQL integration tests")
   111  	}
   112  	switch "" {
   113  	case *mysqlConnName:
   114  		t.Fatal("'mysql_conn_name' not set")
   115  	}
   116  
   117  	singleInstanceDial(t, *mysqlConnName, mysqlPort)
   118  }
   119  

View as plain text