@@ -817,56 +817,81 @@ func TestExternalPlugin_DatabaseReload(t *testing.T) {
817
817
t .Fatal (err )
818
818
}
819
819
820
- dbName := fmt .Sprintf ("%s-%d" , plugin .Name , 0 )
821
- roleName := "test-role-" + dbName
822
-
823
- cleanupContainer , connURL := postgreshelper .PrepareTestContainerWithVaultUser (t , context .Background ())
824
- t .Cleanup (cleanupContainer )
820
+ roleName := "test-role"
821
+
822
+ // create 4 databases to create connections for
823
+ cleanupContainer0 , connURL0 := postgreshelper .PrepareTestContainerWithVaultUser (t , context .Background ())
824
+ t .Cleanup (cleanupContainer0 )
825
+ cleanupContainer1 , connURL1 := postgreshelper .PrepareTestContainerWithVaultUser (t , context .Background ())
826
+ t .Cleanup (cleanupContainer1 )
827
+ cleanupContainer2 , connURL2 := postgreshelper .PrepareTestContainerWithVaultUser (t , context .Background ())
828
+ t .Cleanup (cleanupContainer2 )
829
+
830
+ var roles []string
831
+ // write the config and roles for the first 3 DBs
832
+ for i , url := range []string {connURL0 , connURL1 , connURL2 } {
833
+ dbName := fmt .Sprintf ("%s-%d" , plugin .Name , i )
834
+ _ , err := client .Logical ().Write ("database/config/" + dbName , map [string ]interface {}{
835
+ "connection_url" : url ,
836
+ "plugin_name" : plugin .Name ,
837
+ "allowed_roles" : []string {"*" },
838
+ "username" : "vaultadmin" ,
839
+ "password" : "vaultpass" ,
840
+ })
841
+ require .NoError (t , err )
825
842
826
- _ , err := client .Logical ().Write ("database/config/" + dbName , map [string ]interface {}{
827
- "connection_url" : connURL ,
828
- "plugin_name" : plugin .Name ,
829
- "allowed_roles" : []string {roleName },
830
- "username" : "vaultadmin" ,
831
- "password" : "vaultpass" ,
832
- })
833
- if err != nil {
834
- t .Fatal (err )
843
+ r := fmt .Sprintf ("%s-%d" , roleName , i )
844
+ roles = append (roles , r )
845
+ _ , err = client .Logical ().Write ("database/roles/" + r , map [string ]interface {}{
846
+ "db_name" : dbName ,
847
+ "creation_statements" : testRole ,
848
+ "max_ttl" : "10m" ,
849
+ })
850
+ require .NoError (t , err )
835
851
}
836
852
837
- _ , err = client .Logical ().Write ("database/roles/" + roleName , map [string ]interface {}{
838
- "db_name" : dbName ,
839
- "creation_statements" : testRole ,
840
- "max_ttl" : "10m" ,
853
+ // the 4th db connection has a bad connURL on purpose, it should not fail
854
+ // the plugin reload
855
+ _ , err := client .Logical ().Write ("database/config/" + plugin .Name + "-3" , map [string ]interface {}{
856
+ "connection_url" : "foobar" ,
857
+ "verify_connection" : false , // this db connection should not fail the plugin reload
858
+ "plugin_name" : plugin .Name ,
859
+ "allowed_roles" : []string {"*" },
860
+ "username" : "vaultadmin" ,
861
+ "password" : "vaultpass" ,
841
862
})
842
- if err != nil {
843
- t .Fatal (err )
844
- }
863
+ require .NoError (t , err )
845
864
846
- resp , err := client . Logical (). Read ( "database/creds/" + roleName )
847
- if err != nil {
848
- t . Fatal ( err )
849
- }
850
- if resp == nil {
851
- t . Fatal ( "read creds response is nil" )
865
+ for _ , role := range roles {
866
+ resp , err := client . Logical (). Read ( "database/creds/" + role )
867
+ require . NoError ( t , err )
868
+ if resp == nil {
869
+ t . Fatal ( "read creds response is nil" )
870
+ }
852
871
}
853
872
854
873
// Reload plugin
855
- if _ , err : = client .Sys ().ReloadPlugin (& api.ReloadPluginInput {
874
+ _ , err = client .Sys ().ReloadPlugin (& api.ReloadPluginInput {
856
875
Plugin : plugin .Name ,
857
- }); err != nil {
858
- t .Fatal (err )
859
- }
876
+ })
877
+ require .NoError (t , err )
860
878
861
879
// Generate credentials after reload
862
- resp , err = client . Logical (). Read ( "database/creds/" + roleName )
863
- if err != nil {
864
- t . Fatal ( err )
865
- }
866
- if resp == nil {
867
- t . Fatal ( "read creds response is nil" )
880
+ for _ , role := range roles {
881
+ resp , err := client . Logical (). Read ( "database/creds/" + role )
882
+ require . NoError ( t , err )
883
+ if resp == nil {
884
+ t . Fatal ( "read creds response is nil" )
885
+ }
868
886
}
869
887
888
+ // remove one postgres database so that the plugin reload will fail
889
+ cleanupContainer1 ()
890
+ _ , err = client .Sys ().ReloadPlugin (& api.ReloadPluginInput {
891
+ Plugin : plugin .Name ,
892
+ })
893
+ require .NoError (t , err )
894
+
870
895
if err := client .Sys ().Unmount (plugin .Name ); err != nil {
871
896
t .Fatal (err )
872
897
}
0 commit comments