weave_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package app_test
  2. import (
  3. "net"
  4. "sync"
  5. "testing"
  6. "time"
  7. fsouza "github.com/fsouza/go-dockerclient"
  8. "github.com/weaveworks/scope/app"
  9. "github.com/weaveworks/scope/test"
  10. )
  11. type mockDockerClient struct{}
  12. func (mockDockerClient) ListContainers(fsouza.ListContainersOptions) ([]fsouza.APIContainers, error) {
  13. return []fsouza.APIContainers{
  14. {
  15. Names: []string{"/" + containerName},
  16. ID: containerID,
  17. },
  18. {
  19. Names: []string{"/notme"},
  20. ID: "1234abcd",
  21. },
  22. }, nil
  23. }
  24. type entry struct {
  25. containerid string
  26. ip net.IP
  27. }
  28. type mockWeaveClient struct {
  29. sync.Mutex
  30. published map[string]entry
  31. }
  32. func (m *mockWeaveClient) AddDNSEntry(hostname, containerid string, ip net.IP) error {
  33. m.Lock()
  34. defer m.Unlock()
  35. m.published[hostname] = entry{containerid, ip}
  36. return nil
  37. }
  38. func (m *mockWeaveClient) Expose() error {
  39. return nil
  40. }
  41. const (
  42. hostname = "foo.weave"
  43. containerName = "bar"
  44. containerID = "a1b2c3d4"
  45. )
  46. var (
  47. ip = net.ParseIP("1.2.3.4")
  48. )
  49. func TestWeave(t *testing.T) {
  50. weaveClient := &mockWeaveClient{
  51. published: map[string]entry{},
  52. }
  53. dockerClient := mockDockerClient{}
  54. interfaces := func() ([]app.Interface, error) {
  55. return []app.Interface{
  56. {
  57. Name: "eth0",
  58. Addrs: []net.Addr{
  59. &net.IPAddr{
  60. IP: ip,
  61. },
  62. },
  63. },
  64. {
  65. Name: "docker0",
  66. Addrs: []net.Addr{
  67. &net.IPAddr{
  68. IP: net.ParseIP("4.3.2.1"),
  69. },
  70. },
  71. },
  72. }, nil
  73. }
  74. publisher := app.NewWeavePublisher(
  75. weaveClient, dockerClient, interfaces,
  76. hostname, containerName)
  77. defer publisher.Stop()
  78. want := map[string]entry{
  79. hostname: {containerID, ip},
  80. }
  81. test.Poll(t, 100*time.Millisecond, want, func() interface{} {
  82. weaveClient.Lock()
  83. defer weaveClient.Unlock()
  84. result := map[string]entry{}
  85. for k, v := range weaveClient.published {
  86. result[k] = v
  87. }
  88. return result
  89. })
  90. }