From b5109cf55a7243407e429f74b72fb72291711783 Mon Sep 17 00:00:00 2001 From: Nicholas Shaddox <98495215+nsshaddox@users.noreply.github.com> Date: Wed, 11 Oct 2023 06:11:54 -0700 Subject: [PATCH] test: add tests for config.go (#144) --- .../gitproviderreceiver/config_test.go | 63 +++++++++++++++++-- .../testdata/config-invalidscraperkey.yaml | 20 ++++++ 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 pkg/receiver/gitproviderreceiver/testdata/config-invalidscraperkey.yaml diff --git a/pkg/receiver/gitproviderreceiver/config_test.go b/pkg/receiver/gitproviderreceiver/config_test.go index b4a1a1e6..e4f8694d 100644 --- a/pkg/receiver/gitproviderreceiver/config_test.go +++ b/pkg/receiver/gitproviderreceiver/config_test.go @@ -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) { @@ -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) + } + }) + } +} diff --git a/pkg/receiver/gitproviderreceiver/testdata/config-invalidscraperkey.yaml b/pkg/receiver/gitproviderreceiver/testdata/config-invalidscraperkey.yaml new file mode 100644 index 00000000..077ccef6 --- /dev/null +++ b/pkg/receiver/gitproviderreceiver/testdata/config-invalidscraperkey.yaml @@ -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]