Skip to content

Commit

Permalink
chore: do not panic in testable examples (#2193)
Browse files Browse the repository at this point in the history
* chore: do not panic in testable examples

* chore: ignore gocritic errors
  • Loading branch information
mdelapenya authored Feb 2, 2024
1 parent 69b8fea commit 9a9da1a
Show file tree
Hide file tree
Showing 40 changed files with 282 additions and 248 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
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
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
11 changes: 6 additions & 5 deletions modules/clickhouse/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clickhouse_test
import (
"context"
"fmt"
"log"
"path/filepath"
"strings"

Expand All @@ -29,30 +30,30 @@ func ExampleRunContainer() {
clickhouse.WithConfigFile(filepath.Join("testdata", "config.xml")),
)
if err != nil {
panic(err)
log.Fatalf("failed to start container: %s", err)
}
defer func() {
if err := clickHouseContainer.Terminate(ctx); err != nil {
panic(err)
log.Fatalf("failed to terminate container: %s", err)
}
}()
// }

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

fmt.Println(state.Running)

connectionString, err := clickHouseContainer.ConnectionString(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get connection string: %s", err)
}

opts, err := ch.ParseDSN(connectionString)
if err != nil {
panic(err)
log.Fatalf("failed to parse DSN: %s", err)
}

fmt.Println(strings.HasPrefix(opts.ClientInfo.String(), "clickhouse-go/"))
Expand Down
11 changes: 6 additions & 5 deletions modules/cockroachdb/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cockroachdb_test
import (
"context"
"fmt"
"log"
"net/url"

"github.com/testcontainers/testcontainers-go/modules/cockroachdb"
Expand All @@ -14,30 +15,30 @@ func ExampleRunContainer() {

cockroachdbContainer, err := cockroachdb.RunContainer(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to start container: %s", err)
}

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

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

addr, err := cockroachdbContainer.ConnectionString(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get connection string: %s", err)
}
u, err := url.Parse(addr)
if err != nil {
panic(err)
log.Fatalf("failed to parse connection string: %s", err)
}
u.Host = fmt.Sprintf("%s:%s", u.Hostname(), "xxx")
fmt.Println(u.String())
Expand Down
13 changes: 7 additions & 6 deletions modules/couchbase/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package couchbase_test
import (
"context"
"fmt"
"log"

"github.com/couchbase/gocb/v2"

Expand All @@ -28,38 +29,38 @@ func ExampleRunContainer() {
couchbase.WithBuckets(bucket),
)
if err != nil {
panic(err)
log.Fatalf("failed to start container: %s", err)
}
defer func() {
if err := couchbaseContainer.Terminate(ctx); err != nil {
panic(err)
log.Fatalf("failed to terminate container: %s", err)
}
}()
// }

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

fmt.Println(state.Running)

connectionString, err := couchbaseContainer.ConnectionString(ctx)
if err != nil {
panic(err)
log.Fatalf("failed to get connection string: %s", err)
}

cluster, err := gocb.Connect(connectionString, gocb.ClusterOptions{
Username: couchbaseContainer.Username(),
Password: couchbaseContainer.Password(),
})
if err != nil {
panic(err)
log.Fatalf("failed to connect to cluster: %s", err)
}

buckets, err := cluster.Buckets().GetAllBuckets(nil)
if err != nil {
panic(err)
log.Fatalf("failed to get buckets: %s", err)
}

fmt.Println(len(buckets))
Expand Down
Loading

0 comments on commit 9a9da1a

Please sign in to comment.