feat: Add to detect test

This commit is contained in:
Louis PERDEREAU 2023-06-25 00:46:57 +02:00 committed by lperdereau
parent 2017e6b113
commit 9520670d97
4 changed files with 23 additions and 4 deletions

View file

@ -108,6 +108,12 @@ jobs:
--health-timeout 10s --health-timeout 10s
--health-retries 5 --health-retries 5
loki:
image: grafana/loki:2.8.0
ports:
- "3100:3100"
command: -config.file=/etc/loki/local-config.yaml
steps: steps:
- name: Check out CrowdSec repository - name: Check out CrowdSec repository

View file

@ -136,14 +136,14 @@ func (lc *LokiClient) getURLFor(endpoint string, params map[string]string) strin
return "" return ""
} }
switch endpoint { if endpoint == "loki/api/v1/tail" {
case "loki/api/v1/tail":
if u.Scheme == "http" { if u.Scheme == "http" {
u.Scheme = "ws" u.Scheme = "ws"
} else { } else {
u.Scheme = "wss" u.Scheme = "wss"
} }
} }
return u.String() return u.String()
} }

View file

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -235,7 +235,7 @@ func feedLoki(logger *log.Entry, n int, title string) error {
return err return err
} }
if resp.StatusCode != 204 { if resp.StatusCode != 204 {
b, _ := ioutil.ReadAll(resp.Body) b, _ := io.ReadAll(resp.Body)
logger.Error(string(b)) logger.Error(string(b))
return fmt.Errorf("Bad post status %d", resp.StatusCode) return fmt.Errorf("Bad post status %d", resp.StatusCode)
} }
@ -423,6 +423,9 @@ query: >
} }
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
feedLoki(subLogger, 1, title) feedLoki(subLogger, 1, title)
if err != nil {
t.Fatalf("Unexpected error : %s", err)
}
lokiTomb.Kill(nil) lokiTomb.Kill(nil)
err = lokiTomb.Wait() err = lokiTomb.Wait()

View file

@ -983,6 +983,16 @@ func TestDetectDatasourceValidation(t *testing.T) {
source: kafka`, source: kafka`,
expected: setup.Setup{Setup: []setup.ServiceSetup{}}, expected: setup.Setup{Setup: []setup.ServiceSetup{}},
expectedErr: "invalid datasource for foobar: cannot create a kafka reader with an empty list of broker addresses", 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",
}, },
} }