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

     1  package test_env
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  	"github.com/testcontainers/testcontainers-go"
    10  
    11  	"github.com/smartcontractkit/chainlink-testing-framework/libs/docker"
    12  	"github.com/smartcontractkit/chainlink-testing-framework/libs/logging"
    13  	"github.com/smartcontractkit/chainlink-testing-framework/libs/utils/testcontext"
    14  )
    15  
    16  type TestLogConsumer struct {
    17  	Msgs []string
    18  }
    19  
    20  func (g *TestLogConsumer) Accept(l testcontainers.Log) {
    21  	g.Msgs = append(g.Msgs, string(l.Content))
    22  }
    23  
    24  func followLogs(t *testing.T, c testcontainers.Container) *TestLogConsumer {
    25  	consumer := &TestLogConsumer{
    26  		Msgs: make([]string, 0),
    27  	}
    28  	go func() {
    29  		c.FollowOutput(consumer)
    30  		err := c.StartLogProducer(testcontext.Get(t), time.Duration(5*time.Second))
    31  		require.NoError(t, err)
    32  	}()
    33  	return consumer
    34  }
    35  
    36  func TestEnvComponentPauseChaos(t *testing.T) {
    37  	l := logging.GetTestLogger(t)
    38  	network, err := docker.CreateNetwork(l)
    39  	require.NoError(t, err)
    40  
    41  	defaultChainCfg := GetDefaultChainConfig()
    42  	g := NewGeth([]string{network.Name}, &defaultChainCfg).
    43  		WithTestInstance(t)
    44  	_, _, err = g.StartContainer()
    45  	require.NoError(t, err)
    46  	t.Run("check that testcontainers can be paused", func(t *testing.T) {
    47  		consumer := followLogs(t, g.Container)
    48  
    49  		timeStrNow := time.Now().Add(3 * time.Second).UTC().String()
    50  		justTime := strings.Split(timeStrNow, " ")[1]
    51  		justTimeWithoutMicrosecs := justTime[:len(justTime)-7]
    52  
    53  		// blocking
    54  		err = g.ChaosPause(l, 5*time.Second)
    55  
    56  		// check that there were no logs when paused
    57  		for _, lo := range consumer.Msgs {
    58  			if strings.Contains(lo, justTimeWithoutMicrosecs) {
    59  				t.Fail()
    60  			}
    61  		}
    62  	})
    63  
    64  	t.Run("check container traffic can be lost", func(t *testing.T) {
    65  		// TODO: assert with a busybox container that the traffic is lost
    66  		err = g.ChaosNetworkLoss(l, 30*time.Second, 100, "", nil, nil, nil)
    67  		require.NoError(t, err)
    68  	})
    69  	t.Run("check container latency can be changed", func(t *testing.T) {
    70  		// TODO: assert with a busybox container that the traffic is delayed
    71  		err = g.ChaosNetworkDelay(l, 30*time.Second, 5*time.Second, "", nil, nil, nil)
    72  		require.NoError(t, err)
    73  	})
    74  }