github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/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  	userEmail, _ := tfOut["user_email"].Value.(string)
    45  	if project == "" || region == "" || instance == "" || username == "" || databaseName == "" || userEmail == "" {
    46  		t.Fatalf("Missing one or more required Terraform outputs; got project=%q region=%q instance=%q username=%q database=%q userEmail=%q", project, region, instance, username, databaseName, userEmail)
    47  	}
    48  	tests := []struct {
    49  		name    string
    50  		urlstr  string
    51  		wantErr bool
    52  	}{
    53  		{
    54  			name:   "SuccessIam",
    55  			urlstr: fmt.Sprintf("gcppostgres://%s@%s/%s/%s/%s", userEmail, project, region, instance, databaseName),
    56  		},
    57  		{
    58  			name:   "SuccessBuiltin",
    59  			urlstr: fmt.Sprintf("gcppostgres://%s:%s@%s/%s/%s/%s", username, password, project, region, instance, databaseName),
    60  		},
    61  		{
    62  			name:    "SSLModeForbidden",
    63  			urlstr:  fmt.Sprintf("gcppostgres://%s:%s@%s/%s/%s/%s?sslmode=require", username, password, project, region, instance, databaseName),
    64  			wantErr: true,
    65  		},
    66  	}
    67  
    68  	ctx := context.Background()
    69  	for _, test := range tests {
    70  		t.Run(test.name, func(t *testing.T) {
    71  			db, err := postgres.Open(ctx, test.urlstr)
    72  			if err != nil {
    73  				t.Log(err)
    74  				if !test.wantErr {
    75  					t.Fail()
    76  				}
    77  				return
    78  			}
    79  			if test.wantErr {
    80  				db.Close()
    81  				t.Fatal("Open succeeded; want error")
    82  			}
    83  			if err := db.Ping(); err != nil {
    84  				t.Error("Ping:", err)
    85  			}
    86  			if err := db.Close(); err != nil {
    87  				t.Error("Close:", err)
    88  			}
    89  		})
    90  	}
    91  }
    92  
    93  func TestInstanceFromURL(t *testing.T) {
    94  	tests := []struct {
    95  		name         string
    96  		urlString    string
    97  		wantInstance string
    98  		wantDatabase string
    99  		wantErr      bool
   100  	}{
   101  		{
   102  			name:         "AllValuesSpecified",
   103  			urlString:    "gcppostgres://username:password@my-project-id/us-central1/my-instance-id/my-db?foo=bar&baz=quux",
   104  			wantInstance: "my-project-id:us-central1:my-instance-id",
   105  			wantDatabase: "my-db",
   106  		},
   107  		{
   108  			name:         "OptionalValuesOmitted",
   109  			urlString:    "gcppostgres://my-project-id/us-central1/my-instance-id/my-db",
   110  			wantInstance: "my-project-id:us-central1:my-instance-id",
   111  			wantDatabase: "my-db",
   112  		},
   113  		{
   114  			name:      "DatabaseNameEmpty",
   115  			urlString: "gcppostgres://my-project-id/us-central1/my-instance-id/",
   116  			wantErr:   true,
   117  		},
   118  		{
   119  			name:      "InstanceEmpty",
   120  			urlString: "gcppostgres://my-project-id/us-central1//my-db",
   121  			wantErr:   true,
   122  		},
   123  		{
   124  			name:      "RegionEmpty",
   125  			urlString: "gcppostgres://my-project-id//my-instance-id/my-db",
   126  			wantErr:   true,
   127  		},
   128  		{
   129  			name:      "ProjectEmpty",
   130  			urlString: "gcppostgres:///us-central1/my-instance-id/my-db",
   131  			wantErr:   true,
   132  		},
   133  		{
   134  			name:         "DatabaseNameWithSlashes",
   135  			urlString:    "gcppostgres://my-project-id/us-central1/my-instance-id/foo/bar/baz",
   136  			wantInstance: "my-project-id:us-central1:my-instance-id",
   137  			wantDatabase: "foo/bar/baz",
   138  		},
   139  	}
   140  	for _, test := range tests {
   141  		t.Run(test.name, func(t *testing.T) {
   142  			u, err := url.Parse(test.urlString)
   143  			if err != nil {
   144  				t.Fatalf("failed to parse URL %q: %v", test.urlString, err)
   145  			}
   146  			instance, database, err := instanceFromURL(u)
   147  			if err != nil {
   148  				t.Logf("instanceFromURL(url.Parse(%q)): %v", u, err)
   149  				if !test.wantErr {
   150  					t.Fail()
   151  				}
   152  				return
   153  			}
   154  			if test.wantErr {
   155  				t.Fatalf("instanceFromURL(url.Parse(%q)) = %q, %q, <nil>; want error", test.urlString, instance, database)
   156  			}
   157  			if instance != test.wantInstance || database != test.wantDatabase {
   158  				t.Errorf("instanceFromURL(url.Parse(%q)) = %q, %q, <nil>; want %q, %q, <nil>", test.urlString, instance, database, test.wantInstance, test.wantDatabase)
   159  			}
   160  		})
   161  	}
   162  }