deployment_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 TestComputeDeployments(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.Deployment
  17. }{
  18. {
  19. name: "no data returns empty deployment map",
  20. overrides: map[string]any{},
  21. want: map[string]*kubemodel.Deployment{},
  22. },
  23. {
  24. name: "basic deployment info and uptime",
  25. overrides: map[string]any{
  26. source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
  27. {UID: "dep-1", Deployment: "my-app", NamespaceUID: "ns-1"},
  28. },
  29. source.QueryDeploymentUptime: []*source.UptimeResult{
  30. {UID: "dep-1", First: start, Last: end},
  31. },
  32. },
  33. want: map[string]*kubemodel.Deployment{
  34. "dep-1": {
  35. UID: "dep-1",
  36. Name: "my-app",
  37. NamespaceUID: "ns-1",
  38. Start: start,
  39. End: end,
  40. },
  41. },
  42. },
  43. {
  44. name: "deployment without uptime is not registered",
  45. overrides: map[string]any{
  46. source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
  47. {UID: "dep-1", Deployment: "my-app", NamespaceUID: "ns-1"},
  48. },
  49. },
  50. want: map[string]*kubemodel.Deployment{},
  51. },
  52. {
  53. name: "deployment without namespace uid is not registered",
  54. overrides: map[string]any{
  55. source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
  56. {UID: "dep-1", Deployment: "my-app"},
  57. },
  58. source.QueryDeploymentUptime: []*source.UptimeResult{
  59. {UID: "dep-1", First: start, Last: end},
  60. },
  61. },
  62. want: map[string]*kubemodel.Deployment{},
  63. },
  64. {
  65. name: "deployment labels and annotations are attached",
  66. overrides: map[string]any{
  67. source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
  68. {UID: "dep-1", Deployment: "my-app", NamespaceUID: "ns-1"},
  69. },
  70. source.QueryDeploymentUptime: []*source.UptimeResult{
  71. {UID: "dep-1", First: start, Last: end},
  72. },
  73. source.QueryDeploymentLabels: []*source.LabelsResult{
  74. {UID: "dep-1", Labels: map[string]string{"app": "web"}},
  75. },
  76. source.QueryDeploymentAnnotations: []*source.AnnotationsResult{
  77. {UID: "dep-1", Annotations: map[string]string{"team": "platform"}},
  78. },
  79. },
  80. want: map[string]*kubemodel.Deployment{
  81. "dep-1": {
  82. UID: "dep-1",
  83. Name: "my-app",
  84. NamespaceUID: "ns-1",
  85. Start: start,
  86. End: end,
  87. Labels: map[string]string{"app": "web"},
  88. Annotations: map[string]string{"team": "platform"},
  89. },
  90. },
  91. },
  92. {
  93. name: "deployment match labels are attached",
  94. overrides: map[string]any{
  95. source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
  96. {UID: "dep-1", Deployment: "my-app", NamespaceUID: "ns-1"},
  97. },
  98. source.QueryDeploymentUptime: []*source.UptimeResult{
  99. {UID: "dep-1", First: start, Last: end},
  100. },
  101. source.QueryDeploymentMatchLabels: []*source.DeploymentLabelsResult{
  102. {UID: "dep-1", Labels: map[string]string{"app": "web", "tier": "frontend"}},
  103. },
  104. },
  105. want: map[string]*kubemodel.Deployment{
  106. "dep-1": {
  107. UID: "dep-1",
  108. Name: "my-app",
  109. NamespaceUID: "ns-1",
  110. Start: start,
  111. End: end,
  112. MatchLabels: map[string]string{"app": "web", "tier": "frontend"},
  113. },
  114. },
  115. },
  116. {
  117. name: "uptime for unknown deployment is ignored",
  118. overrides: map[string]any{
  119. source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
  120. {UID: "dep-1", Deployment: "my-app", NamespaceUID: "ns-1"},
  121. },
  122. source.QueryDeploymentUptime: []*source.UptimeResult{
  123. {UID: "dep-1", First: start, Last: end},
  124. {UID: "unknown-dep", First: start, Last: end},
  125. },
  126. },
  127. want: map[string]*kubemodel.Deployment{
  128. "dep-1": {
  129. UID: "dep-1",
  130. Name: "my-app",
  131. NamespaceUID: "ns-1",
  132. Start: start,
  133. End: end,
  134. },
  135. },
  136. },
  137. }
  138. for _, tt := range tests {
  139. t.Run(tt.name, func(t *testing.T) {
  140. ds := source.NewMockOpenCostDataSource()
  141. ds.ResolutionValue = 5 * time.Minute
  142. seedCluster(ds, start, end)
  143. for method, result := range tt.overrides {
  144. ds.Querier.SetOverride(method, result)
  145. }
  146. km, err := NewKubeModel(testClusterUID, false, ds)
  147. require.NoError(t, err)
  148. kms, err := km.ComputeKubeModelSet(start, end)
  149. require.NoError(t, err)
  150. assert.Equal(t, tt.want, kms.Deployments)
  151. })
  152. }
  153. }