From afc96aa23b175b7b86691b7128da7d8941cd8b91 Mon Sep 17 00:00:00 2001 From: Wildum Date: Fri, 8 Nov 2024 15:15:43 +0100 Subject: [PATCH] fix windows test --- .../docker-compose.windows.yaml | 12 -- internal/cmd/integration-tests/main.go | 6 +- .../tests-windows/windows/config.alloy | 2 +- .../windows/windows_metrics_test.go | 111 ++++++++++++++++-- internal/cmd/integration-tests/utils.go | 4 +- 5 files changed, 106 insertions(+), 29 deletions(-) delete mode 100644 internal/cmd/integration-tests/docker-compose.windows.yaml diff --git a/internal/cmd/integration-tests/docker-compose.windows.yaml b/internal/cmd/integration-tests/docker-compose.windows.yaml deleted file mode 100644 index 16a92167e4..0000000000 --- a/internal/cmd/integration-tests/docker-compose.windows.yaml +++ /dev/null @@ -1,12 +0,0 @@ -services: - - mimir: - image: grafana/mimir:2.10.4 - platform: linux/amd64 - volumes: - - ./configs/mimir:/etc/mimir-config - entrypoint: - - /bin/mimir - - -config.file=/etc/mimir-config/mimir.yaml - ports: - - "9009:9009" diff --git a/internal/cmd/integration-tests/main.go b/internal/cmd/integration-tests/main.go index 8b993ba9d8..dd38328ce8 100644 --- a/internal/cmd/integration-tests/main.go +++ b/internal/cmd/integration-tests/main.go @@ -36,21 +36,19 @@ func runIntegrationTests(cmd *cobra.Command, args []string) { testFolder := "./tests/" alloyBinaryPath := "../../../../../build/alloy" alloyBinary := "build/alloy" - dockerComposeFile := "docker-compose.yaml" if runtime.GOOS == "windows" { testFolder = "./tests-windows/" alloyBinaryPath = "..\\..\\..\\..\\..\\build\\alloy.exe" alloyBinary = "build/alloy.exe" - dockerComposeFile = "docker-compose.windows.yaml" + } else { + setupEnvironment() } if !skipBuild { buildAlloy(alloyBinary) } - setupEnvironment(dockerComposeFile) - if specificTest != "" { fmt.Println("Running", specificTest) if !filepath.IsAbs(specificTest) && !strings.HasPrefix(specificTest, testFolder) { diff --git a/internal/cmd/integration-tests/tests-windows/windows/config.alloy b/internal/cmd/integration-tests/tests-windows/windows/config.alloy index aa795e229c..82aea7b83e 100644 --- a/internal/cmd/integration-tests/tests-windows/windows/config.alloy +++ b/internal/cmd/integration-tests/tests-windows/windows/config.alloy @@ -9,7 +9,7 @@ prometheus.scrape "default" { prometheus.remote_write "default" { endpoint { - url = "http://localhost:9009/api/v1/push" + url = "http://localhost:9090/receive" metadata_config { send_interval = "1s" } diff --git a/internal/cmd/integration-tests/tests-windows/windows/windows_metrics_test.go b/internal/cmd/integration-tests/tests-windows/windows/windows_metrics_test.go index f78f9dc7d5..cfae5cd620 100644 --- a/internal/cmd/integration-tests/tests-windows/windows/windows_metrics_test.go +++ b/internal/cmd/integration-tests/tests-windows/windows/windows_metrics_test.go @@ -3,20 +3,111 @@ package main import ( + "context" + "fmt" + "io" + "net/http" "testing" + "time" - "github.com/grafana/alloy/internal/cmd/integration-tests/common" + "github.com/golang/snappy" + "github.com/prometheus/prometheus/prompb" + "github.com/stretchr/testify/require" ) +// List of expected Windows metrics +var winMetrics = []string{ + "windows_cpu_time_total", // cpu + "windows_cs_logical_processors", // cs + "windows_logical_disk_info", // logical_disk + "windows_net_bytes_received_total", // net + "windows_os_info", // os + "windows_service_info", // service + "windows_system_system_up_time", // system +} + +// TestWindowsMetrics sets up a server to receive remote write requests +// and checks if required metrics appear within a one minute timeout func TestWindowsMetrics(t *testing.T) { - var winMetrics = []string{ - "windows_cpu_time_total", // cpu - "windows_cs_logical_processors", // cs - "windows_logical_disk_info", // logical_disk - "windows_net_bytes_received_total", // net - "windows_os_info", // os - "windows_service_info", // service - "windows_system_system_up_time", // system + foundMetrics := make(map[string]bool) + for _, metric := range winMetrics { + foundMetrics[metric] = false } - common.MimirMetricsTest(t, winMetrics, []string{}, "win_metrics") + + done := make(chan bool) + + srv := &http.Server{Addr: ":9090"} + + http.HandleFunc("/receive", func(w http.ResponseWriter, r *http.Request) { + ts, _ := handlePost(t, w, r) + + // Check each received time series for required metrics + for _, timeseries := range ts { + for _, label := range timeseries.Labels { + metricName := label.Name + if label.Name == "__name__" { + metricName = label.Value + } + + for _, requiredMetric := range winMetrics { + if requiredMetric == metricName && !foundMetrics[requiredMetric] { + foundMetrics[requiredMetric] = true + } + } + } + } + + allFound := true + for _, found := range foundMetrics { + if !found { + allFound = false + break + } + } + + if allFound { + done <- true + } + }) + + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + panic(fmt.Errorf("could not start server: %v", err)) + } + }() + defer srv.Shutdown(context.Background()) + + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + defer cancel() + + select { + case <-ctx.Done(): + missingMetrics := []string{} + for metric, found := range foundMetrics { + if !found { + missingMetrics = append(missingMetrics, metric) + } + } + if len(missingMetrics) > 0 { + t.Errorf("Timeout reached. Missing metrics: %v", missingMetrics) + } else { + t.Log("All required metrics received.") + } + case <-done: + t.Log("All required metrics received within the timeout.") + } +} + +func handlePost(t *testing.T, _ http.ResponseWriter, r *http.Request) ([]prompb.TimeSeries, []prompb.MetricMetadata) { + defer r.Body.Close() + data, err := io.ReadAll(r.Body) + require.NoError(t, err) + + data, err = snappy.Decode(nil, data) + require.NoError(t, err) + + var req prompb.WriteRequest + err = req.Unmarshal(data) + require.NoError(t, err) + return req.GetTimeseries(), req.Metadata } diff --git a/internal/cmd/integration-tests/utils.go b/internal/cmd/integration-tests/utils.go index 2f3696fe5e..d9f837817a 100644 --- a/internal/cmd/integration-tests/utils.go +++ b/internal/cmd/integration-tests/utils.go @@ -34,8 +34,8 @@ func buildAlloy(alloyBinary string) { executeCommand("make", []string{"-C", "../../..", "alloy", fmt.Sprintf("ALLOY_BINARY=%s", alloyBinary)}, "Building Alloy") } -func setupEnvironment(dockerComposeFile string) { - executeCommand("docker", []string{"compose", "-f", dockerComposeFile, "up", "-d"}, "Setting up environment with Docker Compose") +func setupEnvironment() { + executeCommand("docker", []string{"compose", "up", "-d"}, "Setting up environment with Docker Compose") fmt.Println("Sleep for 45 seconds to ensure that the env has time to initialize...") time.Sleep(45 * time.Second) }