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

     1  package test_env
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/google/uuid"
     9  	"github.com/imdario/mergo"
    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 SchemaRegistry struct {
    20  	EnvComponent
    21  	EnvVars     map[string]string
    22  	InternalUrl string
    23  	ExternalUrl string
    24  	l           zerolog.Logger
    25  	t           *testing.T
    26  }
    27  
    28  func NewSchemaRegistry(networks []string) *SchemaRegistry {
    29  	id, _ := uuid.NewRandom()
    30  	defaultEnvVars := map[string]string{
    31  		"SCHEMA_REGISTRY_DEBUG": "true",
    32  	}
    33  	return &SchemaRegistry{
    34  		EnvComponent: EnvComponent{
    35  			ContainerName: fmt.Sprintf("schema-registry-%s", id.String()),
    36  			Networks:      networks,
    37  		},
    38  		EnvVars: defaultEnvVars,
    39  	}
    40  }
    41  
    42  func (r *SchemaRegistry) WithTestInstance(t *testing.T) *SchemaRegistry {
    43  	r.l = logging.GetTestLogger(t)
    44  	r.t = t
    45  	return r
    46  }
    47  
    48  func (r *SchemaRegistry) WithContainerName(name string) *SchemaRegistry {
    49  	r.ContainerName = name
    50  	return r
    51  }
    52  
    53  func (r *SchemaRegistry) WithKafka(kafkaUrl string) *SchemaRegistry {
    54  	envVars := map[string]string{
    55  		"SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS": kafkaUrl,
    56  	}
    57  	return r.WithEnvVars(envVars)
    58  }
    59  
    60  func (r *SchemaRegistry) WithEnvVars(envVars map[string]string) *SchemaRegistry {
    61  	if err := mergo.Merge(&r.EnvVars, envVars, mergo.WithOverride); err != nil {
    62  		r.l.Fatal().Err(err).Msg("Failed to merge env vars")
    63  	}
    64  	return r
    65  }
    66  
    67  func (r *SchemaRegistry) StartContainer() error {
    68  	r.InternalUrl = fmt.Sprintf("http://%s:%s", r.ContainerName, "8081")
    69  	l := logging.GetTestContainersGoTestLogger(r.t)
    70  	envVars := map[string]string{
    71  		"SCHEMA_REGISTRY_HOST_NAME": r.ContainerName,
    72  		"SCHEMA_REGISTRY_LISTENERS": r.InternalUrl,
    73  	}
    74  	r.WithEnvVars(envVars)
    75  	cr, err := r.getContainerRequest()
    76  	if err != nil {
    77  		return err
    78  	}
    79  	req := tc.GenericContainerRequest{
    80  		ContainerRequest: cr,
    81  		Started:          true,
    82  		Reuse:            true,
    83  		Logger:           l,
    84  	}
    85  	c, err := tc.GenericContainer(testcontext.Get(r.t), req)
    86  	if err != nil {
    87  		return fmt.Errorf("cannot start Schema Registry container: %w", err)
    88  	}
    89  	host, err := GetHost(testcontext.Get(r.t), c)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	port, err := c.MappedPort(testcontext.Get(r.t), "8081/tcp")
    94  	if err != nil {
    95  		return err
    96  	}
    97  	r.ExternalUrl = fmt.Sprintf("%s:%s", host, port.Port())
    98  	r.l.Info().
    99  		Str("containerName", r.ContainerName).
   100  		Str("internalUrl", r.InternalUrl).
   101  		Str("externalUrl", r.ExternalUrl).
   102  		Msgf("Started Schema Registry container")
   103  
   104  	r.Container = c
   105  
   106  	return nil
   107  }
   108  
   109  func (r *SchemaRegistry) getContainerRequest() (tc.ContainerRequest, error) {
   110  	schemaImage, err := mirror.GetImage("confluentinc/cp-schema-registry")
   111  	if err != nil {
   112  		return tc.ContainerRequest{}, err
   113  	}
   114  	return tc.ContainerRequest{
   115  		Name:         r.ContainerName,
   116  		Image:        schemaImage,
   117  		ExposedPorts: []string{"8081/tcp"},
   118  		Env:          r.EnvVars,
   119  		Networks:     r.Networks,
   120  		WaitingFor: tcwait.ForLog("INFO Server started, listening for requests").
   121  			WithStartupTimeout(30 * time.Second).
   122  			WithPollInterval(100 * time.Millisecond),
   123  	}, nil
   124  }