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

     1  package test_env
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/google/uuid"
    11  	"github.com/rs/zerolog"
    12  	"github.com/rs/zerolog/log"
    13  	tc "github.com/testcontainers/testcontainers-go"
    14  	tcwait "github.com/testcontainers/testcontainers-go/wait"
    15  
    16  	ctfClient "github.com/smartcontractkit/chainlink-testing-framework/libs/client"
    17  	"github.com/smartcontractkit/chainlink-testing-framework/libs/docker"
    18  	"github.com/smartcontractkit/chainlink-testing-framework/libs/logging"
    19  	"github.com/smartcontractkit/chainlink-testing-framework/libs/mirror"
    20  	"github.com/smartcontractkit/chainlink-testing-framework/libs/utils/testcontext"
    21  )
    22  
    23  type MockServer struct {
    24  	EnvComponent
    25  	Client           *ctfClient.MockserverClient
    26  	Endpoint         string
    27  	InternalEndpoint string
    28  	EAMockUrls       []*url.URL
    29  	t                *testing.T
    30  	l                zerolog.Logger
    31  }
    32  
    33  func NewMockServer(networks []string, opts ...EnvComponentOption) *MockServer {
    34  	ms := &MockServer{
    35  		EnvComponent: EnvComponent{
    36  			ContainerName: fmt.Sprintf("%s-%s", "mockserver", uuid.NewString()[0:8]),
    37  			Networks:      networks,
    38  		},
    39  		l: log.Logger,
    40  	}
    41  	for _, opt := range opts {
    42  		opt(&ms.EnvComponent)
    43  	}
    44  	return ms
    45  }
    46  
    47  func (ms *MockServer) WithTestInstance(t *testing.T) *MockServer {
    48  	ms.l = logging.GetTestLogger(t)
    49  	ms.t = t
    50  	return ms
    51  }
    52  
    53  func (ms *MockServer) SetExternalAdapterMocks(count int) error {
    54  	for i := 0; i < count; i++ {
    55  		path := fmt.Sprintf("/ea-%d", i)
    56  		err := ms.Client.SetRandomValuePath(path)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		cName, err := ms.Container.Name(testcontext.Get(ms.t))
    61  		if err != nil {
    62  			return err
    63  		}
    64  		cName = strings.Replace(cName, "/", "", -1)
    65  		eaUrl, err := url.Parse(fmt.Sprintf("http://%s:%s%s",
    66  			cName, "1080", path))
    67  		if err != nil {
    68  			return err
    69  		}
    70  		ms.EAMockUrls = append(ms.EAMockUrls, eaUrl)
    71  	}
    72  	return nil
    73  }
    74  
    75  func (ms *MockServer) StartContainer() error {
    76  	l := logging.GetTestContainersGoTestLogger(ms.t)
    77  	cr, err := ms.getContainerRequest()
    78  	if err != nil {
    79  		return err
    80  	}
    81  	c, err := docker.StartContainerWithRetry(ms.l, tc.GenericContainerRequest{
    82  		ContainerRequest: cr,
    83  		Reuse:            true,
    84  		Started:          true,
    85  		Logger:           l,
    86  	})
    87  	if err != nil {
    88  		return fmt.Errorf("cannot start MockServer container: %w", err)
    89  	}
    90  	ms.Container = c
    91  	endpoint, err := GetEndpoint(testcontext.Get(ms.t), c, "http")
    92  	if err != nil {
    93  		return err
    94  	}
    95  	ms.l.Info().Any("endpoint", endpoint).Str("containerName", ms.ContainerName).
    96  		Msgf("Started MockServer container")
    97  	ms.Endpoint = endpoint
    98  	ms.InternalEndpoint = fmt.Sprintf("http://%s:%s", ms.ContainerName, "1080")
    99  
   100  	client := ctfClient.NewMockserverClient(&ctfClient.MockserverConfig{
   101  		LocalURL:   endpoint,
   102  		ClusterURL: ms.InternalEndpoint,
   103  	})
   104  	if err != nil {
   105  		return fmt.Errorf("cannot create MockServer client: %w", err)
   106  	}
   107  	ms.Client = client
   108  
   109  	return nil
   110  }
   111  
   112  func (ms *MockServer) getContainerRequest() (tc.ContainerRequest, error) {
   113  	msImage, err := mirror.GetImage("mockserver/mockserver")
   114  	if err != nil {
   115  		return tc.ContainerRequest{}, err
   116  	}
   117  	return tc.ContainerRequest{
   118  		Name:         ms.ContainerName,
   119  		Image:        msImage,
   120  		ExposedPorts: []string{"1080/tcp"},
   121  		Env: map[string]string{
   122  			"SERVER_PORT": "1080",
   123  		},
   124  		Networks: ms.Networks,
   125  		WaitingFor: tcwait.ForLog("INFO 1080 started on port: 1080").
   126  			WithStartupTimeout(30 * time.Second).
   127  			WithPollInterval(100 * time.Millisecond),
   128  		LifecycleHooks: []tc.ContainerLifecycleHooks{
   129  			{
   130  				PostStarts: ms.PostStartsHooks,
   131  				PostStops:  ms.PostStopsHooks,
   132  			},
   133  		},
   134  	}, nil
   135  }