factory_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package k8sclusterreceiver
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. quotaclientset "github.com/openshift/client-go/quota/clientset/versioned"
  9. fakeQuota "github.com/openshift/client-go/quota/clientset/versioned/fake"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/component"
  13. "go.opentelemetry.io/collector/component/componenttest"
  14. "go.opentelemetry.io/collector/consumer/consumertest"
  15. "go.opentelemetry.io/collector/receiver/receivertest"
  16. "k8s.io/client-go/kubernetes"
  17. "k8s.io/client-go/kubernetes/fake"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent"
  20. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
  21. )
  22. func TestFactory(t *testing.T) {
  23. f := NewFactory()
  24. require.Equal(t, component.Type("k8s_cluster"), f.Type())
  25. cfg := f.CreateDefaultConfig()
  26. rCfg, ok := cfg.(*Config)
  27. require.True(t, ok)
  28. require.Equal(t, &Config{
  29. Distribution: distributionKubernetes,
  30. CollectionInterval: 10 * time.Second,
  31. NodeConditionTypesToReport: defaultNodeConditionsToReport,
  32. APIConfig: k8sconfig.APIConfig{
  33. AuthType: k8sconfig.AuthTypeServiceAccount,
  34. },
  35. MetadataCollectionInterval: 5 * time.Minute,
  36. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  37. }, rCfg)
  38. r, err := f.CreateTracesReceiver(
  39. context.Background(), receivertest.NewNopCreateSettings(),
  40. cfg, consumertest.NewNop(),
  41. )
  42. require.Error(t, err)
  43. require.Nil(t, r)
  44. r = newTestReceiver(t, rCfg)
  45. // Test metadata exporters setup.
  46. ctx := context.Background()
  47. require.NoError(t, r.Start(ctx, newNopHostWithExporters()))
  48. require.NoError(t, r.Shutdown(ctx))
  49. rCfg.MetadataExporters = []string{"nop/withoutmetadata"}
  50. r = newTestReceiver(t, rCfg)
  51. require.Error(t, r.Start(context.Background(), newNopHostWithExporters()))
  52. }
  53. func TestFactoryDistributions(t *testing.T) {
  54. f := NewFactory()
  55. require.Equal(t, component.Type("k8s_cluster"), f.Type())
  56. cfg := f.CreateDefaultConfig()
  57. rCfg, ok := cfg.(*Config)
  58. require.True(t, ok)
  59. // default
  60. r := newTestReceiver(t, rCfg)
  61. err := r.Start(context.Background(), componenttest.NewNopHost())
  62. require.NoError(t, err)
  63. require.Nil(t, r.resourceWatcher.osQuotaClient)
  64. // openshift
  65. rCfg.Distribution = "openshift"
  66. r = newTestReceiver(t, rCfg)
  67. err = r.Start(context.Background(), componenttest.NewNopHost())
  68. require.NoError(t, err)
  69. require.NotNil(t, r.resourceWatcher.osQuotaClient)
  70. }
  71. func newTestReceiver(t *testing.T, cfg *Config) *kubernetesReceiver {
  72. r, err := newReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg)
  73. require.NoError(t, err)
  74. require.NotNil(t, r)
  75. rcvr, ok := r.(*kubernetesReceiver)
  76. require.True(t, ok)
  77. rcvr.resourceWatcher.makeClient = func(_ k8sconfig.APIConfig) (kubernetes.Interface, error) {
  78. return fake.NewSimpleClientset(), nil
  79. }
  80. rcvr.resourceWatcher.makeOpenShiftQuotaClient = func(_ k8sconfig.APIConfig) (quotaclientset.Interface, error) {
  81. return fakeQuota.NewSimpleClientset(), nil
  82. }
  83. return rcvr
  84. }
  85. // nopHostWithExporters mocks a receiver.ReceiverHost for test purposes.
  86. type nopHostWithExporters struct {
  87. component.Host
  88. }
  89. func newNopHostWithExporters() component.Host {
  90. return &nopHostWithExporters{Host: componenttest.NewNopHost()}
  91. }
  92. func (n *nopHostWithExporters) GetExporters() map[component.DataType]map[component.ID]component.Component {
  93. return map[component.DataType]map[component.ID]component.Component{
  94. component.DataTypeMetrics: {
  95. component.NewIDWithName("nop", "withoutmetadata"): MockExporter{},
  96. component.NewIDWithName("nop", "withmetadata"): mockExporterWithK8sMetadata{},
  97. },
  98. }
  99. }
  100. func TestNewSharedReceiver(t *testing.T) {
  101. f := NewFactory()
  102. cfg := f.CreateDefaultConfig()
  103. mc := consumertest.NewNop()
  104. mr, err := newMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, mc)
  105. require.NoError(t, err)
  106. // Verify that the metric consumer is correctly set.
  107. kr := mr.(*sharedcomponent.SharedComponent).Unwrap().(*kubernetesReceiver)
  108. assert.Equal(t, mc, kr.metricsConsumer)
  109. lc := consumertest.NewNop()
  110. lr, err := newLogsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, lc)
  111. require.NoError(t, err)
  112. // Verify that the log consumer is correct set.
  113. kr = lr.(*sharedcomponent.SharedComponent).Unwrap().(*kubernetesReceiver)
  114. assert.Equal(t, lc, kr.resourceWatcher.entityLogConsumer)
  115. // Make sure only one receiver is created both for metrics and logs.
  116. assert.Equal(t, mr, lr)
  117. }