Skip to content

Commit

Permalink
docs: use Go testable examples in modules (testcontainers#1603)
Browse files Browse the repository at this point in the history
* fix: rename vault container in tests

* chore: adjust comment in template

* docs(vault): use RunContainer example in usage section

* docs: update vault docs

* docs: add RunContainer example for redpanda

* docs: add RunContainer example for redis

* docs: document creating networks

* docs: add RunContainer example for pulsar

* docs: add RunContainer example for postgres

* docs: add RunContainer example for neo4j

* docs: add RunContainer example for nats

* docs: update quickstart to not use testing libraries

* docs: add RunContainer example for mysql

* docs: add RunContainer example for mongo

* docs: add RunContainer example for mariadb

* chore: use logger to print out hostname external reason

* docs: document new Go examples file

* docs: add RunContainer example for localstack

* chore: convert k3s test into a testable example

* chore: convert mongodb test into testable example

* fix: handle errors in example tests for elasticsearch

* fix: do not deprecate used fields

They are not directly called, but the customisers are populating their values

* chore: convert couchbase test into testable example

* chore: convert clickhouse test into testable example

* docs: adjust artemis docs

* chore: simplify postgres example

* fix: lint

* chore(deps): bump github.com/hashicorp/vault-client-go in /modules/vault (testcontainers#1566)

* chore(deps): bump github.com/hashicorp/vault-client-go in /modules/vault

Bumps [github.com/hashicorp/vault-client-go](https://github.com/hashicorp/vault-client-go) from 0.2.0 to 0.3.3.
- [Release notes](https://github.com/hashicorp/vault-client-go/releases)
- [Changelog](https://github.com/hashicorp/vault-client-go/blob/main/CHANGELOG.md)
- [Commits](hashicorp/vault-client-go@v0.2.0...v0.3.3)

---
updated-dependencies:
- dependency-name: github.com/hashicorp/vault-client-go
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* up

* Update vault_test.go

* Update vault_test.go

* Update vault_test.go

* Update vault_test.go

* Update vault_test.go

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Manuel de la Peña <[email protected]>

* ci(lint): enable gocritic linter (testcontainers#1605)

Signed-off-by: Matthieu MOREL <[email protected]>

* ci(lint): enable errorlint linter (testcontainers#1604)

* ci(lint): enable errorlint linter

Signed-off-by: Matthieu MOREL <[email protected]>

* Apply suggestions from code review

Co-authored-by: Manuel de la Peña <[email protected]>

---------

Signed-off-by: Matthieu MOREL <[email protected]>
Co-authored-by: Manuel de la Peña <[email protected]>

* fix: handle errors

* chore(pulsar): create/remove the network properly

* chore: better container logs on errors during startup

* Revert "chore: better container logs on errors during startup"

This reverts commit 0e28881.

* chore: better container logs on errors during startup

* fix: do not wrap error but print it

* fix: avoid polluting default wait strategies in pulsar

---------

Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: Matthieu MOREL <[email protected]>
Co-authored-by: Matthieu MOREL <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 11, 2023
1 parent c2b4bdc commit 7d665b8
Showing 1 changed file with 50 additions and 19 deletions.
69 changes: 50 additions & 19 deletions mongodb_test.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,78 @@
package mongodb
package mongodb_test

import (
"context"
"fmt"
"testing"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mongodb"
)

func TestMongoDB(t *testing.T) {
func ExampleRunContainer() {
// runMongoDBContainer {
ctx := context.Background()

// createMongoDBContainer {
container, err := RunContainer(ctx)
mongodbContainer, err := mongodb.RunContainer(ctx, testcontainers.WithImage("mongo:6"))
if err != nil {
t.Fatal(err)
panic(err)
}
// }

// Clean up the container after the test is complete
t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
// Clean up the container
defer func() {
if err := mongodbContainer.Terminate(ctx); err != nil {
panic(err)
}
})
}()
// }

state, err := mongodbContainer.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

// perform assertions
// Output:
// true
}

func ExampleRunContainer_connect() {
// connectToMongo {
ctx := context.Background()

// connectionString {
endpoint, err := container.ConnectionString(ctx)
mongodbContainer, err := mongodb.RunContainer(ctx, testcontainers.WithImage("mongo:6"))
if err != nil {
t.Error(fmt.Errorf("failed to get connection string: %w", err))
panic(err)
}

// Clean up the container
defer func() {
if err := mongodbContainer.Terminate(ctx); err != nil {
panic(err)
}
}()

endpoint, err := mongodbContainer.ConnectionString(ctx)
if err != nil {
panic(err)
}
// }

mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(endpoint))
if err != nil {
t.Fatal(fmt.Errorf("error creating mongo client: %w", err))
panic(err)
}
// }

err = mongoClient.Ping(ctx, nil)
if err != nil {
t.Fatal(fmt.Errorf("error pinging mongo: %w", err))
panic(err)
}

fmt.Println(mongoClient.Database("test").Name())

// Output:
// test
}

0 comments on commit 7d665b8

Please sign in to comment.