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

[receiver/k8sobjects] Switch to current config testing pattern #26078

Merged
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
263 changes: 132 additions & 131 deletions receiver/k8sobjectsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,83 +13,151 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"
"k8s.io/apimachinery/pkg/runtime/schema"

"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) {
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
}{
{
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: "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, "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,
},
},
{
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, "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
Expand Down Expand Up @@ -121,75 +189,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)

}
24 changes: 23 additions & 1 deletion receiver/k8sobjectsreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,26 @@ k8sobjects:
- name: events
mode: watch
group: events.k8s.io
namespaces: [default]
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

This file was deleted.

4 changes: 0 additions & 4 deletions receiver/k8sobjectsreceiver/testdata/invalid_config.yaml

This file was deleted.

This file was deleted.