|
| 1 | +package elasticsearch |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "time" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + obs "github.com/openshift/cluster-logging-operator/api/observability/v1" |
| 11 | + "github.com/openshift/cluster-logging-operator/test/framework/functional" |
| 12 | + |
| 13 | + "github.com/openshift/cluster-logging-operator/test/helpers/types" |
| 14 | + obstestruntime "github.com/openshift/cluster-logging-operator/test/runtime/observability" |
| 15 | +) |
| 16 | + |
| 17 | +var _ = Describe("[Functional][Outputs][ElasticSearch] Logforwarding to VictoriaLogs", func() { |
| 18 | + |
| 19 | + var ( |
| 20 | + framework *functional.CollectorFunctionalFramework |
| 21 | + |
| 22 | + // Template expected as output Log |
| 23 | + outputLogTemplate = functional.NewApplicationLogTemplate() |
| 24 | + ) |
| 25 | + |
| 26 | + Context("should write to victorialogs", func() { |
| 27 | + DescribeTable("with custom headers", func(headers map[string]string) { |
| 28 | + outputLogTemplate.ViaqIndexName = "app-write" |
| 29 | + framework = functional.NewCollectorFunctionalFramework() |
| 30 | + obstestruntime.NewClusterLogForwarderBuilder(framework.Forwarder). |
| 31 | + FromInput(obs.InputTypeApplication). |
| 32 | + ToElasticSearchOutput(func(output *obs.OutputSpec) { |
| 33 | + output.Elasticsearch.Headers = headers |
| 34 | + output.Elasticsearch.URL = "http://0.0.0.0:9428/insert/elasticsearch/" |
| 35 | + }) |
| 36 | + defer framework.Cleanup() |
| 37 | + Expect(framework.Deploy()).To(BeNil()) |
| 38 | + timestamp := functional.CRIOTime(time.Now()) |
| 39 | + ukr := "привіт " |
| 40 | + jp := "こんにちは " |
| 41 | + ch := "你好" |
| 42 | + msg := functional.NewCRIOLogMessage(timestamp, ukr+jp+ch, false) |
| 43 | + Expect(framework.WriteMessagesToApplicationLog(msg, 1)).To(BeNil()) |
| 44 | + Expect(framework.WriteMessagesWithNotUTF8SymbolsToLog()).To(BeNil()) |
| 45 | + requestHeaders := map[string]string{ |
| 46 | + "AccountID": "0", |
| 47 | + "ProjectID": "0", |
| 48 | + } |
| 49 | + for headerName := range requestHeaders { |
| 50 | + if v, ok := headers[headerName]; ok { |
| 51 | + requestHeaders[headerName] = v |
| 52 | + } |
| 53 | + } |
| 54 | + raw, err := framework.GetLogsFromVL(string(obs.OutputTypeElasticsearch), requestHeaders) |
| 55 | + Expect(err).To(BeNil(), "Expected no errors reading the logs") |
| 56 | + Expect(raw).To(Not(BeEmpty())) |
| 57 | + // Parse log line |
| 58 | + var logs []types.ApplicationLog |
| 59 | + err = types.StrictlyParseLogsFromSlice(raw, &logs) |
| 60 | + Expect(err).To(BeNil(), "Expected no errors parsing the logs") |
| 61 | + Expect(len(logs)).To(Equal(2)) |
| 62 | + //sort log by time before matching |
| 63 | + sort.Slice(logs, func(i, j int) bool { |
| 64 | + return logs[i].TimestampLegacy.Before(logs[j].TimestampLegacy) |
| 65 | + }) |
| 66 | + |
| 67 | + Expect(logs[0].Message).To(Equal(ukr + jp + ch)) |
| 68 | + Expect(logs[1].Message).To(Equal("������������")) |
| 69 | + }, |
| 70 | + Entry("Non-default tenant ID", map[string]string{ |
| 71 | + "AccountID": "10", |
| 72 | + "ProjectID": "10", |
| 73 | + "VL-Msg-Field": "message", |
| 74 | + }), |
| 75 | + Entry("Add extra fields", map[string]string{ |
| 76 | + "VL-Extra-Fields": "field_name=field_value", |
| 77 | + }), |
| 78 | + ) |
| 79 | + }) |
| 80 | +}) |
0 commit comments