Skip to content

Commit 6752b0b

Browse files
authored
integration-tests: Always run down and use stable network name (grafana#4213)
* Make sure we cannot leave the containers running and use a stable network name * Make sure unix test can pass on mac and should be skipped on windows
1 parent d969d6c commit 6752b0b

File tree

3 files changed

+30
-44
lines changed

3 files changed

+30
-44
lines changed

internal/cmd/integration-tests/main.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76
"path/filepath"
@@ -42,18 +41,25 @@ func runIntegrationTests(cmd *cobra.Command, args []string) {
4241
return
4342
}
4443

45-
compose, err := tc.NewDockerCompose("./docker-compose.yaml")
44+
compose, err := tc.NewDockerComposeWith(tc.WithStackFiles("./docker-compose.yaml"), tc.StackIdentifier("alloy-integration-tests"))
4645
if err != nil {
4746
panic(fmt.Errorf("failed to parse the docker compose file: %v", err))
4847
}
4948

50-
ctx := context.Background()
49+
ctx := cmd.Context()
50+
5151
fmt.Println("Start test containers with docker compose config")
52-
err = compose.Up(ctx)
53-
if err != nil {
52+
if err = compose.Up(ctx, tc.Wait(true), tc.RemoveOrphans(true)); err != nil {
5453
panic(fmt.Errorf("could not start the docker compose: %v", err))
5554
}
56-
defer compose.Down(context.Background(), tc.RemoveImagesAll)
55+
56+
defer func() {
57+
fmt.Println("Stop test containers with docker compose config")
58+
err := compose.Down(ctx, tc.RemoveImagesAll)
59+
if err != nil {
60+
panic(fmt.Errorf("could not remove the docker compose: %v", err))
61+
}
62+
}()
5763

5864
fmt.Println("Sleep for 10 seconds to ensure that the env has time to initialize...")
5965
time.Sleep(10 * time.Second)

internal/cmd/integration-tests/tests/unix/unix_metrics_test.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package main
22

33
import (
4+
"runtime"
45
"testing"
56

67
"github.com/grafana/alloy/internal/cmd/integration-tests/common"
78
)
89

910
func TestUnixMetrics(t *testing.T) {
10-
var unixMetrics = []string{
11+
var expectedMetrics = []string{
1112
"node_arp_entries",
1213
"node_boot_time_seconds",
1314
"node_context_switches_total",
@@ -43,11 +44,6 @@ func TestUnixMetrics(t *testing.T) {
4344
"node_memory_CommitLimit_bytes",
4445
"node_memory_Committed_AS_bytes",
4546
"node_memory_Dirty_bytes",
46-
"node_memory_HugePages_Free",
47-
"node_memory_HugePages_Rsvd",
48-
"node_memory_HugePages_Surp",
49-
"node_memory_HugePages_Total",
50-
"node_memory_Hugepagesize_bytes",
5147
"node_memory_Inactive_anon_bytes",
5248
"node_memory_Inactive_bytes",
5349
"node_memory_Inactive_file_bytes",
@@ -154,5 +150,19 @@ func TestUnixMetrics(t *testing.T) {
154150
"node_vmstat_pswpin",
155151
"node_vmstat_pswpout",
156152
}
157-
common.MimirMetricsTest(t, unixMetrics, []string{}, "unix_metrics")
153+
154+
switch runtime.GOOS {
155+
case "windows":
156+
t.Skip("Skipping Unix metrics test on Windows")
157+
// darwin does not support hugepages (aka super pages on mac), so we only add them for linux
158+
case "linux":
159+
expectedMetrics = append(expectedMetrics,
160+
"node_memory_HugePages_Free",
161+
"node_memory_HugePages_Rsvd",
162+
"node_memory_HugePages_Surp",
163+
"node_memory_HugePages_Total",
164+
"node_memory_Hugepagesize_bytes")
165+
}
166+
167+
common.MimirMetricsTest(t, expectedMetrics, []string{}, "unix_metrics")
158168
}

internal/cmd/integration-tests/utils.go

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"sync"
1515

1616
"github.com/docker/docker/api/types/container"
17-
"github.com/docker/docker/api/types/network"
18-
"github.com/docker/docker/client"
1917
"github.com/docker/go-connections/nat"
2018
"github.com/testcontainers/testcontainers-go"
2119
"github.com/testcontainers/testcontainers-go/wait"
@@ -183,14 +181,8 @@ func runSingleTest(ctx context.Context, testDir string, port int) {
183181
}
184182
}()
185183

186-
// Get network name
187-
networkName, err := getTestcontainersNetworkName(ctx)
188-
if err != nil {
189-
panic(fmt.Sprintf("failed to get Testcontainers network name: %v", err))
190-
}
191-
192184
// Create container request
193-
req := createContainerRequest(dirName, port, networkName, containerFiles)
185+
req := createContainerRequest(dirName, port, "alloy-integration-tests_integration-tests", containerFiles)
194186

195187
// Start container
196188
alloyContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
@@ -266,7 +258,6 @@ func reportResults() {
266258

267259
if testsFailed > 0 {
268260
fmt.Printf("%d tests failed!\n", testsFailed)
269-
os.Exit(1)
270261
} else {
271262
fmt.Println("All integration tests passed!")
272263
}
@@ -293,24 +284,3 @@ func collectFiles(root string) ([]fileInfo, error) {
293284
})
294285
return filesToAdd, err
295286
}
296-
297-
func getTestcontainersNetworkName(ctx context.Context) (string, error) {
298-
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
299-
if err != nil {
300-
return "", fmt.Errorf("failed to create Docker client: %v", err)
301-
}
302-
defer cli.Close()
303-
304-
networks, err := cli.NetworkList(ctx, network.ListOptions{})
305-
if err != nil {
306-
return "", fmt.Errorf("failed to list networks: %v", err)
307-
}
308-
309-
for _, network := range networks {
310-
if strings.HasSuffix(network.Name, "_integration-tests") {
311-
return network.Name, nil
312-
}
313-
}
314-
315-
return "", fmt.Errorf("could not find Testcontainers network")
316-
}

0 commit comments

Comments
 (0)