2
0

cloudcostprops_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package opencost
  2. import "testing"
  3. func TestCloudCostPropertiesIntersection(t *testing.T) {
  4. testCases := map[string]struct {
  5. baseCCP *CloudCostProperties
  6. intCCP *CloudCostProperties
  7. expectedCCP *CloudCostProperties
  8. }{
  9. "When properties match between both CloudCostProperties": {
  10. baseCCP: &CloudCostProperties{
  11. Provider: "CustomProvider",
  12. ProviderID: "ProviderID1",
  13. AccountID: "WorkGroupID1",
  14. AccountName: "AccountName1",
  15. InvoiceEntityID: "InvoiceEntityID1",
  16. InvoiceEntityName: "InvoiceEntityName1",
  17. RegionID: "RegionID1",
  18. AvailabilityZone: "AvailabilityZone1",
  19. Service: "Service1",
  20. Category: "Category1",
  21. Labels: map[string]string{
  22. "key1": "value1",
  23. },
  24. },
  25. intCCP: &CloudCostProperties{
  26. Provider: "CustomProvider",
  27. ProviderID: "ProviderID1",
  28. AccountID: "WorkGroupID1",
  29. AccountName: "AccountName1",
  30. InvoiceEntityID: "InvoiceEntityID1",
  31. InvoiceEntityName: "InvoiceEntityName1",
  32. RegionID: "RegionID1",
  33. AvailabilityZone: "AvailabilityZone1",
  34. Service: "Service1",
  35. Category: "Category1",
  36. Labels: map[string]string{
  37. "key1": "value1",
  38. },
  39. },
  40. expectedCCP: &CloudCostProperties{
  41. Provider: "CustomProvider",
  42. ProviderID: "ProviderID1",
  43. AccountID: "WorkGroupID1",
  44. AccountName: "AccountName1",
  45. InvoiceEntityID: "InvoiceEntityID1",
  46. InvoiceEntityName: "InvoiceEntityName1",
  47. RegionID: "RegionID1",
  48. AvailabilityZone: "AvailabilityZone1",
  49. Service: "Service1",
  50. Category: "Category1",
  51. Labels: map[string]string{
  52. "key1": "value1",
  53. },
  54. },
  55. },
  56. "When one of the properties differ in the two CloudCostProperties": {
  57. baseCCP: &CloudCostProperties{
  58. Provider: "CustomProvider",
  59. ProviderID: "ProviderID1",
  60. AccountID: "WorkGroupID1",
  61. AccountName: "AccountName1",
  62. InvoiceEntityID: "InvoiceEntityID1",
  63. InvoiceEntityName: "InvoiceEntityName1",
  64. RegionID: "RegionID1",
  65. AvailabilityZone: "AvailabilityZone1",
  66. Service: "Service1",
  67. Category: "Category1",
  68. Labels: map[string]string{
  69. "key1": "value1",
  70. },
  71. },
  72. intCCP: &CloudCostProperties{
  73. Provider: "CustomProvider",
  74. ProviderID: "ProviderID1",
  75. AccountID: "WorkGroupID1",
  76. AccountName: "AccountName1",
  77. InvoiceEntityID: "InvoiceEntityID1",
  78. InvoiceEntityName: "InvoiceEntityName1",
  79. RegionID: "RegionID1",
  80. AvailabilityZone: "AvailabilityZone1",
  81. Service: "Service2",
  82. Category: "Category1",
  83. Labels: map[string]string{
  84. "key1": "value1",
  85. },
  86. },
  87. expectedCCP: &CloudCostProperties{
  88. Provider: "CustomProvider",
  89. ProviderID: "ProviderID1",
  90. AccountID: "WorkGroupID1",
  91. AccountName: "AccountName1",
  92. InvoiceEntityID: "InvoiceEntityID1",
  93. InvoiceEntityName: "InvoiceEntityName1",
  94. RegionID: "RegionID1",
  95. AvailabilityZone: "AvailabilityZone1",
  96. Service: "",
  97. Category: "Category1",
  98. Labels: map[string]string{
  99. "key1": "value1",
  100. },
  101. },
  102. },
  103. "When two of the properties differ in the two CloudCostProperties": {
  104. baseCCP: &CloudCostProperties{
  105. Provider: "CustomProvider",
  106. ProviderID: "ProviderID1",
  107. AccountID: "WorkGroupID1",
  108. AccountName: "AccountName1",
  109. InvoiceEntityID: "InvoiceEntityID1",
  110. InvoiceEntityName: "InvoiceEntityName1",
  111. RegionID: "RegionID1",
  112. AvailabilityZone: "AvailabilityZone1",
  113. Service: "Service1",
  114. Category: "Category1",
  115. Labels: map[string]string{
  116. "key1": "value1",
  117. },
  118. },
  119. intCCP: &CloudCostProperties{
  120. Provider: "CustomProvider",
  121. ProviderID: "ProviderID1",
  122. AccountID: "WorkGroupID2",
  123. AccountName: "AccountName1",
  124. InvoiceEntityID: "InvoiceEntityID1",
  125. InvoiceEntityName: "InvoiceEntityName1",
  126. RegionID: "RegionID1",
  127. AvailabilityZone: "AvailabilityZone1",
  128. Service: "Service2",
  129. Category: "Category1",
  130. Labels: map[string]string{
  131. "key1": "value1",
  132. },
  133. },
  134. expectedCCP: &CloudCostProperties{
  135. Provider: "CustomProvider",
  136. ProviderID: "ProviderID1",
  137. AccountID: "",
  138. AccountName: "AccountName1",
  139. InvoiceEntityID: "InvoiceEntityID1",
  140. InvoiceEntityName: "InvoiceEntityName1",
  141. RegionID: "RegionID1",
  142. AvailabilityZone: "AvailabilityZone1",
  143. Service: "",
  144. Category: "Category1",
  145. Labels: map[string]string{
  146. "key1": "value1",
  147. },
  148. },
  149. },
  150. "When all properties differ in the two CloudCostProperties": {
  151. baseCCP: &CloudCostProperties{
  152. Provider: "CustomProvider",
  153. ProviderID: "ProviderID1",
  154. AccountID: "WorkGroupID1",
  155. AccountName: "AccountName1",
  156. InvoiceEntityID: "InvoiceEntityID1",
  157. InvoiceEntityName: "InvoiceEntityName1",
  158. RegionID: "RegionID1",
  159. AvailabilityZone: "AvailabilityZone1",
  160. Service: "Service1",
  161. Category: "Category1",
  162. Labels: map[string]string{
  163. "key1": "value1",
  164. },
  165. },
  166. intCCP: &CloudCostProperties{
  167. Provider: "CustomProvider2",
  168. ProviderID: "ProviderID2",
  169. AccountID: "WorkGroupID2",
  170. AccountName: "AccountName2",
  171. InvoiceEntityID: "InvoiceEntityID2",
  172. InvoiceEntityName: "InvoiceEntityName2",
  173. RegionID: "RegionID2",
  174. AvailabilityZone: "AvailabilityZone2",
  175. Service: "Service2",
  176. Category: "Category2",
  177. Labels: map[string]string{
  178. "key2": "value2",
  179. },
  180. },
  181. expectedCCP: &CloudCostProperties{
  182. Provider: "",
  183. ProviderID: "",
  184. AccountID: "",
  185. AccountName: "",
  186. InvoiceEntityID: "",
  187. InvoiceEntityName: "",
  188. RegionID: "",
  189. AvailabilityZone: "",
  190. Service: "",
  191. Category: "",
  192. Labels: map[string]string{},
  193. },
  194. },
  195. "When labels differ": {
  196. baseCCP: &CloudCostProperties{
  197. Provider: "CustomProvider",
  198. ProviderID: "ProviderID1",
  199. AccountID: "WorkGroupID1",
  200. AccountName: "AccountName1",
  201. InvoiceEntityID: "InvoiceEntityID1",
  202. InvoiceEntityName: "InvoiceEntityName1",
  203. RegionID: "RegionID1",
  204. AvailabilityZone: "AvailabilityZone1",
  205. Service: "Service1",
  206. Category: "Category1",
  207. Labels: map[string]string{
  208. "key1": "value1",
  209. "key2": "value2",
  210. "key3": "value3",
  211. },
  212. },
  213. intCCP: &CloudCostProperties{
  214. Provider: "CustomProvider",
  215. ProviderID: "ProviderID1",
  216. AccountID: "WorkGroupID1",
  217. AccountName: "AccountName1",
  218. InvoiceEntityID: "InvoiceEntityID1",
  219. InvoiceEntityName: "InvoiceEntityName1",
  220. RegionID: "RegionID1",
  221. AvailabilityZone: "AvailabilityZone1",
  222. Service: "Service1",
  223. Category: "Category1",
  224. Labels: map[string]string{
  225. "key1": "value2",
  226. "key2": "value2",
  227. "key4": "value4",
  228. },
  229. },
  230. expectedCCP: &CloudCostProperties{
  231. Provider: "CustomProvider",
  232. ProviderID: "ProviderID1",
  233. AccountID: "WorkGroupID1",
  234. AccountName: "AccountName1",
  235. InvoiceEntityID: "InvoiceEntityID1",
  236. InvoiceEntityName: "InvoiceEntityName1",
  237. RegionID: "RegionID1",
  238. AvailabilityZone: "AvailabilityZone1",
  239. Service: "Service1",
  240. Category: "Category1",
  241. Labels: map[string]string{
  242. "key2": "value2",
  243. },
  244. },
  245. },
  246. }
  247. for name, tc := range testCases {
  248. t.Run(name, func(t *testing.T) {
  249. actualCCP := tc.baseCCP.Intersection(tc.intCCP)
  250. if !actualCCP.Equal(tc.expectedCCP) {
  251. t.Errorf("Case %s: properties dont match with expected CloudCostProperties: %v actual %v", name, tc.expectedCCP, actualCCP)
  252. }
  253. })
  254. }
  255. }
  256. func TestCloudCostProperties_hashKey(t *testing.T) {
  257. tests := map[string]struct {
  258. props *CloudCostProperties
  259. want string
  260. }{
  261. "enpty props": {
  262. props: &CloudCostProperties{},
  263. want: "cbf29ce484222325",
  264. },
  265. "All props no labels": {
  266. props: &CloudCostProperties{
  267. ProviderID: "providerid1",
  268. Provider: "provider1",
  269. AccountID: "workgroup1",
  270. AccountName: "AccountName1",
  271. InvoiceEntityID: "billing1",
  272. InvoiceEntityName: "InvoiceEntityName1",
  273. RegionID: "RegionID1",
  274. AvailabilityZone: "AvailabilityZone1",
  275. Service: "service1",
  276. Category: "category1",
  277. Labels: map[string]string{},
  278. },
  279. want: "d07ffd0bd6d5eaf1",
  280. },
  281. "All props": {
  282. props: &CloudCostProperties{
  283. ProviderID: "providerid1",
  284. Provider: "provider1",
  285. AccountID: "workgroup1",
  286. AccountName: "AccountName1",
  287. InvoiceEntityID: "billing1",
  288. InvoiceEntityName: "InvoiceEntityName1",
  289. RegionID: "RegionID1",
  290. AvailabilityZone: "AvailabilityZone1",
  291. Service: "service1",
  292. Category: "category1",
  293. Labels: map[string]string{
  294. "label1": "value1",
  295. "label2": "value2",
  296. },
  297. },
  298. want: "318cb6294bf9e2d5",
  299. },
  300. "All props swap labels": {
  301. props: &CloudCostProperties{
  302. ProviderID: "providerid1",
  303. Provider: "provider1",
  304. AccountID: "workgroup1",
  305. AccountName: "AccountName1",
  306. InvoiceEntityID: "billing1",
  307. InvoiceEntityName: "InvoiceEntityName1",
  308. RegionID: "RegionID1",
  309. AvailabilityZone: "AvailabilityZone1",
  310. Service: "service1",
  311. Category: "category1",
  312. Labels: map[string]string{
  313. "label2": "value2",
  314. "label1": "value1",
  315. },
  316. },
  317. want: "318cb6294bf9e2d5",
  318. },
  319. }
  320. for name, tt := range tests {
  321. t.Run(name, func(t *testing.T) {
  322. if got := tt.props.hashKey(); got != tt.want {
  323. t.Errorf("hashKey() = %v, want %v", got, tt.want)
  324. }
  325. })
  326. }
  327. }