aggregator_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package main
  2. import "testing"
  3. func TestEndpointTreeFromSpanTreeMultiAppAlias(t *testing.T) {
  4. aa := "aAlias"
  5. ba := "bAlias"
  6. rootSpan := Span2Cal{
  7. ServiceName: "A",
  8. AppAlias: ba,
  9. children: []*Span2Cal{
  10. {
  11. ServiceName: "B",
  12. AppAlias: aa,
  13. children: []*Span2Cal{
  14. {
  15. ServiceName: "C",
  16. AppAlias: aa,
  17. children: nil,
  18. },
  19. },
  20. },
  21. {
  22. ServiceName: "A",
  23. AppAlias: aa,
  24. children: []*Span2Cal{
  25. {
  26. ServiceName: "E",
  27. AppAlias: aa,
  28. children: nil,
  29. },
  30. {
  31. ServiceName: "G",
  32. AppAlias: aa,
  33. children: nil,
  34. },
  35. },
  36. },
  37. {
  38. ServiceName: "A",
  39. AppAlias: aa,
  40. children: []*Span2Cal{
  41. {
  42. ServiceName: "A",
  43. AppAlias: aa,
  44. children: []*Span2Cal{
  45. {
  46. ServiceName: "F",
  47. AppAlias: aa,
  48. children: nil,
  49. },
  50. },
  51. },
  52. },
  53. },
  54. {
  55. ServiceName: "A",
  56. AppAlias: aa,
  57. children: []*Span2Cal{
  58. {
  59. ServiceName: "H",
  60. AppAlias: ba,
  61. children: []*Span2Cal{
  62. {
  63. ServiceName: "I",
  64. AppAlias: ba,
  65. children: nil,
  66. },
  67. },
  68. },
  69. },
  70. },
  71. },
  72. }
  73. eps := EndpointTreeFromSpanTree(&rootSpan, nil, aa)
  74. if len(eps) != 4 {
  75. t.Fatalf("len(eps) = %d, want %d, eps:%+v", len(eps), 4, eps)
  76. }
  77. t.Logf("endpoint tree:%+v", eps[0])
  78. }
  79. func TestEndpointTreeFromSpanTree(t *testing.T) {
  80. appAlias := "ABC"
  81. rootSpan := Span2Cal{
  82. ServiceName: "A",
  83. AppAlias: appAlias,
  84. children: []*Span2Cal{
  85. {
  86. ServiceName: "B",
  87. AppAlias: appAlias,
  88. children: []*Span2Cal{
  89. {
  90. ServiceName: "C",
  91. AppAlias: appAlias,
  92. children: nil,
  93. },
  94. },
  95. },
  96. {
  97. ServiceName: "A",
  98. AppAlias: appAlias,
  99. children: []*Span2Cal{
  100. {
  101. ServiceName: "E",
  102. AppAlias: appAlias,
  103. children: nil,
  104. },
  105. {
  106. ServiceName: "G",
  107. AppAlias: appAlias,
  108. children: nil,
  109. },
  110. },
  111. },
  112. {
  113. ServiceName: "A",
  114. AppAlias: appAlias,
  115. children: []*Span2Cal{
  116. {
  117. ServiceName: "A",
  118. AppAlias: appAlias,
  119. children: []*Span2Cal{
  120. {
  121. ServiceName: "F",
  122. AppAlias: appAlias,
  123. children: nil,
  124. },
  125. },
  126. },
  127. },
  128. },
  129. {
  130. ServiceName: "A",
  131. children: []*Span2Cal{
  132. {
  133. ServiceName: "H",
  134. AppAlias: appAlias,
  135. children: []*Span2Cal{
  136. {
  137. ServiceName: "I",
  138. AppAlias: appAlias,
  139. children: nil,
  140. },
  141. },
  142. },
  143. },
  144. },
  145. },
  146. }
  147. eps := EndpointTreeFromSpanTree(&rootSpan, nil, appAlias)
  148. if len(eps) != 1 {
  149. t.Fatalf("len(eps) = %d, want %d, eps:%+v", len(eps), 1, eps)
  150. }
  151. t.Logf("endpoint tree:%+v", eps[0])
  152. }
  153. func TestSubTreesWithEndpointRoot(t *testing.T) {
  154. root := ServerEndpointNode{
  155. ServerName: "A",
  156. Endpoint: "",
  157. children: []*ServerEndpointNode{
  158. {
  159. ServerName: "B2",
  160. Endpoint: "",
  161. children: []*ServerEndpointNode{
  162. {
  163. ServerName: "C2",
  164. Endpoint: "c2.endpoint",
  165. children: []*ServerEndpointNode{
  166. {
  167. ServerName: "D2",
  168. Endpoint: "d2.endpoint",
  169. children: nil,
  170. },
  171. },
  172. },
  173. {
  174. ServerName: "E2",
  175. Endpoint: "e2.endpoint",
  176. children: []*ServerEndpointNode{
  177. {
  178. ServerName: "f2",
  179. Endpoint: "f2.endpoint",
  180. children: nil,
  181. },
  182. },
  183. },
  184. },
  185. },
  186. {
  187. ServerName: "B1",
  188. Endpoint: "b1.endpoint",
  189. children: []*ServerEndpointNode{
  190. {
  191. ServerName: "C1",
  192. Endpoint: "",
  193. children: []*ServerEndpointNode{
  194. {
  195. ServerName: "D1",
  196. Endpoint: "d1.endpoint",
  197. children: []*ServerEndpointNode{
  198. {
  199. ServerName: "E1",
  200. Endpoint: "e1.endpoint",
  201. children: nil,
  202. },
  203. {
  204. ServerName: "F1",
  205. Endpoint: "f1.endpoint",
  206. children: nil,
  207. },
  208. },
  209. },
  210. },
  211. },
  212. },
  213. },
  214. },
  215. }
  216. subTrees := subTreesWithEndpointRoot(&root)
  217. if subTrees == nil || len(subTrees) != 3 {
  218. t.Fatalf("subtrees length != 3:%+v", subTrees)
  219. }
  220. }
  221. func TestFindSubTreeEndpointPair(t *testing.T) {
  222. root := ServerEndpointNode{
  223. ServerName: "A",
  224. Endpoint: "",
  225. children: []*ServerEndpointNode{
  226. {
  227. ServerName: "B2",
  228. Endpoint: "",
  229. children: []*ServerEndpointNode{
  230. {
  231. ServerName: "C2",
  232. Endpoint: "c2.endpoint",
  233. children: []*ServerEndpointNode{
  234. {
  235. ServerName: "D2",
  236. Endpoint: "d2.endpoint",
  237. children: nil,
  238. },
  239. },
  240. },
  241. {
  242. ServerName: "E2",
  243. Endpoint: "e2.endpoint",
  244. children: []*ServerEndpointNode{
  245. {
  246. ServerName: "f2",
  247. Endpoint: "f2.endpoint",
  248. children: nil,
  249. },
  250. },
  251. },
  252. },
  253. },
  254. {
  255. ServerName: "B1",
  256. Endpoint: "b1.endpoint",
  257. children: []*ServerEndpointNode{
  258. {
  259. ServerName: "C1",
  260. Endpoint: "",
  261. children: []*ServerEndpointNode{
  262. {
  263. ServerName: "D1",
  264. Endpoint: "d1.endpoint",
  265. children: []*ServerEndpointNode{
  266. {
  267. ServerName: "E1",
  268. Endpoint: "e1.endpoint",
  269. children: nil,
  270. },
  271. {
  272. ServerName: "F1",
  273. Endpoint: "f1.endpoint",
  274. children: nil,
  275. },
  276. },
  277. },
  278. },
  279. },
  280. {
  281. ServerName: "G1",
  282. Endpoint: "G1.endpoint",
  283. children: []*ServerEndpointNode{
  284. {
  285. ServerName: "H1",
  286. Endpoint: "h1.endpoint",
  287. children: []*ServerEndpointNode{
  288. {
  289. ServerName: "I1",
  290. Endpoint: "i1.endpoint",
  291. children: nil,
  292. },
  293. },
  294. },
  295. },
  296. },
  297. },
  298. },
  299. },
  300. }
  301. pairs := findTreeEndpointPair(&root)
  302. if pairs == nil || len(pairs) != 7 {
  303. t.Fatalf("subtrees length != 6:%+v", pairs)
  304. }
  305. }
  306. func TestServiceTopoDataWithEndpoint(t *testing.T) {
  307. root := ServerEndpointNode{
  308. ServerName: "A",
  309. Endpoint: "",
  310. children: []*ServerEndpointNode{
  311. {
  312. ServerName: "B2",
  313. Endpoint: "",
  314. children: []*ServerEndpointNode{
  315. {
  316. ServerName: "C2",
  317. Endpoint: "c2.endpoint",
  318. children: []*ServerEndpointNode{
  319. {
  320. ServerName: "D2",
  321. Endpoint: "d2.endpoint",
  322. children: nil,
  323. },
  324. },
  325. },
  326. {
  327. ServerName: "E2",
  328. Endpoint: "e2.endpoint",
  329. children: []*ServerEndpointNode{
  330. {
  331. ServerName: "f2",
  332. Endpoint: "f2.endpoint",
  333. children: nil,
  334. },
  335. },
  336. },
  337. },
  338. },
  339. {
  340. ServerName: "B1",
  341. Endpoint: "b1.endpoint",
  342. children: []*ServerEndpointNode{
  343. {
  344. ServerName: "C1",
  345. Endpoint: "",
  346. children: []*ServerEndpointNode{
  347. {
  348. ServerName: "D1",
  349. Endpoint: "d1.endpoint",
  350. children: []*ServerEndpointNode{
  351. {
  352. ServerName: "E1",
  353. Endpoint: "e1.endpoint",
  354. children: nil,
  355. },
  356. {
  357. ServerName: "F1",
  358. Endpoint: "f1.endpoint",
  359. children: nil,
  360. },
  361. },
  362. },
  363. },
  364. },
  365. {
  366. ServerName: "G1",
  367. Endpoint: "G1.endpoint",
  368. children: []*ServerEndpointNode{
  369. {
  370. ServerName: "H1",
  371. Endpoint: "h1.endpoint",
  372. children: []*ServerEndpointNode{
  373. {
  374. ServerName: "I1",
  375. Endpoint: "i1.endpoint",
  376. children: nil,
  377. },
  378. },
  379. },
  380. },
  381. },
  382. },
  383. },
  384. },
  385. }
  386. pairs := serviceTopoDataWithEndpoint("cmdbID", &root)
  387. if pairs == nil || len(pairs) != 7 {
  388. t.Fatalf("subtrees length != 6:%+v", pairs)
  389. }
  390. }