Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver006 committed May 2, 2019
1 parent 48b934d commit e928651
Show file tree
Hide file tree
Showing 6 changed files with 527 additions and 655 deletions.
9 changes: 5 additions & 4 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ steps:
environment:
GO111MODULE: on
LOG_LEVEL: "info"
TEST_TILE38_URI: "tile38:9851"
TEST_TILE38_URI: "redis://tile38:9851"
TEST_REDIS_URI: "redis://redis:6379"
TEST_SECOND_REDIS_URI: "redis://moar-redis:6380"
TEST_REDIS_CLUSTER_MASTER_URI: "redis-cluster:7000"
TEST_REDIS_CLUSTER_SLAVE_URI: "redis-cluster:7005"
TEST_REDIS_CLUSTER_MASTER_URI: "redis://redis-cluster:7000"
TEST_REDIS_CLUSTER_SLAVE_URI: "redis://redis-cluster:7005"
COVERALLS_TOKEN:
from_secret: coveralls-token
commands:
- 'go build -mod=vendor'
- "sleep 10" # let the redis test instances all come up first
- 'go test -mod=vendor -v -covermode=atomic -cover -race -coverprofile=coverage.txt ./exporter/... --redis.addr=redis'
- 'go test -mod=vendor -v -covermode=atomic -cover -race -coverprofile=coverage.txt ./exporter/...'
- 'echo "checking gofmt"'
- 'echo " ! gofmt -d main.go 2>&1 | read " | bash'
- 'echo " ! gofmt -d exporter/*.go 2>&1 | read " | bash'
Expand Down
75 changes: 38 additions & 37 deletions exporter/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,93 +12,94 @@ import (
// loadRedisArgs loads the configuration for which redis hosts to monitor from either
// the environment or as passed from program arguments. Returns the list of host addrs,
// passwords, and their aliases.
func LoadRedisArgs(addr, password, alias, separator string) ([]string, []string, []string) {
func LoadRedisArgs(addr, password, alias, separator string) []RedisHost {
if addr == "" {
addr = "redis://localhost:6379"
}
var res []RedisHost
addrs := strings.Split(addr, separator)
passwords := strings.Split(password, separator)
for len(passwords) < len(addrs) {
passwords = append(passwords, passwords[0])
}
aliases := strings.Split(alias, separator)
for len(aliases) < len(addrs) {
aliases = append(aliases, aliases[0])
for idx, addr := range addrs {
var pwd, alias string
if idx < len(passwords) {
pwd = passwords[idx]
}
if idx < len(aliases) {
alias = aliases[idx]
}
res = append(res, RedisHost{Addr: addr, Password: pwd, Alias: alias})
}
return addrs, passwords, aliases
return res
}

// loadRedisFile opens the specified file and loads the configuration for which redis
// hosts to monitor. Returns the list of hosts addrs, passwords, and their aliases.
func LoadRedisFile(fileName string) ([]string, []string, []string, error) {
var addrs []string
var passwords []string
var aliases []string
func LoadRedisFile(fileName string) ([]RedisHost, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, nil, nil, err
return nil, err
}
r := csv.NewReader(file)
r.FieldsPerRecord = -1
records, err := r.ReadAll()
if err != nil {
return nil, nil, nil, err
return nil, err
}
file.Close()
// For each line, test if it contains an optional password and alias and provide them,
// else give them empty strings

var res []RedisHost
for _, record := range records {
length := len(record)
switch length {
var addr, pwd, alias string
switch len(record) {
case 3:
addrs = append(addrs, record[0])
passwords = append(passwords, record[1])
aliases = append(aliases, record[2])
addr = record[0]
pwd = record[1]
alias = record[2]
case 2:
addrs = append(addrs, record[0])
passwords = append(passwords, record[1])
aliases = append(aliases, "")
addr = record[0]
pwd = record[1]
case 1:
addrs = append(addrs, record[0])
passwords = append(passwords, "")
aliases = append(aliases, "")
addr = record[0]

default:
continue
}
res = append(res, RedisHost{Addr: addr, Password: pwd, Alias: alias})
}
return addrs, passwords, aliases, nil
return res, nil
}

func GetCloudFoundryRedisBindings() (addrs, passwords, aliases []string) {
func GetCloudFoundryRedisBindings() []RedisHost {
var res []RedisHost
if !cfenv.IsRunningOnCF() {
return
return res
}

appEnv, err := cfenv.Current()
if err != nil {
log.Warnln("Unable to get current CF environment", err)
return
return res
}

redisServices, err := appEnv.Services.WithTag("redis")
if err != nil {
log.Warnln("Error while getting redis services", err)
return
return res
}

for _, redisService := range redisServices {
credentials := redisService.Credentials
host := getAlternative(credentials, "host", "hostname")
port := getAlternative(credentials, "port")
password := getAlternative(credentials, "password")

addr := host + ":" + port
alias := redisService.Name

addrs = append(addrs, addr)
passwords = append(passwords, password)
aliases = append(aliases, alias)
pwd := getAlternative(credentials, "pwd")
res = append(res, RedisHost{Addr: addr, Password: pwd, Alias: redisService.Name})
}

return
return res
}

func getAlternative(credentials map[string]interface{}, alternatives ...string) string {
Expand Down
107 changes: 67 additions & 40 deletions exporter/discovery_test.go
Original file line number Diff line number Diff line change
@@ -1,100 +1,127 @@
package exporter

import (
"log"
"testing"
)

func cmpStringArrays(a1, a2 []string) bool {
if len(a1) != len(a2) {
return false
}
for n := range a1 {
if a1[n] != a2[n] {
return false
}
}
return true
}
log "github.com/sirupsen/logrus"
)

func TestLoadRedisArgs(t *testing.T) {
log.Println("TestLoadRedisArgs()")
tests := []struct {
addr, pwd, alias, sep string
wantAddr, wantPwds, wantAliases []string
addr, pwd, alias, sep string
wantAddrs, wantPwds, wantAliases []string
}{
{
addr: "",
sep: ",",
wantAddr: []string{"redis://localhost:6379"},
wantAddrs: []string{"redis://localhost:6379"},
wantPwds: []string{""},
wantAliases: []string{""},
},
{
addr: "redis://localhost:6379",
sep: ",",
wantAddr: []string{"redis://localhost:6379"},
wantAddrs: []string{"redis://localhost:6379"},
wantPwds: []string{""},
wantAliases: []string{""},
},
{
addr: "redis://localhost:6379,redis://localhost:7000",
sep: ",",
wantAddr: []string{"redis://localhost:6379", "redis://localhost:7000"},
wantAddrs: []string{"redis://localhost:6379", "redis://localhost:7000"},
wantPwds: []string{"", ""},
wantAliases: []string{"", ""},
},
{
addr: "redis://localhost:6379,redis://localhost:7000,redis://localhost:7001",
sep: ",",
wantAddr: []string{"redis://localhost:6379", "redis://localhost:7000", "redis://localhost:7001"},
wantAddrs: []string{"redis://localhost:6379", "redis://localhost:7000", "redis://localhost:7001"},
wantPwds: []string{"", "", ""},
wantAliases: []string{"", "", ""},
},
{
alias: "host-1",
sep: ",",
wantAddr: []string{"redis://localhost:6379"},
wantAddrs: []string{"redis://localhost:6379"},
wantPwds: []string{""},
wantAliases: []string{"host-1"},
},
}

for _, test := range tests {
sep := test.sep
addrs, pwds, aliases := LoadRedisArgs(test.addr, test.pwd, test.alias, sep)
if !cmpStringArrays(addrs, test.wantAddr) {
t.Errorf("addrs not matching wantAliases, got: %v want: %v", addrs, test.wantAddr)
}
if !cmpStringArrays(pwds, test.wantPwds) {
t.Errorf("pwds not matching wantAliases, got: %v want: %v", pwds, test.wantPwds)
}
if !cmpStringArrays(aliases, test.wantAliases) {
t.Errorf("aliases not matching wantAliases, got: %v want: %v", aliases, test.wantAliases)
}
hosts := LoadRedisArgs(test.addr, test.pwd, test.alias, sep)
checkHosts(
t, hosts,
test.wantAddrs,
test.wantPwds,
test.wantAliases)
}
}

func TestLoadRedisFile(t *testing.T) {
if _, _, _, err := LoadRedisFile("doesnt-exist.txt"); err == nil {
if _, err := LoadRedisFile("doesnt-exist.txt"); err == nil {
t.Errorf("should have failed opening non existing file")
return
}

addrs, pwds, aliases, err := LoadRedisFile("../contrib/sample_redis_hosts_file.txt")
hosts, err := LoadRedisFile("../contrib/sample_redis_hosts_file.txt")
if err != nil {
t.Errorf("LoadRedisFile() failed, err: %s", err)
return
}
log.Printf("aliases: %v \n", aliases)
if !cmpStringArrays(addrs, []string{"redis://localhost:6379", "redis://localhost:7000", "redis://localhost:7000"}) {
t.Errorf("addrs not matching want")

log.Debugf("hosts: %v \n", hosts)

checkHosts(
t, hosts,
[]string{"redis://localhost:6379", "redis://localhost:7000", "redis://localhost:7000"},
[]string{"", "password", "second-pwd"},
[]string{"", "alias", ""},
)
}

func checkHosts(t *testing.T, hosts []RedisHost, addrs, pwds, aliases []string) {
for _, addr := range addrs {
found := false
for _, host := range hosts {
if host.Addr == addr {
found = true
break
}
}
if !found {
t.Errorf("Didn't find addr: %s, got hosts: %#v", addr, hosts)
return
}
}
if !cmpStringArrays(pwds, []string{"", "password", "second-pwd"}) {
t.Errorf("pwds not matching want")

for _, pwd := range pwds {
found := false
for _, host := range hosts {
if host.Password == pwd {
found = true
break
}
}
if !found {
t.Errorf("Didn't find pwd: %s, got hosts: %#v", pwd, hosts)
return
}
}
if !cmpStringArrays(aliases, []string{"", "alias", ""}) {
t.Errorf("aliases not matching want")

for _, alias := range aliases {
found := false
for _, host := range hosts {
if host.Alias == alias {
found = true
break
}
}
if !found {
t.Errorf("Didn't find alias: %s, got hosts: %#v", alias, hosts)
return
}
}
}

Expand Down
Loading

0 comments on commit e928651

Please sign in to comment.