@@ -319,6 +319,20 @@ func TestBuildPgDumpArgs(t *testing.T) {
319319 "-h" , "remote" , "-p" , "6543" , "-d" , "prod" ,
320320 },
321321 },
322+ {
323+ name : "TlsHost overrides -h" ,
324+ config : & protos.PostgresConfig {
325+ Host : "10.0.0.1" ,
326+ Port : 5432 ,
327+ Database : "mydb" ,
328+ User : "admin" ,
329+ TlsHost : "db.example.com" ,
330+ },
331+ wantArgs : []string {
332+ "--schema-only" , "--no-owner" , "--no-privileges" ,
333+ "-h" , "db.example.com" , "-p" , "5432" , "-d" , "mydb" , "-U" , "admin" ,
334+ },
335+ },
322336 }
323337
324338 for _ , tt := range tests {
@@ -367,6 +381,23 @@ func TestBuildPsqlArgs(t *testing.T) {
367381 "--quiet" ,
368382 },
369383 },
384+ {
385+ name : "TlsHost overrides -h" ,
386+ config : & protos.PostgresConfig {
387+ Host : "10.0.0.1" ,
388+ Port : 5432 ,
389+ Database : "db" ,
390+ User : "u" ,
391+ TlsHost : "db.example.com" ,
392+ },
393+ wantArgs : []string {
394+ "-h" , "db.example.com" , "-p" , "5432" , "-d" , "db" ,
395+ "--single-transaction" ,
396+ "-v" , "ON_ERROR_STOP=1" ,
397+ "--quiet" ,
398+ "-U" , "u" ,
399+ },
400+ },
370401 }
371402
372403 for _ , tt := range tests {
@@ -515,6 +546,68 @@ func TestAppendTLSEnv(t *testing.T) {
515546 }
516547}
517548
549+ func TestAppendTLSEnv_TlsHost (t * testing.T ) {
550+ ctx := t .Context ()
551+
552+ t .Run ("PGHOSTADDR set when TlsHost configured" , func (t * testing.T ) {
553+ cmd := exec .CommandContext (ctx , "echo" ) //nolint:gosec
554+ cmd .Env = os .Environ ()
555+
556+ config := & protos.PostgresConfig {
557+ Host : "10.0.0.1" , Port : 5432 , Database : "d" ,
558+ RequireTls : true ,
559+ TlsHost : "db.example.com" ,
560+ }
561+ appendTLSEnv (ctx , cmd , config )
562+
563+ if got := envValue (cmd , "PGHOSTADDR" ); got != "10.0.0.1" {
564+ t .Errorf ("PGHOSTADDR = %q, want %q" , got , "10.0.0.1" )
565+ }
566+ if got := envValue (cmd , "PGSSLMODE" ); got != "require" {
567+ t .Errorf ("PGSSLMODE = %q, want %q" , got , "require" )
568+ }
569+ })
570+
571+ t .Run ("PGHOSTADDR not set when TlsHost empty" , func (t * testing.T ) {
572+ cmd := exec .CommandContext (ctx , "echo" ) //nolint:gosec
573+ cmd .Env = os .Environ ()
574+
575+ config := & protos.PostgresConfig {
576+ Host : "10.0.0.1" , Port : 5432 , Database : "d" ,
577+ RequireTls : true ,
578+ }
579+ appendTLSEnv (ctx , cmd , config )
580+
581+ if got := envValue (cmd , "PGHOSTADDR" ); got != "" {
582+ t .Errorf ("PGHOSTADDR should be empty, got %q" , got )
583+ }
584+ })
585+
586+ t .Run ("PGHOSTADDR with root CA and TlsHost" , func (t * testing.T ) {
587+ cmd := exec .CommandContext (ctx , "echo" ) //nolint:gosec
588+ cmd .Env = os .Environ ()
589+
590+ config := & protos.PostgresConfig {
591+ Host : "10.0.0.1" , Port : 5432 , Database : "d" ,
592+ RequireTls : true ,
593+ TlsHost : "db.example.com" ,
594+ RootCa : strPtr ("-----BEGIN CERTIFICATE-----\n test\n -----END CERTIFICATE-----" ),
595+ }
596+ appendTLSEnv (ctx , cmd , config )
597+
598+ if got := envValue (cmd , "PGHOSTADDR" ); got != "10.0.0.1" {
599+ t .Errorf ("PGHOSTADDR = %q, want %q" , got , "10.0.0.1" )
600+ }
601+ if got := envValue (cmd , "PGSSLMODE" ); got != "verify-ca" {
602+ t .Errorf ("PGSSLMODE = %q, want %q" , got , "verify-ca" )
603+ }
604+ // clean up temp file
605+ if f := envValue (cmd , "PGSSLROOTCERT" ); f != "" {
606+ os .Remove (f )
607+ }
608+ })
609+ }
610+
518611func TestIncompatibleLineRegex (t * testing.T ) {
519612 tests := []struct {
520613 line string
0 commit comments