client_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package weave_test
  2. import (
  3. "fmt"
  4. "net"
  5. "net/http"
  6. "net/http/httptest"
  7. "reflect"
  8. "strings"
  9. "sync"
  10. "testing"
  11. "github.com/weaveworks/common/exec"
  12. "github.com/weaveworks/common/test"
  13. testExec "github.com/weaveworks/common/test/exec"
  14. "github.com/weaveworks/scope/common/weave"
  15. )
  16. const (
  17. mockHostID = "host1"
  18. mockWeavePeerName = "winnebago"
  19. mockWeavePeerNickName = "winny"
  20. mockContainerID = "83183a667c01"
  21. mockContainerMAC = "d6:f2:5a:12:36:a8"
  22. mockContainerIP = "10.0.0.123"
  23. mockContainerIPWithScope = ";10.0.0.123"
  24. mockHostname = "hostname.weave.local"
  25. mockProxyAddress = "unix:///foo/bar/weave.sock"
  26. mockDriverName = "weave_mock"
  27. )
  28. var (
  29. mockResponse = fmt.Sprintf(`{
  30. "Router": {
  31. "Peers": [{
  32. "Name": "%s",
  33. "NickName": "%s"
  34. }]
  35. },
  36. "DNS": {
  37. "Entries": [{
  38. "ContainerID": "%s",
  39. "Hostname": "%s.",
  40. "Tombstone": 0
  41. }]
  42. },
  43. "Proxy": {
  44. "Addresses": [
  45. "%s"
  46. ]
  47. },
  48. "Plugin": {
  49. "DriverName": "%s"
  50. }
  51. }`, mockWeavePeerName, mockWeavePeerNickName, mockContainerID, mockHostname, mockProxyAddress, mockDriverName)
  52. mockIP = net.ParseIP("1.2.3.4")
  53. )
  54. func mockWeaveRouter(w http.ResponseWriter, r *http.Request) {
  55. if _, err := w.Write([]byte(mockResponse)); err != nil {
  56. panic(err)
  57. }
  58. }
  59. func TestStatus(t *testing.T) {
  60. s := httptest.NewServer(http.HandlerFunc(mockWeaveRouter))
  61. defer s.Close()
  62. client := weave.NewClient(s.URL)
  63. status, err := client.Status()
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. want := weave.Status{
  68. Router: weave.Router{
  69. Peers: []weave.Peer{
  70. {
  71. Name: mockWeavePeerName,
  72. NickName: mockWeavePeerNickName,
  73. },
  74. },
  75. },
  76. DNS: &weave.DNS{
  77. Entries: []struct {
  78. Hostname string
  79. ContainerID string
  80. Tombstone int64
  81. }{
  82. {
  83. Hostname: mockHostname + ".",
  84. ContainerID: mockContainerID,
  85. Tombstone: 0,
  86. },
  87. },
  88. },
  89. Proxy: &weave.Proxy{
  90. Addresses: []string{mockProxyAddress},
  91. },
  92. Plugin: &weave.Plugin{
  93. DriverName: mockDriverName,
  94. },
  95. }
  96. if !reflect.DeepEqual(status, want) {
  97. t.Fatal(test.Diff(status, want))
  98. }
  99. }
  100. type entry struct {
  101. containerid string
  102. ip net.IP
  103. }
  104. func TestDNSAdd(t *testing.T) {
  105. mtx := sync.Mutex{}
  106. published := map[string]entry{}
  107. s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  108. mtx.Lock()
  109. defer mtx.Unlock()
  110. parts := strings.SplitN(r.URL.Path, "/", 4)
  111. containerID, ip := parts[2], net.ParseIP(parts[3])
  112. fqdn := r.FormValue("fqdn")
  113. published[fqdn] = entry{containerID, ip}
  114. w.WriteHeader(http.StatusNoContent)
  115. }))
  116. defer s.Close()
  117. client := weave.NewClient(s.URL)
  118. err := client.AddDNSEntry(mockHostname, mockContainerID, mockIP)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. want := map[string]entry{
  123. mockHostname: {mockContainerID, mockIP},
  124. }
  125. if !reflect.DeepEqual(published, want) {
  126. t.Fatal(test.Diff(published, want))
  127. }
  128. }
  129. func TestExpose(t *testing.T) {
  130. oldExecCmd := exec.Command
  131. defer func() { exec.Command = oldExecCmd }()
  132. psCalled := false
  133. exec.Command = func(name string, args ...string) exec.Cmd {
  134. if args[0] == "expose" {
  135. t.Fatal("Expose not expected")
  136. return nil
  137. }
  138. psCalled = true
  139. return testExec.NewMockCmdString(fmt.Sprintf("%s %s %s/24\n", mockContainerID, mockContainerMAC, mockContainerIP))
  140. }
  141. client := weave.NewClient("")
  142. if err := client.Expose(); err != nil {
  143. t.Fatal(err)
  144. }
  145. if !psCalled {
  146. t.Fatal("Expected a call to weave ps")
  147. }
  148. psCalled, exposeCalled := false, false
  149. exec.Command = func(name string, args ...string) exec.Cmd {
  150. if len(args) >= 2 && args[1] == "expose" {
  151. exposeCalled = true
  152. return testExec.NewMockCmdString("")
  153. }
  154. psCalled = true
  155. return testExec.NewMockCmdString("")
  156. }
  157. if err := client.Expose(); err != nil {
  158. t.Fatal(err)
  159. }
  160. if !psCalled || !exposeCalled {
  161. t.Fatal("Expected a call to weave ps & expose")
  162. }
  163. }