allocationprops_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package kubecost
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestGenerateKey(t *testing.T) {
  7. customOwnerLabelConfig := NewLabelConfig()
  8. customOwnerLabelConfig.OwnerLabel = "example_com_project"
  9. cases := map[string]struct {
  10. aggregate []string
  11. allocationProps *AllocationProperties
  12. labelConfig *LabelConfig
  13. expected string
  14. }{
  15. "aggregate by owner without owner labels": {
  16. aggregate: []string{"owner"},
  17. allocationProps: &AllocationProperties{
  18. Labels: map[string]string{"app": "cost-analyzer"},
  19. Annotations: map[string]string{"owner": "test owner 123"},
  20. },
  21. expected: "test owner 123",
  22. },
  23. "aggregate by owner without labels": {
  24. aggregate: []string{"owner"},
  25. allocationProps: &AllocationProperties{
  26. Annotations: map[string]string{"owner": "test owner 123"},
  27. },
  28. expected: "test owner 123",
  29. },
  30. "aggregate by owner with owner label and annotation": {
  31. aggregate: []string{"owner"},
  32. allocationProps: &AllocationProperties{
  33. Labels: map[string]string{"owner": "owner-label"},
  34. Annotations: map[string]string{"owner": "owner-annotation"},
  35. },
  36. expected: "owner-label",
  37. },
  38. "aggregate by environment with environment label and annotation": {
  39. aggregate: []string{"environment"},
  40. allocationProps: &AllocationProperties{
  41. Labels: map[string]string{"env": "environment-label"},
  42. Annotations: map[string]string{"env": "environment-annotation"},
  43. },
  44. expected: "environment-label",
  45. },
  46. "aggregate by department with department label and annotation": {
  47. aggregate: []string{"department"},
  48. allocationProps: &AllocationProperties{
  49. Labels: map[string]string{"department": "department-label"},
  50. Annotations: map[string]string{"department": "department-annotation"},
  51. },
  52. expected: "department-label",
  53. },
  54. "aggregate by team with team label and annotation": {
  55. aggregate: []string{"team"},
  56. allocationProps: &AllocationProperties{
  57. Labels: map[string]string{"team": "team-label"},
  58. Annotations: map[string]string{"team": "team-annotation"},
  59. },
  60. expected: "team-label",
  61. },
  62. "aggregate by product with product label and annotation": {
  63. aggregate: []string{"product"},
  64. allocationProps: &AllocationProperties{
  65. Labels: map[string]string{"app": "product-label"},
  66. Annotations: map[string]string{"app": "product-annotation"},
  67. },
  68. expected: "product-label",
  69. },
  70. "aggregate by product and owner with multiple labels and annotations": {
  71. aggregate: []string{"product", "owner"},
  72. allocationProps: &AllocationProperties{
  73. Labels: map[string]string{"app": "product-label", "owner": "owner-label", "team": "team-label"},
  74. Annotations: map[string]string{"app": "product-annotation", "owner": "owner-annotation", "team": "team-annotation"},
  75. },
  76. expected: "product-label/owner-label",
  77. },
  78. "user test": {
  79. aggregate: []string{"owner"},
  80. allocationProps: &AllocationProperties{
  81. Labels: map[string]string{"app_kubernetes_io_name": "x-mongo", "example_com_service_owner": "x", "component": "primary", "controller_revision_hash": "x-mongo-primary-x", "kubernetes_io_metadata_name": "app-microservices", "name": "app-microservices", "statefulset_kubernetes_io_pod_name": "x-mongo-primary-0"},
  82. Annotations: map[string]string{"example_com_project": "redacted"},
  83. },
  84. labelConfig: customOwnerLabelConfig,
  85. expected: "redacted",
  86. },
  87. }
  88. for name, tc := range cases {
  89. t.Run(name, func(t *testing.T) {
  90. lc := NewLabelConfig()
  91. if tc.labelConfig != nil {
  92. lc = tc.labelConfig
  93. }
  94. result := tc.allocationProps.GenerateKey(tc.aggregate, lc)
  95. if !reflect.DeepEqual(result, tc.expected) {
  96. t.Fatalf("expected %+v; got %+v", tc.expected, result)
  97. }
  98. })
  99. }
  100. }