...

Source file src/cloud.google.com/go/cloudsqlconn/e2e_sqlserver_test.go

Documentation: cloud.google.com/go/cloudsqlconn

     1  // Copyright 2022 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  //     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  //go:build !skip_sqlserver
    16  // +build !skip_sqlserver
    17  
    18  package cloudsqlconn_test
    19  
    20  import (
    21  	"database/sql"
    22  	"fmt"
    23  	"os"
    24  	"testing"
    25  	"time"
    26  
    27  	"cloud.google.com/go/cloudsqlconn/sqlserver/mssql"
    28  )
    29  
    30  var (
    31  	sqlserverConnName = os.Getenv("SQLSERVER_CONNECTION_NAME") // "Cloud SQL SqlServer instance connection name, in the form of 'project:region:instance'.
    32  	sqlserverUser     = os.Getenv("SQLSERVER_USER")            // Name of database user.
    33  	sqlserverPass     = 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).
    34  	sqlserverDB       = os.Getenv("SQLSERVER_DB")              // Name of the database to connect to.
    35  )
    36  
    37  func requireSQLServerVars(t *testing.T) {
    38  	switch "" {
    39  	case sqlserverConnName:
    40  		t.Fatal("'SQLSERVER_CONNECTION_NAME' env var not set")
    41  	case sqlserverUser:
    42  		t.Fatal("'SQLSERVER_USER' env var not set")
    43  	case sqlserverPass:
    44  		t.Fatal("'SQLSERVER_PASS' env var not set")
    45  	case sqlserverDB:
    46  		t.Fatal("'SQLSERVER_DB' env var not set")
    47  	}
    48  }
    49  
    50  func TestSqlServerHook(t *testing.T) {
    51  	if testing.Short() {
    52  		t.Skip("skipping SqlServer integration tests")
    53  	}
    54  	requireSQLServerVars(t)
    55  
    56  	testConn := func(db *sql.DB) {
    57  		var now time.Time
    58  		if err := db.QueryRow("SELECT getdate()").Scan(&now); err != nil {
    59  			t.Fatalf("QueryRow failed: %v", err)
    60  		}
    61  		t.Log(now)
    62  	}
    63  	cleanup, err := mssql.RegisterDriver("cloudsql-sqlserver")
    64  	if err != nil {
    65  		t.Fatalf("failed to register driver: %v", err)
    66  	}
    67  	defer cleanup()
    68  	db, err := sql.Open(
    69  		"cloudsql-sqlserver",
    70  		fmt.Sprintf("sqlserver://%s:%s@localhost?database=%s&cloudsql=%s",
    71  			sqlserverUser, sqlserverPass, sqlserverDB, sqlserverConnName),
    72  	)
    73  	if err != nil {
    74  		t.Fatalf("sql.Open want err = nil, got = %v", err)
    75  	}
    76  	defer db.Close()
    77  	testConn(db)
    78  }
    79  

View as plain text