config_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package kubecost
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/pkg/util/cloudutil"
  5. )
  6. func TestLabelConfig_Map(t *testing.T) {
  7. var m map[string]string
  8. var lc *LabelConfig
  9. m = lc.Map()
  10. if len(m) != 18 {
  11. t.Fatalf("Map: expected length %d; got length %d", 18, len(m))
  12. }
  13. if val, ok := m["deployment_external_label"]; !ok || val != "kubernetes_deployment" {
  14. t.Fatalf("Map: expected %s; got %s", "kubernetes_deployment", val)
  15. }
  16. if val, ok := m["namespace_external_label"]; !ok || val != "kubernetes_namespace" {
  17. t.Fatalf("Map: expected %s; got %s", "kubernetes_namespace", val)
  18. }
  19. lc = &LabelConfig{
  20. DaemonsetExternalLabel: "kubernetes_ds",
  21. }
  22. m = lc.Map()
  23. if len(m) != 18 {
  24. t.Fatalf("Map: expected length %d; got length %d", 18, len(m))
  25. }
  26. if val, ok := m["daemonset_external_label"]; !ok || val != "kubernetes_ds" {
  27. t.Fatalf("Map: expected %s; got %s", "kubernetes_ds", val)
  28. }
  29. if val, ok := m["namespace_external_label"]; !ok || val != "kubernetes_namespace" {
  30. t.Fatalf("Map: expected %s; got %s", "kubernetes_namespace", val)
  31. }
  32. }
  33. func TestLabelConfig_GetExternalAllocationName(t *testing.T) {
  34. // Make sure that AWS's Glue/Athena column formatting is supported
  35. glueFormattedLabel := cloudutil.ConvertToGlueColumnFormat("___Non_&GlueFormattedLabel___&")
  36. labels := map[string]string{
  37. "kubens": "kubecost-staging",
  38. "kubeowner": "kubecost-owner",
  39. "env": "env1",
  40. "app": "app1",
  41. "team": "team1",
  42. glueFormattedLabel: "glue",
  43. "prom_sanitization_test": "pass",
  44. "kubernetes_cluster": "cluster-one",
  45. "kubernetes_namespace": "kubecost",
  46. "kubernetes_controller": "kubecost-controller",
  47. "kubernetes_daemonset": "kubecost-daemonset",
  48. "kubernetes_deployment": "kubecost-deployment",
  49. "kubernetes_statefulset": "kubecost-statefulset",
  50. "kubernetes_service": "kubecost-service",
  51. "kubernetes_pod": "kubecost-cost-analyzer-abc123",
  52. "kubernetes_label_department": "kubecost-department",
  53. "kubernetes_label_env": "kubecost-env",
  54. "kubernetes_label_owner": "kubecost-owner",
  55. "kubernetes_label_app": "kubecost-app",
  56. "kubernetes_label_team": "kubecost-team",
  57. }
  58. testCases := []struct {
  59. aggBy string
  60. expected string
  61. }{
  62. {"label:env", "env=env1"},
  63. {"label:app", "app=app1"},
  64. {"label:Non__GlueFormattedLabel", "non_glue_formatted_label=glue"},
  65. {"cluster", "cluster-one"},
  66. {"namespace", "kubecost"},
  67. {"controller", "kubecost-controller"},
  68. {"daemonset", "kubecost-daemonset"},
  69. {"deployment", "kubecost-deployment"},
  70. {"statefulset", "kubecost-statefulset"},
  71. {"service", "kubecost-service"},
  72. {"pod", "kubecost-cost-analyzer-abc123"},
  73. {"pod", "kubecost-cost-analyzer-abc123"},
  74. {"notathing", ""},
  75. {"", ""},
  76. }
  77. var lc *LabelConfig
  78. // If lc is nil, everything should still work off of defaults.
  79. for _, tc := range testCases {
  80. actual := lc.GetExternalAllocationName(labels, tc.aggBy)
  81. if actual != tc.expected {
  82. t.Fatalf("GetExternalAllocationName failed; expected '%s'; got '%s'", tc.expected, actual)
  83. }
  84. }
  85. // If lc is default, everything should work, just like the nil.
  86. lc = NewLabelConfig()
  87. for _, tc := range testCases {
  88. actual := lc.GetExternalAllocationName(labels, tc.aggBy)
  89. if actual != tc.expected {
  90. t.Fatalf("GetExternalAllocationName failed; expected '%s'; got '%s'", tc.expected, actual)
  91. }
  92. }
  93. // Change the external label for namespace and confirm it still works
  94. lc.NamespaceExternalLabel = "kubens"
  95. lc.ServiceExternalLabel = "prom/sanitization-test"
  96. lc.PodExternalLabel = "Non__GlueFormattedLabel"
  97. lc.OwnerExternalLabel = "kubeowner"
  98. lc.DepartmentExternalLabel = "doesntexist,env"
  99. lc.TeamExternalLabel = "team,env"
  100. // TODO how is e.g. OwnerExternalLabel supposed to work?
  101. testCases = []struct {
  102. aggBy string
  103. expected string
  104. }{
  105. {"namespace", "kubecost-staging"},
  106. {"service", "pass"},
  107. {"pod", "glue"},
  108. {"owner", "kubecost-owner"},
  109. {"department", "env1"},
  110. {"team", "team1"},
  111. }
  112. for _, tc := range testCases {
  113. actual := lc.GetExternalAllocationName(labels, tc.aggBy)
  114. if actual != tc.expected {
  115. t.Fatalf("GetExternalAllocationName failed; expected '%s'; got '%s'", tc.expected, actual)
  116. }
  117. }
  118. }
  119. func TestLabelConfig_Sanitize(t *testing.T) {
  120. testCases := []struct {
  121. label string
  122. expected string
  123. }{
  124. {"", ""},
  125. {"simple", "simple"},
  126. {"prom/sanitization-test", "prom_sanitization_test"},
  127. {" prom/sanitization-test$ ", "prom_sanitization_test_"},
  128. }
  129. lc := NewLabelConfig()
  130. for _, tc := range testCases {
  131. actual := lc.Sanitize(tc.label)
  132. if actual != tc.expected {
  133. t.Fatalf("Sanitize failed; expected '%s'; got '%s'", tc.expected, actual)
  134. }
  135. }
  136. }