From 9520670d978ffafb83db12912b64af8ad3327f4b Mon Sep 17 00:00:00 2001 From: Louis PERDEREAU Date: Sun, 25 Jun 2023 00:46:57 +0200 Subject: [PATCH] feat: Add to detect test --- .github/workflows/go-tests.yml | 6 ++++++ .../modules/loki/internal/lokiclient/loki_client.go | 4 ++-- pkg/acquisition/modules/loki/loki_test.go | 7 +++++-- pkg/setup/detect_test.go | 10 ++++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml index 2a760d149..08392a686 100644 --- a/.github/workflows/go-tests.yml +++ b/.github/workflows/go-tests.yml @@ -108,6 +108,12 @@ jobs: --health-timeout 10s --health-retries 5 + loki: + image: grafana/loki:2.8.0 + ports: + - "3100:3100" + command: -config.file=/etc/loki/local-config.yaml + steps: - name: Check out CrowdSec repository diff --git a/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go b/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go index 5353fd64d..1b11a2284 100644 --- a/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go +++ b/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go @@ -136,14 +136,14 @@ func (lc *LokiClient) getURLFor(endpoint string, params map[string]string) strin return "" } - switch endpoint { - case "loki/api/v1/tail": + if endpoint == "loki/api/v1/tail" { if u.Scheme == "http" { u.Scheme = "ws" } else { u.Scheme = "wss" } } + return u.String() } diff --git a/pkg/acquisition/modules/loki/loki_test.go b/pkg/acquisition/modules/loki/loki_test.go index 6b3e0ce82..74a1a2d1f 100644 --- a/pkg/acquisition/modules/loki/loki_test.go +++ b/pkg/acquisition/modules/loki/loki_test.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "os" @@ -235,7 +235,7 @@ func feedLoki(logger *log.Entry, n int, title string) error { return err } if resp.StatusCode != 204 { - b, _ := ioutil.ReadAll(resp.Body) + b, _ := io.ReadAll(resp.Body) logger.Error(string(b)) return fmt.Errorf("Bad post status %d", resp.StatusCode) } @@ -423,6 +423,9 @@ query: > } time.Sleep(time.Second * 2) feedLoki(subLogger, 1, title) + if err != nil { + t.Fatalf("Unexpected error : %s", err) + } lokiTomb.Kill(nil) err = lokiTomb.Wait() diff --git a/pkg/setup/detect_test.go b/pkg/setup/detect_test.go index 162df0db2..4093ec72d 100644 --- a/pkg/setup/detect_test.go +++ b/pkg/setup/detect_test.go @@ -983,6 +983,16 @@ func TestDetectDatasourceValidation(t *testing.T) { source: kafka`, expected: setup.Setup{Setup: []setup.ServiceSetup{}}, expectedErr: "invalid datasource for foobar: cannot create a kafka reader with an empty list of broker addresses", + }, { + name: "source loki: required fields", + config: ` + version: 1.0 + detect: + foobar: + datasource: + source: loki`, + expected: setup.Setup{Setup: []setup.ServiceSetup{}}, + expectedErr: "invalid datasource for foobar: Loki query is mandatory", }, }