file_test.go 2.6 KB

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