Skip to content

Commit

Permalink
fix windows test
Browse files Browse the repository at this point in the history
  • Loading branch information
wildum committed Nov 8, 2024
1 parent 539f4fe commit afc96aa
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 29 deletions.
12 changes: 0 additions & 12 deletions internal/cmd/integration-tests/docker-compose.windows.yaml

This file was deleted.

6 changes: 2 additions & 4 deletions internal/cmd/integration-tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions internal/cmd/integration-tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit afc96aa

Please sign in to comment.