Skip to content

Commit

Permalink
test: add tests for config.go (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsshaddox authored Oct 11, 2023
1 parent fe9f131 commit b5109cf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
63 changes: 59 additions & 4 deletions pkg/receiver/gitproviderreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"testing"
"time"

"github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver/internal"
"github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver/internal/metadata"
"github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver/internal/scraper/githubscraper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/otelcol/otelcoltest"
"go.opentelemetry.io/collector/receiver/scraperhelper"

"github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver/internal"
"github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver/internal/metadata"
"github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver/internal/scraper/githubscraper"
)

func TestLoadConfig(t *testing.T) {
Expand Down Expand Up @@ -63,3 +63,58 @@ func TestLoadInvalidConfig_NoScrapers(t *testing.T) {

require.Contains(t, err.Error(), "must specify at least one scraper")
}

func TestLoadInvalidConfig_InvalidScraperKey(t *testing.T) {
factories, err := otelcoltest.NopFactories()
require.NoError(t, err)

factory := NewFactory()
factories.Receivers[metadata.Type] = factory
_, err = otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-invalidscraperkey.yaml"), factories)

require.Contains(t, err.Error(), "error reading configuration for \"gitprovider\": invalid scraper key: invalidscraperkey")
}

func TestConfig_Unmarshal(t *testing.T) {
type fields struct {
ScraperControllerSettings scraperhelper.ScraperControllerSettings
Scrapers map[string]internal.Config
MetricsBuilderConfig metadata.MetricsBuilderConfig
}

type args struct {
componentParser *confmap.Conf
}

tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "Empty Component Parser",
fields: fields{},
args: args{componentParser: nil},
wantErr: false,
},
// {
// name: "valid Compenent Parser",
// fields: fields{},
// args: args{ componentParser: notnil },
// wantErr: false,
// },
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfg := &Config{
ScraperControllerSettings: test.fields.ScraperControllerSettings,
Scrapers: test.fields.Scrapers,
MetricsBuilderConfig: test.fields.MetricsBuilderConfig,
}
if err := cfg.Unmarshal(test.args.componentParser); (err != nil) != test.wantErr {
t.Errorf("Config.Unmarshal() error = %v, wantErr %v", err, test.wantErr)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
receivers:
gitprovider:
initial_delay: 1s
collection_interval: 60s
scrapers:
invalidscraperkey:

processors:
nop:

exporters:
nop:

service:
pipelines:
metrics:
receivers: [gitprovider]
processors: [nop]
exporters: [nop]

0 comments on commit b5109cf

Please sign in to comment.