cloudcostprops_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. InvoiceEntityID: "InvoiceEntityID1",
  15. Service: "Service1",
  16. Category: "Category1",
  17. Labels: map[string]string{
  18. "key1": "value1",
  19. },
  20. },
  21. intCCP: &CloudCostProperties{
  22. Provider: "CustomProvider",
  23. ProviderID: "ProviderID1",
  24. AccountID: "WorkGroupID1",
  25. InvoiceEntityID: "InvoiceEntityID1",
  26. Service: "Service1",
  27. Category: "Category1",
  28. Labels: map[string]string{
  29. "key1": "value1",
  30. },
  31. },
  32. expectedCCP: &CloudCostProperties{
  33. Provider: "CustomProvider",
  34. ProviderID: "ProviderID1",
  35. AccountID: "WorkGroupID1",
  36. InvoiceEntityID: "InvoiceEntityID1",
  37. Service: "Service1",
  38. Category: "Category1",
  39. Labels: map[string]string{
  40. "key1": "value1",
  41. },
  42. },
  43. },
  44. "When one of the properties differ in the two CloudCostProperties": {
  45. baseCCP: &CloudCostProperties{
  46. Provider: "CustomProvider",
  47. ProviderID: "ProviderID1",
  48. AccountID: "WorkGroupID1",
  49. InvoiceEntityID: "InvoiceEntityID1",
  50. Service: "Service1",
  51. Category: "Category1",
  52. Labels: map[string]string{
  53. "key1": "value1",
  54. },
  55. },
  56. intCCP: &CloudCostProperties{
  57. Provider: "CustomProvider",
  58. ProviderID: "ProviderID1",
  59. AccountID: "WorkGroupID1",
  60. InvoiceEntityID: "InvoiceEntityID1",
  61. Service: "Service2",
  62. Category: "Category1",
  63. Labels: map[string]string{
  64. "key1": "value1",
  65. },
  66. },
  67. expectedCCP: &CloudCostProperties{
  68. Provider: "CustomProvider",
  69. ProviderID: "ProviderID1",
  70. AccountID: "WorkGroupID1",
  71. InvoiceEntityID: "InvoiceEntityID1",
  72. Service: "",
  73. Category: "Category1",
  74. Labels: map[string]string{
  75. "key1": "value1",
  76. },
  77. },
  78. },
  79. "When two of the properties differ in the two CloudCostProperties": {
  80. baseCCP: &CloudCostProperties{
  81. Provider: "CustomProvider",
  82. ProviderID: "ProviderID1",
  83. AccountID: "WorkGroupID1",
  84. InvoiceEntityID: "InvoiceEntityID1",
  85. Service: "Service1",
  86. Category: "Category1",
  87. Labels: map[string]string{
  88. "key1": "value1",
  89. },
  90. },
  91. intCCP: &CloudCostProperties{
  92. Provider: "CustomProvider",
  93. ProviderID: "ProviderID1",
  94. AccountID: "WorkGroupID2",
  95. InvoiceEntityID: "InvoiceEntityID1",
  96. Service: "Service2",
  97. Category: "Category1",
  98. Labels: map[string]string{
  99. "key1": "value1",
  100. },
  101. },
  102. expectedCCP: &CloudCostProperties{
  103. Provider: "CustomProvider",
  104. ProviderID: "ProviderID1",
  105. AccountID: "",
  106. InvoiceEntityID: "InvoiceEntityID1",
  107. Service: "",
  108. Category: "Category1",
  109. Labels: map[string]string{
  110. "key1": "value1",
  111. },
  112. },
  113. },
  114. "When labels differ": {
  115. baseCCP: &CloudCostProperties{
  116. Provider: "CustomProvider",
  117. ProviderID: "ProviderID1",
  118. AccountID: "WorkGroupID1",
  119. InvoiceEntityID: "InvoiceEntityID1",
  120. Service: "Service1",
  121. Category: "Category1",
  122. Labels: map[string]string{
  123. "key1": "value1",
  124. "key2": "value2",
  125. "key3": "value3",
  126. },
  127. },
  128. intCCP: &CloudCostProperties{
  129. Provider: "CustomProvider",
  130. ProviderID: "ProviderID1",
  131. AccountID: "WorkGroupID1",
  132. InvoiceEntityID: "InvoiceEntityID1",
  133. Service: "Service1",
  134. Category: "Category1",
  135. Labels: map[string]string{
  136. "key1": "value2",
  137. "key2": "value2",
  138. "key4": "value4",
  139. },
  140. },
  141. expectedCCP: &CloudCostProperties{
  142. Provider: "CustomProvider",
  143. ProviderID: "ProviderID1",
  144. AccountID: "WorkGroupID1",
  145. InvoiceEntityID: "InvoiceEntityID1",
  146. Service: "Service1",
  147. Category: "Category1",
  148. Labels: map[string]string{
  149. "key2": "value2",
  150. },
  151. },
  152. },
  153. }
  154. for name, tc := range testCases {
  155. t.Run(name, func(t *testing.T) {
  156. actualCCP := tc.baseCCP.Intersection(tc.intCCP)
  157. if !actualCCP.Equal(tc.expectedCCP) {
  158. t.Errorf("Case %s: properties dont match with expected CloudCostProperties: %v actual %v", name, tc.expectedCCP, actualCCP)
  159. }
  160. })
  161. }
  162. }
  163. func TestCloudCostProperties_hashKey(t *testing.T) {
  164. tests := map[string]struct {
  165. props *CloudCostProperties
  166. want string
  167. }{
  168. "enpty props": {
  169. props: &CloudCostProperties{},
  170. want: "cbf29ce484222325",
  171. },
  172. "All props no labels": {
  173. props: &CloudCostProperties{
  174. ProviderID: "providerid1",
  175. Provider: "provider1",
  176. AccountID: "workgroup1",
  177. InvoiceEntityID: "billing1",
  178. Service: "service1",
  179. Category: "category1",
  180. Labels: map[string]string{},
  181. },
  182. want: "a19b7dddf0032572",
  183. },
  184. "All props": {
  185. props: &CloudCostProperties{
  186. ProviderID: "providerid1",
  187. Provider: "provider1",
  188. AccountID: "workgroup1",
  189. InvoiceEntityID: "billing1",
  190. Service: "service1",
  191. Category: "category1",
  192. Labels: map[string]string{
  193. "label1": "value1",
  194. "label2": "value2",
  195. },
  196. },
  197. want: "9d54403e40ad4db6",
  198. },
  199. "All props swap labels": {
  200. props: &CloudCostProperties{
  201. ProviderID: "providerid1",
  202. Provider: "provider1",
  203. AccountID: "workgroup1",
  204. InvoiceEntityID: "billing1",
  205. Service: "service1",
  206. Category: "category1",
  207. Labels: map[string]string{
  208. "label2": "value2",
  209. "label1": "value1",
  210. },
  211. },
  212. want: "9d54403e40ad4db6",
  213. },
  214. }
  215. for name, tt := range tests {
  216. t.Run(name, func(t *testing.T) {
  217. if got := tt.props.hashKey(); got != tt.want {
  218. t.Errorf("hashKey() = %v, want %v", got, tt.want)
  219. }
  220. })
  221. }
  222. }