|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2016-present Datadog, Inc. |
| 5 | + |
| 6 | +package eventplatformimpl |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/suite" |
| 12 | + |
| 13 | + "github.com/DataDog/datadog-agent/comp/core/config" |
| 14 | + "github.com/DataDog/datadog-agent/comp/forwarder/eventplatformreceiver" |
| 15 | + "github.com/DataDog/datadog-agent/comp/forwarder/eventplatformreceiver/eventplatformreceiverimpl" |
| 16 | + laconfig "github.com/DataDog/datadog-agent/comp/logs/agent/config" |
| 17 | + logscompression "github.com/DataDog/datadog-agent/comp/serializer/logscompression/def" |
| 18 | + logscompressionfxmock "github.com/DataDog/datadog-agent/comp/serializer/logscompression/fx-mock" |
| 19 | + "github.com/DataDog/datadog-agent/pkg/util/fxutil" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + defaultUseCompression = true |
| 24 | + zstdCompressionKind = "zstd" |
| 25 | + defaultZstdCompressionLevel = 1 |
| 26 | + gzipCompressionKind = "gzip" |
| 27 | + defaultGzipCompressionLevel = 6 |
| 28 | +) |
| 29 | + |
| 30 | +type EventPlatformForwarderTestSuite struct { |
| 31 | + suite.Suite |
| 32 | + config config.Component |
| 33 | + receiver eventplatformreceiver.Component |
| 34 | + compression logscompression.Component |
| 35 | +} |
| 36 | + |
| 37 | +func (suite *EventPlatformForwarderTestSuite) SetupTest() { |
| 38 | + suite.config = config.NewMock(suite.T()) |
| 39 | + |
| 40 | + suite.receiver = fxutil.Test[eventplatformreceiver.Component](suite.T(), eventplatformreceiverimpl.MockModule()) |
| 41 | + suite.compression = fxutil.Test[logscompression.Component](suite.T(), logscompressionfxmock.MockModule()) |
| 42 | +} |
| 43 | + |
| 44 | +func TestEventPlatformForwarderTestSuite(t *testing.T) { |
| 45 | + suite.Run(t, new(EventPlatformForwarderTestSuite)) |
| 46 | +} |
| 47 | + |
| 48 | +func (suite *EventPlatformForwarderTestSuite) TestNewHTTPPassthroughPipelineCompression() { |
| 49 | + |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + configSetup func(config.Component) |
| 53 | + expectedKind string |
| 54 | + expectedLevel int |
| 55 | + useCompression bool |
| 56 | + }{ |
| 57 | + { |
| 58 | + name: "additional endpoints", |
| 59 | + configSetup: func(c config.Component) { |
| 60 | + c.SetWithoutSource("database_monitoring.metrics.additional_endpoints", `[{"api_key":"foo","host":"bar"}]`) |
| 61 | + }, |
| 62 | + expectedKind: gzipCompressionKind, |
| 63 | + expectedLevel: defaultGzipCompressionLevel, |
| 64 | + useCompression: defaultUseCompression, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "no compression", |
| 68 | + configSetup: func(c config.Component) { |
| 69 | + c.SetWithoutSource("database_monitoring.metrics.use_compression", false) |
| 70 | + }, |
| 71 | + expectedKind: "none", |
| 72 | + expectedLevel: 0, |
| 73 | + useCompression: !defaultUseCompression, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "default compression", |
| 77 | + configSetup: func(_ config.Component) {}, |
| 78 | + expectedKind: zstdCompressionKind, |
| 79 | + expectedLevel: defaultZstdCompressionLevel, |
| 80 | + useCompression: defaultUseCompression, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "zstd custom compression level", |
| 84 | + configSetup: func(c config.Component) { |
| 85 | + c.SetWithoutSource("database_monitoring.metrics.compression_kind", "zstd") |
| 86 | + c.SetWithoutSource("database_monitoring.metrics.zstd_compression_level", 3) |
| 87 | + }, |
| 88 | + expectedKind: zstdCompressionKind, |
| 89 | + expectedLevel: 3, |
| 90 | + useCompression: defaultUseCompression, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "gzip compression", |
| 94 | + configSetup: func(c config.Component) { |
| 95 | + c.SetWithoutSource("database_monitoring.metrics.compression_kind", "gzip") |
| 96 | + }, |
| 97 | + expectedKind: gzipCompressionKind, |
| 98 | + expectedLevel: defaultGzipCompressionLevel, |
| 99 | + useCompression: defaultUseCompression, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "gzip custom compression level", |
| 103 | + configSetup: func(c config.Component) { |
| 104 | + c.SetWithoutSource("database_monitoring.metrics.compression_kind", "gzip") |
| 105 | + c.SetWithoutSource("database_monitoring.metrics.compression_level", 8) |
| 106 | + }, |
| 107 | + expectedKind: gzipCompressionKind, |
| 108 | + expectedLevel: 8, |
| 109 | + useCompression: defaultUseCompression, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "invalid compression", |
| 113 | + configSetup: func(c config.Component) { |
| 114 | + c.SetWithoutSource("database_monitoring.metrics.use_compression", true) |
| 115 | + c.SetWithoutSource("database_monitoring.metrics.compression_kind", "gipz") |
| 116 | + }, |
| 117 | + expectedKind: zstdCompressionKind, |
| 118 | + expectedLevel: defaultZstdCompressionLevel, |
| 119 | + useCompression: defaultUseCompression, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, t := range tests { |
| 124 | + suite.Run(t.name, func() { |
| 125 | + |
| 126 | + t.configSetup(suite.config) |
| 127 | + |
| 128 | + desc := passthroughPipelineDesc{ |
| 129 | + // Only registered config prefixes trigger correct parsing and defaults. |
| 130 | + endpointsConfigPrefix: "database_monitoring.metrics.", |
| 131 | + } |
| 132 | + |
| 133 | + pipeline, err := newHTTPPassthroughPipeline( |
| 134 | + suite.config, |
| 135 | + nil, |
| 136 | + suite.compression, |
| 137 | + desc, |
| 138 | + nil, |
| 139 | + 0, |
| 140 | + ) |
| 141 | + suite.Require().NoError(err) |
| 142 | + suite.Require().NotNil(pipeline) |
| 143 | + |
| 144 | + configKeys := laconfig.NewLogsConfigKeys(desc.endpointsConfigPrefix, suite.config) |
| 145 | + endpoints, err := laconfig.BuildHTTPEndpointsWithConfig( |
| 146 | + suite.config, |
| 147 | + configKeys, |
| 148 | + "", // hostnameEndpointPrefix not needed |
| 149 | + "", // intakeTrackType not needed |
| 150 | + "", // protocol not needed |
| 151 | + "", // origin not needed |
| 152 | + ) |
| 153 | + suite.Require().NoError(err) |
| 154 | + |
| 155 | + // Check compression settings on endpoint |
| 156 | + suite.Equal(t.useCompression, endpoints.Main.UseCompression, "UseCompression mismatch") |
| 157 | + if t.useCompression { |
| 158 | + suite.Equal(t.expectedKind, endpoints.Main.CompressionKind, "CompressionKind mismatch") |
| 159 | + suite.Equal(t.expectedLevel, endpoints.Main.CompressionLevel, "CompressionLevel mismatch") |
| 160 | + } |
| 161 | + }) |
| 162 | + suite.resetCompression() |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func (suite *EventPlatformForwarderTestSuite) resetCompression() { |
| 167 | + // Reset compression settings to default state |
| 168 | + suite.config.SetWithoutSource("database_monitoring.metrics.use_compression", true) |
| 169 | + suite.config.SetWithoutSource("database_monitoring.metrics.compression_kind", "zstd") |
| 170 | + suite.config.SetWithoutSource("database_monitoring.metrics.compression_level", 6) |
| 171 | + suite.config.SetWithoutSource("database_monitoring.metrics.zstd_compression_level", defaultZstdCompressionLevel) |
| 172 | + suite.config.SetWithoutSource("database_monitoring.metrics.additional_endpoints", "{}") |
| 173 | + |
| 174 | +} |
0 commit comments