allocationprops_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "aggregate by owner without owner labels": {
  13. aggregate: []string{"owner"},
  14. allocationProps: &AllocationProperties{
  15. Labels: map[string]string{"app": "cost-analyzer"},
  16. Annotations: map[string]string{"owner": "test owner 123"},
  17. },
  18. expected: "test owner 123",
  19. },
  20. "aggregate by owner without labels": {
  21. aggregate: []string{"owner"},
  22. allocationProps: &AllocationProperties{
  23. Annotations: map[string]string{"owner": "test owner 123"},
  24. },
  25. expected: "test owner 123",
  26. },
  27. "aggregate by owner with owner label and annotation": {
  28. aggregate: []string{"owner"},
  29. allocationProps: &AllocationProperties{
  30. Labels: map[string]string{"owner": "owner-label"},
  31. Annotations: map[string]string{"owner": "owner-annotation"},
  32. },
  33. expected: "owner-label",
  34. },
  35. "aggregate by environment with environment label and annotation": {
  36. aggregate: []string{"environment"},
  37. allocationProps: &AllocationProperties{
  38. Labels: map[string]string{"env": "environment-label"},
  39. Annotations: map[string]string{"env": "environment-annotation"},
  40. },
  41. expected: "environment-label",
  42. },
  43. "aggregate by department with department label and annotation": {
  44. aggregate: []string{"department"},
  45. allocationProps: &AllocationProperties{
  46. Labels: map[string]string{"department": "department-label"},
  47. Annotations: map[string]string{"department": "department-annotation"},
  48. },
  49. expected: "department-label",
  50. },
  51. "aggregate by team with team label and annotation": {
  52. aggregate: []string{"team"},
  53. allocationProps: &AllocationProperties{
  54. Labels: map[string]string{"team": "team-label"},
  55. Annotations: map[string]string{"team": "team-annotation"},
  56. },
  57. expected: "team-label",
  58. },
  59. "aggregate by product with product label and annotation": {
  60. aggregate: []string{"product"},
  61. allocationProps: &AllocationProperties{
  62. Labels: map[string]string{"app": "product-label"},
  63. Annotations: map[string]string{"app": "product-annotation"},
  64. },
  65. expected: "product-label",
  66. },
  67. "aggregate by product and owner with multiple labels and annotations": {
  68. aggregate: []string{"product", "owner"},
  69. allocationProps: &AllocationProperties{
  70. Labels: map[string]string{"app": "product-label", "owner": "owner-label", "team": "team-label"},
  71. Annotations: map[string]string{"app": "product-annotation", "owner": "owner-annotation", "team": "team-annotation"},
  72. },
  73. expected: "product-label/owner-label",
  74. },
  75. }
  76. for name, tc := range cases {
  77. t.Run(name, func(t *testing.T) {
  78. lc := NewLabelConfig()
  79. result := tc.allocationProps.GenerateKey(tc.aggregate, lc)
  80. if !reflect.DeepEqual(result, tc.expected) {
  81. t.Fatalf("expected %+v; got %+v", tc.expected, result)
  82. }
  83. })
  84. }
  85. }