file_test.go 3.0 KB

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