Path: blob/main/component/loki/source/docker/internal/dockertarget/target_test.go
4096 views
package dockertarget12// This code is copied from Promtail. The dockertarget package is used to3// configure and run the targets that can read logs from Docker containers and4// forward them to other loki components.56import (7"encoding/json"8"net/http"9"net/http/httptest"10"os"11"sort"12"strings"13"testing"14"time"1516"github.com/grafana/agent/component/common/loki/client/fake"1718"github.com/docker/docker/api/types"19"github.com/docker/docker/api/types/container"20"github.com/docker/docker/client"21"github.com/go-kit/log"22"github.com/grafana/agent/component/common/loki/positions"23"github.com/prometheus/client_golang/prometheus"24"github.com/prometheus/common/model"25"github.com/prometheus/prometheus/model/relabel"26"github.com/stretchr/testify/require"27)2829func TestDockerTarget(t *testing.T) {30h := func(w http.ResponseWriter, r *http.Request) {31switch path := r.URL.Path; {32case strings.HasSuffix(path, "/logs"):33dat, err := os.ReadFile("testdata/flog.log")34require.NoError(t, err)35_, err = w.Write(dat)36require.NoError(t, err)37default:38w.Header().Set("Content-Type", "application/json")39info := types.ContainerJSON{40ContainerJSONBase: &types.ContainerJSONBase{},41Mounts: []types.MountPoint{},42Config: &container.Config{Tty: false},43NetworkSettings: &types.NetworkSettings{},44}45err := json.NewEncoder(w).Encode(info)46require.NoError(t, err)47}48}4950ts := httptest.NewServer(http.HandlerFunc(h))51defer ts.Close()5253w := log.NewSyncWriter(os.Stderr)54logger := log.NewLogfmtLogger(w)55entryHandler := fake.NewClient(func() {})56client, err := client.NewClientWithOpts(client.WithHost(ts.URL))57require.NoError(t, err)5859ps, err := positions.New(logger, positions.Config{60SyncPeriod: 10 * time.Second,61PositionsFile: t.TempDir() + "/positions.yml",62})63require.NoError(t, err)6465tgt, err := NewTarget(66NewMetrics(prometheus.NewRegistry()),67logger,68entryHandler,69ps,70"flog",71model.LabelSet{"job": "docker"},72[]*relabel.Config{},73client,74)75require.NoError(t, err)76tgt.StartIfNotRunning()7778require.Eventually(t, func() bool {79return len(entryHandler.Received()) >= 580}, 5*time.Second, 100*time.Millisecond)8182received := entryHandler.Received()83sort.Slice(received, func(i, j int) bool {84return received[i].Timestamp.Before(received[j].Timestamp)85})8687expectedLines := []string{88"5.3.69.55 - - [09/Dec/2021:09:15:02 +0000] \"HEAD /brand/users/clicks-and-mortar/front-end HTTP/2.0\" 503 27087",89"101.54.183.185 - - [09/Dec/2021:09:15:03 +0000] \"POST /next-generation HTTP/1.0\" 416 11468",90"69.27.137.160 - runolfsdottir2670 [09/Dec/2021:09:15:03 +0000] \"HEAD /content/visionary/engineer/cultivate HTTP/1.1\" 302 2975",91"28.104.242.74 - - [09/Dec/2021:09:15:03 +0000] \"PATCH /value-added/cultivate/systems HTTP/2.0\" 405 11843",92"150.187.51.54 - satterfield1852 [09/Dec/2021:09:15:03 +0000] \"GET /incentivize/deliver/innovative/cross-platform HTTP/1.1\" 301 13032",93}94actualLines := make([]string, 0, 5)95for _, entry := range received[:5] {96actualLines = append(actualLines, entry.Line)97}98require.ElementsMatch(t, actualLines, expectedLines)99}100101102