Skip to content

feat(couchbase): adding auth to couchbase initCluster functions to support container reuse #3048

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

Merged

Conversation

waroir20
Copy link
Contributor

@waroir20 waroir20 commented Mar 19, 2025

What does this PR do?

This PR aims to enable a created couchbase container to be reused between test packages, and accomplishes this by passing the basic auth header with every request made to the container's management ports.

Why is it important?

Since Go's test context creates physically separated binaries between packages, re-using a created container directly using the testcontainer's framework is impossible; so to accomplish the same thing utilizing testcontainer's framework we can configure the GenericContainer with a fixed container name. This fixed container name then allows other test packages within an application to reference the already created container minimizing resource consumption and repeated start up cost.

Currently in applications with lots of tests/slow running tests this singleton couchbase container starts up and runs just fine when tests attempt to create their own container via Run but once the Admin user has been enabled by the first test, all subsequent tests fail with "init cluster: context deadline exceeded" because the http requests are failing with 401.

Related Issues

Follow-ups

It is likely that other implementations of testcontainers for other common images will have a similar issue when the container is reused.

@waroir20 waroir20 requested a review from a team as a code owner March 19, 2025 02:42
Copy link

netlify bot commented Mar 19, 2025

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit aba0a34
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-go/deploys/68397abf5c697c00084e2d45
😎 Deploy Preview https://deploy-preview-3048--testcontainers-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@waroir20 waroir20 changed the title feature | Adding auth to couchbase initCluster functions to support container reuse feat | Adding auth to couchbase initCluster functions to support container reuse Mar 19, 2025
@waroir20 waroir20 changed the title feat | Adding auth to couchbase initCluster functions to support container reuse feat: Adding auth to couchbase initCluster functions to support container reuse Mar 19, 2025
@waroir20 waroir20 changed the title feat: Adding auth to couchbase initCluster functions to support container reuse feat: adding auth to couchbase initCluster functions to support container reuse Mar 19, 2025
@waroir20 waroir20 changed the title feat: adding auth to couchbase initCluster functions to support container reuse feat(couchbase): adding auth to couchbase initCluster functions to support container reuse Mar 19, 2025
@eddumelendez
Copy link
Member

We had a similar issue in Testcontainers for Java and was fixed by skipping the configuration when reuse is enabled testcontainers/testcontainers-java#9957

@waroir20
Copy link
Contributor Author

We had a similar issue in Testcontainers for Java and was fixed by skipping the configuration when reuse is enabled testcontainers/testcontainers-java#9957

How would you handle the issue of not knowing which of the calls would be the one to be the first to cause the initialization, as well as returning the container too early in ones where is was skipped (i.e. before the configuration occurred)?

mdelapenya added 4 commits May 7, 2025 05:09
* main: (76 commits)
  chore(deps): bump mkdocs-include-markdown-plugin from 6.2.2 to 7.1.5 (testcontainers#3137)
  chore(deps): bump github.com/shirou/gopsutil/v4 from 4.25.1 to 4.25.4 (testcontainers#3133)
  chore(deps): bump github.com/docker/docker from 28.0.1+incompatible to 28.1.1+incompatible (testcontainers#3152)
  feat(memcached): add memcached module (testcontainers#3132)
  fix(etcd): single node etcd cluster access (testcontainers#3149)
  feat(valkey): add TLS support for Valkey (testcontainers#3131)
  fix(dockermodelrunner): wait for the model to be pulled (testcontainers#3125)
  fix(localstack): remove checksum before parsing version (testcontainers#3130)
  fix(dockermodelrunner): dependency with socat
  chore: prepare for next minor development cycle (0.38.0)
  chore: use new version (v0.37.0) in modules and examples
  fix: handle stopped containers more gracefully when reuse is enabled (testcontainers#3062)
  feat(gcloud): add option to run firestore in datastore mode (testcontainers#3009)
  feat: support for mounting images (testcontainers#3044)
  chore(ci): close PR if it was sent from main (testcontainers#3123)
  feat: add `WithReuseByName` for modifying Generic Container Requests (testcontainers#3064)
  chore(deps): bump github/codeql-action from 3.28.15 to 3.28.16 (testcontainers#3120)
  chore(deps): bump mkdocs-include-markdown-plugin from 6.2.2 to 7.1.5 (testcontainers#3119)
  chore(deps): bump github.com/magiconair/properties from 1.8.9 to 1.8.10 (testcontainers#3118)
  chore(ci): exclude more files for a full-blown build (testcontainers#3122)
  ...
* main:
  feat: support adding wait strategies as functional option (testcontainers#3161)
  fix(etcd): expose ports for the etcd nodes (testcontainers#3162)
  fix(wait): no port to wait for (testcontainers#3158)
  feat: add more functional options for customising containers (testcontainers#3156)
  docs(redpanda): update sasl authentication option to use scram sha 256 (testcontainers#3126)
Copy link
Member

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@mdelapenya
Copy link
Member

@waroir20 I've already approved this PR, although I'd like you to take a look at the current initCluster function, which is executed after the container is started. Do you think it could be converted into parts of the life cycle hooks of the container? Then it would be easier to skip parts of the life cycle hooks when reuse is active, and possible change this behaviour:

https://github.com/testcontainers/testcontainers-go/blob/main/docker.go#L1367-L1382

	// If a container was stopped programmatically, we want to ensure the container
	// is running again, but only if it is not paused, as it's not possible to start
	// a paused container. The Docker Engine returns the "cannot start a paused container,
	// try unpause instead" error.
	switch c.State {
	case "running":
		// cannot re-start a running container, but we still need
		// to call the startup hooks.
	case "paused":
		// TODO: we should unpause the container here.
		return nil, fmt.Errorf("cannot start a paused container: %w", errors.ErrUnsupported)
	default:
		if err := dc.Start(ctx); err != nil {
			return dc, fmt.Errorf("start container %s in state %s: %w", req.Name, c.State, err)
		}
	}

Tell me what you think, and we could maybe merge this as is, and work on the refactor as a follow-up

Thanks!

@mdelapenya mdelapenya self-assigned this May 30, 2025
@mdelapenya mdelapenya added the feature New functionality or new behaviors on the existing one label May 30, 2025
@mdelapenya mdelapenya merged commit b6461e8 into testcontainers:main May 30, 2025
16 checks passed
@mdelapenya
Copy link
Member

Merged, thanks for your work here! Just in case you have time and are interested, I'd like you to check my comment here 🙏

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or new behaviors on the existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants