Skip to content

Commit 9a9da1a

Browse files
authored
chore: do not panic in testable examples (#2193)
* chore: do not panic in testable examples * chore: ignore gocritic errors
1 parent 69b8fea commit 9a9da1a

40 files changed

+282
-248
lines changed

container_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"log"
1011
"strings"
1112
"testing"
1213
"time"
@@ -525,13 +526,13 @@ func ExampleGenericContainer_withSubstitutors() {
525526
})
526527
// }
527528
if err != nil {
528-
panic(err)
529+
log.Fatalf("could not start container: %v", err)
529530
}
530531

531532
defer func() {
532533
err := container.Terminate(ctx)
533534
if err != nil {
534-
panic(err)
535+
log.Fatalf("could not terminate container: %v", err)
535536
}
536537
}()
537538

docker_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ func ExampleDockerProvider_CreateContainer() {
987987

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

993993
fmt.Println(state.Running)
@@ -1019,7 +1019,7 @@ func ExampleContainer_Host() {
10191019

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

10251025
fmt.Println(state.Running)
@@ -1047,7 +1047,7 @@ func ExampleContainer_Start() {
10471047

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

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

10811081
fmt.Println("Container has been stopped")
@@ -1109,7 +1109,7 @@ func ExampleContainer_MappedPort() {
11091109

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

11151115
fmt.Println(state.Running)

docs/quickstart.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ func TestWithRedis(t *testing.T) {
3737
Started: true,
3838
})
3939
if err != nil {
40-
panic(err)
40+
log.Fatalf("Could not start redis: %s", err)
4141
}
4242
defer func() {
4343
if err := redisC.Terminate(ctx); err != nil {
44-
panic(err)
44+
log.Fatalf("Could not stop redis: %s", err)
4545
}
4646
}()
4747
}

from_dockerfile_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"log"
78
"strings"
89
"testing"
910
"time"
@@ -175,17 +176,17 @@ func ExampleGenericContainer_buildFromDockerfile() {
175176
})
176177
// }
177178
if err != nil {
178-
panic(err)
179+
log.Fatalf("failed to start container: %v", err)
179180
}
180181

181182
r, err := c.Logs(ctx)
182183
if err != nil {
183-
panic(err)
184+
log.Fatalf("failed to get logs: %v", err)
184185
}
185186

186187
logs, err := io.ReadAll(r)
187188
if err != nil {
188-
panic(err)
189+
log.Fatalf("failed to read logs: %v", err)
189190
}
190191

191192
fmt.Println(string(logs))

modulegen/_template/examples_test.go.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ func Example{{ $entrypoint }}() {
1414

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

2020
// Clean up the container
2121
defer func() {
2222
if err := {{ $lower }}Container.Terminate(ctx); err != nil {
23-
panic(err)
23+
log.Fatalf("failed to terminate container: %s", err)
2424
}
2525
}()
2626
// }
2727

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

3333
fmt.Println(state.Running)

modules/artemis/examples_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package artemis_test
33
import (
44
"context"
55
"fmt"
6+
"log"
67

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

@@ -19,18 +20,18 @@ func ExampleRunContainer() {
1920
artemis.WithCredentials("test", "test"),
2021
)
2122
if err != nil {
22-
panic(err)
23+
log.Fatalf("failed to start container: %s", err)
2324
}
2425
defer func() {
2526
if err := artemisContainer.Terminate(ctx); err != nil {
26-
panic(err)
27+
log.Fatalf("failed to terminate container: %s", err)
2728
}
2829
}()
2930
// }
3031

3132
state, err := artemisContainer.State(ctx)
3233
if err != nil {
33-
panic(err)
34+
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
3435
}
3536

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

4546
// containerUser {
@@ -52,11 +53,11 @@ func ExampleRunContainer() {
5253
// Connect to Artemis via STOMP.
5354
conn, err := stomp.Dial("tcp", host, stomp.ConnOpt.Login(user, pass))
5455
if err != nil {
55-
panic(err)
56+
log.Fatalf("failed to connect to Artemis: %s", err)
5657
}
5758
defer func() {
5859
if err := conn.Disconnect(); err != nil {
59-
panic(err)
60+
log.Fatalf("failed to disconnect from Artemis: %s", err)
6061
}
6162
}()
6263
// }

modules/cassandra/examples_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cassandra_test
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"path/filepath"
78

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

2728
// Clean up the container
2829
defer func() {
2930
if err := cassandraContainer.Terminate(ctx); err != nil {
30-
panic(err)
31+
log.Fatalf("failed to terminate container: %s", err)
3132
}
3233
}()
3334
// }
3435

3536
state, err := cassandraContainer.State(ctx)
3637
if err != nil {
37-
panic(err)
38+
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
3839
}
3940

4041
fmt.Println(state.Running)
4142

4243
connectionHost, err := cassandraContainer.ConnectionHost(ctx)
4344
if err != nil {
44-
panic(err)
45+
log.Fatalf("failed to get connection host: %s", err)
4546
}
4647

4748
cluster := gocql.NewCluster(connectionHost)
4849
session, err := cluster.CreateSession()
4950
if err != nil {
50-
panic(err)
51+
log.Fatalf("failed to create session: %s", err)
5152
}
5253
defer session.Close()
5354

5455
var version string
5556
err = session.Query("SELECT release_version FROM system.local").Scan(&version)
5657
if err != nil {
57-
panic(err)
58+
log.Fatalf("failed to query: %s", err)
5859
}
5960

6061
fmt.Println(version)

modules/clickhouse/examples_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package clickhouse_test
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"path/filepath"
78
"strings"
89

@@ -29,30 +30,30 @@ func ExampleRunContainer() {
2930
clickhouse.WithConfigFile(filepath.Join("testdata", "config.xml")),
3031
)
3132
if err != nil {
32-
panic(err)
33+
log.Fatalf("failed to start container: %s", err)
3334
}
3435
defer func() {
3536
if err := clickHouseContainer.Terminate(ctx); err != nil {
36-
panic(err)
37+
log.Fatalf("failed to terminate container: %s", err)
3738
}
3839
}()
3940
// }
4041

4142
state, err := clickHouseContainer.State(ctx)
4243
if err != nil {
43-
panic(err)
44+
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
4445
}
4546

4647
fmt.Println(state.Running)
4748

4849
connectionString, err := clickHouseContainer.ConnectionString(ctx)
4950
if err != nil {
50-
panic(err)
51+
log.Fatalf("failed to get connection string: %s", err)
5152
}
5253

5354
opts, err := ch.ParseDSN(connectionString)
5455
if err != nil {
55-
panic(err)
56+
log.Fatalf("failed to parse DSN: %s", err)
5657
}
5758

5859
fmt.Println(strings.HasPrefix(opts.ClientInfo.String(), "clickhouse-go/"))

modules/cockroachdb/examples_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cockroachdb_test
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"net/url"
78

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

1516
cockroachdbContainer, err := cockroachdb.RunContainer(ctx)
1617
if err != nil {
17-
panic(err)
18+
log.Fatalf("failed to start container: %s", err)
1819
}
1920

2021
// Clean up the container
2122
defer func() {
2223
if err := cockroachdbContainer.Terminate(ctx); err != nil {
23-
panic(err)
24+
log.Fatalf("failed to terminate container: %s", err)
2425
}
2526
}()
2627
// }
2728

2829
state, err := cockroachdbContainer.State(ctx)
2930
if err != nil {
30-
panic(err)
31+
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
3132
}
3233
fmt.Println(state.Running)
3334

3435
addr, err := cockroachdbContainer.ConnectionString(ctx)
3536
if err != nil {
36-
panic(err)
37+
log.Fatalf("failed to get connection string: %s", err)
3738
}
3839
u, err := url.Parse(addr)
3940
if err != nil {
40-
panic(err)
41+
log.Fatalf("failed to parse connection string: %s", err)
4142
}
4243
u.Host = fmt.Sprintf("%s:%s", u.Hostname(), "xxx")
4344
fmt.Println(u.String())

modules/couchbase/examples_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package couchbase_test
33
import (
44
"context"
55
"fmt"
6+
"log"
67

78
"github.com/couchbase/gocb/v2"
89

@@ -28,38 +29,38 @@ func ExampleRunContainer() {
2829
couchbase.WithBuckets(bucket),
2930
)
3031
if err != nil {
31-
panic(err)
32+
log.Fatalf("failed to start container: %s", err)
3233
}
3334
defer func() {
3435
if err := couchbaseContainer.Terminate(ctx); err != nil {
35-
panic(err)
36+
log.Fatalf("failed to terminate container: %s", err)
3637
}
3738
}()
3839
// }
3940

4041
state, err := couchbaseContainer.State(ctx)
4142
if err != nil {
42-
panic(err)
43+
log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
4344
}
4445

4546
fmt.Println(state.Running)
4647

4748
connectionString, err := couchbaseContainer.ConnectionString(ctx)
4849
if err != nil {
49-
panic(err)
50+
log.Fatalf("failed to get connection string: %s", err)
5051
}
5152

5253
cluster, err := gocb.Connect(connectionString, gocb.ClusterOptions{
5354
Username: couchbaseContainer.Username(),
5455
Password: couchbaseContainer.Password(),
5556
})
5657
if err != nil {
57-
panic(err)
58+
log.Fatalf("failed to connect to cluster: %s", err)
5859
}
5960

6061
buckets, err := cluster.Buckets().GetAllBuckets(nil)
6162
if err != nil {
62-
panic(err)
63+
log.Fatalf("failed to get buckets: %s", err)
6364
}
6465

6566
fmt.Println(len(buckets))

0 commit comments

Comments
 (0)