@@ -20,14 +20,17 @@ import (
2020 "github.com/twmb/franz-go/pkg/sasl/scram"
2121
2222 "github.com/testcontainers/testcontainers-go"
23+ "github.com/testcontainers/testcontainers-go/log"
2324 "github.com/testcontainers/testcontainers-go/modules/redpanda"
2425 "github.com/testcontainers/testcontainers-go/network"
2526)
2627
28+ const testImage = "docker.redpanda.com/redpandadata/redpanda:v23.3.3"
29+
2730func TestRedpanda (t * testing.T ) {
2831 ctx := context .Background ()
2932
30- ctr , err := redpanda .Run (ctx , "docker.redpanda.com/redpandadata/redpanda:v23.3.3" )
33+ ctr , err := redpanda .Run (ctx , testImage )
3134 testcontainers .CleanupContainer (t , ctr )
3235 require .NoError (t , err )
3336
@@ -78,7 +81,7 @@ func TestRedpandaWithAuthentication(t *testing.T) {
7881 ctx := context .Background ()
7982 // redpandaCreateContainer {
8083 ctr , err := redpanda .Run (ctx ,
81- "docker.redpanda.com/redpandadata/redpanda:v23.3.3" ,
84+ testImage ,
8285 redpanda .WithEnableSASL (),
8386 redpanda .WithEnableKafkaAuthorization (),
8487 redpanda .WithEnableWasmTransform (),
@@ -192,7 +195,7 @@ func TestRedpandaWithAuthentication(t *testing.T) {
192195func TestRedpandaWithBootstrapUserAuthentication (t * testing.T ) {
193196 ctx := context .Background ()
194197 ctr , err := redpanda .Run (ctx ,
195- "docker.redpanda.com/redpandadata/redpanda:v23.3.3" ,
198+ testImage ,
196199 redpanda .WithEnableSASL (),
197200 redpanda .WithEnableKafkaAuthorization (),
198201 redpanda .WithEnableWasmTransform (),
@@ -427,7 +430,7 @@ func TestRedpandaWithOldVersionAndWasm(t *testing.T) {
427430func TestRedpandaProduceWithAutoCreateTopics (t * testing.T ) {
428431 ctx := context .Background ()
429432
430- ctr , err := redpanda .Run (ctx , "docker.redpanda.com/redpandadata/redpanda:v23.3.3" , redpanda .WithAutoCreateTopics ())
433+ ctr , err := redpanda .Run (ctx , testImage , redpanda .WithAutoCreateTopics ())
431434 testcontainers .CleanupContainer (t , ctr )
432435 require .NoError (t , err )
433436
@@ -446,17 +449,17 @@ func TestRedpandaProduceWithAutoCreateTopics(t *testing.T) {
446449}
447450
448451func TestRedpandaWithTLS (t * testing.T ) {
449- tmp := t .TempDir ()
450- cert := tlscert .SelfSignedFromRequest (tlscert.Request {
451- Name : "client" ,
452- Host : "localhost,127.0.0.1" ,
453- ParentDir : tmp ,
454- })
455- require .NotNil (t , cert , "failed to generate cert" )
456-
457452 ctx := context .Background ()
458453
459- ctr , err := redpanda .Run (ctx , "docker.redpanda.com/redpandadata/redpanda:v23.3.3" , redpanda .WithTLS (cert .Bytes , cert .KeyBytes ))
454+ containerHostAddress , err := containerHost (ctx )
455+ require .NoError (t , err )
456+ cert , err := tlscert .SelfSignedFromRequestE (tlscert.Request {
457+ Name : "client" ,
458+ Host : "localhost,127.0.0.1," + containerHostAddress ,
459+ })
460+ require .NoError (t , err , "failed to generate cert" )
461+
462+ ctr , err := redpanda .Run (ctx , testImage , redpanda .WithTLS (cert .Bytes , cert .KeyBytes ))
460463 testcontainers .CleanupContainer (t , ctr )
461464 require .NoError (t , err )
462465
@@ -509,19 +512,18 @@ func TestRedpandaWithTLS(t *testing.T) {
509512}
510513
511514func TestRedpandaWithTLSAndSASL (t * testing.T ) {
512- tmp := t . TempDir ()
515+ ctx := context . Background ()
513516
514- cert := tlscert .SelfSignedFromRequest (tlscert.Request {
515- Name : "client" ,
516- Host : "localhost,127.0.0.1" ,
517- ParentDir : tmp ,
517+ containerHostAddress , err := containerHost (ctx )
518+ require .NoError (t , err )
519+ cert , err := tlscert .SelfSignedFromRequestE (tlscert.Request {
520+ Name : "client" ,
521+ Host : "localhost,127.0.0.1," + containerHostAddress ,
518522 })
519- require .NotNil (t , cert , "failed to generate cert" )
520-
521- ctx := context .Background ()
523+ require .NoError (t , err , "failed to generate cert" )
522524
523525 ctr , err := redpanda .Run (ctx ,
524- "docker.redpanda.com/redpandadata/redpanda:v23.3.3" ,
526+ testImage ,
525527 redpanda .WithTLS (cert .Bytes , cert .KeyBytes ),
526528 redpanda .WithEnableSASL (),
527529 redpanda .WithEnableKafkaAuthorization (),
@@ -688,3 +690,29 @@ func TestRedpandaBootstrapConfig(t *testing.T) {
688690 require .False (t , needsRestart )
689691 }
690692}
693+
694+ func containerHost (ctx context.Context , opts ... testcontainers.ContainerCustomizer ) (string , error ) {
695+ // Use a dummy request to get the provider from options.
696+ var req testcontainers.GenericContainerRequest
697+ for _ , opt := range opts {
698+ if err := opt .Customize (& req ); err != nil {
699+ return "" , err
700+ }
701+ }
702+
703+ logging := req .Logger
704+ if logging == nil {
705+ logging = log .Default ()
706+ }
707+ p , err := req .ProviderType .GetProvider (testcontainers .WithLogger (logging ))
708+ if err != nil {
709+ return "" , err
710+ }
711+
712+ if p , ok := p .(* testcontainers.DockerProvider ); ok {
713+ return p .DaemonHost (ctx )
714+ }
715+
716+ // Fall back to localhost.
717+ return "localhost" , nil
718+ }
0 commit comments