Skip to content

Commit

Permalink
Merge branch 'main' into feature/add-redpanda-wasm-transform
Browse files Browse the repository at this point in the history
* main:
  feat(modules.clickhouse): Add zookeeper for clickhouse clusterization (testcontainers#1995)
  redpanda: allow using SASL and TLS together (testcontainers#2140)
  chore: do not panic in testable examples (testcontainers#2193)
  fix: all mounts should contain the testcontainers labels (testcontainers#2191)
  [redpanda] sasl test for wrong mechanism (testcontainers#2048)
  fix: deprecate BindMounts correctly (testcontainers#2190)
  • Loading branch information
mdelapenya committed Feb 2, 2024
2 parents ee5e2c5 + bc67dc3 commit 6495231
Show file tree
Hide file tree
Showing 50 changed files with 648 additions and 280 deletions.
5 changes: 3 additions & 2 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"log"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -525,13 +526,13 @@ func ExampleGenericContainer_withSubstitutors() {
})
// }
if err != nil {
panic(err)
log.Fatalf("could not start container: %v", err)
}

defer func() {
err := container.Terminate(ctx)
if err != nil {
panic(err)
log.Fatalf("could not terminate container: %v", err)
}
}()

Expand Down
4 changes: 4 additions & 0 deletions docker_mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testcontainers
import "github.com/docker/docker/api/types/mount"

var mountTypeMapping = map[MountType]mount.Type{
MountTypeBind: mount.TypeBind, // Deprecated, it will be removed in a future release
MountTypeVolume: mount.TypeVolume,
MountTypeTmpfs: mount.TypeTmpfs,
MountTypePipe: mount.TypeNamedPipe,
Expand Down Expand Up @@ -100,6 +101,9 @@ func mapToDockerMounts(containerMounts ContainerMounts) []mount.Mount {
Source: m.Source.Source(),
ReadOnly: m.ReadOnly,
Target: m.Target.Target(),
VolumeOptions: &mount.VolumeOptions{
Labels: GenericLabels(),
},
}

switch typedMounter := m.Source.(type) {
Expand Down
10 changes: 5 additions & 5 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ func ExampleDockerProvider_CreateContainer() {

state, err := nginxC.State(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
}

fmt.Println(state.Running)
Expand Down Expand Up @@ -1019,7 +1019,7 @@ func ExampleContainer_Host() {

state, err := nginxC.State(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
}

fmt.Println(state.Running)
Expand Down Expand Up @@ -1047,7 +1047,7 @@ func ExampleContainer_Start() {

state, err := nginxC.State(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
}

fmt.Println(state.Running)
Expand Down Expand Up @@ -1075,7 +1075,7 @@ func ExampleContainer_Stop() {
timeout := 10 * time.Second
err := nginxC.Stop(ctx, &timeout)
if err != nil {
panic(err)
log.Fatalf("failed to stop container: %s", err) // nolint:gocritic
}

fmt.Println("Container has been stopped")
Expand Down Expand Up @@ -1109,7 +1109,7 @@ func ExampleContainer_MappedPort() {

state, err := nginxC.State(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
}

fmt.Println(state.Running)
Expand Down
24 changes: 12 additions & 12 deletions docs/features/docker_compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tc "github.com/testcontainers/testcontainers-go/modules/compose"
)

func TestSomething(t *testing.T) {
compose, err := tc.NewDockerCompose("testdata/docker-compose.yml")
assert.NoError(t, err, "NewDockerComposeAPI()")
require.NoError(t, err, "NewDockerComposeAPI()")

t.Cleanup(func() {
assert.NoError(t, compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal), "compose.Down()")
require.NoError(t, compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal), "compose.Down()")
})

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

assert.NoError(t, compose.Up(ctx, tc.Wait(true)), "compose.Up()")
require.NoError(t, compose.Up(ctx, tc.Wait(true)), "compose.Up()")

// do some testing here
}
Expand All @@ -62,23 +62,23 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tc "github.com/testcontainers/testcontainers-go/modules/compose"
)

func TestSomethingElse(t *testing.T) {
identifier := tc.StackIdentifier("some_ident")
compose, err := tc.NewDockerComposeWith(tc.WithStackFiles("./testdata/docker-compose-simple.yml"), identifier)
assert.NoError(t, err, "NewDockerComposeAPIWith()")
require.NoError(t, err, "NewDockerComposeAPIWith()")

t.Cleanup(func() {
assert.NoError(t, compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal), "compose.Down()")
require.NoError(t, compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal), "compose.Down()")
})

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

assert.NoError(t, compose.Up(ctx, tc.Wait(true)), "compose.Up()")
require.NoError(t, compose.Up(ctx, tc.Wait(true)), "compose.Up()")

// do some testing here
}
Expand Down Expand Up @@ -110,18 +110,18 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tc "github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/testcontainers/testcontainers-go/wait"
)

func TestSomethingWithWaiting(t *testing.T) {
identifier := tc.StackIdentifier("some_ident")
compose, err := tc.NewDockerComposeWith(tc.WithStackFiles("./testdata/docker-compose-simple.yml"), identifier)
assert.NoError(t, err, "NewDockerComposeAPIWith()")
require.NoError(t, err, "NewDockerComposeAPIWith()")

t.Cleanup(func() {
assert.NoError(t, compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal), "compose.Down()")
require.NoError(t, compose.Down(context.Background(), tc.RemoveOrphans(true), tc.RemoveImagesLocal), "compose.Down()")
})

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -131,7 +131,7 @@ func TestSomethingWithWaiting(t *testing.T) {
WaitForService("nginx", wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).
Up(ctx, tc.Wait(true))

assert.NoError(t, err, "compose.Up()")
require.NoError(t, err, "compose.Up()")

// do some testing here
}
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/clickhouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ initialization before starting the service.
[Init script content](../../modules/clickhouse/testdata/init-db.sh)
<!--/codeinclude-->
#### Zookeeper
Clusterized ClickHouse requires to start Zookeeper and pass link to it via `config.xml`.
<!--codeinclude-->
[Include zookeeper](../../modules/clickhouse/clickhouse_test.go) inside_block:withZookeeper
<!--/codeinclude-->
!!!warning
The `WithZookeeper` option will `panic` if it's not possible to create the Zookeeper config file.

#### Custom configuration

If you need to set a custom configuration, the module provides the `WithConfigFile` option to pass the path to a custom configuration file in XML format.
Expand Down
4 changes: 2 additions & 2 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func TestWithRedis(t *testing.T) {
Started: true,
})
if err != nil {
panic(err)
log.Fatalf("Could not start redis: %s", err)
}
defer func() {
if err := redisC.Terminate(ctx); err != nil {
panic(err)
log.Fatalf("Could not stop redis: %s", err)
}
}()
}
Expand Down
7 changes: 4 additions & 3 deletions from_dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"log"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -175,17 +176,17 @@ func ExampleGenericContainer_buildFromDockerfile() {
})
// }
if err != nil {
panic(err)
log.Fatalf("failed to start container: %v", err)
}

r, err := c.Logs(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get logs: %v", err)
}

logs, err := io.ReadAll(r)
if err != nil {
panic(err)
log.Fatalf("failed to read logs: %v", err)
}

fmt.Println(string(logs))
Expand Down
6 changes: 3 additions & 3 deletions modulegen/_template/examples_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ func Example{{ $entrypoint }}() {

{{ $lower }}Container, err := {{ $lower }}.{{ $entrypoint }}(ctx, testcontainers.WithImage("{{ $image }}"))
if err != nil {
panic(err)
log.Fatalf("failed to start container: %s", err)
}

// Clean up the container
defer func() {
if err := {{ $lower }}Container.Terminate(ctx); err != nil {
panic(err)
log.Fatalf("failed to terminate container: %s", err)
}
}()
// }

state, err := {{ $lower }}Container.State(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get container state: %s", err)
}

fmt.Println(state.Running)
Expand Down
13 changes: 7 additions & 6 deletions modules/artemis/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package artemis_test
import (
"context"
"fmt"
"log"

"github.com/go-stomp/stomp/v3"

Expand All @@ -19,18 +20,18 @@ func ExampleRunContainer() {
artemis.WithCredentials("test", "test"),
)
if err != nil {
panic(err)
log.Fatalf("failed to start container: %s", err)
}
defer func() {
if err := artemisContainer.Terminate(ctx); err != nil {
panic(err)
log.Fatalf("failed to terminate container: %s", err)
}
}()
// }

state, err := artemisContainer.State(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
}

fmt.Println(state.Running)
Expand All @@ -39,7 +40,7 @@ func ExampleRunContainer() {
// Get broker endpoint.
host, err := artemisContainer.BrokerEndpoint(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get broker endpoint: %s", err)
}

// containerUser {
Expand All @@ -52,11 +53,11 @@ func ExampleRunContainer() {
// Connect to Artemis via STOMP.
conn, err := stomp.Dial("tcp", host, stomp.ConnOpt.Login(user, pass))
if err != nil {
panic(err)
log.Fatalf("failed to connect to Artemis: %s", err)
}
defer func() {
if err := conn.Disconnect(); err != nil {
panic(err)
log.Fatalf("failed to disconnect from Artemis: %s", err)
}
}()
// }
Expand Down
13 changes: 7 additions & 6 deletions modules/cassandra/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cassandra_test
import (
"context"
"fmt"
"log"
"path/filepath"

"github.com/gocql/gocql"
Expand All @@ -21,40 +22,40 @@ func ExampleRunContainer() {
cassandra.WithConfigFile(filepath.Join("testdata", "config.yaml")),
)
if err != nil {
panic(err)
log.Fatalf("failed to start container: %s", err)
}

// Clean up the container
defer func() {
if err := cassandraContainer.Terminate(ctx); err != nil {
panic(err)
log.Fatalf("failed to terminate container: %s", err)
}
}()
// }

state, err := cassandraContainer.State(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
}

fmt.Println(state.Running)

connectionHost, err := cassandraContainer.ConnectionHost(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get connection host: %s", err)
}

cluster := gocql.NewCluster(connectionHost)
session, err := cluster.CreateSession()
if err != nil {
panic(err)
log.Fatalf("failed to create session: %s", err)
}
defer session.Close()

var version string
err = session.Query("SELECT release_version FROM system.local").Scan(&version)
if err != nil {
panic(err)
log.Fatalf("failed to query: %s", err)
}

fmt.Println(version)
Expand Down
Loading

0 comments on commit 6495231

Please sign in to comment.