Skip to content

Commit

Permalink
Error checking in the customizer
Browse files Browse the repository at this point in the history
  • Loading branch information
bstrausser committed Apr 26, 2024
1 parent 8502649 commit 5d8598f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion modules/postgres/postgres.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -200,7 +201,7 @@ func WithSSLSettings(sslSettings SSLSettings) testcontainers.CustomizeRequestOpt

const defaultPermission = 0o600

return func(req *testcontainers.GenericContainerRequest) {
return func(req *testcontainers.GenericContainerRequest) error {
req.Files = append(req.Files, testcontainers.ContainerFile{
HostFilePath: sslSettings.CACertFile,
ContainerFilePath: postgresCaCertPath,
Expand All @@ -217,9 +218,18 @@ func WithSSLSettings(sslSettings SSLSettings) testcontainers.CustomizeRequestOpt
FileMode: defaultPermission,
})

expectedFiles := []string{sslSettings.CACertFile, sslSettings.CertFile, sslSettings.KeyFile}
for _, expectedFile := range expectedFiles {
_, err := os.Stat(expectedFile)
if err != nil {
return err
}
}

req.WaitingFor = wait.ForAll(req.WaitingFor, wait.ForLog("database system is ready to accept connections"))

internalEntrypoint(req)
return nil
}
}

Expand Down
20 changes: 20 additions & 0 deletions modules/postgres/postgres_test.go
Expand Up @@ -277,6 +277,26 @@ func TestWithSSL(t *testing.T) {
assert.NotNil(t, result)
}

func TestSSLValidatesKeyMaterialPath(t *testing.T) {

ctx := context.Background()

sslSettings := postgres.SSLSettings{}

_, err := postgres.RunContainer(ctx,
postgres.WithConfigFile(filepath.Join("testdata", "postgres-ssl.conf")),
postgres.WithInitScripts(filepath.Join("testdata", "init-user-db.sh")),
postgres.WithDatabase(dbname),
postgres.WithUsername(user),
postgres.WithPassword(password),
testcontainers.WithWaitStrategy(wait.ForLog("database system is ready to accept connections").WithOccurrence(2).WithStartupTimeout(5*time.Second)),
postgres.WithSSLSettings(sslSettings),
)
if err == nil {
t.Fatal(err)
}

}
func TestWithInitScript(t *testing.T) {
ctx := context.Background()

Expand Down

0 comments on commit 5d8598f

Please sign in to comment.