properties_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package kubecost
  2. import (
  3. "testing"
  4. )
  5. // TODO niko/etl
  6. // func TestParseProperty(t *testing.T) {}
  7. // TODO niko/etl
  8. // func TestProperty_String(t *testing.T) {}
  9. // TODO niko/etl
  10. // func TestProperties_Clone(t *testing.T) {}
  11. // TODO niko/etl
  12. // func TestProperties_Intersection(t *testing.T) {}
  13. func TestProperties_Matches(t *testing.T) {
  14. // nil Properties should match empty Properties
  15. var p *Properties
  16. propsEmpty := Properties{}
  17. if !p.Matches(propsEmpty) {
  18. t.Fatalf("Properties.Matches: expect nil to match empty")
  19. }
  20. // Empty Properties should match empty Properties
  21. p = &Properties{}
  22. if !p.Matches(propsEmpty) {
  23. t.Fatalf("Properties.Matches: expect nil to match empty")
  24. }
  25. p.SetCluster("cluster-one")
  26. p.SetNamespace("kubecost")
  27. p.SetController("kubecost-deployment")
  28. p.SetControllerKind("deployment")
  29. p.SetPod("kubecost-deployment-abc123")
  30. p.SetContainer("kubecost-cost-model")
  31. p.SetServices([]string{"kubecost-frontend"})
  32. p.SetLabels(map[string]string{
  33. "app": "kubecost",
  34. "tier": "frontend",
  35. })
  36. // Non-empty Properties should match empty Properties, but not vice-a-versa
  37. if !p.Matches(propsEmpty) {
  38. t.Fatalf("Properties.Matches: expect nil to match empty")
  39. }
  40. if propsEmpty.Matches(*p) {
  41. t.Fatalf("Properties.Matches: expect empty to not match non-empty")
  42. }
  43. // Non-empty Properties should match itself
  44. if !p.Matches(*p) {
  45. t.Fatalf("Properties.Matches: expect non-empty to match itself")
  46. }
  47. // Match on all
  48. if !p.Matches(Properties{
  49. ClusterProp: "cluster-one",
  50. NamespaceProp: "kubecost",
  51. ControllerProp: "kubecost-deployment",
  52. ControllerKindProp: "deployment",
  53. PodProp: "kubecost-deployment-abc123",
  54. ContainerProp: "kubecost-cost-model",
  55. ServiceProp: []string{"kubecost-frontend"},
  56. LabelProp: map[string]string{
  57. "app": "kubecost",
  58. "tier": "frontend",
  59. },
  60. }) {
  61. t.Fatalf("Properties.Matches: expect match on all")
  62. }
  63. // Match on cluster
  64. if !p.Matches(Properties{
  65. ClusterProp: "cluster-one",
  66. }) {
  67. t.Fatalf("Properties.Matches: expect match on cluster")
  68. }
  69. // No match on cluster
  70. if p.Matches(Properties{
  71. ClusterProp: "miss",
  72. }) {
  73. t.Fatalf("Properties.Matches: expect no match on cluster")
  74. }
  75. // Match on namespace
  76. if !p.Matches(Properties{
  77. NamespaceProp: "kubecost",
  78. }) {
  79. t.Fatalf("Properties.Matches: expect match on namespace")
  80. }
  81. // No match on namespace
  82. if p.Matches(Properties{
  83. NamespaceProp: "miss",
  84. }) {
  85. t.Fatalf("Properties.Matches: expect no match on namespace")
  86. }
  87. // Match on controller
  88. if !p.Matches(Properties{
  89. ControllerProp: "kubecost-deployment",
  90. }) {
  91. t.Fatalf("Properties.Matches: expect match on controller")
  92. }
  93. // No match on controller
  94. if p.Matches(Properties{
  95. ControllerProp: "miss",
  96. }) {
  97. t.Fatalf("Properties.Matches: expect no match on controller")
  98. }
  99. // Match on controller kind
  100. if !p.Matches(Properties{
  101. ControllerKindProp: "deployment",
  102. }) {
  103. t.Fatalf("Properties.Matches: expect match on controller kind")
  104. }
  105. // No match on controller kind
  106. if p.Matches(Properties{
  107. ControllerKindProp: "miss",
  108. }) {
  109. t.Fatalf("Properties.Matches: expect no match on controller kind")
  110. }
  111. // Match on pod
  112. if !p.Matches(Properties{
  113. PodProp: "kubecost-deployment-abc123",
  114. }) {
  115. t.Fatalf("Properties.Matches: expect match on pod")
  116. }
  117. // No match on pod
  118. if p.Matches(Properties{
  119. PodProp: "miss",
  120. }) {
  121. t.Fatalf("Properties.Matches: expect no match on pod")
  122. }
  123. // Match on container
  124. if !p.Matches(Properties{
  125. ContainerProp: "kubecost-cost-model",
  126. }) {
  127. t.Fatalf("Properties.Matches: expect match on container")
  128. }
  129. // No match on container
  130. if p.Matches(Properties{
  131. ContainerProp: "miss",
  132. }) {
  133. t.Fatalf("Properties.Matches: expect no match on container")
  134. }
  135. // Match on single service
  136. if !p.Matches(Properties{
  137. ServiceProp: []string{"kubecost-frontend"},
  138. }) {
  139. t.Fatalf("Properties.Matches: expect match on service")
  140. }
  141. // No match on one missing service
  142. if p.Matches(Properties{
  143. ServiceProp: []string{"missing-service", "kubecost-frontend"},
  144. }) {
  145. t.Fatalf("Properties.Matches: expect no match on 1 of 2 services")
  146. }
  147. // Match on single label
  148. if !p.Matches(Properties{
  149. LabelProp: map[string]string{
  150. "app": "kubecost",
  151. },
  152. }) {
  153. t.Fatalf("Properties.Matches: expect match on label")
  154. }
  155. // No match on one missing label
  156. if !p.Matches(Properties{
  157. LabelProp: map[string]string{
  158. "app": "kubecost",
  159. "tier": "frontend",
  160. "label": "missing",
  161. },
  162. }) {
  163. t.Fatalf("Properties.Matches: expect no match on 2 of 3 labels")
  164. }
  165. }
  166. // TODO niko/etl
  167. // func TestProperties_GetCluster(t *testing.T) {}
  168. // TODO niko/etl
  169. // func TestProperties_SetCluster(t *testing.T) {}
  170. // TODO niko/etl
  171. // func TestProperties_GetContainer(t *testing.T) {}
  172. // TODO niko/etl
  173. // func TestProperties_SetContainer(t *testing.T) {}
  174. // TODO niko/etl
  175. // func TestProperties_GetController(t *testing.T) {}
  176. // TODO niko/etl
  177. // func TestProperties_SetController(t *testing.T) {}
  178. // TODO niko/etl
  179. // func TestProperties_GetControllerKind(t *testing.T) {}
  180. // TODO niko/etl
  181. // func TestProperties_SetControllerKind(t *testing.T) {}
  182. // TODO niko/etl
  183. // func TestProperties_GetLabels(t *testing.T) {}
  184. // TODO niko/etl
  185. // func TestProperties_SetLabels(t *testing.T) {}
  186. // TODO niko/etl
  187. // func TestProperties_GetNamespace(t *testing.T) {}
  188. // TODO niko/etl
  189. // func TestProperties_SetNamespace(t *testing.T) {}
  190. // TODO niko/etl
  191. // func TestProperties_GetPod(t *testing.T) {}
  192. // TODO niko/etl
  193. // func TestProperties_SetPod(t *testing.T) {}
  194. // TODO niko/etl
  195. // func TestProperties_GetServices(t *testing.T) {}
  196. // TODO niko/etl
  197. // func TestProperties_SetServices(t *testing.T) {}