From 353d8bf81919a6648ff66b3b08efbea51f9662e6 Mon Sep 17 00:00:00 2001 From: vsamidurai Date: Fri, 25 Aug 2023 17:38:54 +0100 Subject: [PATCH] fix: image tests - use local image instead of previous version Signed-off-by: vsamidurai --- base-image/Makefile | 2 +- base-image/fluentd_test.go | 63 +++++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/base-image/Makefile b/base-image/Makefile index ff611a2..e17da04 100644 --- a/base-image/Makefile +++ b/base-image/Makefile @@ -34,7 +34,7 @@ test: build-image $(IMAGE):$(TAG) build-test-ci: build-image - go test -mod=readonly -v -count=1 --race ./... + TEST_IMAGE_NAME=$(IMAGE) TEST_IMAGE_TAG=$(TAG) go test -mod=readonly -v -count=1 --race ./... list-gems: @docker run -ti --rm \ diff --git a/base-image/fluentd_test.go b/base-image/fluentd_test.go index f983e6f..feba57a 100644 --- a/base-image/fluentd_test.go +++ b/base-image/fluentd_test.go @@ -25,12 +25,23 @@ var counterTotal = 5 func TestFluentd(t *testing.T) { assert := assert.New(t) path, err := os.Getwd() + log.Println(path) if err != nil { log.Println(err) } ctx := context.Background() + + imageName := os.Getenv("TEST_IMAGE_NAME") + if imageName == "" { + imageName = "vmware/base-fluentd-operator" + } + imageTag := os.Getenv("TEST_IMAGE_TAG") + if imageTag != "" { + imageName = fmt.Sprintf("%s:%s", imageName, imageTag) + } + req := testcontainers.ContainerRequest{ - Image: "vmware/base-fluentd-operator:latest", + Image: imageName, Env: map[string]string{ "FLUENTD_OPT": "--no-supervisor", }, @@ -64,6 +75,7 @@ func TestFluentd(t *testing.T) { NetworkMode: "host", WaitingFor: wait.ForLog("Found configuration file: /fluentd/etc/fluent.conf"), } + container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ContainerRequest: req, Started: true, @@ -77,7 +89,7 @@ func TestFluentd(t *testing.T) { } }() startReceiverServer() - time.Sleep(15 * time.Second) + time.Sleep(30 * time.Second) mu.Lock() assert.Equal(counterTotal, counterOutput) mu.Unlock() @@ -85,28 +97,49 @@ func TestFluentd(t *testing.T) { func startReceiverServer() { server := &http.Server{ - Addr: ":9090", + Addr: "0.0.0.0:9090", } http.HandleFunc("/", printLogs) - go server.ListenAndServe() + go func() { + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalf("Could not start server: %s", err.Error()) + } + }() + } func printLogs(w http.ResponseWriter, r *http.Request) { bodyBytes, err := io.ReadAll(r.Body) - tagName := r.Header["Tag"][0] + if err != nil { + log.Printf("Error reading request body: %v", err) + return + } + + tags, ok := r.Header["Tag"] + if !ok || len(tags) == 0 { + http.Error(w, "Tag header not found", http.StatusBadRequest) + } + tagName := tags[0] + bodyString := string(bodyBytes) - log.Println("Received data for tag: " + tagName) - b, err := os.ReadFile("test/results/" + tagName + ".out") // just pass the file name + log.Printf("Received data for tag: %s", tagName) + + b, err := os.ReadFile(fmt.Sprintf("test/results/%s.out", tagName)) if err != nil { - log.Fatal(err) + log.Printf("Error reading result file for tag %s: %v", tagName, err) } - str := string(b) // convert content to a 'string' + + str := string(b) + log.Printf("Result from file: %s", str) + if str != bodyString { - log.Fatal("Unmatch for tag " + tagName) - } else { - mu.Lock() - counterOutput++ - mu.Unlock() - log.Println("Matching results for tag " + tagName + " and counter value: " + fmt.Sprint(counterOutput)) + log.Printf("Unmatch for tag %s", tagName) + http.Error(w, "Mismatched data", http.StatusBadRequest) + return } + + mu.Lock() + defer mu.Unlock() // Always release the lock + counterOutput++ + log.Printf("Matching results for tag %s and counter value: %d", tagName, counterOutput) }