| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelcol |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
|
| 16 |
"go.opentelemetry.io/collector/component" |
| 17 |
) |
| 18 |
|
| 19 |
func TestNewBuildSubCommand(t *testing.T) { |
| 20 |
set := CollectorSettings{ |
| 21 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 22 |
Factories: nopFactories, |
| 23 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}), |
| 24 |
ProviderModules: map[string]string{ |
| 25 |
"nop": "go.opentelemetry.io/collector/confmap/provider/testprovider v1.2.3", |
| 26 |
}, |
| 27 |
ConverterModules: []string{ |
| 28 |
"go.opentelemetry.io/collector/converter/testconverter v1.2.3", |
| 29 |
}, |
| 30 |
} |
| 31 |
cmd := NewCommand(set) |
| 32 |
cmd.SetArgs([]string{"components"}) |
| 33 |
|
| 34 |
ExpectedOutput, err := os.ReadFile(filepath.Join("testdata", "components-output.yaml")) |
| 35 |
require.NoError(t, err) |
| 36 |
|
| 37 |
b := bytes.NewBufferString("") |
| 38 |
cmd.SetOut(b) |
| 39 |
err = cmd.Execute() |
| 40 |
require.NoError(t, err) |
| 41 |
|
| 42 |
// Trim new line at the end of the two strings to make a better comparison as string() adds an extra new |
| 43 |
// line that makes the test fail. |
| 44 |
assert.Equal(t, strings.ReplaceAll(strings.ReplaceAll(string(ExpectedOutput), "\n", ""), "\r", ""), strings.ReplaceAll(strings.ReplaceAll(b.String(), "\n", ""), "\r", "")) |
| 45 |
} |