allocationfilter_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. package kubecost
  2. import (
  3. "testing"
  4. )
  5. func Test_AllocationFilterCondition_Matches(t *testing.T) {
  6. cases := []struct {
  7. name string
  8. a *Allocation
  9. filter AllocationFilter
  10. expected bool
  11. }{
  12. {
  13. name: "ClusterID Equals -> true",
  14. a: &Allocation{
  15. Properties: &AllocationProperties{
  16. Cluster: "cluster-one",
  17. },
  18. },
  19. filter: AllocationFilterCondition{
  20. Field: FilterClusterID,
  21. Op: FilterEquals,
  22. Value: "cluster-one",
  23. },
  24. expected: true,
  25. },
  26. {
  27. name: "Node Equals -> true",
  28. a: &Allocation{
  29. Properties: &AllocationProperties{
  30. Node: "node123",
  31. },
  32. },
  33. filter: AllocationFilterCondition{
  34. Field: FilterNode,
  35. Op: FilterEquals,
  36. Value: "node123",
  37. },
  38. expected: true,
  39. },
  40. {
  41. name: "Namespace NotEquals -> false",
  42. a: &Allocation{
  43. Properties: &AllocationProperties{
  44. Namespace: "kube-system",
  45. },
  46. },
  47. filter: AllocationFilterCondition{
  48. Field: FilterNamespace,
  49. Op: FilterNotEquals,
  50. Value: "kube-system",
  51. },
  52. expected: false,
  53. },
  54. {
  55. name: "Namespace NotEquals Unallocated -> true",
  56. a: &Allocation{
  57. Properties: &AllocationProperties{
  58. Namespace: "kube-system",
  59. },
  60. },
  61. filter: AllocationFilterCondition{
  62. Field: FilterNamespace,
  63. Op: FilterNotEquals,
  64. Value: UnallocatedSuffix,
  65. },
  66. expected: true,
  67. },
  68. {
  69. name: "Namespace NotEquals Unallocated -> false",
  70. a: &Allocation{
  71. Properties: &AllocationProperties{
  72. Namespace: "",
  73. },
  74. },
  75. filter: AllocationFilterCondition{
  76. Field: FilterNamespace,
  77. Op: FilterNotEquals,
  78. Value: UnallocatedSuffix,
  79. },
  80. expected: false,
  81. },
  82. {
  83. name: "Namespace Equals Unallocated -> true",
  84. a: &Allocation{
  85. Properties: &AllocationProperties{
  86. Namespace: "",
  87. },
  88. },
  89. filter: AllocationFilterCondition{
  90. Field: FilterNamespace,
  91. Op: FilterEquals,
  92. Value: UnallocatedSuffix,
  93. },
  94. expected: true,
  95. },
  96. {
  97. name: "ControllerKind Equals -> true",
  98. a: &Allocation{
  99. Properties: &AllocationProperties{
  100. ControllerKind: "deployment", // We generally store controller kinds as all lowercase
  101. },
  102. },
  103. filter: AllocationFilterCondition{
  104. Field: FilterControllerKind,
  105. Op: FilterEquals,
  106. Value: "deployment",
  107. },
  108. expected: true,
  109. },
  110. {
  111. name: "ControllerName Equals -> true",
  112. a: &Allocation{
  113. Properties: &AllocationProperties{
  114. Controller: "kc-cost-analyzer",
  115. },
  116. },
  117. filter: AllocationFilterCondition{
  118. Field: FilterControllerName,
  119. Op: FilterEquals,
  120. Value: "kc-cost-analyzer",
  121. },
  122. expected: true,
  123. },
  124. {
  125. name: "Pod (with UID) Equals -> true",
  126. a: &Allocation{
  127. Properties: &AllocationProperties{
  128. Pod: "pod-123 UID-ABC",
  129. },
  130. },
  131. filter: AllocationFilterCondition{
  132. Field: FilterPod,
  133. Op: FilterEquals,
  134. Value: "pod-123 UID-ABC",
  135. },
  136. expected: true,
  137. },
  138. {
  139. name: "Container Equals -> true",
  140. a: &Allocation{
  141. Properties: &AllocationProperties{
  142. Container: "cost-model",
  143. },
  144. },
  145. filter: AllocationFilterCondition{
  146. Field: FilterContainer,
  147. Op: FilterEquals,
  148. Value: "cost-model",
  149. },
  150. expected: true,
  151. },
  152. {
  153. name: `label[app]="foo" -> true`,
  154. a: &Allocation{
  155. Properties: &AllocationProperties{
  156. Labels: map[string]string{
  157. "app": "foo",
  158. },
  159. },
  160. },
  161. filter: AllocationFilterCondition{
  162. Field: FilterLabel,
  163. Op: FilterEquals,
  164. Key: "app",
  165. Value: "foo",
  166. },
  167. expected: true,
  168. },
  169. {
  170. name: `label[app]="foo" -> different value -> false`,
  171. a: &Allocation{
  172. Properties: &AllocationProperties{
  173. Labels: map[string]string{
  174. "app": "bar",
  175. },
  176. },
  177. },
  178. filter: AllocationFilterCondition{
  179. Field: FilterLabel,
  180. Op: FilterEquals,
  181. Key: "app",
  182. Value: "foo",
  183. },
  184. expected: false,
  185. },
  186. {
  187. name: `label[app]="foo" -> label missing -> false`,
  188. a: &Allocation{
  189. Properties: &AllocationProperties{
  190. Labels: map[string]string{
  191. "someotherlabel": "someothervalue",
  192. },
  193. },
  194. },
  195. filter: AllocationFilterCondition{
  196. Field: FilterLabel,
  197. Op: FilterEquals,
  198. Key: "app",
  199. Value: "foo",
  200. },
  201. expected: false,
  202. },
  203. {
  204. name: `label[app]!="foo" -> label missing -> true`,
  205. a: &Allocation{
  206. Properties: &AllocationProperties{
  207. Labels: map[string]string{
  208. "someotherlabel": "someothervalue",
  209. },
  210. },
  211. },
  212. filter: AllocationFilterCondition{
  213. Field: FilterLabel,
  214. Op: FilterNotEquals,
  215. Key: "app",
  216. Value: "foo",
  217. },
  218. expected: true,
  219. },
  220. {
  221. name: `annotation[prom_modified_name]="testing123" -> true`,
  222. a: &Allocation{
  223. Properties: &AllocationProperties{
  224. Annotations: map[string]string{
  225. "prom_modified_name": "testing123",
  226. },
  227. },
  228. },
  229. filter: AllocationFilterCondition{
  230. Field: FilterAnnotation,
  231. Op: FilterEquals,
  232. Key: "prom_modified_name",
  233. Value: "testing123",
  234. },
  235. expected: true,
  236. },
  237. {
  238. name: `annotation[app]="foo" -> different value -> false`,
  239. a: &Allocation{
  240. Properties: &AllocationProperties{
  241. Annotations: map[string]string{
  242. "app": "bar",
  243. },
  244. },
  245. },
  246. filter: AllocationFilterCondition{
  247. Field: FilterAnnotation,
  248. Op: FilterEquals,
  249. Key: "app",
  250. Value: "foo",
  251. },
  252. expected: false,
  253. },
  254. {
  255. name: `annotation[app]="foo" -> annotation missing -> false`,
  256. a: &Allocation{
  257. Properties: &AllocationProperties{
  258. Annotations: map[string]string{
  259. "someotherannotation": "someothervalue",
  260. },
  261. },
  262. },
  263. filter: AllocationFilterCondition{
  264. Field: FilterAnnotation,
  265. Op: FilterEquals,
  266. Key: "app",
  267. Value: "foo",
  268. },
  269. expected: false,
  270. },
  271. {
  272. name: `annotation[app]!="foo" -> annotation missing -> true`,
  273. a: &Allocation{
  274. Properties: &AllocationProperties{
  275. Annotations: map[string]string{
  276. "someotherannotation": "someothervalue",
  277. },
  278. },
  279. },
  280. filter: AllocationFilterCondition{
  281. Field: FilterAnnotation,
  282. Op: FilterNotEquals,
  283. Key: "app",
  284. Value: "foo",
  285. },
  286. expected: true,
  287. },
  288. {
  289. name: `namespace unallocated -> true`,
  290. a: &Allocation{
  291. Properties: &AllocationProperties{
  292. Namespace: "",
  293. },
  294. },
  295. filter: AllocationFilterCondition{
  296. Field: FilterNamespace,
  297. Op: FilterEquals,
  298. Value: UnallocatedSuffix,
  299. },
  300. expected: true,
  301. },
  302. }
  303. for _, c := range cases {
  304. result := c.filter.Matches(c.a)
  305. if result != c.expected {
  306. t.Errorf("%s: expected %t, got %t", c.name, c.expected, result)
  307. }
  308. }
  309. }
  310. func Test_AllocationFilterAnd_Matches(t *testing.T) {
  311. cases := []struct {
  312. name string
  313. a *Allocation
  314. filter AllocationFilter
  315. expected bool
  316. }{
  317. {
  318. name: `label[app]="foo" and namespace="kubecost" -> both true`,
  319. a: &Allocation{
  320. Properties: &AllocationProperties{
  321. Namespace: "kubecost",
  322. Labels: map[string]string{
  323. "app": "foo",
  324. },
  325. },
  326. },
  327. filter: AllocationFilterAnd{[]AllocationFilter{
  328. AllocationFilterCondition{
  329. Field: FilterLabel,
  330. Op: FilterEquals,
  331. Key: "app",
  332. Value: "foo",
  333. },
  334. AllocationFilterCondition{
  335. Field: FilterNamespace,
  336. Op: FilterEquals,
  337. Value: "kubecost",
  338. },
  339. }},
  340. expected: true,
  341. },
  342. {
  343. name: `label[app]="foo" and namespace="kubecost" -> first true`,
  344. a: &Allocation{
  345. Properties: &AllocationProperties{
  346. Namespace: "kube-system",
  347. Labels: map[string]string{
  348. "app": "foo",
  349. },
  350. },
  351. },
  352. filter: AllocationFilterAnd{[]AllocationFilter{
  353. AllocationFilterCondition{
  354. Field: FilterLabel,
  355. Op: FilterEquals,
  356. Key: "app",
  357. Value: "foo",
  358. },
  359. AllocationFilterCondition{
  360. Field: FilterNamespace,
  361. Op: FilterEquals,
  362. Value: "kubecost",
  363. },
  364. }},
  365. expected: false,
  366. },
  367. {
  368. name: `label[app]="foo" and namespace="kubecost" -> second true`,
  369. a: &Allocation{
  370. Properties: &AllocationProperties{
  371. Namespace: "kubecost",
  372. Labels: map[string]string{
  373. "app": "bar",
  374. },
  375. },
  376. },
  377. filter: AllocationFilterAnd{[]AllocationFilter{
  378. AllocationFilterCondition{
  379. Field: FilterLabel,
  380. Op: FilterEquals,
  381. Key: "app",
  382. Value: "foo",
  383. },
  384. AllocationFilterCondition{
  385. Field: FilterNamespace,
  386. Op: FilterEquals,
  387. Value: "kubecost",
  388. },
  389. }},
  390. expected: false,
  391. },
  392. {
  393. name: `label[app]="foo" and namespace="kubecost" -> both false`,
  394. a: &Allocation{
  395. Properties: &AllocationProperties{
  396. Namespace: "kube-system",
  397. Labels: map[string]string{
  398. "app": "bar",
  399. },
  400. },
  401. },
  402. filter: AllocationFilterAnd{[]AllocationFilter{
  403. AllocationFilterCondition{
  404. Field: FilterLabel,
  405. Op: FilterEquals,
  406. Key: "app",
  407. Value: "foo",
  408. },
  409. AllocationFilterCondition{
  410. Field: FilterNamespace,
  411. Op: FilterEquals,
  412. Value: "kubecost",
  413. },
  414. }},
  415. expected: false,
  416. },
  417. }
  418. for _, c := range cases {
  419. result := c.filter.Matches(c.a)
  420. if result != c.expected {
  421. t.Errorf("%s: expected %t, got %t", c.name, c.expected, result)
  422. }
  423. }
  424. }
  425. func Test_AllocationFilterOr_Matches(t *testing.T) {
  426. cases := []struct {
  427. name string
  428. a *Allocation
  429. filter AllocationFilter
  430. expected bool
  431. }{
  432. {
  433. name: `label[app]="foo" or namespace="kubecost" -> both true`,
  434. a: &Allocation{
  435. Properties: &AllocationProperties{
  436. Namespace: "kubecost",
  437. Labels: map[string]string{
  438. "app": "foo",
  439. },
  440. },
  441. },
  442. filter: AllocationFilterOr{[]AllocationFilter{
  443. AllocationFilterCondition{
  444. Field: FilterLabel,
  445. Op: FilterEquals,
  446. Key: "app",
  447. Value: "foo",
  448. },
  449. AllocationFilterCondition{
  450. Field: FilterNamespace,
  451. Op: FilterEquals,
  452. Value: "kubecost",
  453. },
  454. }},
  455. expected: true,
  456. },
  457. {
  458. name: `label[app]="foo" or namespace="kubecost" -> first true`,
  459. a: &Allocation{
  460. Properties: &AllocationProperties{
  461. Namespace: "kube-system",
  462. Labels: map[string]string{
  463. "app": "foo",
  464. },
  465. },
  466. },
  467. filter: AllocationFilterOr{[]AllocationFilter{
  468. AllocationFilterCondition{
  469. Field: FilterLabel,
  470. Op: FilterEquals,
  471. Key: "app",
  472. Value: "foo",
  473. },
  474. AllocationFilterCondition{
  475. Field: FilterNamespace,
  476. Op: FilterEquals,
  477. Value: "kubecost",
  478. },
  479. }},
  480. expected: true,
  481. },
  482. {
  483. name: `label[app]="foo" or namespace="kubecost" -> second true`,
  484. a: &Allocation{
  485. Properties: &AllocationProperties{
  486. Namespace: "kubecost",
  487. Labels: map[string]string{
  488. "app": "bar",
  489. },
  490. },
  491. },
  492. filter: AllocationFilterOr{[]AllocationFilter{
  493. AllocationFilterCondition{
  494. Field: FilterLabel,
  495. Op: FilterEquals,
  496. Key: "app",
  497. Value: "foo",
  498. },
  499. AllocationFilterCondition{
  500. Field: FilterNamespace,
  501. Op: FilterEquals,
  502. Value: "kubecost",
  503. },
  504. }},
  505. expected: true,
  506. },
  507. {
  508. name: `label[app]="foo" or namespace="kubecost" -> both false`,
  509. a: &Allocation{
  510. Properties: &AllocationProperties{
  511. Namespace: "kube-system",
  512. Labels: map[string]string{
  513. "app": "bar",
  514. },
  515. },
  516. },
  517. filter: AllocationFilterOr{[]AllocationFilter{
  518. AllocationFilterCondition{
  519. Field: FilterLabel,
  520. Op: FilterEquals,
  521. Key: "app",
  522. Value: "foo",
  523. },
  524. AllocationFilterCondition{
  525. Field: FilterNamespace,
  526. Op: FilterEquals,
  527. Value: "kubecost",
  528. },
  529. }},
  530. expected: false,
  531. },
  532. }
  533. for _, c := range cases {
  534. result := c.filter.Matches(c.a)
  535. if result != c.expected {
  536. t.Errorf("%s: expected %t, got %t", c.name, c.expected, result)
  537. }
  538. }
  539. }