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