informer_transform_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package k8sclusterreceiver
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. appsv1 "k8s.io/api/apps/v1"
  8. corev1 "k8s.io/api/core/v1"
  9. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  10. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/testutils"
  11. )
  12. func TestTransformObject(t *testing.T) {
  13. i := 1
  14. intPtr := &i
  15. tests := []struct {
  16. name string
  17. object any
  18. want any
  19. same bool
  20. }{
  21. {
  22. name: "pod",
  23. object: testutils.NewPodWithContainer(
  24. "1",
  25. testutils.NewPodSpecWithContainer("container-name"),
  26. testutils.NewPodStatusWithContainer("container-name", "container-id"),
  27. ),
  28. want: func() *corev1.Pod {
  29. pod := testutils.NewPodWithContainer(
  30. "1",
  31. testutils.NewPodSpecWithContainer("container-name"),
  32. testutils.NewPodStatusWithContainer("container-name", "container-id"),
  33. )
  34. pod.Spec.Containers[0].Image = ""
  35. pod.Status.ContainerStatuses[0].State = corev1.ContainerState{}
  36. return pod
  37. }(),
  38. same: false,
  39. },
  40. {
  41. name: "node",
  42. object: testutils.NewNode("1"),
  43. want: testutils.NewNode("1"),
  44. same: false,
  45. },
  46. {
  47. name: "replicaset",
  48. object: testutils.NewReplicaSet("1"),
  49. want: testutils.NewReplicaSet("1"),
  50. same: false,
  51. },
  52. {
  53. name: "job",
  54. object: testutils.NewJob("1"),
  55. want: testutils.NewJob("1"),
  56. same: false,
  57. },
  58. {
  59. name: "deployment",
  60. object: testutils.NewDeployment("1"),
  61. want: testutils.NewDeployment("1"),
  62. same: false,
  63. },
  64. {
  65. name: "daemonset",
  66. object: testutils.NewDaemonset("1"),
  67. want: testutils.NewDaemonset("1"),
  68. same: false,
  69. },
  70. {
  71. name: "statefulset",
  72. object: &appsv1.StatefulSet{
  73. Spec: appsv1.StatefulSetSpec{
  74. Replicas: func() *int32 { i := int32(3); return &i }(),
  75. Template: corev1.PodTemplateSpec{
  76. ObjectMeta: metav1.ObjectMeta{
  77. Labels: map[string]string{
  78. "app": "my-app",
  79. },
  80. },
  81. },
  82. },
  83. },
  84. want: &appsv1.StatefulSet{
  85. Spec: appsv1.StatefulSetSpec{
  86. Replicas: func() *int32 { i := int32(3); return &i }(),
  87. },
  88. },
  89. same: false,
  90. },
  91. {
  92. name: "service",
  93. object: &corev1.Service{
  94. Spec: corev1.ServiceSpec{
  95. Selector: map[string]string{
  96. "app": "my-app",
  97. },
  98. Type: corev1.ServiceTypeClusterIP,
  99. },
  100. },
  101. want: &corev1.Service{
  102. Spec: corev1.ServiceSpec{
  103. Selector: map[string]string{
  104. "app": "my-app",
  105. },
  106. },
  107. },
  108. same: false,
  109. },
  110. {
  111. // This is a case where we don't transform the object.
  112. name: "hpa",
  113. object: testutils.NewHPA("1"),
  114. want: testutils.NewHPA("1"),
  115. same: true,
  116. },
  117. {
  118. name: "invalid_type",
  119. object: intPtr,
  120. want: intPtr,
  121. same: true,
  122. },
  123. }
  124. for _, tt := range tests {
  125. t.Run(tt.name, func(t *testing.T) {
  126. got, err := transformObject(tt.object)
  127. assert.NoError(t, err)
  128. assert.Equal(t, tt.want, got)
  129. if tt.same {
  130. assert.Same(t, tt.object, got)
  131. } else {
  132. assert.NotSame(t, tt.object, got)
  133. }
  134. })
  135. }
  136. }