github.com/thiagoyeds/go-cloud@v0.26.0/postgres/awspostgres/awspostgres_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 awspostgres 16 17 import ( 18 "context" 19 "fmt" 20 "testing" 21 22 "gocloud.dev/internal/testing/terraform" 23 "gocloud.dev/postgres" 24 ) 25 26 func TestURLOpener(t *testing.T) { 27 // This test will be skipped unless the project is set up with Terraform. 28 // Before running go test, run in this directory: 29 // 30 // terraform init 31 // terraform apply 32 33 tfOut, err := terraform.ReadOutput(".") 34 if err != nil || len(tfOut) == 0 { 35 t.Skipf("Could not obtain harness info: %v", err) 36 } 37 endpoint, _ := tfOut["endpoint"].Value.(string) 38 username, _ := tfOut["username"].Value.(string) 39 password, _ := tfOut["password"].Value.(string) 40 databaseName, _ := tfOut["database"].Value.(string) 41 if endpoint == "" || username == "" || databaseName == "" { 42 t.Fatalf("Missing one or more required Terraform outputs; got endpoint=%q username=%q database=%q", endpoint, username, databaseName) 43 } 44 45 tests := []struct { 46 urlstr string 47 wantErr bool 48 wantPingErr bool 49 }{ 50 // OK. 51 {fmt.Sprintf("awspostgres://%s:%s@%s/%s", username, password, endpoint, databaseName), false, false}, 52 // Invalid URL parameters: db creation fails. 53 {fmt.Sprintf("awspostgres://%s:%s@%s/%s?sslcert=foo", username, password, endpoint, databaseName), true, false}, 54 {fmt.Sprintf("awspostgres://%s:%s@%s/%s?sslkey=foo", username, password, endpoint, databaseName), true, false}, 55 {fmt.Sprintf("awspostgres://%s:%s@%s/%s?sslrootcert=foo", username, password, endpoint, databaseName), true, false}, 56 {fmt.Sprintf("awspostgres://%s:%s@%s/%s?sslmode=require", username, password, endpoint, databaseName), true, false}, 57 // Invalid connection info: db is created, but Ping fails. 58 {fmt.Sprintf("awspostgres://%s:badpwd@%s/%s", username, endpoint, databaseName), false, true}, 59 {fmt.Sprintf("awspostgres://badusername:%s@%s/%s", password, endpoint, databaseName), false, true}, 60 {fmt.Sprintf("awspostgres://%s:%s@localhost:9999/%s", username, password, databaseName), false, true}, 61 {fmt.Sprintf("awspostgres://%s:%s@%s/wrongdbname", username, password, endpoint), false, true}, 62 {fmt.Sprintf("awspostgres://%s:%s@%s/%s?foo=bar", username, password, endpoint, databaseName), false, true}, 63 } 64 ctx := context.Background() 65 for _, test := range tests { 66 t.Run(test.urlstr, func(t *testing.T) { 67 db, err := postgres.Open(ctx, test.urlstr) 68 if err != nil != test.wantErr { 69 t.Fatalf("got err %v, wanted error? %v", err, test.wantErr) 70 } 71 if err != nil { 72 return 73 } 74 defer func() { 75 if err := db.Close(); err != nil { 76 t.Error("Close:", err) 77 } 78 }() 79 err = db.Ping() 80 if err != nil != test.wantPingErr { 81 t.Errorf("ping got err %v, wanted error? %v", err, test.wantPingErr) 82 } 83 }) 84 } 85 }