short_lived_connections_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package render_test
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "github.com/weaveworks/common/mtime"
  7. "github.com/weaveworks/scope/render"
  8. "github.com/weaveworks/scope/report"
  9. "github.com/weaveworks/scope/test/utils"
  10. )
  11. var (
  12. serverHostID = "host1"
  13. serverHostNodeID = report.MakeHostNodeID(serverHostID)
  14. randomIP = "3.4.5.6"
  15. randomPort = "56789"
  16. randomEndpointNodeID = report.MakeEndpointNodeID(serverHostID, "", randomIP, randomPort)
  17. serverIP = "192.168.1.1"
  18. serverPort = "80"
  19. serverEndpointNodeID = report.MakeEndpointNodeID(serverHostID, "", serverIP, serverPort)
  20. container1ID = "11b2c3d4e5"
  21. container1IP = "192.168.0.1"
  22. container1Name = "foo"
  23. container1NodeID = report.MakeContainerNodeID(container1ID)
  24. container1Port = "16782"
  25. container1EndpointNodeID = report.MakeEndpointNodeID(serverHostID, "", container1IP, container1Port)
  26. duplicatedIP = "192.168.0.2"
  27. duplicatedPort = "80"
  28. duplicatedEndpointNodeID = report.MakeEndpointNodeID(serverHostID, "", duplicatedIP, duplicatedPort)
  29. container2ID = "21b2c3d4e5"
  30. container2IP = duplicatedIP
  31. container2Name = "bar"
  32. container2NodeID = report.MakeContainerNodeID(container2ID)
  33. pauseContainerID = "31b2c3d4e5"
  34. pauseContainerIP = duplicatedIP
  35. pauseContainerName = "POD"
  36. pauseContainerNodeID = report.MakeContainerNodeID(pauseContainerID)
  37. rpt = report.Report{
  38. Endpoint: report.Topology{
  39. Nodes: report.Nodes{
  40. randomEndpointNodeID: report.MakeNode(randomEndpointNodeID).
  41. WithTopology(report.Endpoint).WithAdjacent(serverEndpointNodeID),
  42. serverEndpointNodeID: report.MakeNode(serverEndpointNodeID).
  43. WithTopology(report.Endpoint),
  44. container1EndpointNodeID: report.MakeNode(container1EndpointNodeID).
  45. WithTopology(report.Endpoint).WithAdjacent(duplicatedEndpointNodeID),
  46. duplicatedEndpointNodeID: report.MakeNode(duplicatedEndpointNodeID).
  47. WithTopology(report.Endpoint),
  48. },
  49. },
  50. Container: report.Topology{
  51. Nodes: report.Nodes{
  52. container1NodeID: report.MakeNodeWith(container1NodeID, map[string]string{
  53. report.DockerContainerID: container1ID,
  54. report.DockerContainerName: container1Name,
  55. report.HostNodeID: serverHostNodeID,
  56. }).
  57. WithSets(report.MakeSets().
  58. Add(report.DockerContainerIPs, report.MakeStringSet(container1IP)).
  59. Add(report.DockerContainerIPsWithScopes, report.MakeStringSet(report.MakeAddressNodeID("", container1IP))).
  60. Add(report.DockerContainerPorts, report.MakeStringSet(fmt.Sprintf("%s:%s->%s/tcp", serverIP, serverPort, serverPort))),
  61. ).WithTopology(report.Container),
  62. container2NodeID: report.MakeNodeWith(container2NodeID, map[string]string{
  63. report.DockerContainerID: container2ID,
  64. report.DockerContainerName: container2Name,
  65. report.HostNodeID: serverHostNodeID,
  66. }).
  67. WithSets(report.MakeSets().
  68. Add(report.DockerContainerIPs, report.MakeStringSet(container2IP)).
  69. Add(report.DockerContainerIPsWithScopes, report.MakeStringSet(report.MakeAddressNodeID("", container2IP))),
  70. ).WithTopology(report.Container),
  71. pauseContainerNodeID: report.MakeNodeWith(pauseContainerNodeID, map[string]string{
  72. report.DockerContainerID: pauseContainerID,
  73. report.DockerContainerName: pauseContainerName,
  74. report.HostNodeID: serverHostNodeID,
  75. }).
  76. WithSets(report.MakeSets().
  77. Add(report.DockerContainerIPs, report.MakeStringSet(pauseContainerIP)).
  78. Add(report.DockerContainerIPsWithScopes, report.MakeStringSet(report.MakeAddressNodeID("", pauseContainerIP))),
  79. ).WithTopology(report.Container).WithLatest(report.DoesNotMakeConnections, mtime.Now(), ""),
  80. },
  81. },
  82. Host: report.Topology{
  83. Nodes: report.Nodes{
  84. serverHostNodeID: report.MakeNodeWith(serverHostNodeID, map[string]string{
  85. report.HostNodeID: serverHostNodeID,
  86. }).
  87. WithSets(report.MakeSets().
  88. Add(report.HostLocalNetworks, report.MakeStringSet("192.168.0.0/16")),
  89. ).WithTopology(report.Host),
  90. },
  91. },
  92. }
  93. )
  94. func TestShortLivedInternetNodeConnections(t *testing.T) {
  95. have := utils.Prune(render.ContainerWithImageNameRenderer.Render(context.Background(), rpt).Nodes)
  96. // Conntracked-only connections from the internet should be assigned to the internet pseudonode
  97. internet, ok := have[render.IncomingInternetID]
  98. if !ok {
  99. t.Fatal("Expected output to have an incoming internet node")
  100. }
  101. if !internet.Adjacency.Contains(container1NodeID) {
  102. t.Errorf("Expected internet node to have adjacency to %s, but only had %v", container1NodeID, internet.Adjacency)
  103. }
  104. }
  105. func TestPauseContainerDiscarded(t *testing.T) {
  106. have := utils.Prune(render.ContainerWithImageNameRenderer.Render(context.Background(), rpt).Nodes)
  107. // There should only be a connection from container1 and the destination should be container2
  108. container1, ok := have[container1NodeID]
  109. if !ok {
  110. t.Fatal("Expected output to have container1")
  111. }
  112. if len(container1.Adjacency) != 1 || !container1.Adjacency.Contains(container2NodeID) {
  113. t.Errorf("Expected container1 to have a unique adjacency to %s, but instead had %v", container2NodeID, container1.Adjacency)
  114. }
  115. }