github.com/thiagoyeds/go-cloud@v0.26.0/postgres/gcppostgres/gcppostgres_test.go (about)

     1  // Copyright 2018 The Go Cloud Development Kit Authors
     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  package gcppostgres
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/url"
    21  	"testing"
    22  
    23  	"gocloud.dev/internal/testing/terraform"
    24  	"gocloud.dev/postgres"
    25  )
    26  
    27  func TestURLOpener(t *testing.T) {
    28  	// This test will be skipped unless the project is set up with Terraform.
    29  	// Before running go test, run in this directory:
    30  	//
    31  	// terraform init
    32  	// terraform apply
    33  
    34  	tfOut, err := terraform.ReadOutput(".")
    35  	if err != nil || len(tfOut) == 0 {
    36  		t.Skipf("Could not obtain harness info: %v", err)
    37  	}
    38  	project, _ := tfOut["project"].Value.(string)
    39  	region, _ := tfOut["region"].Value.(string)
    40  	instance, _ := tfOut["instance"].Value.(string)
    41  	username, _ := tfOut["username"].Value.(string)
    42  	password, _ := tfOut["password"].Value.(string)
    43  	databaseName, _ := tfOut["database"].Value.(string)
    44  	if project == "" || region == "" || instance == "" || username == "" || databaseName == "" {
    45  		t.Fatalf("Missing one or more required Terraform outputs; got project=%q region=%q instance=%q username=%q database=%q", project, region, instance, username, databaseName)
    46  	}
    47  	tests := []struct {
    48  		name    string
    49  		urlstr  string
    50  		wantErr bool
    51  	}{
    52  		{
    53  			name:   "Success",
    54  			urlstr: fmt.Sprintf("gcppostgres://%s:%s@%s/%s/%s/%s", username, password, project, region, instance, databaseName),
    55  		},
    56  		{
    57  			name:    "SSLModeForbidden",
    58  			urlstr:  fmt.Sprintf("gcppostgres://%s:%s@%s/%s/%s/%s?sslmode=require", username, password, project, region, instance, databaseName),
    59  			wantErr: true,
    60  		},
    61  	}
    62  
    63  	ctx := context.Background()
    64  	for _, test := range tests {
    65  		t.Run(test.name, func(t *testing.T) {
    66  			db, err := postgres.Open(ctx, test.urlstr)
    67  			if err != nil {
    68  				t.Log(err)
    69  				if !test.wantErr {
    70  					t.Fail()
    71  				}
    72  				return
    73  			}
    74  			if test.wantErr {
    75  				db.Close()
    76  				t.Fatal("Open succeeded; want error")
    77  			}
    78  			if err := db.Ping(); err != nil {
    79  				t.Error("Ping:", err)
    80  			}
    81  			if err := db.Close(); err != nil {
    82  				t.Error("Close:", err)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestInstanceFromURL(t *testing.T) {
    89  	tests := []struct {
    90  		name         string
    91  		urlString    string
    92  		wantInstance string
    93  		wantDatabase string
    94  		wantErr      bool
    95  	}{
    96  		{
    97  			name:         "AllValuesSpecified",
    98  			urlString:    "gcppostgres://username:password@my-project-id/us-central1/my-instance-id/my-db?foo=bar&baz=quux",
    99  			wantInstance: "my-project-id:us-central1:my-instance-id",
   100  			wantDatabase: "my-db",
   101  		},
   102  		{
   103  			name:         "OptionalValuesOmitted",
   104  			urlString:    "gcppostgres://my-project-id/us-central1/my-instance-id/my-db",
   105  			wantInstance: "my-project-id:us-central1:my-instance-id",
   106  			wantDatabase: "my-db",
   107  		},
   108  		{
   109  			name:      "DatabaseNameEmpty",
   110  			urlString: "gcppostgres://my-project-id/us-central1/my-instance-id/",
   111  			wantErr:   true,
   112  		},
   113  		{
   114  			name:      "InstanceEmpty",
   115  			urlString: "gcppostgres://my-project-id/us-central1//my-db",
   116  			wantErr:   true,
   117  		},
   118  		{
   119  			name:      "RegionEmpty",
   120  			urlString: "gcppostgres://my-project-id//my-instance-id/my-db",
   121  			wantErr:   true,
   122  		},
   123  		{
   124  			name:      "ProjectEmpty",
   125  			urlString: "gcppostgres:///us-central1/my-instance-id/my-db",
   126  			wantErr:   true,
   127  		},
   128  		{
   129  			name:         "DatabaseNameWithSlashes",
   130  			urlString:    "gcppostgres://my-project-id/us-central1/my-instance-id/foo/bar/baz",
   131  			wantInstance: "my-project-id:us-central1:my-instance-id",
   132  			wantDatabase: "foo/bar/baz",
   133  		},
   134  	}
   135  	for _, test := range tests {
   136  		t.Run(test.name, func(t *testing.T) {
   137  			u, err := url.Parse(test.urlString)
   138  			if err != nil {
   139  				t.Fatalf("failed to parse URL %q: %v", test.urlString, err)
   140  			}
   141  			instance, database, err := instanceFromURL(u)
   142  			if err != nil {
   143  				t.Logf("instanceFromURL(url.Parse(%q)): %v", u, err)
   144  				if !test.wantErr {
   145  					t.Fail()
   146  				}
   147  				return
   148  			}
   149  			if test.wantErr {
   150  				t.Fatalf("instanceFromURL(url.Parse(%q)) = %q, %q, <nil>; want error", test.urlString, instance, database)
   151  			}
   152  			if instance != test.wantInstance || database != test.wantDatabase {
   153  				t.Errorf("instanceFromURL(url.Parse(%q)) = %q, %q, <nil>; want %q, %q, <nil>", test.urlString, instance, database, test.wantInstance, test.wantDatabase)
   154  			}
   155  		})
   156  	}
   157  }