From a6fd5ae9836fd4875636cf51c277281f00fc2451 Mon Sep 17 00:00:00 2001 From: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Wed, 23 Aug 2023 12:35:55 -0600 Subject: [PATCH 1/3] Switch to current config testing pattern --- receiver/k8sobjectsreceiver/config_test.go | 269 +++++++++--------- .../k8sobjectsreceiver/testdata/config.yaml | 24 +- .../config_watch_resource_version.yaml | 11 - .../testdata/invalid_config.yaml | 4 - .../pull_resource_version_config.yaml | 8 - 5 files changed, 157 insertions(+), 159 deletions(-) delete mode 100644 receiver/k8sobjectsreceiver/testdata/config_watch_resource_version.yaml delete mode 100644 receiver/k8sobjectsreceiver/testdata/invalid_config.yaml delete mode 100644 receiver/k8sobjectsreceiver/testdata/pull_resource_version_config.yaml diff --git a/receiver/k8sobjectsreceiver/config_test.go b/receiver/k8sobjectsreceiver/config_test.go index 35eb59daf292..66f5bf8c8ec7 100644 --- a/receiver/k8sobjectsreceiver/config_test.go +++ b/receiver/k8sobjectsreceiver/config_test.go @@ -4,92 +4,158 @@ package k8sobjectsreceiver import ( - "path/filepath" - "testing" - "time" - + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig" + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sobjectsreceiver/internal/metadata" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/confmap/confmaptest" "k8s.io/apimachinery/pkg/runtime/schema" + "path/filepath" + "testing" + "time" ) func TestLoadConfig(t *testing.T) { t.Parallel() - cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) - require.NoError(t, err) - - factory := NewFactory() - cfg := factory.CreateDefaultConfig().(*Config) - - sub, err := cm.Sub("k8sobjects") - require.NoError(t, err) - require.NoError(t, component.UnmarshalConfig(sub, cfg)) - require.NotNil(t, cfg) - - err = component.ValidateConfig(cfg) - require.Error(t, err) - - cfg.makeDiscoveryClient = getMockDiscoveryClient - - err = component.ValidateConfig(cfg) - require.NoError(t, err) - - expected := []*K8sObjectsConfig{ + tests := []struct { + id component.ID + expected *Config + }{ { - Name: "pods", - Mode: PullMode, - Interval: time.Hour, - FieldSelector: "status.phase=Running", - LabelSelector: "environment in (production),tier in (frontend)", - gvr: &schema.GroupVersionResource{ - Group: "", - Version: "v1", - Resource: "pods", + id: component.NewIDWithName(metadata.Type, ""), + expected: &Config{ + APIConfig: k8sconfig.APIConfig{ + AuthType: k8sconfig.AuthTypeServiceAccount, + }, + Objects: []*K8sObjectsConfig{ + { + Name: "pods", + Mode: PullMode, + Interval: time.Hour, + FieldSelector: "status.phase=Running", + LabelSelector: "environment in (production),tier in (frontend)", + gvr: &schema.GroupVersionResource{ + Group: "", + Version: "v1", + Resource: "pods", + }, + }, + { + Name: "events", + Mode: WatchMode, + Namespaces: []string{"default"}, + Group: "events.k8s.io", + ResourceVersion: "", + gvr: &schema.GroupVersionResource{ + Group: "events.k8s.io", + Version: "v1", + Resource: "events", + }, + }, + }, + makeDiscoveryClient: getMockDiscoveryClient, }, }, { - Name: "events", - Mode: WatchMode, - Namespaces: []string{"default"}, - Group: "events.k8s.io", - ResourceVersion: "", - gvr: &schema.GroupVersionResource{ - Group: "events.k8s.io", - Version: "v1", - Resource: "events", + id: component.NewIDWithName(metadata.Type, "pull_with_resource"), + expected: &Config{ + APIConfig: k8sconfig.APIConfig{ + AuthType: k8sconfig.AuthTypeServiceAccount, + }, + Objects: []*K8sObjectsConfig{ + { + Name: "pods", + Mode: PullMode, + ResourceVersion: "1", + Interval: time.Hour, + gvr: &schema.GroupVersionResource{ + Group: "", + Version: "v1", + Resource: "pods", + }, + }, + { + Name: "events", + Mode: PullMode, + Interval: time.Hour, + gvr: &schema.GroupVersionResource{ + Group: "", + Version: "v1", + Resource: "events", + }, + }, + }, + makeDiscoveryClient: getMockDiscoveryClient, }, }, + { + id: component.NewIDWithName(metadata.Type, "watch_with_resource"), + expected: &Config{ + APIConfig: k8sconfig.APIConfig{ + AuthType: k8sconfig.AuthTypeServiceAccount, + }, + Objects: []*K8sObjectsConfig{ + { + Name: "events", + Mode: WatchMode, + Namespaces: []string{"default"}, + Group: "events.k8s.io", + ResourceVersion: "", + gvr: &schema.GroupVersionResource{ + Group: "events.k8s.io", + Version: "v1", + Resource: "events", + }, + }, + { + Name: "events", + Mode: WatchMode, + Namespaces: []string{"default"}, + Group: "events.k8s.io", + ResourceVersion: "2", + gvr: &schema.GroupVersionResource{ + Group: "events.k8s.io", + Version: "v1", + Resource: "events", + }, + }, + }, + makeDiscoveryClient: getMockDiscoveryClient, + }, + }, + { + id: component.NewIDWithName(metadata.Type, "invalid_resource"), + }, } - assert.EqualValues(t, expected, cfg.Objects) - -} - -func TestValidConfigs(t *testing.T) { - t.Parallel() - - cm, err := confmaptest.LoadConf(filepath.Join("testdata", "invalid_config.yaml")) - require.NoError(t, err) - - factory := NewFactory() - cfg := factory.CreateDefaultConfig().(*Config) - - sub, err := cm.Sub("k8sobjects/invalid_resource") - require.NoError(t, err) - require.NoError(t, component.UnmarshalConfig(sub, cfg)) - - cfg.makeDiscoveryClient = getMockDiscoveryClient - - err = component.ValidateConfig(cfg) - assert.ErrorContains(t, err, "resource fake_resource not found") + for _, tt := range tests { + t.Run(tt.id.String(), func(t *testing.T) { + cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) + require.NoError(t, err) + + factory := NewFactory() + cfg := factory.CreateDefaultConfig().(*Config) + cfg.makeDiscoveryClient = getMockDiscoveryClient + + sub, err := cm.Sub(tt.id.String()) + require.NoError(t, err) + require.NoError(t, component.UnmarshalConfig(sub, cfg)) + + if tt.expected == nil { + err = component.ValidateConfig(cfg) + assert.Error(t, err) + return + } + assert.NoError(t, component.ValidateConfig(cfg)) + assert.Equal(t, tt.expected.AuthType, cfg.AuthType) + assert.Equal(t, tt.expected.Objects, cfg.Objects) + }) + } } func TestValidateResourceConflict(t *testing.T) { - t.Parallel() - mockClient := newMockDynamicClient() rCfg := createDefaultConfig().(*Config) rCfg.makeDynamicClient = mockClient.getMockDynamicClient @@ -121,75 +187,8 @@ func TestValidateResourceConflict(t *testing.T) { assert.Equal(t, "group2", rCfg.Objects[0].gvr.Group) } -func TestPullResourceVersion(t *testing.T) { - t.Parallel() - - cm, err := confmaptest.LoadConf(filepath.Join("testdata", "pull_resource_version_config.yaml")) - require.NoError(t, err) - - factory := NewFactory() - cfg := factory.CreateDefaultConfig().(*Config) - - sub, err := cm.Sub("k8sobjects") - require.NoError(t, err) - require.NoError(t, component.UnmarshalConfig(sub, cfg)) - require.NotNil(t, cfg) - - err = component.ValidateConfig(cfg) - require.Error(t, err) - - require.Equal(t, "1", cfg.Objects[0].ResourceVersion) - require.Equal(t, "", cfg.Objects[1].ResourceVersion) -} - -func TestWatchResourceVersion(t *testing.T) { - t.Parallel() - - cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config_watch_resource_version.yaml")) - require.NoError(t, err) - - factory := NewFactory() - cfg := factory.CreateDefaultConfig().(*Config) - - sub, err := cm.Sub("k8sobjects") - require.NoError(t, err) - require.NoError(t, component.UnmarshalConfig(sub, cfg)) - require.NotNil(t, cfg) - - err = component.ValidateConfig(cfg) +func TestClientRequired(t *testing.T) { + rCfg := createDefaultConfig().(*Config) + err := rCfg.Validate() require.Error(t, err) - - cfg.makeDiscoveryClient = getMockDiscoveryClient - - err = component.ValidateConfig(cfg) - require.NoError(t, err) - - expected := []*K8sObjectsConfig{ - { - Name: "events", - Mode: WatchMode, - Namespaces: []string{"default"}, - Group: "events.k8s.io", - ResourceVersion: "", - gvr: &schema.GroupVersionResource{ - Group: "events.k8s.io", - Version: "v1", - Resource: "events", - }, - }, - { - Name: "events", - Mode: WatchMode, - Namespaces: []string{"default"}, - Group: "events.k8s.io", - ResourceVersion: "2", - gvr: &schema.GroupVersionResource{ - Group: "events.k8s.io", - Version: "v1", - Resource: "events", - }, - }, - } - assert.EqualValues(t, expected, cfg.Objects) - } diff --git a/receiver/k8sobjectsreceiver/testdata/config.yaml b/receiver/k8sobjectsreceiver/testdata/config.yaml index 835084b1b429..bb2dcee98866 100644 --- a/receiver/k8sobjectsreceiver/testdata/config.yaml +++ b/receiver/k8sobjectsreceiver/testdata/config.yaml @@ -7,4 +7,26 @@ k8sobjects: - name: events mode: watch group: events.k8s.io - namespaces: [default] \ No newline at end of file + namespaces: [default] +k8sobjects/pull_with_resource: + objects: + - name: pods + mode: pull + resource_version: "1" + - name: events + mode: pull +k8sobjects/watch_with_resource: + objects: + - name: events + mode: watch + group: events.k8s.io + namespaces: [default] + - name: events + mode: watch + group: events.k8s.io + namespaces: [default] + resource_version: "2" +k8sobjects/invalid_resource: + objects: + - name: fake_resource + mode: watch \ No newline at end of file diff --git a/receiver/k8sobjectsreceiver/testdata/config_watch_resource_version.yaml b/receiver/k8sobjectsreceiver/testdata/config_watch_resource_version.yaml deleted file mode 100644 index 352684229b88..000000000000 --- a/receiver/k8sobjectsreceiver/testdata/config_watch_resource_version.yaml +++ /dev/null @@ -1,11 +0,0 @@ -k8sobjects: - objects: - - name: events - mode: watch - group: events.k8s.io - namespaces: [default] - - name: events - mode: watch - group: events.k8s.io - namespaces: [default] - resource_version: "2" \ No newline at end of file diff --git a/receiver/k8sobjectsreceiver/testdata/invalid_config.yaml b/receiver/k8sobjectsreceiver/testdata/invalid_config.yaml deleted file mode 100644 index bb1f99cd6fd2..000000000000 --- a/receiver/k8sobjectsreceiver/testdata/invalid_config.yaml +++ /dev/null @@ -1,4 +0,0 @@ -k8sobjects/invalid_resource: - objects: - - name: fake_resource - mode: watch \ No newline at end of file diff --git a/receiver/k8sobjectsreceiver/testdata/pull_resource_version_config.yaml b/receiver/k8sobjectsreceiver/testdata/pull_resource_version_config.yaml deleted file mode 100644 index bc5de3144377..000000000000 --- a/receiver/k8sobjectsreceiver/testdata/pull_resource_version_config.yaml +++ /dev/null @@ -1,8 +0,0 @@ -k8sobjects: - objects: - - name: pods - mode: pull - resource_version: "1" - - name: events - mode: pull - \ No newline at end of file From 7da0bd8e6690bfd581ffefee8322e613183506f6 Mon Sep 17 00:00:00 2001 From: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Wed, 23 Aug 2023 12:37:33 -0600 Subject: [PATCH 2/3] Add newline --- receiver/k8sobjectsreceiver/testdata/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/receiver/k8sobjectsreceiver/testdata/config.yaml b/receiver/k8sobjectsreceiver/testdata/config.yaml index bb2dcee98866..765d06269eba 100644 --- a/receiver/k8sobjectsreceiver/testdata/config.yaml +++ b/receiver/k8sobjectsreceiver/testdata/config.yaml @@ -29,4 +29,4 @@ k8sobjects/watch_with_resource: k8sobjects/invalid_resource: objects: - name: fake_resource - mode: watch \ No newline at end of file + mode: watch From 0a14b43ac94522ad31016daa99b10d72572971eb Mon Sep 17 00:00:00 2001 From: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Wed, 23 Aug 2023 12:38:33 -0600 Subject: [PATCH 3/3] Fix lint --- receiver/k8sobjectsreceiver/config_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/receiver/k8sobjectsreceiver/config_test.go b/receiver/k8sobjectsreceiver/config_test.go index 66f5bf8c8ec7..293f73245cb0 100644 --- a/receiver/k8sobjectsreceiver/config_test.go +++ b/receiver/k8sobjectsreceiver/config_test.go @@ -4,16 +4,18 @@ package k8sobjectsreceiver import ( - "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig" - "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sobjectsreceiver/internal/metadata" + "path/filepath" + "testing" + "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/confmap/confmaptest" "k8s.io/apimachinery/pkg/runtime/schema" - "path/filepath" - "testing" - "time" + + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig" + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sobjectsreceiver/internal/metadata" ) func TestLoadConfig(t *testing.T) {