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