github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/pkg/discovery/file/file_test.go (about) 1 package file 2 3 import ( 4 "io/ioutil" 5 "os" 6 "testing" 7 8 "github.com/docker/docker/pkg/discovery" 9 10 "github.com/go-check/check" 11 ) 12 13 // Hook up gocheck into the "go test" runner. 14 func Test(t *testing.T) { check.TestingT(t) } 15 16 type DiscoverySuite struct{} 17 18 var _ = check.Suite(&DiscoverySuite{}) 19 20 func (s *DiscoverySuite) TestInitialize(c *check.C) { 21 d := &Discovery{} 22 d.Initialize("/path/to/file", 1000, 0, nil) 23 c.Assert(d.path, check.Equals, "/path/to/file") 24 } 25 26 func (s *DiscoverySuite) TestNew(c *check.C) { 27 d, err := discovery.New("file:///path/to/file", 0, 0, nil) 28 c.Assert(err, check.IsNil) 29 c.Assert(d.(*Discovery).path, check.Equals, "/path/to/file") 30 } 31 32 func (s *DiscoverySuite) TestContent(c *check.C) { 33 data := ` 34 1.1.1.[1:2]:1111 35 2.2.2.[2:4]:2222 36 ` 37 ips := parseFileContent([]byte(data)) 38 c.Assert(ips, check.HasLen, 5) 39 c.Assert(ips[0], check.Equals, "1.1.1.1:1111") 40 c.Assert(ips[1], check.Equals, "1.1.1.2:1111") 41 c.Assert(ips[2], check.Equals, "2.2.2.2:2222") 42 c.Assert(ips[3], check.Equals, "2.2.2.3:2222") 43 c.Assert(ips[4], check.Equals, "2.2.2.4:2222") 44 } 45 46 func (s *DiscoverySuite) TestRegister(c *check.C) { 47 discovery := &Discovery{path: "/path/to/file"} 48 c.Assert(discovery.Register("0.0.0.0"), check.NotNil) 49 } 50 51 func (s *DiscoverySuite) TestParsingContentsWithComments(c *check.C) { 52 data := ` 53 ### test ### 54 1.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 c.Assert(ips, check.HasLen, 2) 62 c.Assert("1.1.1.1:1111", check.Equals, ips[0]) 63 c.Assert("3.3.3.3:3333", check.Equals, ips[1]) 64 } 65 66 func (s *DiscoverySuite) TestWatch(c *check.C) { 67 data := ` 68 1.1.1.1:1111 69 2.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 c.Assert(err, check.IsNil) 79 c.Assert(tmp.Close(), check.IsNil) 80 c.Assert(os.Remove(tmp.Name()), check.IsNil) 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 c.Assert(<-errCh, check.NotNil) 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 c.Assert(ioutil.WriteFile(tmp.Name(), []byte(data), 0600), check.IsNil) 98 c.Assert(<-ch, check.DeepEquals, 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 c.Assert(err, check.IsNil) 104 c.Assert(f, check.NotNil) 105 _, err = f.WriteString("\n3.3.3.3:3333\n") 106 c.Assert(err, check.IsNil) 107 f.Close() 108 c.Assert(<-ch, check.DeepEquals, expected) 109 110 // Stop and make sure it closes all channels. 111 close(stopCh) 112 c.Assert(<-ch, check.IsNil) 113 c.Assert(<-errCh, check.IsNil) 114 }