...

Source file src/github.com/GoogleCloudPlatform/cloudsql-proxy/tests/postgres_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  // postgres_test runs various tests against a Postgres flavored Cloud SQL instance.
    16  package tests
    17  
    18  import (
    19  	"context"
    20  	"database/sql"
    21  	"flag"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"log"
    25  	"os"
    26  	"path"
    27  	"runtime"
    28  	"testing"
    29  	"time"
    30  
    31  	_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
    32  	_ "github.com/lib/pq"
    33  )
    34  
    35  var (
    36  	postgresConnName = flag.String("postgres_conn_name", os.Getenv("POSTGRES_CONNECTION_NAME"), "Cloud SQL Postgres instance connection name, in the form of 'project:region:instance'.")
    37  	postgresUser     = flag.String("postgres_user", os.Getenv("POSTGRES_USER"), "Name of database user.")
    38  	postgresPass     = flag.String("postgres_pass", os.Getenv("POSTGRES_PASS"), "Password for the database user; be careful when entering a password on the command line (it may go into your terminal's history).")
    39  	postgresDb       = flag.String("postgres_db", os.Getenv("POSTGRES_DB"), "Name of the database to connect to.")
    40  
    41  	postgresIAMUser = flag.String("postgres_user_iam", os.Getenv("POSTGRES_USER_IAM"), "Name of database user configured with IAM DB Authentication.")
    42  
    43  	postgresPort = 5432
    44  )
    45  
    46  func requirePostgresVars(t *testing.T) {
    47  	switch "" {
    48  	case *postgresConnName:
    49  		t.Fatal("'postgres_conn_name' not set")
    50  	case *postgresUser:
    51  		t.Fatal("'postgres_user' not set")
    52  	case *postgresPass:
    53  		t.Fatal("'postgres_pass' not set")
    54  	case *postgresDb:
    55  		t.Fatal("'postgres_db' not set")
    56  	}
    57  }
    58  
    59  func TestPostgresTcp(t *testing.T) {
    60  	if testing.Short() {
    61  		t.Skip("skipping Postgres integration tests")
    62  	}
    63  	requirePostgresVars(t)
    64  
    65  	dsn := fmt.Sprintf("user=%s password=%s database=%s sslmode=disable", *postgresUser, *postgresPass, *postgresDb)
    66  	proxyConnTest(t, *postgresConnName, "postgres", dsn, postgresPort, "")
    67  }
    68  
    69  func TestPostgresSocket(t *testing.T) {
    70  	if testing.Short() {
    71  		t.Skip("skipping Postgres integration tests")
    72  	}
    73  	if runtime.GOOS == "windows" {
    74  		t.Skip("Skipped Unix socket test on Windows")
    75  	}
    76  	requirePostgresVars(t)
    77  
    78  	dir, err := ioutil.TempDir("", "csql-proxy")
    79  	if err != nil {
    80  		log.Fatalf("unable to create tmp dir: %s", err)
    81  	}
    82  	defer os.RemoveAll(dir)
    83  
    84  	dsn := fmt.Sprintf("user=%s password=%s database=%s host=%s", *postgresUser, *postgresPass, *postgresDb, path.Join(dir, *postgresConnName))
    85  	proxyConnTest(t, *postgresConnName, "postgres", dsn, 0, dir)
    86  }
    87  
    88  func TestPostgresConnLimit(t *testing.T) {
    89  	if testing.Short() {
    90  		t.Skip("skipping Postgres integration tests")
    91  	}
    92  	requirePostgresVars(t)
    93  
    94  	dsn := fmt.Sprintf("user=%s password=%s database=%s sslmode=disable", *postgresUser, *postgresPass, *postgresDb)
    95  	proxyConnLimitTest(t, *postgresConnName, "postgres", dsn, postgresPort)
    96  }
    97  
    98  func TestPostgresIAMDBAuthn(t *testing.T) {
    99  	if testing.Short() {
   100  		t.Skip("skipping Postgres integration tests")
   101  	}
   102  	requirePostgresVars(t)
   103  	if *postgresIAMUser == "" {
   104  		t.Fatal("'postgres_user_iam' not set")
   105  	}
   106  
   107  	ctx := context.Background()
   108  
   109  	// Start the proxy
   110  	p, err := StartProxy(ctx, fmt.Sprintf("-instances=%s=tcp:%d", *postgresConnName, 5432), "-enable_iam_login")
   111  	if err != nil {
   112  		t.Fatalf("unable to start proxy: %v", err)
   113  	}
   114  	defer p.Close()
   115  	output, err := p.WaitForServe(ctx)
   116  	if err != nil {
   117  		t.Fatalf("unable to verify proxy was serving: %s \n %s", err, output)
   118  	}
   119  
   120  	dsn := fmt.Sprintf("user=%s database=%s sslmode=disable", *postgresIAMUser, *postgresDb)
   121  	db, err := sql.Open("postgres", dsn)
   122  	if err != nil {
   123  		t.Fatalf("unable to connect to db: %s", err)
   124  	}
   125  	defer db.Close()
   126  	_, err = db.Exec("SELECT 1;")
   127  	if err != nil {
   128  
   129  		t.Fatalf("unable to exec on db: %s", err)
   130  	}
   131  }
   132  
   133  func TestPostgresHook(t *testing.T) {
   134  	if testing.Short() {
   135  		t.Skip("skipping Postgres integration tests")
   136  	}
   137  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
   138  	defer cancel()
   139  
   140  	dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable", *postgresConnName, *postgresUser, *postgresPass, *postgresDb)
   141  	db, err := sql.Open("cloudsqlpostgres", dsn)
   142  	if err != nil {
   143  		t.Fatalf("connect failed: %s", err)
   144  	}
   145  	defer db.Close()
   146  	var now time.Time
   147  	err = db.QueryRowContext(ctx, "SELECT NOW()").Scan(&now)
   148  	if err != nil {
   149  		t.Fatalf("query failed: %s", err)
   150  	}
   151  }
   152  
   153  // Test to verify that when a proxy client serves one postgres instance that can be
   154  // dialed successfully, the health check readiness endpoint serves http.StatusOK.
   155  func TestPostgresDial(t *testing.T) {
   156  	if testing.Short() {
   157  		t.Skip("skipping Postgres integration tests")
   158  	}
   159  	switch "" {
   160  	case *postgresConnName:
   161  		t.Fatal("'postgres_conn_name' not set")
   162  	}
   163  
   164  	singleInstanceDial(t, *postgresConnName, postgresPort)
   165  }
   166  

View as plain text