-
Notifications
You must be signed in to change notification settings - Fork 62
Add support for extended logging in DbConfig #1003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,13 +7,19 @@ | |
| package postgres | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/docker/docker/api/types" | ||
| "github.com/docker/docker/api/types/container" | ||
| "github.com/docker/docker/client" | ||
| "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver" | ||
| "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/common" | ||
| testing2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/common/testing" | ||
| common3 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/common" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| _ "modernc.org/sqlite" | ||
| ) | ||
|
|
||
|
|
@@ -49,3 +55,86 @@ | |
| return p.(*KeyValueStore).KeyValueStore | ||
| }) | ||
| } | ||
|
|
||
| func verifyExtendedLoggingInCommand(t *testing.T, config *ContainerConfig, expectExtendedLogging bool) { | ||
| cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||
| require.NoError(t, err) | ||
|
|
||
| ctx := context.Background() | ||
| containers, err := cli.ContainerList(ctx, container.ListOptions{}) | ||
| require.NoError(t, err) | ||
|
|
||
| // Find our container | ||
| var foundContainer *types.Container | ||
| for _, c := range containers { | ||
| for _, name := range c.Names { | ||
| if name == "/"+config.Container { | ||
| foundContainer = &c | ||
| break | ||
| } | ||
| } | ||
| } | ||
| require.NotNil(t, foundContainer, "Container not found") | ||
|
|
||
| // Inspect the container to get the command | ||
| inspect, err := cli.ContainerInspect(ctx, foundContainer.ID) | ||
| require.NoError(t, err) | ||
|
|
||
| // Check extended logging parameters based on expectation | ||
| cmd := inspect.Config.Cmd | ||
| if expectExtendedLogging { | ||
| assert.Contains(t, cmd, "log_destination=stderr") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am wondering if there is a particular reason to use |
||
| assert.Contains(t, cmd, "logging_collector=on") | ||
| assert.Contains(t, cmd, "log_min_duration_statement=0") | ||
| assert.Contains(t, cmd, "log_connections=on") | ||
| assert.Contains(t, cmd, "log_disconnections=on") | ||
| } else { | ||
| // When extended logging is disabled, these parameters should not be present | ||
| assert.NotContains(t, cmd, "log_destination=stderr") | ||
| assert.NotContains(t, cmd, "logging_collector=on") | ||
| assert.NotContains(t, cmd, "log_min_duration_statement=0") | ||
| assert.NotContains(t, cmd, "log_connections=on") | ||
| assert.NotContains(t, cmd, "log_disconnections=on") | ||
| } | ||
| } | ||
|
|
||
| func TestContainerStartup_ExtendedLogging(t *testing.T) { | ||
| if os.Getenv("TEST_POSTGRES") != "true" { | ||
| t.Skip("set environment variable TEST_POSTGRES to true to include postgres test") | ||
| } | ||
| if testing.Short() { | ||
| t.Skip("skipping postgres container test in short mode") | ||
| } | ||
|
|
||
| t.Run("Container starts successfully with extended logging enabled", func(t *testing.T) { | ||
| config := DefaultConfig("issuer-extended") | ||
| config.DbConfig.ExtendedLogging = true | ||
|
|
||
| closeFunc, err := startPostgresWithLogger(*config, t, false) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, closeFunc) | ||
|
|
||
| // Verify the container command includes extended logging parameters | ||
| verifyExtendedLoggingInCommand(t, config, true) | ||
|
|
||
| if closeFunc != nil { | ||
| closeFunc() | ||
| } | ||
| }) | ||
|
|
||
| t.Run("Container starts successfully with extended logging disabled", func(t *testing.T) { | ||
| config := DefaultConfig("issuer-normal") | ||
| config.DbConfig.ExtendedLogging = false | ||
|
|
||
| closeFunc, err := startPostgresWithLogger(*config, t, false) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, closeFunc) | ||
|
|
||
| // Verify the container command does not include extended logging parameters | ||
| verifyExtendedLoggingInCommand(t, config, false) | ||
|
|
||
| if closeFunc != nil { | ||
| closeFunc() | ||
| } | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,11 +47,12 @@ type ContainerConfig struct { | |
| } | ||
|
|
||
| type DbConfig struct { | ||
| DBName string | ||
| User string | ||
| Pass string | ||
| Host string | ||
| Port int | ||
| DBName string | ||
| User string | ||
| Pass string | ||
| Host string | ||
| Port int | ||
| ExtendedLogging bool // enables extended loggings for PostgreSQL | ||
| } | ||
|
|
||
| func (c *DbConfig) DataSource() string { | ||
|
|
@@ -163,7 +164,7 @@ func startPostgresWithLogger(c ContainerConfig, t Logger, printLogs bool) (func( | |
| return nil, fmt.Errorf("can't start postgres: %s", err) | ||
| } | ||
| ctx := context.Background() | ||
| resp, err := cli.ContainerCreate(ctx, &container.Config{ | ||
| containerConfig := &container.Config{ | ||
| Env: []string{ | ||
| "POSTGRES_DB=" + c.DBName, | ||
| "POSTGRES_USER=" + c.User, | ||
|
|
@@ -181,7 +182,28 @@ func startPostgresWithLogger(c ContainerConfig, t Logger, printLogs bool) (func( | |
| Timeout: time.Second, | ||
| Retries: 10, | ||
| }, | ||
| }, &container.HostConfig{ | ||
| } | ||
|
|
||
| // Add extended logging configuration if enabled | ||
| if c.ExtendedLogging { | ||
| containerConfig.Cmd = []string{ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I think it would be nice to allow the |
||
| "postgres", | ||
| "-c", "log_destination=stderr", | ||
| "-c", "logging_collector=on", | ||
| "-c", "log_directory=pg_log", | ||
| "-c", "log_filename=postgresql-%Y-%m-%d_%H%M%S.log", | ||
| "-c", "log_min_duration_statement=0", | ||
| "-c", "log_line_prefix=%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h ", | ||
| "-c", "log_checkpoints=on", | ||
| "-c", "log_connections=on", | ||
| "-c", "log_disconnections=on", | ||
| "-c", "log_lock_waits=on", | ||
| "-c", "log_temp_files=0", | ||
| "-c", "log_autovacuum_min_duration=0", | ||
| } | ||
| } | ||
|
|
||
| resp, err := cli.ContainerCreate(ctx, containerConfig, &container.HostConfig{ | ||
| PortBindings: nat.PortMap{ | ||
| nat.Port("5432/tcp"): []nat.PortBinding{ | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use
t.Context()as this allows the test harness to cancel the context and all probably all consumers of the context.