|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package datadogexporter |
| 16 | + |
| 17 | +import ( |
| 18 | + "path" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + "go.opentelemetry.io/collector/component/componenttest" |
| 24 | + "go.opentelemetry.io/collector/config/configmodels" |
| 25 | + "go.opentelemetry.io/collector/config/configtest" |
| 26 | +) |
| 27 | + |
| 28 | +// TestLoadConfig tests that the configuration is loaded correctly |
| 29 | +func TestLoadConfig(t *testing.T) { |
| 30 | + factories, err := componenttest.ExampleComponents() |
| 31 | + assert.NoError(t, err) |
| 32 | + |
| 33 | + factory := NewFactory() |
| 34 | + factories.Exporters[typeStr] = factory |
| 35 | + cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories) |
| 36 | + |
| 37 | + require.NoError(t, err) |
| 38 | + require.NotNil(t, cfg) |
| 39 | + |
| 40 | + e0 := cfg.Exporters["datadog"].(*Config) |
| 41 | + err = e0.Sanitize() |
| 42 | + |
| 43 | + require.NoError(t, err) |
| 44 | + assert.Equal(t, e0, &Config{ |
| 45 | + ExporterSettings: configmodels.ExporterSettings{ |
| 46 | + NameVal: "datadog", |
| 47 | + TypeVal: "datadog", |
| 48 | + }, |
| 49 | + APIKey: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 50 | + Site: DefaultSite, |
| 51 | + MetricsEndpoint: "https://api.datadoghq.com", |
| 52 | + }) |
| 53 | + |
| 54 | + e1 := cfg.Exporters["datadog/2"].(*Config) |
| 55 | + err = e1.Sanitize() |
| 56 | + |
| 57 | + require.NoError(t, err) |
| 58 | + assert.Equal(t, e1, |
| 59 | + &Config{ |
| 60 | + ExporterSettings: configmodels.ExporterSettings{ |
| 61 | + NameVal: "datadog/2", |
| 62 | + TypeVal: "datadog", |
| 63 | + }, |
| 64 | + APIKey: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 65 | + Site: "datadoghq.eu", |
| 66 | + MetricsEndpoint: "https://api.datadoghq.eu", |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +// TestOverrideMetricsEndpoint tests that the metrics endpoint is overridden |
| 71 | +// correctly when set manually. |
| 72 | +func TestOverrideMetricsEndpoint(t *testing.T) { |
| 73 | + |
| 74 | + const DebugEndpoint string = "http://localhost:8080" |
| 75 | + |
| 76 | + cfg := &Config{ |
| 77 | + APIKey: "notnull", |
| 78 | + Site: DefaultSite, |
| 79 | + MetricsEndpoint: DebugEndpoint, |
| 80 | + } |
| 81 | + |
| 82 | + err := cfg.Sanitize() |
| 83 | + require.NoError(t, err) |
| 84 | + assert.Equal(t, cfg.MetricsEndpoint, DebugEndpoint) |
| 85 | +} |
| 86 | + |
| 87 | +// TestUnsetAPIKey tests that the config sanitizing throws an error |
| 88 | +// when the API key has not been set |
| 89 | +func TestUnsetAPIKey(t *testing.T) { |
| 90 | + |
| 91 | + cfg := &Config{} |
| 92 | + err := cfg.Sanitize() |
| 93 | + |
| 94 | + require.Error(t, err) |
| 95 | +} |
0 commit comments