-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from oliver006/oh_cluster_tests
Add cluster tests
- Loading branch information
Showing
7 changed files
with
439 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
redis://localhost:6379 | ||
|
||
redis://localhost:7000,password,alias | ||
redis://localhost:7000,second-pwd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package exporter | ||
|
||
import ( | ||
"encoding/csv" | ||
"os" | ||
"strings" | ||
|
||
"github.com/cloudfoundry-community/go-cfenv" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// 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) { | ||
if addr == "" { | ||
addr = "redis://localhost:6379" | ||
} | ||
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]) | ||
} | ||
return addrs, passwords, aliases | ||
} | ||
|
||
// 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 | ||
file, err := os.Open(fileName) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
r := csv.NewReader(file) | ||
r.FieldsPerRecord = -1 | ||
records, err := r.ReadAll() | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
file.Close() | ||
// For each line, test if it contains an optional password and alias and provide them, | ||
// else give them empty strings | ||
for _, record := range records { | ||
length := len(record) | ||
switch length { | ||
case 3: | ||
addrs = append(addrs, record[0]) | ||
passwords = append(passwords, record[1]) | ||
aliases = append(aliases, record[2]) | ||
case 2: | ||
addrs = append(addrs, record[0]) | ||
passwords = append(passwords, record[1]) | ||
aliases = append(aliases, "") | ||
case 1: | ||
addrs = append(addrs, record[0]) | ||
passwords = append(passwords, "") | ||
aliases = append(aliases, "") | ||
} | ||
} | ||
return addrs, passwords, aliases, nil | ||
} | ||
|
||
func GetCloudFoundryRedisBindings() (addrs, passwords, aliases []string) { | ||
if !cfenv.IsRunningOnCF() { | ||
return | ||
} | ||
|
||
appEnv, err := cfenv.Current() | ||
if err != nil { | ||
log.Warnln("Unable to get current CF environment", err) | ||
return | ||
} | ||
|
||
redisServices, err := appEnv.Services.WithTag("redis") | ||
if err != nil { | ||
log.Warnln("Error while getting redis services", err) | ||
return | ||
} | ||
|
||
for _, redisService := range redisServices { | ||
credentials := redisService.Credentials | ||
addr := credentials["hostname"].(string) + ":" + credentials["port"].(string) | ||
password := credentials["password"].(string) | ||
alias := redisService.Name | ||
|
||
addrs = append(addrs, addr) | ||
passwords = append(passwords, password) | ||
aliases = append(aliases, alias) | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
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 | ||
} | ||
|
||
func TestLoadRedisArgs(t *testing.T) { | ||
log.Println("TestLoadRedisArgs()") | ||
tests := []struct { | ||
addr, pwd, alias, sep string | ||
wantAddr, wantPwds, wantAliases []string | ||
}{ | ||
{ | ||
addr: "", | ||
sep: ",", | ||
wantAddr: []string{"redis://localhost:6379"}, | ||
wantPwds: []string{""}, | ||
wantAliases: []string{""}, | ||
}, | ||
{ | ||
addr: "redis://localhost:6379", | ||
sep: ",", | ||
wantAddr: []string{"redis://localhost:6379"}, | ||
wantPwds: []string{""}, | ||
wantAliases: []string{""}, | ||
}, | ||
{ | ||
addr: "redis://localhost:6379,redis://localhost:7000", | ||
sep: ",", | ||
wantAddr: []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"}, | ||
wantPwds: []string{"", "", ""}, | ||
wantAliases: []string{"", "", ""}, | ||
}, | ||
{ | ||
alias: "host-1", | ||
sep: ",", | ||
wantAddr: []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) | ||
} | ||
} | ||
} | ||
|
||
func TestLoadRedisFile(t *testing.T) { | ||
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") | ||
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") | ||
} | ||
if !cmpStringArrays(pwds, []string{"", "password", "second-pwd"}) { | ||
t.Errorf("pwds not matching want") | ||
} | ||
if !cmpStringArrays(aliases, []string{"", "alias", ""}) { | ||
t.Errorf("aliases not matching want") | ||
} | ||
} | ||
|
||
func TestGetCloudFoundryRedisBindings(t *testing.T) { | ||
GetCloudFoundryRedisBindings() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.