namespace_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package kubemodel
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/opencost/opencost/core/pkg/model/kubemodel"
  8. "github.com/opencost/opencost/core/pkg/source"
  9. )
  10. func TestComputeNamespaces(t *testing.T) {
  11. start := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
  12. end := start.Add(time.Hour)
  13. tests := []struct {
  14. name string
  15. overrides map[string]any
  16. want map[string]*kubemodel.Namespace
  17. }{
  18. {
  19. name: "no data returns empty namespace map",
  20. overrides: map[string]any{},
  21. want: map[string]*kubemodel.Namespace{},
  22. },
  23. {
  24. name: "basic namespace info and uptime",
  25. overrides: map[string]any{
  26. source.QueryNamespaceInfo: []*source.NamespaceInfoResult{
  27. {UID: "ns-1", Namespace: "default"},
  28. },
  29. source.QueryNamespaceUptime: []*source.UptimeResult{
  30. {UID: "ns-1", First: start, Last: end},
  31. },
  32. },
  33. want: map[string]*kubemodel.Namespace{
  34. "ns-1": {
  35. UID: "ns-1",
  36. Name: "default",
  37. Start: start,
  38. End: end,
  39. },
  40. },
  41. },
  42. {
  43. name: "namespace without uptime is not registered",
  44. overrides: map[string]any{
  45. source.QueryNamespaceInfo: []*source.NamespaceInfoResult{
  46. {UID: "ns-1", Namespace: "default"},
  47. },
  48. },
  49. want: map[string]*kubemodel.Namespace{},
  50. },
  51. {
  52. name: "namespace labels are attached",
  53. overrides: map[string]any{
  54. source.QueryNamespaceInfo: []*source.NamespaceInfoResult{
  55. {UID: "ns-1", Namespace: "production"},
  56. },
  57. source.QueryNamespaceUptime: []*source.UptimeResult{
  58. {UID: "ns-1", First: start, Last: end},
  59. },
  60. source.QueryNamespaceLabels: []*source.NamespaceLabelsResult{
  61. {UID: "ns-1", Labels: map[string]string{"env": "prod", "team": "platform"}},
  62. },
  63. },
  64. want: map[string]*kubemodel.Namespace{
  65. "ns-1": {
  66. UID: "ns-1",
  67. Name: "production",
  68. Start: start,
  69. End: end,
  70. Labels: map[string]string{"env": "prod", "team": "platform"},
  71. },
  72. },
  73. },
  74. {
  75. name: "namespace annotations are attached",
  76. overrides: map[string]any{
  77. source.QueryNamespaceInfo: []*source.NamespaceInfoResult{
  78. {UID: "ns-1", Namespace: "staging"},
  79. },
  80. source.QueryNamespaceUptime: []*source.UptimeResult{
  81. {UID: "ns-1", First: start, Last: end},
  82. },
  83. source.QueryNamespaceAnnotations: []*source.NamespaceAnnotationsResult{
  84. {UID: "ns-1", Annotations: map[string]string{"owner": "team-a"}},
  85. },
  86. },
  87. want: map[string]*kubemodel.Namespace{
  88. "ns-1": {
  89. UID: "ns-1",
  90. Name: "staging",
  91. Start: start,
  92. End: end,
  93. Annotations: map[string]string{"owner": "team-a"},
  94. },
  95. },
  96. },
  97. {
  98. name: "multiple namespaces are registered",
  99. overrides: map[string]any{
  100. source.QueryNamespaceInfo: []*source.NamespaceInfoResult{
  101. {UID: "ns-1", Namespace: "default"},
  102. {UID: "ns-2", Namespace: "kube-system"},
  103. },
  104. source.QueryNamespaceUptime: []*source.UptimeResult{
  105. {UID: "ns-1", First: start, Last: end},
  106. {UID: "ns-2", First: start, Last: end},
  107. },
  108. },
  109. want: map[string]*kubemodel.Namespace{
  110. "ns-1": {UID: "ns-1", Name: "default", Start: start, End: end},
  111. "ns-2": {UID: "ns-2", Name: "kube-system", Start: start, End: end},
  112. },
  113. },
  114. {
  115. name: "uptime for unknown namespace is ignored",
  116. overrides: map[string]any{
  117. source.QueryNamespaceInfo: []*source.NamespaceInfoResult{
  118. {UID: "ns-1", Namespace: "default"},
  119. },
  120. source.QueryNamespaceUptime: []*source.UptimeResult{
  121. {UID: "ns-1", First: start, Last: end},
  122. {UID: "unknown-ns", First: start, Last: end},
  123. },
  124. },
  125. want: map[string]*kubemodel.Namespace{
  126. "ns-1": {UID: "ns-1", Name: "default", Start: start, End: end},
  127. },
  128. },
  129. }
  130. for _, tt := range tests {
  131. t.Run(tt.name, func(t *testing.T) {
  132. ds := source.NewMockOpenCostDataSource()
  133. ds.ResolutionValue = 5 * time.Minute
  134. seedCluster(ds, start, end)
  135. for method, result := range tt.overrides {
  136. ds.Querier.SetOverride(method, result)
  137. }
  138. km, err := NewKubeModel(testClusterUID, false, ds)
  139. require.NoError(t, err)
  140. kms, err := km.ComputeKubeModelSet(start, end)
  141. require.NoError(t, err)
  142. assert.Equal(t, tt.want, kms.Namespaces)
  143. })
  144. }
  145. }