allocationprops_test.go 3.2 KB

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