Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for config.go #144

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
// {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add some additional test cases here. Let's merge this in and iterate on another tasks to add more test cases.

// 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]