cloudcost_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package gcp
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/opencost"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestIsK8s(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. labels map[string]string
  11. expect bool
  12. }{
  13. {
  14. name: "GKE volume label",
  15. labels: map[string]string{
  16. "goog-gke-volume": "true",
  17. },
  18. expect: true,
  19. },
  20. {
  21. name: "GKE node label",
  22. labels: map[string]string{
  23. "goog-gke-node": "true",
  24. },
  25. expect: true,
  26. },
  27. {
  28. name: "GKE cluster name label",
  29. labels: map[string]string{
  30. "goog-k8s-cluster-name": "my-cluster",
  31. },
  32. expect: true,
  33. },
  34. {
  35. name: "Multiple GKE labels",
  36. labels: map[string]string{
  37. "goog-gke-volume": "true",
  38. "goog-gke-node": "true",
  39. "goog-k8s-cluster-name": "my-cluster",
  40. },
  41. expect: true,
  42. },
  43. {
  44. name: "No GKE labels",
  45. labels: map[string]string{
  46. "other-label": "value",
  47. },
  48. expect: false,
  49. },
  50. {
  51. name: "Empty labels",
  52. labels: map[string]string{},
  53. expect: false,
  54. },
  55. {
  56. name: "Nil labels",
  57. labels: nil,
  58. expect: false,
  59. },
  60. }
  61. for _, tt := range tests {
  62. t.Run(tt.name, func(t *testing.T) {
  63. result := IsK8s(tt.labels)
  64. assert.Equal(t, tt.expect, result)
  65. })
  66. }
  67. }
  68. func TestParseProviderID(t *testing.T) {
  69. tests := []struct {
  70. name string
  71. providerID string
  72. expected string
  73. }{
  74. {
  75. name: "Standard GCE provider ID",
  76. providerID: "projects/123456789/instances/gke-cluster-3-default-pool-xxxx-yy",
  77. expected: "gke-cluster-3-default-pool-xxxx-yy",
  78. },
  79. {
  80. name: "Provider ID with trailing slash",
  81. providerID: "projects/123456789/instances/gke-cluster-3-default-pool-xxxx-yy/",
  82. expected: "", // The function doesn't handle trailing slashes, so expect empty string
  83. },
  84. {
  85. name: "Provider ID without project prefix",
  86. providerID: "gke-cluster-3-default-pool-xxxx-yy",
  87. expected: "gke-cluster-3-default-pool-xxxx-yy",
  88. },
  89. {
  90. name: "Empty provider ID",
  91. providerID: "",
  92. expected: "",
  93. },
  94. {
  95. name: "Provider ID with no match",
  96. providerID: "invalid-format",
  97. expected: "invalid-format",
  98. },
  99. }
  100. for _, tt := range tests {
  101. t.Run(tt.name, func(t *testing.T) {
  102. result := ParseProviderID(tt.providerID)
  103. assert.Equal(t, tt.expected, result)
  104. })
  105. }
  106. }
  107. func TestSelectCategory(t *testing.T) {
  108. tests := []struct {
  109. name string
  110. service string
  111. description string
  112. expected string
  113. }{
  114. // Network category tests
  115. {
  116. name: "Network download",
  117. service: "Compute Engine",
  118. description: "Network download",
  119. expected: opencost.NetworkCategory,
  120. },
  121. {
  122. name: "Network ingress",
  123. service: "Compute Engine",
  124. description: "Network ingress",
  125. expected: opencost.NetworkCategory,
  126. },
  127. {
  128. name: "Network egress",
  129. service: "Compute Engine",
  130. description: "Network egress",
  131. expected: opencost.NetworkCategory,
  132. },
  133. {
  134. name: "Static IP",
  135. service: "Compute Engine",
  136. description: "Static IP",
  137. expected: opencost.NetworkCategory,
  138. },
  139. {
  140. name: "External IP",
  141. service: "Compute Engine",
  142. description: "External IP",
  143. expected: opencost.NetworkCategory,
  144. },
  145. {
  146. name: "Load balanced",
  147. service: "Compute Engine",
  148. description: "Load balanced",
  149. expected: opencost.NetworkCategory,
  150. },
  151. {
  152. name: "Pub/Sub service",
  153. service: "pub/sub",
  154. description: "Some description",
  155. expected: opencost.NetworkCategory,
  156. },
  157. // Storage category tests
  158. {
  159. name: "Storage service",
  160. service: "storage",
  161. description: "Some description",
  162. expected: opencost.StorageCategory,
  163. },
  164. {
  165. name: "PD capacity",
  166. service: "Compute Engine",
  167. description: "PD capacity",
  168. expected: opencost.StorageCategory,
  169. },
  170. {
  171. name: "PD IOPS",
  172. service: "Compute Engine",
  173. description: "PD IOPS",
  174. expected: opencost.StorageCategory,
  175. },
  176. {
  177. name: "PD snapshot",
  178. service: "Compute Engine",
  179. description: "PD snapshot",
  180. expected: opencost.StorageCategory,
  181. },
  182. {
  183. name: "SQL service",
  184. service: "sql",
  185. description: "Some description",
  186. expected: opencost.StorageCategory,
  187. },
  188. {
  189. name: "BigQuery service",
  190. service: "bigquery",
  191. description: "Some description",
  192. expected: opencost.StorageCategory,
  193. },
  194. // Compute category tests
  195. {
  196. name: "Compute service",
  197. service: "compute",
  198. description: "Some description",
  199. expected: opencost.ComputeCategory,
  200. },
  201. // Management category tests
  202. {
  203. name: "Kubernetes service",
  204. service: "kubernetes",
  205. description: "Some description",
  206. expected: opencost.ManagementCategory,
  207. },
  208. // Other category tests
  209. {
  210. name: "Licensing fee",
  211. service: "Compute Engine",
  212. description: "Licensing fee",
  213. expected: opencost.OtherCategory,
  214. },
  215. {
  216. name: "Unknown service",
  217. service: "unknown-service",
  218. description: "Some description",
  219. expected: opencost.OtherCategory,
  220. },
  221. {
  222. name: "Empty service and description",
  223. service: "",
  224. description: "",
  225. expected: opencost.OtherCategory,
  226. },
  227. }
  228. for _, tt := range tests {
  229. t.Run(tt.name, func(t *testing.T) {
  230. result := SelectCategory(tt.service, tt.description)
  231. assert.Equal(t, tt.expected, result)
  232. })
  233. }
  234. }