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
15package hostmetricsreceiver
16
17import (
18	"context"
19	"fmt"
20	"testing"
21
22	"github.com/stretchr/testify/assert"
23
24	"go.opentelemetry.io/collector/component/componenterror"
25	"go.opentelemetry.io/collector/component/componenttest"
26	"go.opentelemetry.io/collector/config/configcheck"
27	"go.opentelemetry.io/collector/consumer/consumertest"
28	"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
29)
30
31var creationSet = componenttest.NewNopReceiverCreateSettings()
32
33func TestCreateDefaultConfig(t *testing.T) {
34	factory := NewFactory()
35	cfg := factory.CreateDefaultConfig()
36	assert.NotNil(t, cfg, "failed to create default config")
37	assert.NoError(t, configcheck.ValidateConfig(cfg))
38}
39
40func TestCreateReceiver(t *testing.T) {
41	factory := NewFactory()
42	cfg := factory.CreateDefaultConfig()
43
44	tReceiver, err := factory.CreateTracesReceiver(context.Background(), creationSet, cfg, consumertest.NewNop())
45	assert.Equal(t, err, componenterror.ErrDataTypeIsNotSupported)
46	assert.Nil(t, tReceiver)
47
48	mReceiver, err := factory.CreateMetricsReceiver(context.Background(), creationSet, cfg, consumertest.NewNop())
49	assert.NoError(t, err)
50	assert.NotNil(t, mReceiver)
51
52	tLogs, err := factory.CreateLogsReceiver(context.Background(), creationSet, cfg, consumertest.NewNop())
53	assert.Equal(t, err, componenterror.ErrDataTypeIsNotSupported)
54	assert.Nil(t, tLogs)
55}
56
57func TestCreateReceiver_ScraperKeyConfigError(t *testing.T) {
58	const errorKey string = "error"
59
60	factory := NewFactory()
61	cfg := &Config{Scrapers: map[string]internal.Config{errorKey: &mockConfig{}}}
62
63	_, err := factory.CreateMetricsReceiver(context.Background(), creationSet, cfg, consumertest.NewNop())
64	assert.EqualError(t, err, fmt.Sprintf("host metrics scraper factory not found for key: %q", errorKey))
65}
66