|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package loadgenreceiver |
| 19 | + |
| 20 | +import ( |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/elastic/opentelemetry-collector-components/receiver/loadgenreceiver/internal/metadata" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "go.opentelemetry.io/collector/component" |
| 27 | + "go.opentelemetry.io/collector/confmap/confmaptest" |
| 28 | + "go.opentelemetry.io/collector/confmap/xconfmap" |
| 29 | +) |
| 30 | + |
| 31 | +func TestLoadConfig(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + id component.ID |
| 34 | + expected component.Config |
| 35 | + expectedErrMessage string |
| 36 | + }{ |
| 37 | + { |
| 38 | + id: component.NewID(metadata.Type), |
| 39 | + expected: &Config{ |
| 40 | + Metrics: MetricsConfig{ |
| 41 | + SignalConfig: SignalConfig{ |
| 42 | + MaxBufferSize: maxScannerBufSize, |
| 43 | + }, |
| 44 | + AddCounterAttr: true, |
| 45 | + }, |
| 46 | + Logs: LogsConfig{ |
| 47 | + SignalConfig: SignalConfig{ |
| 48 | + MaxBufferSize: maxScannerBufSize, |
| 49 | + }, |
| 50 | + }, |
| 51 | + Traces: TracesConfig{ |
| 52 | + SignalConfig: SignalConfig{ |
| 53 | + MaxBufferSize: maxScannerBufSize, |
| 54 | + }, |
| 55 | + }, |
| 56 | + Concurrency: 1, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + id: component.NewIDWithName(metadata.Type, "logs_invalid_max_replay"), |
| 61 | + expectedErrMessage: "logs::max_replay must be >= 0", |
| 62 | + }, |
| 63 | + { |
| 64 | + id: component.NewIDWithName(metadata.Type, "metrics_invalid_max_replay"), |
| 65 | + expectedErrMessage: "metrics::max_replay must be >= 0", |
| 66 | + }, |
| 67 | + { |
| 68 | + id: component.NewIDWithName(metadata.Type, "traces_invalid_max_replay"), |
| 69 | + expectedErrMessage: "traces::max_replay must be >= 0", |
| 70 | + }, |
| 71 | + { |
| 72 | + id: component.NewIDWithName(metadata.Type, "logs_invalid_max_buffer_size"), |
| 73 | + expectedErrMessage: "logs::max_buffer_size must be >= 0", |
| 74 | + }, |
| 75 | + { |
| 76 | + id: component.NewIDWithName(metadata.Type, "metrics_invalid_max_buffer_size"), |
| 77 | + expectedErrMessage: "metrics::max_buffer_size must be >= 0", |
| 78 | + }, |
| 79 | + { |
| 80 | + id: component.NewIDWithName(metadata.Type, "traces_invalid_max_buffer_size"), |
| 81 | + expectedErrMessage: "traces::max_buffer_size must be >= 0", |
| 82 | + }, |
| 83 | + } |
| 84 | + for _, tt := range tests { |
| 85 | + t.Run(tt.id.String(), func(t *testing.T) { |
| 86 | + t.Parallel() |
| 87 | + |
| 88 | + cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) |
| 89 | + assert.NoError(t, err) |
| 90 | + |
| 91 | + factory := NewFactory() |
| 92 | + cfg := factory.CreateDefaultConfig() |
| 93 | + sub, err := cm.Sub(tt.id.String()) |
| 94 | + assert.NoError(t, err) |
| 95 | + assert.NoError(t, sub.Unmarshal(cfg)) |
| 96 | + |
| 97 | + err = xconfmap.Validate(cfg) |
| 98 | + if tt.expectedErrMessage != "" { |
| 99 | + assert.EqualError(t, err, tt.expectedErrMessage) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + assert.NoError(t, err) |
| 104 | + assert.Equal(t, tt.expected, cfg) |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments