|
| 1 | +package test_env |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/rs/zerolog" |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | + "github.com/smartcontractkit/chainlink-testing-framework/lib/docker" |
| 12 | + "github.com/smartcontractkit/chainlink-testing-framework/lib/logging" |
| 13 | + "github.com/smartcontractkit/chainlink-testing-framework/lib/mirror" |
| 14 | + "github.com/smartcontractkit/chainlink-testing-framework/lib/utils/testcontext" |
| 15 | + "github.com/smartcontractkit/chainlink-testing-framework/parrot" |
| 16 | + tc "github.com/testcontainers/testcontainers-go" |
| 17 | + tcwait "github.com/testcontainers/testcontainers-go/wait" |
| 18 | +) |
| 19 | + |
| 20 | +const defaultParrotImage = "parrot:latest" |
| 21 | + |
| 22 | +// Parrot is a test environment component that wraps a Parrot server. |
| 23 | +type Parrot struct { |
| 24 | + EnvComponent |
| 25 | + Client *parrot.Client |
| 26 | + ExternalEndpoint string |
| 27 | + InternalEndpoint string |
| 28 | + t *testing.T |
| 29 | + l zerolog.Logger |
| 30 | +} |
| 31 | + |
| 32 | +// NewParrot creates a new instance of ParrotServer with specified networks and options. |
| 33 | +// It initializes the server with a unique container name and a default startup timeout. |
| 34 | +// This function is useful for testing decentralized applications in a controlled environment. |
| 35 | +func NewParrot(networks []string, opts ...EnvComponentOption) *Parrot { |
| 36 | + p := &Parrot{ |
| 37 | + EnvComponent: EnvComponent{ |
| 38 | + ContainerName: fmt.Sprintf("%s-%s", "parrot", uuid.NewString()[0:8]), |
| 39 | + Networks: networks, |
| 40 | + StartupTimeout: 1 * time.Minute, |
| 41 | + }, |
| 42 | + l: log.Logger, |
| 43 | + } |
| 44 | + for _, opt := range opts { |
| 45 | + opt(&p.EnvComponent) |
| 46 | + } |
| 47 | + return p |
| 48 | +} |
| 49 | + |
| 50 | +// WithTestInstance configures the MockServer with a test logger and test context. |
| 51 | +// It returns the updated MockServer instance for use in testing scenarios. |
| 52 | +func (p *Parrot) WithTestInstance(t *testing.T) *Parrot { |
| 53 | + p.l = logging.GetTestLogger(t) |
| 54 | + p.t = t |
| 55 | + return p |
| 56 | +} |
| 57 | + |
| 58 | +// SetExternalAdapterMocks configures a specified number of mock external adapter endpoints. |
| 59 | +// It generates unique paths for each adapter and stores their URLs for later use. |
| 60 | +// This function is useful for testing scenarios that require multiple external adapter interactions. |
| 61 | +func (p *Parrot) SetExternalAdapterMocks(count int) error { |
| 62 | + // for i := 0; i < count; i++ { |
| 63 | + // path := fmt.Sprintf("/ea-%d", i) |
| 64 | + // err := ms.Client.SetRandomValuePath(path) |
| 65 | + // if err != nil { |
| 66 | + // return err |
| 67 | + // } |
| 68 | + // cName, err := ms.Container.Name(testcontext.Get(ms.t)) |
| 69 | + // if err != nil { |
| 70 | + // return err |
| 71 | + // } |
| 72 | + // cName = strings.Replace(cName, "/", "", -1) |
| 73 | + // eaUrl, err := url.Parse(fmt.Sprintf("http://%s:%s%s", |
| 74 | + // cName, "1080", path)) |
| 75 | + // if err != nil { |
| 76 | + // return err |
| 77 | + // } |
| 78 | + // ms.EAMockUrls = append(ms.EAMockUrls, eaUrl) |
| 79 | + // } |
| 80 | + // return nil |
| 81 | + return fmt.Errorf("not implemented") |
| 82 | +} |
| 83 | + |
| 84 | +// StartContainer initializes and starts a Parrot container. |
| 85 | +// It sets up logging, retrieves the container request, and establishes endpoints for communication. |
| 86 | +// This function is essential for testing environments that require a mock server instance. |
| 87 | +func (p *Parrot) StartContainer() error { |
| 88 | + l := logging.GetTestContainersGoTestLogger(p.t) |
| 89 | + cr, err := p.getContainerRequest() |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + c, err := docker.StartContainerWithRetry(p.l, tc.GenericContainerRequest{ |
| 94 | + ContainerRequest: cr, |
| 95 | + Reuse: true, |
| 96 | + Started: true, |
| 97 | + Logger: l, |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("cannot start MockServer container: %w", err) |
| 101 | + } |
| 102 | + p.Container = c |
| 103 | + endpoint, err := GetEndpoint(testcontext.Get(p.t), c, "http") |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + p.ExternalEndpoint = endpoint |
| 108 | + p.InternalEndpoint = fmt.Sprintf("http://%s", p.ContainerName) |
| 109 | + |
| 110 | + p.Client = parrot.NewClient(p.ExternalEndpoint) |
| 111 | + |
| 112 | + p.l.Info().Str("External Endpoint", p.ExternalEndpoint). |
| 113 | + Str("Internal Endpoint", p.InternalEndpoint). |
| 114 | + Str("Container Name", p.ContainerName). |
| 115 | + Msg("Started Parrot Container") |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func (p *Parrot) getContainerRequest() (tc.ContainerRequest, error) { |
| 120 | + pImage := mirror.AddMirrorToImageIfSet(defaultParrotImage) |
| 121 | + |
| 122 | + return tc.ContainerRequest{ |
| 123 | + Name: p.ContainerName, |
| 124 | + Image: pImage, |
| 125 | + ExposedPorts: []string{"80/tcp"}, |
| 126 | + Networks: p.Networks, |
| 127 | + WaitingFor: tcwait.ForHealthCheck(). |
| 128 | + WithPollInterval(100 * time.Millisecond).WithStartupTimeout(p.StartupTimeout), |
| 129 | + LifecycleHooks: []tc.ContainerLifecycleHooks{ |
| 130 | + { |
| 131 | + PostStarts: p.PostStartsHooks, |
| 132 | + PostStops: p.PostStopsHooks, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, nil |
| 136 | +} |
0 commit comments