Skip to content

Commit 7509e8f

Browse files
committed
leverage the testing pkg
1 parent 83a2a8d commit 7509e8f

File tree

2 files changed

+35
-51
lines changed

2 files changed

+35
-51
lines changed

systemtest/containers.go

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"runtime"
3333
"strings"
3434
"sync"
35+
"testing"
3536
"time"
3637

3738
"github.com/docker/docker/api/types"
@@ -41,6 +42,7 @@ import (
4142
"github.com/docker/docker/client"
4243
"github.com/docker/docker/pkg/stdcopy"
4344
"github.com/docker/go-connections/nat"
45+
"github.com/stretchr/testify/require"
4446
"github.com/testcontainers/testcontainers-go"
4547
"github.com/testcontainers/testcontainers-go/wait"
4648
)
@@ -53,6 +55,8 @@ const (
5355

5456
var (
5557
systemtestDir string
58+
agentImageMu sync.RWMutex
59+
agentImages = make(map[string]bool)
5660
)
5761

5862
func initContainers() {
@@ -468,36 +472,28 @@ func BuildElasticAgentImage(
468472
return nil
469473
}
470474

471-
var (
472-
agentImageMu sync.RWMutex
473-
agentImages = make(map[string]bool)
474-
)
475+
func ToggleGeoIpDatabase(t *testing.T, available bool) {
476+
t.Helper()
477+
478+
ctx := t.Context()
475479

476-
func ToggleGeoIpDatabase(ctx context.Context, available bool) error {
477480
cli, err := client.NewClientWithOpts(client.FromEnv)
478-
if err != nil {
479-
return err
480-
}
481+
require.NoError(t, err)
481482
defer cli.Close()
483+
482484
cli.NegotiateAPIVersion(ctx)
483485

484486
c, err := stackContainerInfo(ctx, cli, "elasticsearch")
485-
if err != nil {
486-
return err
487-
}
487+
require.NoError(t, err)
488488

489489
inspect, err := cli.ContainerInspect(ctx, c.ID)
490-
if err != nil {
491-
return err
492-
}
490+
require.NoError(t, err)
493491

494-
if err := cli.ContainerStop(ctx, c.ID, container.StopOptions{}); err != nil {
495-
return fmt.Errorf("stopping container: %w", err)
496-
}
492+
err = cli.ContainerStop(ctx, c.ID, container.StopOptions{})
493+
require.NoError(t, err)
497494

498-
if err := cli.ContainerRemove(ctx, c.ID, container.RemoveOptions{Force: true}); err != nil {
499-
return fmt.Errorf("stopping container: %w", err)
500-
}
495+
err = cli.ContainerRemove(ctx, c.ID, container.RemoveOptions{Force: true})
496+
require.NoError(t, err)
501497

502498
var newEnv []string
503499
for _, e := range inspect.Config.Env {
@@ -512,49 +508,40 @@ func ToggleGeoIpDatabase(ctx context.Context, available bool) error {
512508
)
513509
}
514510

515-
cfgCopy, _ := deepCopyConfig(inspect.Config)
511+
cfgCopy := deepCopyCfg(t, inspect.Config)
516512
cfgCopy.Env = newEnv
517513

518-
hostCfgCopy, _ := deepCopyConfig(inspect.HostConfig)
519-
520514
createResp, err := cli.ContainerCreate(
521515
ctx,
522516
cfgCopy,
523-
hostCfgCopy,
517+
deepCopyCfg(t, inspect.HostConfig),
524518
&network.NetworkingConfig{
525519
EndpointsConfig: inspect.NetworkSettings.Networks,
526520
},
527521
nil,
528522
inspect.Name,
529523
)
530-
if err != nil {
531-
return fmt.Errorf("creating new container: %w", err)
532-
}
524+
require.NoError(t, err)
533525

534-
if err := cli.ContainerStart(ctx, createResp.ID, container.StartOptions{}); err != nil {
535-
return fmt.Errorf("starting new container: %w", err)
536-
}
526+
err = cli.ContainerStart(ctx, createResp.ID, container.StartOptions{})
527+
require.NoError(t, err)
537528

538529
kb, err := stackContainerInfo(ctx, cli, "kibana")
539-
if err != nil {
540-
return err
541-
}
530+
require.NoError(t, err)
542531

543-
if err := cli.ContainerRestart(ctx, kb.ID, container.StopOptions{Timeout: nil}); err != nil {
544-
return fmt.Errorf("starting kibana container: %w", err)
545-
}
532+
err = cli.ContainerRestart(ctx, kb.ID, container.StopOptions{Timeout: nil})
533+
require.NoError(t, err)
546534

547-
return waitContainerHealthy(ctx, "kibana")
535+
err = waitContainerHealthy(ctx, "kibana")
536+
require.NoError(t, err)
548537
}
549538

550-
func deepCopyConfig[T any](src *T) (*T, error) {
539+
func deepCopyCfg[T any](t *testing.T, src *T) *T {
540+
t.Helper()
551541
var dst T
552542
bytes, err := json.Marshal(src)
553-
if err != nil {
554-
return nil, err
555-
}
556-
if err := json.Unmarshal(bytes, &dst); err != nil {
557-
return nil, err
558-
}
559-
return &dst, nil
543+
require.NoError(t, err)
544+
err = json.Unmarshal(bytes, &dst)
545+
require.NoError(t, err)
546+
return &dst
560547
}

systemtest/rum_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package systemtest_test
1919

2020
import (
2121
"bytes"
22-
"context"
2322
"fmt"
2423
"io"
2524
"net/http"
@@ -47,18 +46,16 @@ const requestBody = `{"metadata":{"service":{"name":"rum-js-test","agent":{"name
4746
func TestGeoIp_DatabaseUnavailable(t *testing.T) {
4847
systemtest.CleanupElasticsearch(t)
4948

50-
err := systemtest.ToggleGeoIpDatabase(context.Background(), false)
51-
require.NoError(t, err)
49+
systemtest.ToggleGeoIpDatabase(t, false)
5250

5351
// Make GeoIP database available for other tests.
5452
defer func() {
55-
err := systemtest.ToggleGeoIpDatabase(context.Background(), true)
56-
require.NoError(t, err)
53+
systemtest.ToggleGeoIpDatabase(t, true)
5754
}()
5855

5956
srv := apmservertest.NewUnstartedServerTB(t)
6057
srv.Config.RUM = &apmservertest.RUMConfig{Enabled: true}
61-
err = srv.Start()
58+
err := srv.Start()
6259
require.NoError(t, err)
6360

6461
serverURL, err := url.Parse(srv.URL)

0 commit comments

Comments
 (0)