node_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package detailed_test
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "github.com/weaveworks/common/test"
  7. "github.com/weaveworks/scope/probe/docker"
  8. "github.com/weaveworks/scope/probe/kubernetes"
  9. "github.com/weaveworks/scope/probe/process"
  10. "github.com/weaveworks/scope/render"
  11. "github.com/weaveworks/scope/render/detailed"
  12. "github.com/weaveworks/scope/render/expected"
  13. "github.com/weaveworks/scope/report"
  14. "github.com/weaveworks/scope/test/fixture"
  15. "github.com/weaveworks/scope/test/reflect"
  16. )
  17. func child(t *testing.T, r render.Renderer, id string) detailed.NodeSummary {
  18. s, ok := detailed.MakeNodeSummary(detailed.RenderContext{Report: fixture.Report}, r.Render(context.Background(), fixture.Report).Nodes[id])
  19. if !ok {
  20. t.Fatalf("Expected node %s to be summarizable, but wasn't", id)
  21. }
  22. return s.SummarizeMetrics()
  23. }
  24. func connectionID(nodeID string, addr string) string {
  25. return fmt.Sprintf("%s-%s-%s-%d", nodeID, addr, "", 80)
  26. }
  27. func TestMakeDetailedHostNode(t *testing.T) {
  28. renderableNodes := render.HostRenderer.Render(context.Background(), fixture.Report).Nodes
  29. renderableNode := renderableNodes[fixture.ClientHostNodeID]
  30. have := detailed.MakeNode("hosts", detailed.RenderContext{Report: fixture.Report}, renderableNodes, renderableNode)
  31. containerImageNodeSummary := child(t, render.ContainerImageRenderer, expected.ClientContainerImageNodeID)
  32. containerNodeSummary := child(t, render.ContainerRenderer, fixture.ClientContainerNodeID)
  33. process1NodeSummary := child(t, render.ProcessRenderer, fixture.ClientProcess1NodeID)
  34. process2NodeSummary := child(t, render.ProcessRenderer, fixture.ClientProcess2NodeID)
  35. podNodeSummary := child(t, render.PodRenderer, fixture.ClientPodNodeID)
  36. want := detailed.Node{
  37. NodeSummary: detailed.NodeSummary{
  38. BasicNodeSummary: detailed.BasicNodeSummary{
  39. ID: fixture.ClientHostNodeID,
  40. Label: "client",
  41. LabelMinor: "hostname.com",
  42. Rank: "hostname.com",
  43. Pseudo: false,
  44. Shape: "circle",
  45. Tag: "",
  46. },
  47. Adjacency: report.MakeIDList(fixture.ServerHostNodeID),
  48. Metadata: []report.MetadataRow{
  49. {
  50. ID: "host_name",
  51. Label: "Hostname",
  52. Value: "client.hostname.com",
  53. Priority: 11,
  54. },
  55. {
  56. ID: "os",
  57. Label: "OS",
  58. Value: "Linux",
  59. Priority: 12,
  60. },
  61. {
  62. ID: "local_networks",
  63. Label: "Local networks",
  64. Value: "10.10.10.0/24",
  65. Priority: 13,
  66. },
  67. },
  68. Metrics: []report.MetricRow{
  69. {
  70. ID: report.HostCPUUsage,
  71. Label: "CPU",
  72. Format: "percent",
  73. Value: 0.07,
  74. Priority: 1,
  75. Metric: &fixture.ClientHostCPUMetric,
  76. },
  77. {
  78. ID: report.HostMemoryUsage,
  79. Label: "Memory",
  80. Format: "filesize",
  81. Value: 0.08,
  82. Priority: 2,
  83. Metric: &fixture.ClientHostMemoryMetric,
  84. },
  85. {
  86. ID: report.Load1,
  87. Label: "Load (1m)",
  88. Group: "load",
  89. Value: 0.09,
  90. Priority: 11,
  91. Metric: &fixture.ClientHostLoad1Metric,
  92. },
  93. },
  94. },
  95. Controls: []detailed.ControlInstance{},
  96. Children: []detailed.NodeSummaryGroup{
  97. {
  98. Label: "Pods",
  99. TopologyID: "pods",
  100. Columns: []detailed.Column{
  101. {ID: kubernetes.State, Label: "State"},
  102. {ID: report.Container, Label: "# Containers", Datatype: report.Number},
  103. {ID: kubernetes.IP, Label: "IP", Datatype: report.IP},
  104. },
  105. Nodes: []detailed.NodeSummary{podNodeSummary},
  106. },
  107. {
  108. Label: "Containers",
  109. TopologyID: "containers",
  110. Columns: []detailed.Column{
  111. {ID: docker.CPUTotalUsage, Label: "CPU", Datatype: report.Number},
  112. {ID: docker.MemoryUsage, Label: "Memory", Datatype: report.Number},
  113. },
  114. Nodes: []detailed.NodeSummary{containerNodeSummary},
  115. },
  116. {
  117. Label: "Processes",
  118. TopologyID: "processes",
  119. Columns: []detailed.Column{
  120. {ID: process.PID, Label: "PID", Datatype: report.Number},
  121. {ID: process.CPUUsage, Label: "CPU", Datatype: report.Number},
  122. {ID: process.MemoryUsage, Label: "Memory", Datatype: report.Number},
  123. },
  124. Nodes: []detailed.NodeSummary{process1NodeSummary, process2NodeSummary},
  125. },
  126. {
  127. Label: "Container images",
  128. TopologyID: "containers-by-image",
  129. Columns: []detailed.Column{},
  130. Nodes: []detailed.NodeSummary{containerImageNodeSummary},
  131. },
  132. },
  133. Connections: []detailed.ConnectionsSummary{
  134. {
  135. ID: "incoming-connections",
  136. TopologyID: "hosts",
  137. Label: "Inbound",
  138. Columns: detailed.NormalColumns,
  139. Connections: []detailed.Connection{},
  140. },
  141. {
  142. ID: "outgoing-connections",
  143. TopologyID: "hosts",
  144. Label: "Outbound",
  145. Columns: detailed.NormalColumns,
  146. Connections: []detailed.Connection{
  147. {
  148. ID: connectionID(fixture.ServerHostNodeID, ""),
  149. NodeID: fixture.ServerHostNodeID,
  150. Label: "server",
  151. LabelMinor: "hostname.com",
  152. Metadata: []report.MetadataRow{
  153. {
  154. ID: "port",
  155. Value: "80",
  156. },
  157. {
  158. ID: "count",
  159. Value: "2",
  160. },
  161. },
  162. },
  163. },
  164. },
  165. },
  166. }
  167. if !reflect.DeepEqual(want, have) {
  168. t.Errorf("%s", test.Diff(want, have))
  169. }
  170. }
  171. func TestMakeDetailedContainerNode(t *testing.T) {
  172. id := fixture.ServerContainerNodeID
  173. renderableNodes := render.ContainerWithImageNameRenderer.Render(context.Background(), fixture.Report).Nodes
  174. renderableNode, ok := renderableNodes[id]
  175. if !ok {
  176. t.Fatalf("Node not found: %s", id)
  177. }
  178. have := detailed.MakeNode("containers", detailed.RenderContext{Report: fixture.Report}, renderableNodes, renderableNode)
  179. serverProcessNodeSummary := child(t, render.ProcessRenderer, fixture.ServerProcessNodeID)
  180. want := detailed.Node{
  181. NodeSummary: detailed.NodeSummary{
  182. BasicNodeSummary: detailed.BasicNodeSummary{
  183. ID: id,
  184. Label: "server",
  185. LabelMinor: "server.hostname.com",
  186. Rank: fixture.ServerContainerImageName,
  187. Shape: "hexagon",
  188. Tag: "",
  189. Pseudo: false,
  190. },
  191. Metadata: []report.MetadataRow{
  192. {ID: "docker_image_name", Label: "Image name", Value: fixture.ServerContainerImageName, Priority: 2},
  193. {ID: "docker_container_state_human", Label: "State", Value: "running", Priority: 4},
  194. {ID: "docker_container_id", Label: "ID", Value: fixture.ServerContainerID, Priority: 11, Truncate: 12},
  195. },
  196. Metrics: []report.MetricRow{
  197. {
  198. ID: docker.CPUTotalUsage,
  199. Label: "CPU",
  200. Format: "percent",
  201. Value: 0.05,
  202. Priority: 1,
  203. Metric: &fixture.ServerContainerCPUMetric,
  204. },
  205. {
  206. ID: docker.MemoryUsage,
  207. Label: "Memory",
  208. Format: "filesize",
  209. Value: 0.06,
  210. Priority: 2,
  211. Metric: &fixture.ServerContainerMemoryMetric,
  212. },
  213. },
  214. Parents: []detailed.Parent{
  215. {
  216. ID: expected.ServerContainerImageNodeID,
  217. Label: fixture.ServerContainerImageName,
  218. TopologyID: "containers-by-image",
  219. },
  220. {
  221. ID: fixture.ServerPodNodeID,
  222. Label: "pong-b",
  223. TopologyID: "pods",
  224. },
  225. {
  226. ID: fixture.ServerHostNodeID,
  227. Label: "server",
  228. TopologyID: "hosts",
  229. },
  230. },
  231. },
  232. Controls: []detailed.ControlInstance{},
  233. Children: []detailed.NodeSummaryGroup{
  234. {
  235. Label: "Processes",
  236. TopologyID: "processes",
  237. Columns: []detailed.Column{
  238. {ID: process.PID, Label: "PID", Datatype: report.Number},
  239. {ID: process.CPUUsage, Label: "CPU", Datatype: report.Number},
  240. {ID: process.MemoryUsage, Label: "Memory", Datatype: report.Number},
  241. },
  242. Nodes: []detailed.NodeSummary{serverProcessNodeSummary},
  243. },
  244. },
  245. Connections: []detailed.ConnectionsSummary{
  246. {
  247. ID: "incoming-connections",
  248. TopologyID: "containers",
  249. Label: "Inbound",
  250. Columns: detailed.NormalColumns,
  251. Connections: []detailed.Connection{
  252. {
  253. ID: connectionID(fixture.ClientContainerNodeID, ""),
  254. NodeID: fixture.ClientContainerNodeID,
  255. Label: "client",
  256. LabelMinor: "client.hostname.com",
  257. Metadata: []report.MetadataRow{
  258. {
  259. ID: "port",
  260. Value: "80",
  261. },
  262. {
  263. ID: "count",
  264. Value: "2",
  265. },
  266. },
  267. },
  268. {
  269. ID: connectionID(render.IncomingInternetID, fixture.RandomClientIP),
  270. NodeID: render.IncomingInternetID,
  271. Label: fixture.RandomClientIP,
  272. Metadata: []report.MetadataRow{
  273. {
  274. ID: "port",
  275. Value: "80",
  276. },
  277. {
  278. ID: "count",
  279. Value: "1",
  280. },
  281. },
  282. },
  283. },
  284. },
  285. {
  286. ID: "outgoing-connections",
  287. TopologyID: "containers",
  288. Label: "Outbound",
  289. Columns: detailed.NormalColumns,
  290. Connections: []detailed.Connection{},
  291. },
  292. },
  293. }
  294. if !reflect.DeepEqual(want, have) {
  295. t.Errorf("%s", test.Diff(want, have))
  296. }
  297. }
  298. func TestMakeDetailedPodNode(t *testing.T) {
  299. id := fixture.ServerPodNodeID
  300. renderableNodes := render.PodRenderer.Render(context.Background(), fixture.Report).Nodes
  301. renderableNode, ok := renderableNodes[id]
  302. if !ok {
  303. t.Fatalf("Node not found: %s", id)
  304. }
  305. have := detailed.MakeNode("pods", detailed.RenderContext{Report: fixture.Report}, renderableNodes, renderableNode)
  306. containerNodeSummary := child(t, render.ContainerWithImageNameRenderer, fixture.ServerContainerNodeID)
  307. serverProcessNodeSummary := child(t, render.ProcessRenderer, fixture.ServerProcessNodeID)
  308. want := detailed.Node{
  309. NodeSummary: detailed.NodeSummary{
  310. BasicNodeSummary: detailed.BasicNodeSummary{
  311. ID: id,
  312. Label: "pong-b",
  313. LabelMinor: "1 container",
  314. Rank: "ping/pong-b",
  315. Shape: "heptagon",
  316. Tag: "",
  317. Pseudo: false,
  318. },
  319. Metadata: []report.MetadataRow{
  320. {ID: "kubernetes_state", Label: "State", Value: "running", Priority: 2},
  321. {ID: "container", Label: "# Containers", Value: "1", Priority: 4, Datatype: report.Number},
  322. {ID: "kubernetes_namespace", Label: "Namespace", Value: "ping", Priority: 5},
  323. },
  324. Parents: []detailed.Parent{
  325. {
  326. ID: fixture.ServiceNodeID,
  327. Label: fixture.ServiceName,
  328. TopologyID: "services",
  329. },
  330. {
  331. ID: fixture.ServerHostNodeID,
  332. Label: "server",
  333. TopologyID: "hosts",
  334. },
  335. },
  336. },
  337. Controls: []detailed.ControlInstance{},
  338. Children: []detailed.NodeSummaryGroup{
  339. {
  340. Label: "Containers",
  341. TopologyID: "containers",
  342. Columns: []detailed.Column{
  343. {ID: docker.CPUTotalUsage, Label: "CPU", Datatype: report.Number},
  344. {ID: docker.MemoryUsage, Label: "Memory", Datatype: report.Number},
  345. },
  346. Nodes: []detailed.NodeSummary{containerNodeSummary},
  347. },
  348. {
  349. Label: "Processes",
  350. TopologyID: "processes",
  351. Columns: []detailed.Column{
  352. {ID: process.PID, Label: "PID", Datatype: report.Number},
  353. {ID: process.CPUUsage, Label: "CPU", Datatype: report.Number},
  354. {ID: process.MemoryUsage, Label: "Memory", Datatype: report.Number},
  355. },
  356. Nodes: []detailed.NodeSummary{serverProcessNodeSummary},
  357. },
  358. },
  359. Connections: []detailed.ConnectionsSummary{
  360. {
  361. ID: "incoming-connections",
  362. TopologyID: "pods",
  363. Label: "Inbound",
  364. Columns: detailed.NormalColumns,
  365. Connections: []detailed.Connection{
  366. {
  367. ID: connectionID(fixture.ClientPodNodeID, ""),
  368. NodeID: fixture.ClientPodNodeID,
  369. Label: "pong-a",
  370. LabelMinor: "1 container",
  371. Metadata: []report.MetadataRow{
  372. {
  373. ID: "port",
  374. Value: "80",
  375. },
  376. {
  377. ID: "count",
  378. Value: "2",
  379. },
  380. },
  381. },
  382. {
  383. ID: connectionID(render.IncomingInternetID, fixture.RandomClientIP),
  384. NodeID: render.IncomingInternetID,
  385. Label: fixture.RandomClientIP,
  386. Metadata: []report.MetadataRow{
  387. {
  388. ID: "port",
  389. Value: "80",
  390. },
  391. {
  392. ID: "count",
  393. Value: "1",
  394. },
  395. },
  396. },
  397. },
  398. },
  399. {
  400. ID: "outgoing-connections",
  401. TopologyID: "pods",
  402. Label: "Outbound",
  403. Columns: detailed.NormalColumns,
  404. Connections: []detailed.Connection{},
  405. },
  406. },
  407. }
  408. if !reflect.DeepEqual(want, have) {
  409. t.Errorf("%s", test.Diff(want, have))
  410. }
  411. }