add ip generator for file

Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
This commit is contained in:
Chanwit Kaewkasi 2015-03-12 11:52:49 +07:00
parent 09b8eb8564
commit 882be558e8
2 changed files with 25 additions and 3 deletions

View File

@ -23,13 +23,22 @@ func (s *FileDiscoveryService) Initialize(path string, heartbeat int) error {
return nil
}
func parseFileContent(content []byte) []string {
result := make([]string, 0)
for _, line := range strings.Split(strings.TrimSpace(string(content)), "\n") {
for _, ip := range discovery.Generate(line) {
result = append(result, ip)
}
}
return result
}
func (s *FileDiscoveryService) Fetch() ([]*discovery.Entry, error) {
data, err := ioutil.ReadFile(s.path)
fileContent, err := ioutil.ReadFile(s.path)
if err != nil {
return nil, err
}
return discovery.CreateEntries(strings.Split(string(data), "\n"))
return discovery.CreateEntries(parseFileContent(fileContent))
}
func (s *FileDiscoveryService) Watch(callback discovery.WatchCallback) {

View File

@ -12,6 +12,19 @@ func TestInitialize(t *testing.T) {
assert.Equal(t, discovery.path, "/path/to/file")
}
func TestContent(t *testing.T) {
data := `
1.1.1.[1:2]:1111
2.2.2.[2:4]:2222
`
ips := parseFileContent([]byte(data))
assert.Equal(t, ips[0], "1.1.1.1:1111")
assert.Equal(t, ips[1], "1.1.1.2:1111")
assert.Equal(t, ips[2], "2.2.2.2:2222")
assert.Equal(t, ips[3], "2.2.2.3:2222")
assert.Equal(t, ips[4], "2.2.2.4:2222")
}
func TestRegister(t *testing.T) {
discovery := &FileDiscoveryService{path: "/path/to/file"}
assert.Error(t, discovery.Register("0.0.0.0"))