api_topology_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package app_test
  2. import (
  3. "fmt"
  4. "net/url"
  5. "testing"
  6. "github.com/gorilla/websocket"
  7. "github.com/ugorji/go/codec"
  8. "github.com/weaveworks/scope/app"
  9. "github.com/weaveworks/scope/render/detailed"
  10. "github.com/weaveworks/scope/render/expected"
  11. "github.com/weaveworks/scope/test/fixture"
  12. )
  13. func TestAll(t *testing.T) {
  14. ts := topologyServer()
  15. defer ts.Close()
  16. body := getRawJSON(t, ts, "/api/topology")
  17. var topologies []app.APITopologyDesc
  18. decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
  19. if err := decoder.Decode(&topologies); err != nil {
  20. t.Fatalf("JSON parse error: %s", err)
  21. }
  22. getTopology := func(topologyURL string) {
  23. body := getRawJSON(t, ts, topologyURL)
  24. var topology app.APITopology
  25. decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
  26. if err := decoder.Decode(&topology); err != nil {
  27. t.Fatalf("JSON parse error: %s", err)
  28. }
  29. for _, node := range topology.Nodes {
  30. body := getRawJSON(t, ts, fmt.Sprintf("%s/%s", topologyURL, url.QueryEscape(node.ID)))
  31. var node app.APINode
  32. decoder = codec.NewDecoderBytes(body, &codec.JsonHandle{})
  33. if err := decoder.Decode(&node); err != nil {
  34. t.Fatalf("JSON parse error: %s", err)
  35. }
  36. }
  37. }
  38. for _, topology := range topologies {
  39. getTopology(topology.URL)
  40. for _, subTopology := range topology.SubTopologies {
  41. getTopology(subTopology.URL)
  42. }
  43. }
  44. }
  45. func TestAPITopologyProcesses(t *testing.T) {
  46. ts := topologyServer()
  47. defer ts.Close()
  48. is404(t, ts, "/api/topology/processes/foobar")
  49. {
  50. body := getRawJSON(t, ts, "/api/topology/processes/"+fixture.ServerProcessNodeID)
  51. var node app.APINode
  52. decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
  53. if err := decoder.Decode(&node); err != nil {
  54. t.Fatal(err)
  55. }
  56. equals(t, fixture.ServerProcessNodeID, node.Node.ID)
  57. equals(t, "apache", node.Node.Label)
  58. equals(t, false, node.Node.Pseudo)
  59. // Let's not unit-test the specific content of the detail tables
  60. }
  61. {
  62. body := getRawJSON(t, ts, "/api/topology/processes-by-name/"+
  63. url.QueryEscape(fixture.Client1Name))
  64. var node app.APINode
  65. decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
  66. if err := decoder.Decode(&node); err != nil {
  67. t.Fatal(err)
  68. }
  69. equals(t, fixture.Client1Name, node.Node.ID)
  70. equals(t, fixture.Client1Name, node.Node.Label)
  71. equals(t, false, node.Node.Pseudo)
  72. // Let's not unit-test the specific content of the detail tables
  73. }
  74. }
  75. func TestAPITopologyHosts(t *testing.T) {
  76. ts := topologyServer()
  77. defer ts.Close()
  78. is404(t, ts, "/api/topology/hosts/foobar")
  79. {
  80. body := getRawJSON(t, ts, "/api/topology/hosts")
  81. var topo app.APITopology
  82. decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
  83. if err := decoder.Decode(&topo); err != nil {
  84. t.Fatal(err)
  85. }
  86. // Should have the rendered host nodes
  87. for id := range expected.RenderedHosts {
  88. if _, ok := topo.Nodes[id]; !ok {
  89. t.Errorf("Expected output to include node: %s, but wasn't found", id)
  90. }
  91. }
  92. }
  93. {
  94. body := getRawJSON(t, ts, "/api/topology/hosts/"+fixture.ServerHostNodeID)
  95. var node app.APINode
  96. decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
  97. if err := decoder.Decode(&node); err != nil {
  98. t.Fatal(err)
  99. }
  100. equals(t, fixture.ServerHostNodeID, node.Node.ID)
  101. equals(t, "server", node.Node.Label)
  102. equals(t, false, node.Node.Pseudo)
  103. // Let's not unit-test the specific content of the detail tables
  104. }
  105. }
  106. // Basic websocket test
  107. func TestAPITopologyWebsocket(t *testing.T) {
  108. ts := topologyServer()
  109. defer ts.Close()
  110. url := "/api/topology/processes/ws"
  111. // Not a websocket request
  112. res, _ := checkGet(t, ts, url)
  113. if have := res.StatusCode; have != 400 {
  114. t.Fatalf("Expected status %d, got %d.", 400, have)
  115. }
  116. // Proper websocket request
  117. ts.URL = "ws" + ts.URL[len("http"):]
  118. dialer := &websocket.Dialer{}
  119. ws, res, err := dialer.Dial(ts.URL+url, nil)
  120. ok(t, err)
  121. defer ws.Close()
  122. if want, have := 101, res.StatusCode; want != have {
  123. t.Fatalf("want %d, have %d", want, have)
  124. }
  125. _, p, err := ws.ReadMessage()
  126. ok(t, err)
  127. var d detailed.Diff
  128. decoder := codec.NewDecoderBytes(p, &codec.JsonHandle{})
  129. if err := decoder.Decode(&d); err != nil {
  130. t.Fatalf("JSON parse error: %s", err)
  131. }
  132. equals(t, 6, len(d.Add))
  133. equals(t, 0, len(d.Update))
  134. equals(t, 0, len(d.Remove))
  135. }
  136. func newu64(value uint64) *uint64 { return &value }