2
0

athenaintegration_coverage_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package aws
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/pkg/cloud"
  7. )
  8. func TestAthenaIntegration_GetListCostColumn(t *testing.T) {
  9. ai := &AthenaIntegration{}
  10. expected := "SUM(CASE line_item_line_item_type WHEN 'EdpDiscount' THEN 0 WHEN 'PrivateRateDiscount' THEN 0 ELSE line_item_unblended_cost END) as list_cost"
  11. actual := ai.GetListCostColumn()
  12. if actual != expected {
  13. t.Errorf("GetListCostColumn() = %v, want %v", actual, expected)
  14. }
  15. }
  16. func TestAthenaIntegration_GetNetCostColumn(t *testing.T) {
  17. ai := &AthenaIntegration{}
  18. // Test case where net pricing column exists
  19. allColumnsWithNet := map[string]bool{
  20. "line_item_net_unblended_cost": true,
  21. }
  22. expectedWithNet := "SUM(COALESCE(line_item_net_unblended_cost, line_item_unblended_cost, 0)) as net_cost"
  23. actualWithNet := ai.GetNetCostColumn(allColumnsWithNet)
  24. if actualWithNet != expectedWithNet {
  25. t.Errorf("GetNetCostColumn() with net pricing = %v, want %v", actualWithNet, expectedWithNet)
  26. }
  27. // Test case where net pricing column doesn't exist
  28. allColumnsWithoutNet := map[string]bool{
  29. "line_item_unblended_cost": true,
  30. }
  31. expectedWithoutNet := "SUM(line_item_unblended_cost) as net_cost"
  32. actualWithoutNet := ai.GetNetCostColumn(allColumnsWithoutNet)
  33. if actualWithoutNet != expectedWithoutNet {
  34. t.Errorf("GetNetCostColumn() without net pricing = %v, want %v", actualWithoutNet, expectedWithoutNet)
  35. }
  36. }
  37. func TestAthenaIntegration_GetAmortizedCostColumn(t *testing.T) {
  38. ai := &AthenaIntegration{}
  39. allColumns := map[string]bool{
  40. "reservation_effective_cost": true,
  41. "savings_plan_savings_plan_effective_cost": true,
  42. "line_item_unblended_cost": true,
  43. }
  44. result := ai.GetAmortizedCostColumn(allColumns)
  45. if !strings.Contains(result, "SUM(") || !strings.Contains(result, " as amortized_cost") {
  46. t.Errorf("GetAmortizedCostColumn() should return a SUM expression with amortized_cost alias, got: %v", result)
  47. }
  48. }
  49. func TestAthenaIntegration_GetAmortizedNetCostColumn(t *testing.T) {
  50. ai := &AthenaIntegration{}
  51. // Test case where net pricing columns exist
  52. allColumnsWithNet := map[string]bool{
  53. "line_item_net_unblended_cost": true,
  54. "reservation_net_effective_cost": true,
  55. "savings_plan_net_savings_plan_effective_cost": true,
  56. "line_item_unblended_cost": true,
  57. }
  58. resultWithNet := ai.GetAmortizedNetCostColumn(allColumnsWithNet)
  59. if !strings.Contains(resultWithNet, "SUM(") || !strings.Contains(resultWithNet, " as amortized_net_cost") {
  60. t.Errorf("GetAmortizedNetCostColumn() with net pricing should return a SUM expression with amortized_net_cost alias, got: %v", resultWithNet)
  61. }
  62. // Test case where net pricing columns don't exist
  63. allColumnsWithoutNet := map[string]bool{
  64. "reservation_effective_cost": true,
  65. "savings_plan_savings_plan_effective_cost": true,
  66. "line_item_unblended_cost": true,
  67. }
  68. resultWithoutNet := ai.GetAmortizedNetCostColumn(allColumnsWithoutNet)
  69. if !strings.Contains(resultWithoutNet, "SUM(") || !strings.Contains(resultWithoutNet, " as amortized_net_cost") {
  70. t.Errorf("GetAmortizedNetCostColumn() without net pricing should return a SUM expression with amortized_net_cost alias, got: %v", resultWithoutNet)
  71. }
  72. }
  73. func TestAthenaIntegration_GetAmortizedCostCase(t *testing.T) {
  74. ai := &AthenaIntegration{}
  75. // Test case where RI and SP pricing columns exist
  76. allColumnsWithRIAndSP := map[string]bool{
  77. "reservation_effective_cost": true,
  78. "savings_plan_savings_plan_effective_cost": true,
  79. "line_item_unblended_cost": true,
  80. }
  81. resultWithRIAndSP := ai.GetAmortizedCostCase(allColumnsWithRIAndSP)
  82. if !strings.Contains(resultWithRIAndSP, "CASE line_item_line_item_type") ||
  83. !strings.Contains(resultWithRIAndSP, "DiscountedUsage") ||
  84. !strings.Contains(resultWithRIAndSP, "SavingsPlanCoveredUsage") {
  85. t.Errorf("GetAmortizedCostCase() with RI and SP should contain CASE statement with DiscountedUsage and SavingsPlanCoveredUsage, got: %v", resultWithRIAndSP)
  86. }
  87. // Test case where neither RI nor SP pricing columns exist
  88. allColumnsWithoutRIOrSP := map[string]bool{
  89. "line_item_unblended_cost": true,
  90. }
  91. resultWithoutRIOrSP := ai.GetAmortizedCostCase(allColumnsWithoutRIOrSP)
  92. expectedWithoutRIOrSP := "line_item_unblended_cost"
  93. if resultWithoutRIOrSP != expectedWithoutRIOrSP {
  94. t.Errorf("GetAmortizedCostCase() without RI or SP should return line_item_unblended_cost, got: %v, want: %v", resultWithoutRIOrSP, expectedWithoutRIOrSP)
  95. }
  96. }
  97. func TestAthenaIntegration_GetAmortizedNetCostCase(t *testing.T) {
  98. ai := &AthenaIntegration{}
  99. // Test case where net RI and SP pricing columns exist
  100. allColumnsWithNetRIAndSP := map[string]bool{
  101. "reservation_net_effective_cost": true,
  102. "savings_plan_net_savings_plan_effective_cost": true,
  103. "line_item_net_unblended_cost": true,
  104. "line_item_unblended_cost": true,
  105. }
  106. resultWithNetRIAndSP := ai.GetAmortizedNetCostCase(allColumnsWithNetRIAndSP)
  107. if !strings.Contains(resultWithNetRIAndSP, "CASE line_item_line_item_type") ||
  108. !strings.Contains(resultWithNetRIAndSP, "DiscountedUsage") ||
  109. !strings.Contains(resultWithNetRIAndSP, "SavingsPlanCoveredUsage") {
  110. t.Errorf("GetAmortizedNetCostCase() with net RI and SP should contain CASE statement with DiscountedUsage and SavingsPlanCoveredUsage, got: %v", resultWithNetRIAndSP)
  111. }
  112. // Test case where neither net RI nor net SP pricing columns exist
  113. allColumnsWithoutNetRIOrSP := map[string]bool{
  114. "line_item_net_unblended_cost": true,
  115. "line_item_unblended_cost": true,
  116. }
  117. resultWithoutNetRIOrSP := ai.GetAmortizedNetCostCase(allColumnsWithoutNetRIOrSP)
  118. expectedStr := "COALESCE(line_item_net_unblended_cost, line_item_unblended_cost, 0)"
  119. if resultWithoutNetRIOrSP != expectedStr {
  120. t.Errorf("GetAmortizedNetCostCase() without net RI or SP should return COALESCE expression, got: %v, want: %v", resultWithoutNetRIOrSP, expectedStr)
  121. }
  122. }
  123. func TestAthenaIntegration_RemoveColumnAliases(t *testing.T) {
  124. ai := &AthenaIntegration{}
  125. columns := []string{
  126. "column1 as alias1",
  127. "column2",
  128. "column3 as alias3",
  129. "column4",
  130. }
  131. ai.RemoveColumnAliases(columns)
  132. if columns[0] != "column1" {
  133. t.Errorf("RemoveColumnAliases() should remove alias from 'column1 as alias1', got: %v", columns[0])
  134. }
  135. if columns[1] != "column2" {
  136. t.Errorf("RemoveColumnAliases() should not modify 'column2', got: %v", columns[1])
  137. }
  138. if columns[2] != "column3" {
  139. t.Errorf("RemoveColumnAliases() should remove alias from 'column3 as alias3', got: %v", columns[2])
  140. }
  141. if columns[3] != "column4" {
  142. t.Errorf("RemoveColumnAliases() should not modify 'column4', got: %v", columns[3])
  143. }
  144. }
  145. func TestAthenaIntegration_ConvertLabelToAWSTag(t *testing.T) {
  146. ai := &AthenaIntegration{}
  147. // Test case where label already has prefix
  148. labelWithPrefix := "resource_tags_user_test_label"
  149. resultWithPrefix := ai.ConvertLabelToAWSTag(labelWithPrefix)
  150. if resultWithPrefix != labelWithPrefix {
  151. t.Errorf("ConvertLabelToAWSTag() should return label unchanged if it already has prefix, got: %v, want: %v", resultWithPrefix, labelWithPrefix)
  152. }
  153. // Test case where label needs prefix
  154. labelWithoutPrefix := "test.label/with:characters-here"
  155. resultWithoutPrefix := ai.ConvertLabelToAWSTag(labelWithoutPrefix)
  156. expectedWithoutPrefix := "resource_tags_user_test_label_with_characters_here"
  157. if resultWithoutPrefix != expectedWithoutPrefix {
  158. t.Errorf("ConvertLabelToAWSTag() should add prefix and replace characters, got: %v, want: %v", resultWithoutPrefix, expectedWithoutPrefix)
  159. }
  160. }
  161. func TestAthenaIntegration_GetIsKubernetesColumn(t *testing.T) {
  162. ai := &AthenaIntegration{}
  163. // Test with some tag columns present
  164. allColumns := map[string]bool{
  165. "resource_tags_user_eks_cluster_name": true,
  166. "resource_tags_user_alpha_eksctl_io_cluster_name": true,
  167. "resource_tags_user_kubernetes_io_service_name": true,
  168. "some_other_column": true,
  169. }
  170. result := ai.GetIsKubernetesColumn(allColumns)
  171. if !strings.Contains(result, "line_item_product_code = 'AmazonEKS'") {
  172. t.Errorf("GetIsKubernetesColumn() should always include EKS check, got: %v", result)
  173. }
  174. if !strings.Contains(result, "resource_tags_user_eks_cluster_name <> ''") {
  175. t.Errorf("GetIsKubernetesColumn() should include checks for tag columns, got: %v", result)
  176. }
  177. if !strings.Contains(result, " as is_kubernetes") {
  178. t.Errorf("GetIsKubernetesColumn() should alias result as is_kubernetes, got: %v", result)
  179. }
  180. }
  181. func TestAthenaQuerier_GetStatus(t *testing.T) {
  182. aq := &AthenaQuerier{}
  183. // Test initial status
  184. status := aq.GetStatus()
  185. if status.String() != cloud.InitialStatus.String() {
  186. t.Errorf("GetStatus() should return InitialStatus for uninitialized querier, got: %v", status)
  187. }
  188. // Test setting a specific status
  189. aq.ConnectionStatus = cloud.SuccessfulConnection
  190. status = aq.GetStatus()
  191. if status != cloud.SuccessfulConnection {
  192. t.Errorf("GetStatus() should return set status, got: %v", status)
  193. }
  194. }
  195. func TestAthenaQuerier_Equals(t *testing.T) {
  196. aq1 := &AthenaQuerier{
  197. AthenaConfiguration: AthenaConfiguration{
  198. Bucket: "bucket1",
  199. Region: "region1",
  200. Database: "database1",
  201. Table: "table1",
  202. Account: "account1",
  203. Authorizer: &AccessKey{
  204. ID: "id1",
  205. Secret: "secret1",
  206. },
  207. },
  208. }
  209. aq2 := &AthenaQuerier{
  210. AthenaConfiguration: AthenaConfiguration{
  211. Bucket: "bucket1",
  212. Region: "region1",
  213. Database: "database1",
  214. Table: "table1",
  215. Account: "account1",
  216. Authorizer: &AccessKey{
  217. ID: "id1",
  218. Secret: "secret1",
  219. },
  220. },
  221. }
  222. aq3 := &AthenaQuerier{
  223. AthenaConfiguration: AthenaConfiguration{
  224. Bucket: "bucket2", // Different bucket
  225. Region: "region1",
  226. Database: "database1",
  227. Table: "table1",
  228. Account: "account1",
  229. Authorizer: &AccessKey{
  230. ID: "id1",
  231. Secret: "secret1",
  232. },
  233. },
  234. }
  235. // Test equality
  236. if !aq1.Equals(aq2) {
  237. t.Errorf("Equals() should return true for identical configurations")
  238. }
  239. // Test inequality
  240. if aq1.Equals(aq3) {
  241. t.Errorf("Equals() should return false for different configurations")
  242. }
  243. // Test comparison with non-AthenaQuerier
  244. accessKey := &AccessKey{
  245. ID: "id1",
  246. Secret: "secret1",
  247. }
  248. if aq1.Equals(accessKey) {
  249. t.Errorf("Equals() should return false when comparing with different type")
  250. }
  251. }
  252. // Helper function for parsing time in tests
  253. func mustParseTime(value string) time.Time {
  254. t, err := time.Parse(time.RFC3339, value)
  255. if err != nil {
  256. panic(err)
  257. }
  258. return t
  259. }