Best Testcontainers-go code snippet using wait_test.Exec
exec_test.go
Source:exec_test.go
...9 "github.com/docker/go-connections/nat"10 "github.com/testcontainers/testcontainers-go"11 "github.com/testcontainers/testcontainers-go/wait"12)13func ExampleExecStrategy() {14 ctx := context.Background()15 req := testcontainers.ContainerRequest{16 Image: "localstack/localstack:latest",17 WaitingFor: wait.ForExec([]string{"awslocal", "dynamodb", "list-tables"}),18 }19 localstack, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{20 ContainerRequest: req,21 Started: true,22 })23 if err != nil {24 panic(err)25 }26 defer localstack.Terminate(ctx) // nolint: errcheck27 // Here you have a running container28}29type mockExecTarget struct {30 waitDuration time.Duration31 successAfter time.Time32 exitCode int33 failure error34}35func (st mockExecTarget) Host(_ context.Context) (string, error) {36 return "", errors.New("not implemented")37}38func (st mockExecTarget) MappedPort(_ context.Context, n nat.Port) (nat.Port, error) {39 return n, errors.New("not implemented")40}41func (st mockExecTarget) Logs(_ context.Context) (io.ReadCloser, error) {42 return nil, errors.New("not implemented")43}44func (st mockExecTarget) Exec(ctx context.Context, _ []string) (int, error) {45 time.Sleep(st.waitDuration)46 if err := ctx.Err(); err != nil {47 return st.exitCode, err48 }49 if !st.successAfter.IsZero() && time.Now().After(st.successAfter) {50 return 0, st.failure51 }52 return st.exitCode, st.failure53}54func (st mockExecTarget) State(_ context.Context) (*types.ContainerState, error) {55 return nil, errors.New("not implemented")56}57func TestExecStrategyWaitUntilReady(t *testing.T) {58 target := mockExecTarget{}59 wg := wait.NewExecStrategy([]string{"true"}).60 WithStartupTimeout(30 * time.Second)61 err := wg.WaitUntilReady(context.Background(), target)62 if err != nil {63 t.Fatal(err)64 }65}66func TestExecStrategyWaitUntilReadyForExec(t *testing.T) {67 target := mockExecTarget{}68 wg := wait.ForExec([]string{"true"})69 err := wg.WaitUntilReady(context.Background(), target)70 if err != nil {71 t.Fatal(err)72 }73}74func TestExecStrategyWaitUntilReady_MultipleChecks(t *testing.T) {75 target := mockExecTarget{76 exitCode: 10,77 successAfter: time.Now().Add(2 * time.Second),78 }79 wg := wait.NewExecStrategy([]string{"true"}).80 WithPollInterval(500 * time.Millisecond)81 err := wg.WaitUntilReady(context.Background(), target)82 if err != nil {83 t.Fatal(err)84 }85}86func TestExecStrategyWaitUntilReady_DeadlineExceeded(t *testing.T) {87 ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)88 defer cancel()89 target := mockExecTarget{90 waitDuration: 1 * time.Second,91 }92 wg := wait.NewExecStrategy([]string{"true"})93 err := wg.WaitUntilReady(ctx, target)94 if err != context.DeadlineExceeded {95 t.Fatal(err)96 }97}98func TestExecStrategyWaitUntilReady_CustomExitCode(t *testing.T) {99 target := mockExecTarget{100 exitCode: 10,101 }102 wg := wait.NewExecStrategy([]string{"true"}).WithExitCodeMatcher(func(exitCode int) bool {103 return exitCode == 10104 })105 err := wg.WaitUntilReady(context.Background(), target)106 if err != nil {107 t.Fatal(err)108 }109}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!