...

Source file src/github.com/GoogleCloudPlatform/cloudsql-proxy/tests/sqlserver_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  // sqlserver_test runs various tests against a SqlServer flavored Cloud SQL instance.
    16  package tests
    17  
    18  import (
    19  	"flag"
    20  	"fmt"
    21  	"os"
    22  	"testing"
    23  
    24  	_ "github.com/denisenkom/go-mssqldb"
    25  )
    26  
    27  var (
    28  	sqlserverConnName = flag.String("sqlserver_conn_name", os.Getenv("SQLSERVER_CONNECTION_NAME"), "Cloud SQL SqlServer instance connection name, in the form of 'project:region:instance'.")
    29  	sqlserverUser     = flag.String("sqlserver_user", os.Getenv("SQLSERVER_USER"), "Name of database user.")
    30  	sqlserverPass     = flag.String("sqlserver_pass", os.Getenv("SQLSERVER_PASS"), "Password for the database user; be careful when entering a password on the command line (it may go into your terminal's history).")
    31  	sqlserverDb       = flag.String("sqlserver_db", os.Getenv("SQLSERVER_DB"), "Name of the database to connect to.")
    32  
    33  	sqlserverPort = 1433
    34  )
    35  
    36  func requireSqlserverVars(t *testing.T) {
    37  	switch "" {
    38  	case *sqlserverConnName:
    39  		t.Fatal("'sqlserver_conn_name' not set")
    40  	case *sqlserverUser:
    41  		t.Fatal("'sqlserver_user' not set")
    42  	case *sqlserverPass:
    43  		t.Fatal("'sqlserver_pass' not set")
    44  	case *sqlserverDb:
    45  		t.Fatal("'sqlserver_db' not set")
    46  	}
    47  }
    48  
    49  func TestSqlServerTcp(t *testing.T) {
    50  	if testing.Short() {
    51  		t.Skip("skipping SQL Server integration tests")
    52  	}
    53  	requireSqlserverVars(t)
    54  
    55  	dsn := fmt.Sprintf("sqlserver://%s:%s@127.0.0.1?database=%s", *sqlserverUser, *sqlserverPass, *sqlserverDb)
    56  	proxyConnTest(t, *sqlserverConnName, "sqlserver", dsn, sqlserverPort, "")
    57  }
    58  
    59  func TestSqlserverConnLimit(t *testing.T) {
    60  	if testing.Short() {
    61  		t.Skip("skipping SQL Server integration tests")
    62  	}
    63  	requireSqlserverVars(t)
    64  
    65  	dsn := fmt.Sprintf("sqlserver://%s:%s@127.0.0.1?database=%s", *sqlserverUser, *sqlserverPass, *sqlserverDb)
    66  	proxyConnLimitTest(t, *sqlserverConnName, "sqlserver", dsn, sqlserverPort)
    67  }
    68  
    69  // Test to verify that when a proxy client serves one sqlserver instance that can be
    70  // dialed successfully, the health check readiness endpoint serves http.StatusOK.
    71  func TestSqlserverDial(t *testing.T) {
    72  	if testing.Short() {
    73  		t.Skip("skipping SQL Server integration tests")
    74  	}
    75  	switch "" {
    76  	case *sqlserverConnName:
    77  		t.Fatal("'sqlserver_conn_name' not set")
    78  	}
    79  
    80  	singleInstanceDial(t, *sqlserverConnName, sqlserverPort)
    81  }
    82  

View as plain text