// Code generated by mdatagen. DO NOT EDIT.

package metadata

import (
	"path/filepath"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"go.opentelemetry.io/collector/confmap"
	"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestMetricsBuilderConfig(t *testing.T) {
	tests := []struct {
		name string
		want MetricsBuilderConfig
	}{
		{
			name: "default",
			want: NewDefaultMetricsBuilderConfig(),
		},
		{
			name: "all_set",
			want: MetricsBuilderConfig{
				Metrics: MetricsConfig{
					K8sPodCPUTime: K8sPodCPUTimeMetricConfig{
						Enabled: true,
					},
					K8sPodPhase: K8sPodPhaseMetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []K8sPodPhaseMetricAttributeKey{K8sPodPhaseMetricAttributeKeyPhase},
					},
					K8sReplicasetDesired: K8sReplicasetDesiredMetricConfig{
						Enabled: true,
					},
				},
				ResourceAttributes: ResourceAttributesConfig{
					K8sNamespaceName:  K8sNamespaceNameResourceAttributeConfig{Enabled: true},
					K8sPodName:        K8sPodNameResourceAttributeConfig{Enabled: true},
					K8sPodUID:         K8sPodUIDResourceAttributeConfig{Enabled: true},
					K8sReplicasetName: K8sReplicasetNameResourceAttributeConfig{Enabled: true},
					K8sReplicasetUID:  K8sReplicasetUIDResourceAttributeConfig{Enabled: true},
				},
			},
		},
		{
			name: "none_set",
			want: MetricsBuilderConfig{
				Metrics: MetricsConfig{
					K8sPodCPUTime: K8sPodCPUTimeMetricConfig{
						Enabled: false,
					},
					K8sPodPhase: K8sPodPhaseMetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []K8sPodPhaseMetricAttributeKey{K8sPodPhaseMetricAttributeKeyPhase},
					},
					K8sReplicasetDesired: K8sReplicasetDesiredMetricConfig{
						Enabled: false,
					},
				},
				ResourceAttributes: ResourceAttributesConfig{
					K8sNamespaceName:  K8sNamespaceNameResourceAttributeConfig{Enabled: false},
					K8sPodName:        K8sPodNameResourceAttributeConfig{Enabled: false},
					K8sPodUID:         K8sPodUIDResourceAttributeConfig{Enabled: false},
					K8sReplicasetName: K8sReplicasetNameResourceAttributeConfig{Enabled: false},
					K8sReplicasetUID:  K8sReplicasetUIDResourceAttributeConfig{Enabled: false},
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cfg := loadMetricsBuilderConfig(t, tt.name)
			diff := cmp.Diff(tt.want, cfg, cmpopts.IgnoreUnexported(K8sPodCPUTimeMetricConfig{}, K8sPodPhaseMetricConfig{}, K8sReplicasetDesiredMetricConfig{}, K8sNamespaceNameResourceAttributeConfig{}, K8sPodNameResourceAttributeConfig{}, K8sPodUIDResourceAttributeConfig{}, K8sReplicasetNameResourceAttributeConfig{}, K8sReplicasetUIDResourceAttributeConfig{}))
			require.Emptyf(t, diff, "Config mismatch (-expected +actual):\n%s", diff)
		})
	}
}

func TestK8sPodPhaseMetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().K8sPodPhase
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []K8sPodPhaseMetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric k8s.pod.phase doesn't have an attribute invalid, valid attributes: [phase]")

	cfg = DefaultMetricsConfig().K8sPodPhase
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func loadMetricsBuilderConfig(t *testing.T, name string) MetricsBuilderConfig {
	cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
	require.NoError(t, err)
	sub, err := cm.Sub(name)
	require.NoError(t, err)
	cfg := NewDefaultMetricsBuilderConfig()
	require.NoError(t, sub.Unmarshal(&cfg, confmap.WithIgnoreUnused()))
	return cfg
}

func TestResourceAttributesConfig(t *testing.T) {
	tests := []struct {
		name string
		want ResourceAttributesConfig
	}{
		{
			name: "default",
			want: DefaultResourceAttributesConfig(),
		},
		{
			name: "all_set",
			want: ResourceAttributesConfig{
				K8sNamespaceName:  K8sNamespaceNameResourceAttributeConfig{Enabled: true},
				K8sPodName:        K8sPodNameResourceAttributeConfig{Enabled: true},
				K8sPodUID:         K8sPodUIDResourceAttributeConfig{Enabled: true},
				K8sReplicasetName: K8sReplicasetNameResourceAttributeConfig{Enabled: true},
				K8sReplicasetUID:  K8sReplicasetUIDResourceAttributeConfig{Enabled: true},
			},
		},
		{
			name: "none_set",
			want: ResourceAttributesConfig{
				K8sNamespaceName:  K8sNamespaceNameResourceAttributeConfig{Enabled: false},
				K8sPodName:        K8sPodNameResourceAttributeConfig{Enabled: false},
				K8sPodUID:         K8sPodUIDResourceAttributeConfig{Enabled: false},
				K8sReplicasetName: K8sReplicasetNameResourceAttributeConfig{Enabled: false},
				K8sReplicasetUID:  K8sReplicasetUIDResourceAttributeConfig{Enabled: false},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cfg := loadResourceAttributesConfig(t, tt.name)
			diff := cmp.Diff(tt.want, cfg, cmpopts.IgnoreUnexported(K8sNamespaceNameResourceAttributeConfig{}, K8sPodNameResourceAttributeConfig{}, K8sPodUIDResourceAttributeConfig{}, K8sReplicasetNameResourceAttributeConfig{}, K8sReplicasetUIDResourceAttributeConfig{}))
			require.Emptyf(t, diff, "Config mismatch (-expected +actual):\n%s", diff)
		})
	}
}

func TestResourceAttributesOverrideConfig(t *testing.T) {
	cfg := loadResourceAttributesConfig(t, "override_set")
	assert.NotNil(t, cfg.K8sNamespaceName.OverrideValue, "override_value should be set for k8s.namespace.name")
	assert.NotNil(t, cfg.K8sPodName.OverrideValue, "override_value should be set for k8s.pod.name")
	assert.NotNil(t, cfg.K8sPodUID.OverrideValue, "override_value should be set for k8s.pod.uid")
	assert.NotNil(t, cfg.K8sReplicasetName.OverrideValue, "override_value should be set for k8s.replicaset.name")
	assert.NotNil(t, cfg.K8sReplicasetUID.OverrideValue, "override_value should be set for k8s.replicaset.uid")
}

func loadResourceAttributesConfig(t *testing.T, name string) ResourceAttributesConfig {
	cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
	require.NoError(t, err)
	sub, err := cm.Sub(name)
	require.NoError(t, err)
	sub, err = sub.Sub("resource_attributes")
	require.NoError(t, err)
	cfg := DefaultResourceAttributesConfig()
	require.NoError(t, sub.Unmarshal(&cfg))
	return cfg
}
