@@ -24,10 +24,12 @@ import (
2424 "github.com/testcontainers/testcontainers-go/network"
2525)
2626
27+ const testImage = "docker.redpanda.com/redpandadata/redpanda:v23.3.3"
28+
2729func TestRedpanda (t * testing.T ) {
2830 ctx := context .Background ()
2931
30- ctr , err := redpanda .Run (ctx , "docker.redpanda.com/redpandadata/redpanda:v23.3.3" )
32+ ctr , err := redpanda .Run (ctx , testImage )
3133 testcontainers .CleanupContainer (t , ctr )
3234 require .NoError (t , err )
3335
@@ -78,7 +80,7 @@ func TestRedpandaWithAuthentication(t *testing.T) {
7880 ctx := context .Background ()
7981 // redpandaCreateContainer {
8082 ctr , err := redpanda .Run (ctx ,
81- "docker.redpanda.com/redpandadata/redpanda:v23.3.3" ,
83+ testImage ,
8284 redpanda .WithEnableSASL (),
8385 redpanda .WithEnableKafkaAuthorization (),
8486 redpanda .WithEnableWasmTransform (),
@@ -192,7 +194,7 @@ func TestRedpandaWithAuthentication(t *testing.T) {
192194func TestRedpandaWithBootstrapUserAuthentication (t * testing.T ) {
193195 ctx := context .Background ()
194196 ctr , err := redpanda .Run (ctx ,
195- "docker.redpanda.com/redpandadata/redpanda:v23.3.3" ,
197+ testImage ,
196198 redpanda .WithEnableSASL (),
197199 redpanda .WithEnableKafkaAuthorization (),
198200 redpanda .WithEnableWasmTransform (),
@@ -427,7 +429,7 @@ func TestRedpandaWithOldVersionAndWasm(t *testing.T) {
427429func TestRedpandaProduceWithAutoCreateTopics (t * testing.T ) {
428430 ctx := context .Background ()
429431
430- ctr , err := redpanda .Run (ctx , "docker.redpanda.com/redpandadata/redpanda:v23.3.3" , redpanda .WithAutoCreateTopics ())
432+ ctr , err := redpanda .Run (ctx , testImage , redpanda .WithAutoCreateTopics ())
431433 testcontainers .CleanupContainer (t , ctr )
432434 require .NoError (t , err )
433435
@@ -446,17 +448,20 @@ func TestRedpandaProduceWithAutoCreateTopics(t *testing.T) {
446448}
447449
448450func TestRedpandaWithTLS (t * testing.T ) {
451+ ctx := context .Background ()
452+
453+ containerHostAddress , err := detectContainerHostAddress (testImage , ctx )
454+ require .NoError (t , err , "failed to detect container host address" )
455+
449456 tmp := t .TempDir ()
450457 cert := tlscert .SelfSignedFromRequest (tlscert.Request {
451458 Name : "client" ,
452- Host : "localhost,127.0.0.1" ,
459+ Host : "localhost,127.0.0.1," + containerHostAddress ,
453460 ParentDir : tmp ,
454461 })
455462 require .NotNil (t , cert , "failed to generate cert" )
456463
457- ctx := context .Background ()
458-
459- ctr , err := redpanda .Run (ctx , "docker.redpanda.com/redpandadata/redpanda:v23.3.3" , redpanda .WithTLS (cert .Bytes , cert .KeyBytes ))
464+ ctr , err := redpanda .Run (ctx , testImage , redpanda .WithTLS (cert .Bytes , cert .KeyBytes ))
460465 testcontainers .CleanupContainer (t , ctr )
461466 require .NoError (t , err )
462467
@@ -509,19 +514,21 @@ func TestRedpandaWithTLS(t *testing.T) {
509514}
510515
511516func TestRedpandaWithTLSAndSASL (t * testing.T ) {
512- tmp := t . TempDir ()
517+ ctx := context . Background ()
513518
519+ containerHostAddress , err := detectContainerHostAddress (testImage , ctx )
520+ require .NoError (t , err , "failed to detect container host address" )
521+
522+ tmp := t .TempDir ()
514523 cert := tlscert .SelfSignedFromRequest (tlscert.Request {
515524 Name : "client" ,
516- Host : "localhost,127.0.0.1" ,
525+ Host : "localhost,127.0.0.1," + containerHostAddress ,
517526 ParentDir : tmp ,
518527 })
519528 require .NotNil (t , cert , "failed to generate cert" )
520529
521- ctx := context .Background ()
522-
523530 ctr , err := redpanda .Run (ctx ,
524- "docker.redpanda.com/redpandadata/redpanda:v23.3.3" ,
531+ testImage ,
525532 redpanda .WithTLS (cert .Bytes , cert .KeyBytes ),
526533 redpanda .WithEnableSASL (),
527534 redpanda .WithEnableKafkaAuthorization (),
@@ -698,3 +705,23 @@ func TestRedpandaBootstrapConfig(t *testing.T) {
698705 require .False (t , needsRestart )
699706 }
700707}
708+
709+ func detectContainerHostAddress (image string , ctx context.Context ) (string , error ) {
710+ c , err := testcontainers .GenericContainer (ctx , testcontainers.GenericContainerRequest {
711+ ContainerRequest : testcontainers.ContainerRequest {
712+ Image : image ,
713+ },
714+ Started : false ,
715+ })
716+ if err != nil {
717+ return "" , fmt .Errorf ("create container: %w" , err )
718+ }
719+ defer func () {
720+ _ = c .Terminate (ctx )
721+ }()
722+ addr , err := c .Host (ctx )
723+ if err != nil {
724+ return "" , fmt .Errorf ("host: %w" , err )
725+ }
726+ return addr , nil
727+ }
0 commit comments