...

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

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

     1  // Copyright 2021 Google LLC All Rights Reserved.
     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  // alldb_test.go contains end to end tests that require all environment variables to be defined.
    16  package tests
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"net/http"
    22  	"testing"
    23  )
    24  
    25  // requireAllVars skips the given test if at least one environment variable is undefined.
    26  func requireAllVars(t *testing.T) {
    27  	var allVars []string
    28  	allVars = append(allVars, *mysqlConnName, *mysqlUser, *mysqlPass, *mysqlDb)
    29  	allVars = append(allVars, *postgresConnName, *postgresUser, *postgresPass, *postgresDb)
    30  	allVars = append(allVars, *sqlserverConnName, *sqlserverUser, *sqlserverPass, *sqlserverDb)
    31  
    32  	for _, envVar := range allVars {
    33  		if envVar == "" {
    34  			t.Skip("skipping test, all environment variable must be defined")
    35  		}
    36  	}
    37  }
    38  
    39  // Test to verify that when a proxy client serves multiple instances that can all be successfully dialed,
    40  // the health check readiness endpoint serves http.StatusOK.
    41  func TestMultiInstanceDial(t *testing.T) {
    42  	if testing.Short() {
    43  		t.Skip("skipping Health Check integration tests")
    44  	}
    45  	requireAllVars(t)
    46  	ctx := context.Background()
    47  
    48  	var args []string
    49  	args = append(args, fmt.Sprintf("-instances=%s=tcp:%d,%s=tcp:%d,%s=tcp:%d", *mysqlConnName, mysqlPort, *postgresConnName, postgresPort, *sqlserverConnName, sqlserverPort))
    50  	args = append(args, "-use_http_health_check")
    51  
    52  	// Start the proxy.
    53  	p, err := StartProxy(ctx, args...)
    54  	if err != nil {
    55  		t.Fatalf("unable to start proxy: %v", err)
    56  	}
    57  	defer p.Close()
    58  	output, err := p.WaitForServe(ctx)
    59  	if err != nil {
    60  		t.Fatalf("unable to verify proxy was serving: %s \n %s", err, output)
    61  	}
    62  
    63  	resp, err := http.Get("http://localhost:" + testPort + readinessPath)
    64  	if err != nil {
    65  		t.Fatalf("HTTP GET failed: %v", err)
    66  	}
    67  	if resp.StatusCode != http.StatusOK {
    68  		t.Errorf("want %v, got %v", http.StatusOK, resp.StatusCode)
    69  	}
    70  }
    71  

View as plain text