|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package datadogfleetautomationextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/datadogfleetautomationextension" |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder" |
| 10 | + implgzip "github.com/DataDog/datadog-agent/pkg/util/compression/impl-gzip" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "go.opentelemetry.io/collector/component" |
| 13 | + "go.uber.org/zap" |
| 14 | + "go.uber.org/zap/zapcore" |
| 15 | + |
| 16 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/datadog" |
| 17 | +) |
| 18 | + |
| 19 | +func TestAgentComponents_NewSerializer(t *testing.T) { |
| 20 | + // Create a zap logger for testing |
| 21 | + config := zap.NewProductionConfig() |
| 22 | + config.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel) |
| 23 | + logger, err := config.Build() |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("Failed to build logger: %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + // Create a TelemetrySettings with the test logger |
| 29 | + telemetrySettings := component.TelemetrySettings{ |
| 30 | + Logger: logger, |
| 31 | + } |
| 32 | + zlog := &datadog.Zaplogger{ |
| 33 | + Logger: logger, |
| 34 | + } |
| 35 | + |
| 36 | + // Create a test Config |
| 37 | + cfg := &Config{} |
| 38 | + cfg.API.Key = "test-api-key" |
| 39 | + cfg.API.Site = "test-site" |
| 40 | + |
| 41 | + // Create a config component |
| 42 | + configComponent := newConfigComponent(telemetrySettings, cfg) |
| 43 | + |
| 44 | + // Create a log component |
| 45 | + logComponent := newLogComponent(telemetrySettings) |
| 46 | + |
| 47 | + // Create a forwarder |
| 48 | + forwarder := newForwarder(configComponent, logComponent) |
| 49 | + |
| 50 | + // Create a compressor |
| 51 | + compressor := newCompressor() |
| 52 | + |
| 53 | + // Call newSerializer |
| 54 | + serial := newSerializer(forwarder, compressor, configComponent, zlog, "test-hostname") |
| 55 | + |
| 56 | + // Assert that the returned serializer is not nil |
| 57 | + assert.NotNil(t, serial) |
| 58 | + |
| 59 | + // Assert that the serializer has the correct configuration |
| 60 | + assert.Equal(t, forwarder, serial.Forwarder) |
| 61 | + assert.Equal(t, compressor, serial.Strategy) |
| 62 | +} |
| 63 | + |
| 64 | +func TestAgentComponents_NewCompressor(t *testing.T) { |
| 65 | + // Call newCompressor |
| 66 | + compressor := newCompressor() |
| 67 | + |
| 68 | + // Assert that the returned compressor is not nil |
| 69 | + assert.NotNil(t, compressor) |
| 70 | + |
| 71 | + // Assert that the returned compressor is of type *compression.GzipCompressor |
| 72 | + _, ok := compressor.(*implgzip.GzipStrategy) |
| 73 | + assert.True(t, ok, "Expected compressor to be of type *compression.GzipCompressor") |
| 74 | +} |
| 75 | + |
| 76 | +func TestAgentComponents_NewForwarder(t *testing.T) { |
| 77 | + // Create a zap logger for testing |
| 78 | + config := zap.NewProductionConfig() |
| 79 | + config.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel) |
| 80 | + logger, err := config.Build() |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("Failed to build logger: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + // Create a TelemetrySettings with the test logger |
| 86 | + telemetrySettings := component.TelemetrySettings{ |
| 87 | + Logger: logger, |
| 88 | + } |
| 89 | + |
| 90 | + // Create a test Config |
| 91 | + cfg := &Config{} |
| 92 | + cfg.API.Key = "test-api-key" |
| 93 | + cfg.API.Site = "test-site" |
| 94 | + |
| 95 | + // Create a config component |
| 96 | + configComponent := newConfigComponent(telemetrySettings, cfg) |
| 97 | + |
| 98 | + // Create a log component |
| 99 | + logComponent := newLogComponent(telemetrySettings) |
| 100 | + |
| 101 | + // Call newForwarder |
| 102 | + forwarder := newForwarder(configComponent, logComponent) |
| 103 | + |
| 104 | + // Assert that the returned forwarder is not nil |
| 105 | + assert.NotNil(t, forwarder) |
| 106 | + |
| 107 | + // Assert that the returned forwarder is of type *defaultforwarder.DefaultForwarder |
| 108 | + _, ok := forwarder.(*defaultforwarder.DefaultForwarder) |
| 109 | + assert.True(t, ok, "Expected forwarder to be of type *defaultforwarder.DefaultForwarder") |
| 110 | + |
| 111 | + // Assert that forwarder implements defaultForwarderInterface |
| 112 | + _, ok = forwarder.(defaultForwarderInterface) |
| 113 | + assert.True(t, ok, "Expected forwarder to implement defaultForwarderInterface") |
| 114 | +} |
| 115 | + |
| 116 | +func TestAgentComponents_NewLogComponent(t *testing.T) { |
| 117 | + // Create a zap logger for testing |
| 118 | + config := zap.NewProductionConfig() |
| 119 | + config.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel) |
| 120 | + logger, err := config.Build() |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("Failed to build logger: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + // Create a TelemetrySettings with the test logger |
| 126 | + telemetrySettings := component.TelemetrySettings{ |
| 127 | + Logger: logger, |
| 128 | + } |
| 129 | + |
| 130 | + // Call newLogComponent |
| 131 | + logComponent := newLogComponent(telemetrySettings) |
| 132 | + |
| 133 | + // Assert that the returned component is not nil |
| 134 | + assert.NotNil(t, logComponent) |
| 135 | + |
| 136 | + // Assert that the returned component is of type *datadog.Zaplogger |
| 137 | + zlog, ok := logComponent.(*datadog.Zaplogger) |
| 138 | + assert.True(t, ok, "Expected logComponent to be of type *datadog.Zaplogger") |
| 139 | + |
| 140 | + // Assert that the logger is correctly set |
| 141 | + assert.Equal(t, logger, zlog.Logger) |
| 142 | +} |
| 143 | + |
| 144 | +func TestAgentComponents_NewConfigComponent(t *testing.T) { |
| 145 | + // Create a zap logger for testing |
| 146 | + config := zap.NewProductionConfig() |
| 147 | + config.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel) |
| 148 | + logger, err := config.Build() |
| 149 | + if err != nil { |
| 150 | + t.Fatalf("Failed to build logger: %v", err) |
| 151 | + } |
| 152 | + |
| 153 | + // Create a TelemetrySettings with the test logger |
| 154 | + telemetrySettings := component.TelemetrySettings{ |
| 155 | + Logger: logger, |
| 156 | + } |
| 157 | + |
| 158 | + // Create a test Config |
| 159 | + cfg := &Config{} |
| 160 | + cfg.API.Key = "test-api-key" |
| 161 | + cfg.API.Site = "test-site" |
| 162 | + |
| 163 | + // Call newConfigComponent |
| 164 | + configComponent := newConfigComponent(telemetrySettings, cfg) |
| 165 | + |
| 166 | + // Assert that the returned component is not nil |
| 167 | + assert.NotNil(t, configComponent) |
| 168 | + |
| 169 | + // Assert that the configuration values are set correctly |
| 170 | + assert.Equal(t, "test-api-key", configComponent.GetString("api_key")) |
| 171 | + assert.Equal(t, "test-site", configComponent.GetString("site")) |
| 172 | + assert.Equal(t, "info", configComponent.GetString("log_level")) |
| 173 | + assert.True(t, configComponent.GetBool("logs_enabled")) |
| 174 | + assert.True(t, configComponent.GetBool("enable_payloads.events")) |
| 175 | + assert.True(t, configComponent.GetBool("enable_payloads.json_to_v1_intake")) |
| 176 | + assert.True(t, configComponent.GetBool("enable_sketch_stream_payload_serialization")) |
| 177 | + assert.Equal(t, 60, configComponent.GetInt("forwarder_apikey_validation_interval")) |
| 178 | + assert.Equal(t, 1, configComponent.GetInt("forwarder_num_workers")) |
| 179 | + assert.Equal(t, 2, configComponent.GetInt("logging_frequency")) |
| 180 | + assert.Equal(t, 2, configComponent.GetInt("forwarder_backoff_factor")) |
| 181 | + assert.Equal(t, 2, configComponent.GetInt("forwarder_backoff_base")) |
| 182 | + assert.Equal(t, 64, configComponent.GetInt("forwarder_backoff_max")) |
| 183 | + assert.Equal(t, 2, configComponent.GetInt("forwarder_recovery_interval")) |
| 184 | +} |
0 commit comments