github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrpq/nrpq_test.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package nrpq 5 6 import ( 7 "testing" 8 9 newrelic "github.com/newrelic/go-agent" 10 ) 11 12 func TestParseDSN(t *testing.T) { 13 testcases := []struct { 14 dsn string 15 expHost string 16 expPortPathOrID string 17 expDatabaseName string 18 env map[string]string 19 }{ 20 // urls 21 { 22 dsn: "postgresql://", 23 expHost: "localhost", 24 expPortPathOrID: "5432", 25 expDatabaseName: "", 26 }, 27 { 28 dsn: "postgresql://localhost", 29 expHost: "localhost", 30 expPortPathOrID: "5432", 31 expDatabaseName: "", 32 }, 33 { 34 dsn: "postgresql://localhost:5433", 35 expHost: "localhost", 36 expPortPathOrID: "5433", 37 expDatabaseName: "", 38 }, 39 { 40 dsn: "postgresql://localhost/mydb", 41 expHost: "localhost", 42 expPortPathOrID: "5432", 43 expDatabaseName: "mydb", 44 }, 45 { 46 dsn: "postgresql://user@localhost", 47 expHost: "localhost", 48 expPortPathOrID: "5432", 49 expDatabaseName: "", 50 }, 51 { 52 dsn: "postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp", 53 expHost: "localhost", 54 expPortPathOrID: "5432", 55 expDatabaseName: "otherdb", 56 }, 57 { 58 dsn: "postgresql:///mydb?host=myhost.com&port=5433", 59 expHost: "myhost.com", 60 expPortPathOrID: "5433", 61 expDatabaseName: "mydb", 62 }, 63 { 64 dsn: "postgresql://[2001:db8::1234]/database", 65 expHost: "2001:db8::1234", 66 expPortPathOrID: "5432", 67 expDatabaseName: "database", 68 }, 69 { 70 dsn: "postgresql://[2001:db8::1234]:7890/database", 71 expHost: "2001:db8::1234", 72 expPortPathOrID: "7890", 73 expDatabaseName: "database", 74 }, 75 { 76 dsn: "postgresql:///dbname?host=/var/lib/postgresql", 77 expHost: "localhost", 78 expPortPathOrID: "/var/lib/postgresql/.s.PGSQL.5432", 79 expDatabaseName: "dbname", 80 }, 81 { 82 dsn: "postgresql://%2Fvar%2Flib%2Fpostgresql/dbname", 83 expHost: "", 84 expPortPathOrID: "", 85 expDatabaseName: "", 86 }, 87 88 // key,value pairs 89 { 90 dsn: "host=1.2.3.4 port=1234 dbname=mydb", 91 expHost: "1.2.3.4", 92 expPortPathOrID: "1234", 93 expDatabaseName: "mydb", 94 }, 95 { 96 dsn: "host =1.2.3.4 port= 1234 dbname = mydb", 97 expHost: "1.2.3.4", 98 expPortPathOrID: "1234", 99 expDatabaseName: "mydb", 100 }, 101 { 102 dsn: "host = 1.2.3.4 port=\t\t1234 dbname =\n\t\t\tmydb", 103 expHost: "1.2.3.4", 104 expPortPathOrID: "1234", 105 expDatabaseName: "mydb", 106 }, 107 { 108 dsn: "host ='1.2.3.4' port= '1234' dbname = 'mydb'", 109 expHost: "1.2.3.4", 110 expPortPathOrID: "1234", 111 expDatabaseName: "mydb", 112 }, 113 { 114 dsn: `host='ain\'t_single_quote' port='port\\slash' dbname='my db spaced'`, 115 expHost: `ain\'t_single_quote`, 116 expPortPathOrID: `port\\slash`, 117 expDatabaseName: "my db spaced", 118 }, 119 { 120 dsn: `host=localhost port=so=does=this`, 121 expHost: "localhost", 122 expPortPathOrID: "so=does=this", 123 }, 124 { 125 dsn: "host=1.2.3.4 hostaddr=5.6.7.8", 126 expHost: "5.6.7.8", 127 expPortPathOrID: "5432", 128 }, 129 { 130 dsn: "hostaddr=5.6.7.8 host=1.2.3.4", 131 expHost: "5.6.7.8", 132 expPortPathOrID: "5432", 133 }, 134 { 135 dsn: "hostaddr=1.2.3.4", 136 expHost: "1.2.3.4", 137 expPortPathOrID: "5432", 138 }, 139 { 140 dsn: "host=example.com,example.org port=80,443", 141 expHost: "example.com", 142 expPortPathOrID: "80", 143 }, 144 { 145 dsn: "hostaddr=example.com,example.org port=80,443", 146 expHost: "example.com", 147 expPortPathOrID: "80", 148 }, 149 { 150 dsn: "hostaddr='' host='' port=80,", 151 expHost: "localhost", 152 expPortPathOrID: "80", 153 }, 154 { 155 dsn: "host=/path/to/socket", 156 expHost: "localhost", 157 expPortPathOrID: "/path/to/socket/.s.PGSQL.5432", 158 }, 159 { 160 dsn: "port=1234 host=/path/to/socket", 161 expHost: "localhost", 162 expPortPathOrID: "/path/to/socket/.s.PGSQL.1234", 163 }, 164 { 165 dsn: "host=/path/to/socket port=1234", 166 expHost: "localhost", 167 expPortPathOrID: "/path/to/socket/.s.PGSQL.1234", 168 }, 169 170 // env vars 171 { 172 dsn: "host=host_string port=port_string dbname=dbname_string", 173 expHost: "host_string", 174 expPortPathOrID: "port_string", 175 expDatabaseName: "dbname_string", 176 env: map[string]string{ 177 "PGHOST": "host_env", 178 "PGPORT": "port_env", 179 "PGDATABASE": "dbname_env", 180 }, 181 }, 182 { 183 dsn: "", 184 expHost: "host_env", 185 expPortPathOrID: "port_env", 186 expDatabaseName: "dbname_env", 187 env: map[string]string{ 188 "PGHOST": "host_env", 189 "PGPORT": "port_env", 190 "PGDATABASE": "dbname_env", 191 }, 192 }, 193 { 194 dsn: "host=host_string", 195 expHost: "host_string", 196 expPortPathOrID: "5432", 197 env: map[string]string{ 198 "PGHOSTADDR": "hostaddr_env", 199 }, 200 }, 201 { 202 dsn: "hostaddr=hostaddr_string", 203 expHost: "hostaddr_string", 204 expPortPathOrID: "5432", 205 env: map[string]string{ 206 "PGHOST": "host_env", 207 }, 208 }, 209 { 210 dsn: "host=host_string hostaddr=hostaddr_string", 211 expHost: "hostaddr_string", 212 expPortPathOrID: "5432", 213 env: map[string]string{ 214 "PGHOST": "host_env", 215 }, 216 }, 217 } 218 219 for _, test := range testcases { 220 getenv := func(env string) string { 221 return test.env[env] 222 } 223 224 s := &newrelic.DatastoreSegment{} 225 parseDSN(getenv)(s, test.dsn) 226 227 if test.expHost != s.Host { 228 t.Errorf(`incorrect host, expected="%s", actual="%s"`, test.expHost, s.Host) 229 } 230 if test.expPortPathOrID != s.PortPathOrID { 231 t.Errorf(`incorrect port path or id, expected="%s", actual="%s"`, test.expPortPathOrID, s.PortPathOrID) 232 } 233 if test.expDatabaseName != s.DatabaseName { 234 t.Errorf(`incorrect database name, expected="%s", actual="%s"`, test.expDatabaseName, s.DatabaseName) 235 } 236 } 237 } 238 239 func TestNewConnector(t *testing.T) { 240 connector, err := NewConnector("client_encoding=") 241 if err == nil { 242 t.Error("error expected from invalid dsn") 243 } 244 if connector != nil { 245 t.Error("nil connector expected from invalid dsn") 246 } 247 connector, err = NewConnector("host=localhost port=5432 user=postgres dbname=postgres password=docker sslmode=disable") 248 if err != nil { 249 t.Error("nil error expected from valid dsn", err) 250 } 251 if connector == nil { 252 t.Error("non-nil connector expected from valid dsn") 253 } 254 }