file_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package file
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "github.com/docker/docker/pkg/discovery"
  7. "github.com/go-check/check"
  8. )
  9. // Hook up gocheck into the "go test" runner.
  10. func Test(t *testing.T) { check.TestingT(t) }
  11. type DiscoverySuite struct{}
  12. var _ = check.Suite(&DiscoverySuite{})
  13. func (s *DiscoverySuite) TestInitialize(c *check.C) {
  14. d := &Discovery{}
  15. d.Initialize("/path/to/file", 1000, 0, nil)
  16. c.Assert(d.path, check.Equals, "/path/to/file")
  17. }
  18. func (s *DiscoverySuite) TestNew(c *check.C) {
  19. d, err := discovery.New("file:///path/to/file", 0, 0, nil)
  20. c.Assert(err, check.IsNil)
  21. c.Assert(d.(*Discovery).path, check.Equals, "/path/to/file")
  22. }
  23. func (s *DiscoverySuite) TestContent(c *check.C) {
  24. data := `
  25. 1.1.1.[1:2]:1111
  26. 2.2.2.[2:4]:2222
  27. `
  28. ips := parseFileContent([]byte(data))
  29. c.Assert(ips, check.HasLen, 5)
  30. c.Assert(ips[0], check.Equals, "1.1.1.1:1111")
  31. c.Assert(ips[1], check.Equals, "1.1.1.2:1111")
  32. c.Assert(ips[2], check.Equals, "2.2.2.2:2222")
  33. c.Assert(ips[3], check.Equals, "2.2.2.3:2222")
  34. c.Assert(ips[4], check.Equals, "2.2.2.4:2222")
  35. }
  36. func (s *DiscoverySuite) TestRegister(c *check.C) {
  37. discovery := &Discovery{path: "/path/to/file"}
  38. c.Assert(discovery.Register("0.0.0.0"), check.NotNil)
  39. }
  40. func (s *DiscoverySuite) TestParsingContentsWithComments(c *check.C) {
  41. data := `
  42. ### test ###
  43. 1.1.1.1:1111 # inline comment
  44. # 2.2.2.2:2222
  45. ### empty line with comment
  46. 3.3.3.3:3333
  47. ### test ###
  48. `
  49. ips := parseFileContent([]byte(data))
  50. c.Assert(ips, check.HasLen, 2)
  51. c.Assert("1.1.1.1:1111", check.Equals, ips[0])
  52. c.Assert("3.3.3.3:3333", check.Equals, ips[1])
  53. }
  54. func (s *DiscoverySuite) TestWatch(c *check.C) {
  55. data := `
  56. 1.1.1.1:1111
  57. 2.2.2.2:2222
  58. `
  59. expected := discovery.Entries{
  60. &discovery.Entry{Host: "1.1.1.1", Port: "1111"},
  61. &discovery.Entry{Host: "2.2.2.2", Port: "2222"},
  62. }
  63. // Create a temporary file and remove it.
  64. tmp, err := ioutil.TempFile(os.TempDir(), "discovery-file-test")
  65. c.Assert(err, check.IsNil)
  66. c.Assert(tmp.Close(), check.IsNil)
  67. c.Assert(os.Remove(tmp.Name()), check.IsNil)
  68. // Set up file discovery.
  69. d := &Discovery{}
  70. d.Initialize(tmp.Name(), 1000, 0, nil)
  71. stopCh := make(chan struct{})
  72. ch, errCh := d.Watch(stopCh)
  73. // Make sure it fires errors since the file doesn't exist.
  74. c.Assert(<-errCh, check.NotNil)
  75. // We have to drain the error channel otherwise Watch will get stuck.
  76. go func() {
  77. for range errCh {
  78. }
  79. }()
  80. // Write the file and make sure we get the expected value back.
  81. c.Assert(ioutil.WriteFile(tmp.Name(), []byte(data), 0600), check.IsNil)
  82. c.Assert(<-ch, check.DeepEquals, expected)
  83. // Add a new entry and look it up.
  84. expected = append(expected, &discovery.Entry{Host: "3.3.3.3", Port: "3333"})
  85. f, err := os.OpenFile(tmp.Name(), os.O_APPEND|os.O_WRONLY, 0600)
  86. c.Assert(err, check.IsNil)
  87. c.Assert(f, check.NotNil)
  88. _, err = f.WriteString("\n3.3.3.3:3333\n")
  89. c.Assert(err, check.IsNil)
  90. f.Close()
  91. c.Assert(<-ch, check.DeepEquals, expected)
  92. // Stop and make sure it closes all channels.
  93. close(stopCh)
  94. c.Assert(<-ch, check.IsNil)
  95. c.Assert(<-errCh, check.IsNil)
  96. }