@@ -2,99 +2,127 @@ import fs from "fs";
22import path from "path" ;
33import { GenericContainer , Network } from "testcontainers" ;
44import { KafkaContainer } from "./kafka-container" ;
5- import { testPubSub } from "./test-helper" ;
5+ import { assertMessageProducedAndConsumed } from "./test-helper" ;
66
77const IMAGE = "confluentinc/cp-kafka:7.9.1" ;
88
99describe ( "KafkaContainer" , { timeout : 240_000 } , ( ) => {
10- // connectBuiltInZK {
1110 it ( "should connect using in-built zoo-keeper" , async ( ) => {
12- await using kafkaContainer = await new KafkaContainer ( IMAGE ) . start ( ) ;
13-
14- await testPubSub ( kafkaContainer ) ;
11+ // connectBuiltInZK {
12+ await using container = await new KafkaContainer ( IMAGE ) . start ( ) ;
13+ await assertMessageProducedAndConsumed ( container ) ;
14+ // }
1515 } ) ;
16- // }
1716
1817 it ( "should connect using in-built zoo-keeper and custom images" , async ( ) => {
19- await using kafkaContainer = await new KafkaContainer ( IMAGE ) . start ( ) ;
18+ await using container = await new KafkaContainer ( IMAGE ) . start ( ) ;
2019
21- await testPubSub ( kafkaContainer ) ;
20+ await assertMessageProducedAndConsumed ( container ) ;
2221 } ) ;
2322
2423 it ( "should connect using in-built zoo-keeper and custom network" , async ( ) => {
2524 await using network = await new Network ( ) . start ( ) ;
2625
27- await using kafkaContainer = await new KafkaContainer ( IMAGE ) . withNetwork ( network ) . start ( ) ;
26+ await using container = await new KafkaContainer ( IMAGE ) . withNetwork ( network ) . start ( ) ;
2827
29- await testPubSub ( kafkaContainer ) ;
28+ await assertMessageProducedAndConsumed ( container ) ;
3029 } ) ;
3130
32- // connectProvidedZK {
3331 it ( "should connect using provided zoo-keeper and network" , async ( ) => {
32+ // connectProvidedZK {
3433 await using network = await new Network ( ) . start ( ) ;
3534
3635 const zooKeeperHost = "zookeeper" ;
3736 const zooKeeperPort = 2181 ;
37+
3838 await using _ = await new GenericContainer ( "confluentinc/cp-zookeeper:5.5.4" )
3939 . withNetwork ( network )
4040 . withNetworkAliases ( zooKeeperHost )
4141 . withEnvironment ( { ZOOKEEPER_CLIENT_PORT : zooKeeperPort . toString ( ) } )
4242 . withExposedPorts ( zooKeeperPort )
4343 . start ( ) ;
4444
45- await using kafkaContainer = await new KafkaContainer ( IMAGE )
45+ await using container = await new KafkaContainer ( IMAGE )
4646 . withNetwork ( network )
4747 . withZooKeeper ( zooKeeperHost , zooKeeperPort )
4848 . start ( ) ;
49+ // }
4950
50- await testPubSub ( kafkaContainer ) ;
51+ await assertMessageProducedAndConsumed ( container ) ;
5152 } ) ;
52- // }
5353
5454 it ( "should be reusable" , async ( ) => {
55- await using originalKafkaContainer = await new KafkaContainer ( IMAGE ) . withReuse ( ) . start ( ) ;
56- const newKafkaContainer = await new KafkaContainer ( IMAGE ) . withReuse ( ) . start ( ) ;
55+ await using container1 = await new KafkaContainer ( IMAGE ) . withReuse ( ) . start ( ) ;
56+ const container2 = await new KafkaContainer ( IMAGE ) . withReuse ( ) . start ( ) ;
5757
58- expect ( newKafkaContainer . getId ( ) ) . toBe ( originalKafkaContainer . getId ( ) ) ;
58+ expect ( container2 . getId ( ) ) . toBe ( container1 . getId ( ) ) ;
5959 } ) ;
6060
61- describe . each ( [
62- {
63- name : "and zookpeer enabled" ,
64- configure : ( ) => ( { } ) ,
65- } ,
66- {
67- name : "and kraft enabled" ,
68- configure : ( kafkaContainer : KafkaContainer ) => kafkaContainer . withKraft ( ) ,
69- } ,
70- ] ) ( "when SASL SSL config listener provided $name" , ( { configure } ) => {
61+ describe ( "when SASL SSL config listener provided with Kraft" , ( ) => {
7162 const certificatesDir = path . resolve ( __dirname , ".." , "test-certs" ) ;
7263
73- // ssl {
74- it ( `should connect locally` , async ( ) => {
75- const kafkaContainer = await new KafkaContainer ( "confluentinc/cp-kafka:7.5.0" ) . withSaslSslListener ( {
76- port : 9096 ,
77- sasl : {
78- mechanism : "SCRAM-SHA-512" ,
79- user : {
80- name : "app-user" ,
81- password : "userPassword" ,
64+ it ( `should connect locally with ZK` , async ( ) => {
65+ // kafkaSsl {
66+ await using container = await new KafkaContainer ( "confluentinc/cp-kafka:7.5.0" )
67+ . withSaslSslListener ( {
68+ port : 9096 ,
69+ sasl : {
70+ mechanism : "SCRAM-SHA-512" ,
71+ user : {
72+ name : "app-user" ,
73+ password : "userPassword" ,
74+ } ,
8275 } ,
76+ keystore : {
77+ content : fs . readFileSync ( path . resolve ( certificatesDir , "kafka.server.keystore.pfx" ) ) ,
78+ passphrase : "serverKeystorePassword" ,
79+ } ,
80+ truststore : {
81+ content : fs . readFileSync ( path . resolve ( certificatesDir , "kafka.server.truststore.pfx" ) ) ,
82+ passphrase : "serverTruststorePassword" ,
83+ } ,
84+ } )
85+ . start ( ) ;
86+
87+ await assertMessageProducedAndConsumed ( container , {
88+ brokers : [ `${ container . getHost ( ) } :${ container . getMappedPort ( 9096 ) } ` ] ,
89+ sasl : {
90+ username : "app-user" ,
91+ password : "userPassword" ,
92+ mechanism : "scram-sha-512" ,
8393 } ,
84- keystore : {
85- content : fs . readFileSync ( path . resolve ( certificatesDir , "kafka.server.keystore.pfx" ) ) ,
86- passphrase : "serverKeystorePassword" ,
87- } ,
88- truststore : {
89- content : fs . readFileSync ( path . resolve ( certificatesDir , "kafka.server.truststore.pfx" ) ) ,
90- passphrase : "serverTruststorePassword" ,
94+ ssl : {
95+ ca : [ fs . readFileSync ( path . resolve ( certificatesDir , "kafka.client.truststore.pem" ) ) ] ,
9196 } ,
9297 } ) ;
93- configure ( kafkaContainer ) ;
94- await using startedKafkaContainer = await kafkaContainer . start ( ) ;
98+ // }
99+ } ) ;
100+
101+ it ( `should connect locally with Kraft` , async ( ) => {
102+ await using container = await new KafkaContainer ( "confluentinc/cp-kafka:7.5.0" )
103+ . withKraft ( )
104+ . withSaslSslListener ( {
105+ port : 9096 ,
106+ sasl : {
107+ mechanism : "SCRAM-SHA-512" ,
108+ user : {
109+ name : "app-user" ,
110+ password : "userPassword" ,
111+ } ,
112+ } ,
113+ keystore : {
114+ content : fs . readFileSync ( path . resolve ( certificatesDir , "kafka.server.keystore.pfx" ) ) ,
115+ passphrase : "serverKeystorePassword" ,
116+ } ,
117+ truststore : {
118+ content : fs . readFileSync ( path . resolve ( certificatesDir , "kafka.server.truststore.pfx" ) ) ,
119+ passphrase : "serverTruststorePassword" ,
120+ } ,
121+ } )
122+ . start ( ) ;
95123
96- await testPubSub ( startedKafkaContainer , {
97- brokers : [ `${ startedKafkaContainer . getHost ( ) } :${ startedKafkaContainer . getMappedPort ( 9096 ) } ` ] ,
124+ await assertMessageProducedAndConsumed ( container , {
125+ brokers : [ `${ container . getHost ( ) } :${ container . getMappedPort ( 9096 ) } ` ] ,
98126 sasl : {
99127 username : "app-user" ,
100128 password : "userPassword" ,
@@ -105,7 +133,6 @@ describe("KafkaContainer", { timeout: 240_000 }, () => {
105133 } ,
106134 } ) ;
107135 } ) ;
108- // }
109136
110137 it ( `should connect within Docker network` , async ( ) => {
111138 await using network = await new Network ( ) . start ( ) ;
@@ -171,13 +198,13 @@ describe("KafkaContainer", { timeout: 240_000 }, () => {
171198 } ) ;
172199 } ) ;
173200
174- // connectKraft {
175201 it ( "should connect using kraft" , async ( ) => {
176- await using kafkaContainer = await new KafkaContainer ( IMAGE ) . withKraft ( ) . start ( ) ;
202+ // connectKraft {
203+ await using container = await new KafkaContainer ( IMAGE ) . withKraft ( ) . start ( ) ;
204+ // }
177205
178- await testPubSub ( kafkaContainer ) ;
206+ await assertMessageProducedAndConsumed ( container ) ;
179207 } ) ;
180- // }
181208
182209 it ( "should throw an error when using kraft and and confluence platfom below 7.0.0" , async ( ) => {
183210 expect ( ( ) => new KafkaContainer ( "confluentinc/cp-kafka:6.2.14" ) . withKraft ( ) ) . toThrow (
@@ -187,9 +214,9 @@ describe("KafkaContainer", { timeout: 240_000 }, () => {
187214
188215 it ( "should connect using kraft and custom network" , async ( ) => {
189216 await using network = await new Network ( ) . start ( ) ;
190- await using kafkaContainer = await new KafkaContainer ( IMAGE ) . withKraft ( ) . withNetwork ( network ) . start ( ) ;
217+ await using container = await new KafkaContainer ( IMAGE ) . withKraft ( ) . withNetwork ( network ) . start ( ) ;
191218
192- await testPubSub ( kafkaContainer ) ;
219+ await assertMessageProducedAndConsumed ( container ) ;
193220 } ) ;
194221
195222 it ( "should throw an error when using kraft wit sasl and confluence platfom below 7.5.0" , async ( ) => {
0 commit comments