github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/docker/test_env/zookeeper.go (about)

     1  package test_env
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/google/uuid"
    10  	"github.com/rs/zerolog"
    11  	tc "github.com/testcontainers/testcontainers-go"
    12  	tcwait "github.com/testcontainers/testcontainers-go/wait"
    13  
    14  	"github.com/smartcontractkit/chainlink-testing-framework/libs/logging"
    15  	"github.com/smartcontractkit/chainlink-testing-framework/libs/mirror"
    16  	"github.com/smartcontractkit/chainlink-testing-framework/libs/utils/testcontext"
    17  )
    18  
    19  type Zookeeper struct {
    20  	EnvComponent
    21  	InternalUrl string
    22  	l           zerolog.Logger
    23  	t           *testing.T
    24  }
    25  
    26  func NewZookeeper(networks []string) *Zookeeper {
    27  	id, _ := uuid.NewRandom()
    28  	z := &Zookeeper{
    29  		EnvComponent: EnvComponent{
    30  			ContainerName: fmt.Sprintf("zookeper-%s", id.String()),
    31  			Networks:      networks,
    32  		},
    33  	}
    34  	z.SetDefaultHooks()
    35  	return z
    36  }
    37  
    38  func (z *Zookeeper) WithTestInstance(t *testing.T) *Zookeeper {
    39  	z.l = logging.GetTestLogger(t)
    40  	z.t = t
    41  	return z
    42  }
    43  
    44  func (z *Zookeeper) WithContainerName(name string) *Zookeeper {
    45  	z.ContainerName = name
    46  	return z
    47  }
    48  
    49  func (z *Zookeeper) StartContainer() error {
    50  	l := logging.GetTestContainersGoTestLogger(z.t)
    51  	cr, err := z.getContainerRequest()
    52  	if err != nil {
    53  		return err
    54  	}
    55  	req := tc.GenericContainerRequest{
    56  		ContainerRequest: cr,
    57  		Started:          true,
    58  		Reuse:            true,
    59  		Logger:           l,
    60  	}
    61  	c, err := tc.GenericContainer(testcontext.Get(z.t), req)
    62  	if err != nil {
    63  		return fmt.Errorf("cannot start Zookeper container: %w", err)
    64  	}
    65  	name, err := c.Name(testcontext.Get(z.t))
    66  	if err != nil {
    67  		return err
    68  	}
    69  	name = strings.Replace(name, "/", "", -1)
    70  	z.InternalUrl = fmt.Sprintf("%s:%s", name, "2181")
    71  
    72  	z.l.Info().Str("containerName", name).
    73  		Str("internalUrl", z.InternalUrl).
    74  		Msgf("Started Zookeper container")
    75  
    76  	z.Container = c
    77  
    78  	return nil
    79  }
    80  
    81  func (z *Zookeeper) getContainerRequest() (tc.ContainerRequest, error) {
    82  	zookeeperImage, err := mirror.GetImage("confluentinc/cp-zookeeper")
    83  	if err != nil {
    84  		return tc.ContainerRequest{}, err
    85  	}
    86  	return tc.ContainerRequest{
    87  		Name:         z.ContainerName,
    88  		Image:        zookeeperImage,
    89  		ExposedPorts: []string{"2181/tcp"},
    90  		Env: map[string]string{
    91  			"ZOOKEEPER_CLIENT_PORT": "2181",
    92  			"ZOOKEEPER_TICK_TIME":   "2000",
    93  		},
    94  		Networks: z.Networks,
    95  		WaitingFor: tcwait.ForLog("ZooKeeper audit is disabled.").
    96  			WithStartupTimeout(30 * time.Second).
    97  			WithPollInterval(100 * time.Millisecond),
    98  		LifecycleHooks: []tc.ContainerLifecycleHooks{
    99  			{
   100  				PostStarts: z.PostStartsHooks,
   101  				PostStops:  z.PostStopsHooks,
   102  			},
   103  		},
   104  	}, nil
   105  }