Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titlekafka_container.go
package containers

import (
    "context"
    "github.com/testcontainers/testcontainers-go"
    "github.com/testcontainers/testcontainers-go/wait"
)

type KafkaContainer struct {
    Container testcontainers.Container
    context context.Context
    dockerId string

}

func NewKafkaContainer(ctx context.Context, networkName string ) (*KafkaContainer, error){

   kafkaContainer := &KafkaContainer{
       Container: nil,
       context: ctx,
   }


   req := testcontainers.ContainerRequest{

       Image:           "bitnami/kafka:3" ,
       Name:            "kafka",
       Hostname:        "kafka",
       Networks:        []string{ networkName},
       WaitingFor:      wait.ForLog("Ready to serve as the new controller with epoch 1"),
       ExposedPorts:    []string{"9092:9092"},
       Env:             map[string]string{
                            "KAFKA_CFG_ZOOKEEPER_CONNECT": "zookeeper:2181",
                            "ALLOW_PLAINTEXT_LISTENER": "yes",
                            "KAFKA_CFG_LISTENERS": "INTERNAL://:9093,EXTERNAL://:9092",
                            "KAFKA_CFG_ADVERTISED_LISTENERS": "INTERNAL://:9093,EXTERNAL://localhost:9092",
                            "KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP": "INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT",
                            "KAFKA_CFG_INTER_BROKER_LISTENER_NAME": "INTERNAL",
                        },
   }
   container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
       ContainerRequest: req,
       Started:          true,
   })
   if err != nil {
       return nil, err
   }

   kafkaContainer.Container =  container

   return kafkaContainer,nil
}

func (k KafkaContainer) Terminate() {
        k.Container.Terminate(k.context)
        //time.Sleep(1 * time.Second)
}


Code Block
titlezookeeper_container.go
package containers

import (
    "context"
    "github.com/testcontainers/testcontainers-go"
    "github.com/testcontainers/testcontainers-go/wait"
)

type ZookeeperContainer struct {
    container testcontainers.Container
    context context.Context
    dockerId string

}

func NewZookeeperContainer(ctx context.Context, networkName string) (*ZookeeperContainer, error){

    zookeeperContainer := &ZookeeperContainer{
        container: nil,
        context: ctx,
    }


    req := testcontainers.ContainerRequest{

        Image:              "bitnami/zookeeper:3.7",
        Name:               "zookeeper",
        Hostname:           "zookeeper",
        Networks:           []string{ networkName},
        WaitingFor:         wait.ForLog("Started AdminServer on address"),
        ExposedPorts:       []string{"2181/tcp"},
        Env:                map[string]string{
                                "ALLOW_ANONYMOUS_LOGIN": "yes",
                            },
    }
    container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
        ContainerRequest: req,
        Started:          true,
    })
    if err != nil {
        return nil, err
    }

    zookeeperContainer.container =  container

    return zookeeperContainer,nil
}

func (z ZookeeperContainer) Terminate() {
         z.container.Terminate(z.context)
         //time.Sleep(1 * time.Second)
}



Sample Test Case using Docker-Compose

...