...

Source file src/github.com/GoogleCloudPlatform/cloudsql-proxy/tests/healthcheck_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  // healthcheck_test.go provides some helpers for end to end health check server tests.
    16  package tests
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"net/http"
    22  	"testing"
    23  )
    24  
    25  const (
    26  	readinessPath = "/readiness"
    27  	testPort      = "8090"
    28  )
    29  
    30  // singleInstanceDial verifies that when a proxy client serves the given instance, the readiness
    31  // endpoint serves http.StatusOK.
    32  func singleInstanceDial(t *testing.T, connName string, port int) {
    33  	ctx := context.Background()
    34  
    35  	var args []string
    36  	args = append(args, fmt.Sprintf("-instances=%s=tcp:%d", connName, port), "-use_http_health_check")
    37  
    38  	// Start the proxy.
    39  	p, err := StartProxy(ctx, args...)
    40  	if err != nil {
    41  		t.Fatalf("unable to start proxy: %v", err)
    42  	}
    43  	defer p.Close()
    44  	output, err := p.WaitForServe(ctx)
    45  	if err != nil {
    46  		t.Fatalf("unable to verify proxy was serving: %s \n %s", err, output)
    47  	}
    48  
    49  	resp, err := http.Get("http://localhost:" + testPort + readinessPath)
    50  	if err != nil {
    51  		t.Fatalf("HTTP GET failed: %v", err)
    52  	}
    53  	if resp.StatusCode != http.StatusOK {
    54  		t.Errorf("want %v, got %v", http.StatusOK, resp.StatusCode)
    55  	}
    56  }
    57  

View as plain text