소스 검색

feat: Add to detect test

Louis PERDEREAU 2 년 전
부모
커밋
9520670d97

+ 6 - 0
.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

+ 2 - 2
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()
 }
 

+ 5 - 2
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()

+ 10 - 0
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",
 		},
 	}