Skip to content
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

fix(cockroachdb): Fixes cockroachdb wait strategy handling #2456

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion modules/cockroachdb/cockroachdb.go
Expand Up @@ -167,7 +167,7 @@ func addWaitingFor(req *testcontainers.GenericContainerRequest, opts options) er
tlsConfig = cfg
}

req.WaitingFor = wait.ForAll(
defaultStrategy := wait.ForAll(
wait.ForHTTP("/health").WithPort(defaultAdminPort),
wait.ForSQL(defaultSQLPort, "pgx/v5", func(host string, port nat.Port) string {
connStr := connString(opts, host, port)
Expand All @@ -186,6 +186,12 @@ func addWaitingFor(req *testcontainers.GenericContainerRequest, opts options) er
}),
)

if req.WaitingFor == nil {
req.WaitingFor = defaultStrategy
} else {
req.WaitingFor = wait.ForAll(req.WaitingFor, defaultStrategy)
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

Expand Down
68 changes: 68 additions & 0 deletions modules/cockroachdb/cockroachdb_test.go
Expand Up @@ -3,16 +3,19 @@ package cockroachdb_test
import (
"context"
"errors"
"fmt"
"net/url"
"strings"
"testing"
"time"

"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/cockroachdb"
"github.com/testcontainers/testcontainers-go/wait"
)

func TestCockroach_Insecure(t *testing.T) {
Expand Down Expand Up @@ -144,6 +147,71 @@ func (suite *AuthNSuite) TestQuery() {
suite.Equal(523123, id)
}

// TestWithWaitStrategyAndDeadline covers a previous regression, container creation needs to fail to cover that path.
func (suite *AuthNSuite) TestWithWaitStrategyAndDeadline() {

mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
contextDeadlineExceeded := fmt.Errorf("failed to start container: context deadline exceeded")
nodeStartUpCompleted := "node startup completed"
suite.Run("Expected Failure To Run", func() {
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Suggested change

ctx := context.Background()

// This will never match a log statement
suite.opts = append(suite.opts, testcontainers.WithWaitStrategyAndDeadline(time.Millisecond*250, wait.ForLog("Won't Exist In Logs")))
container, err := cockroachdb.RunContainer(ctx, suite.opts...)
suite.Require().Error(err, contextDeadlineExceeded)
suite.T().Cleanup(func() {
if container != nil {
err := container.Terminate(ctx)
suite.Require().NoError(err)
}
})

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

})

suite.Run("Expected Failure To Run But Would Succeed ", func() {

ctx := context.Background()
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved

// This will timeout as we didn't give enough time for intialization, but would have succeeded otherwise
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
suite.opts = append(suite.opts, testcontainers.WithWaitStrategyAndDeadline(time.Millisecond*20, wait.ForLog(nodeStartUpCompleted)))
container, err := cockroachdb.RunContainer(ctx, suite.opts...)
suite.Require().Error(err, contextDeadlineExceeded)
suite.T().Cleanup(func() {
if container != nil {
err := container.Terminate(ctx)
suite.Require().NoError(err)
}
})

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

})

suite.Run("Succeeds And Executes Commands", func() {

mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
ctx := context.Background()

// This will succeed
suite.opts = append(suite.opts, testcontainers.WithWaitStrategyAndDeadline(time.Second*60, wait.ForLog(nodeStartUpCompleted)))
container, err := cockroachdb.RunContainer(ctx, suite.opts...)
suite.Require().NoError(err)

conn, err := conn(ctx, container)
suite.Require().NoError(err)
defer conn.Close(ctx)
bearrito marked this conversation as resolved.
Show resolved Hide resolved

_, err = conn.Exec(ctx, "CREATE TABLE test (id INT PRIMARY KEY)")
suite.Require().NoError(err)
suite.T().Cleanup(func() {
if container != nil {
err := container.Terminate(ctx)
suite.Require().NoError(err)
}
})

})
bearrito marked this conversation as resolved.
Show resolved Hide resolved

}

func conn(ctx context.Context, container *cockroachdb.CockroachDBContainer) (*pgx.Conn, error) {
cfg, err := pgx.ParseConfig(container.MustConnectionString(ctx))
if err != nil {
Expand Down