@@ -10,6 +10,7 @@ import (
1010
1111 "gopkg.in/DATA-DOG/go-sqlmock.v1"
1212
13+ "github.com/govau/cf-common/env"
1314 . "github.com/smartystreets/goconvey/convey"
1415)
1516
@@ -24,6 +25,7 @@ func TestDatastore(t *testing.T) {
2425 mockPort = 5432
2526 mockSSLModeDisable = "disable"
2627 mockSSLModeRequire = "require"
28+ mockSSLModeVerifyCA = "verify-ca"
2729 mockConnTimeout = 5
2830 mockSSLCertificate = "ssl-certificate-related-stuff"
2931 mockSSLKey = "ssl-key-related-stuff"
@@ -411,4 +413,45 @@ func TestDatastore(t *testing.T) {
411413 })
412414 })
413415 })
416+
417+ Convey ("Given the requirement for the connection to be TLS secured" , t , func () {
418+ var mockDatabaseConfigSSL = DatabaseConfig {
419+ DatabaseProvider : mockDatabaseProvider ,
420+ Username : mockUsername ,
421+ Password : mockPassword ,
422+ Database : mockDatabase ,
423+ Host : mockHost ,
424+ Port : mockPort ,
425+ SSLMode : mockSSLModeRequire ,
426+ ConnectionTimeoutInSecs : mockConnTimeout ,
427+ SSLRootCertificate : mockSSLRootCertificate ,
428+ }
429+
430+ mockEnvVarsMap := make (map [string ]string )
431+ mockEnvVarsMap ["DB_SSL_MODE" ] = mockSSLModeVerifyCA
432+ mockEnvVarsMap ["VCAP_SERVICES" ] = `{"cf-postgresql-service": [ { "name": "mock-stratos-ssl", "credentials": { "cacrt": "mockcert", "host": "mockhost", "name": "mockname", "password": "mockpassword", "port": 5432, "username": "mockusername", "uri": "postgres://mockuser:mockpassword@mockhost:5432/mockname" } } ] }`
433+
434+ mockVarSet := env .NewVarSet (env .WithMapLookup (mockEnvVarsMap ))
435+
436+ db , _ , err := sqlmock .New ()
437+ if err != nil {
438+ t .Errorf ("an error '%s' was not expected when opening a stub database connection" , err )
439+ }
440+ defer db .Close ()
441+
442+ Convey ("when the cloudfoundry database config is present" , func () {
443+
444+ Convey ("err will be nil" , func () {
445+ _ , err := ParseCFEnvs (& mockDatabaseConfigSSL , mockVarSet )
446+ So (err , ShouldBeNil )
447+ So (mockDatabaseConfigSSL .SSLRootCertificate , ShouldContainSubstring , "postgres-ssl-" )
448+ So (mockDatabaseConfigSSL .SSLRootCertificate , ShouldEndWith , ".crt" )
449+ So (mockDatabaseConfigSSL .Username , ShouldEqual , "mockusername" )
450+ So (mockDatabaseConfigSSL .Password , ShouldEqual , "mockpassword" )
451+ So (mockDatabaseConfigSSL .Database , ShouldEqual , "mockname" )
452+ So (mockDatabaseConfigSSL .Host , ShouldEqual , "mockhost" )
453+ So (mockDatabaseConfigSSL .SSLMode , ShouldEqual , mockSSLModeVerifyCA )
454+ })
455+ })
456+ })
414457}
0 commit comments