2013-12-12 19:07:14 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2015-08-15 21:32:38 +00:00
|
|
|
"errors"
|
2014-05-27 21:45:36 +00:00
|
|
|
"net"
|
2014-05-08 23:31:03 +00:00
|
|
|
"regexp"
|
2013-12-12 19:07:14 +00:00
|
|
|
"testing"
|
2015-05-15 00:59:11 +00:00
|
|
|
"time"
|
2014-06-16 21:36:12 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/serf/serf"
|
2013-12-12 19:07:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStrContains(t *testing.T) {
|
|
|
|
l := []string{"a", "b", "c"}
|
|
|
|
if !strContains(l, "b") {
|
|
|
|
t.Fatalf("should contain")
|
|
|
|
}
|
|
|
|
if strContains(l, "d") {
|
|
|
|
t.Fatalf("should not contain")
|
|
|
|
}
|
|
|
|
}
|
2013-12-31 23:44:17 +00:00
|
|
|
|
2014-07-17 06:37:25 +00:00
|
|
|
func TestToLowerList(t *testing.T) {
|
|
|
|
l := []string{"ABC", "Abc", "abc"}
|
|
|
|
for _, value := range ToLowerList(l) {
|
|
|
|
if value != "abc" {
|
|
|
|
t.Fatalf("failed lowercasing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 21:32:38 +00:00
|
|
|
func TestGetPrivateIP(t *testing.T) {
|
|
|
|
ip, _, err := net.ParseCIDR("10.1.2.3/32")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse private cidr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pubIP, _, err := net.ParseCIDR("8.8.8.8/32")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse public cidr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
addrs []net.Addr
|
|
|
|
expected net.IP
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
addrs: []net.Addr{
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: pubIP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: ip,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
addrs: []net.Addr{
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: pubIP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: errors.New("No private IP address found"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
addrs: []net.Addr{
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: pubIP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: errors.New("Multiple private IPs found. Please configure one."),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
ip, err := getPrivateIP(test.addrs)
|
|
|
|
switch {
|
|
|
|
case test.err != nil && err != nil:
|
|
|
|
if err.Error() != test.err.Error() {
|
|
|
|
t.Fatalf("unexpected error: %v != %v", test.err, err)
|
|
|
|
}
|
|
|
|
case (test.err == nil && err != nil) || (test.err != nil && err == nil):
|
|
|
|
t.Fatalf("unexpected error: %v != %v", test.err, err)
|
|
|
|
default:
|
|
|
|
if !test.expected.Equal(ip) {
|
|
|
|
t.Fatalf("unexpected ip: %v != %v", ip, test.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 23:44:17 +00:00
|
|
|
func TestIsPrivateIP(t *testing.T) {
|
|
|
|
if !isPrivateIP("192.168.1.1") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if !isPrivateIP("172.16.45.100") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if !isPrivateIP("10.1.2.3") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-06-19 19:20:30 +00:00
|
|
|
if !isPrivateIP("100.115.110.19") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2013-12-31 23:44:17 +00:00
|
|
|
if isPrivateIP("8.8.8.8") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if isPrivateIP("127.0.0.1") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 01:22:01 +00:00
|
|
|
|
|
|
|
func TestIsConsulServer(t *testing.T) {
|
|
|
|
m := serf.Member{
|
2014-05-27 22:07:31 +00:00
|
|
|
Name: "foo",
|
2014-05-27 21:45:36 +00:00
|
|
|
Addr: net.IP([]byte{127, 0, 0, 1}),
|
2014-01-30 21:13:29 +00:00
|
|
|
Tags: map[string]string{
|
2014-06-18 22:47:05 +00:00
|
|
|
"role": "consul",
|
|
|
|
"dc": "east-aws",
|
|
|
|
"port": "10000",
|
|
|
|
"vsn": "1",
|
2014-01-30 21:13:29 +00:00
|
|
|
},
|
2014-01-10 01:22:01 +00:00
|
|
|
}
|
2014-01-20 23:39:07 +00:00
|
|
|
valid, parts := isConsulServer(m)
|
|
|
|
if !valid || parts.Datacenter != "east-aws" || parts.Port != 10000 {
|
|
|
|
t.Fatalf("bad: %v %v", valid, parts)
|
2014-01-10 01:22:01 +00:00
|
|
|
}
|
2014-05-27 22:07:31 +00:00
|
|
|
if parts.Name != "foo" {
|
|
|
|
t.Fatalf("bad: %v", parts)
|
|
|
|
}
|
2014-01-30 21:13:29 +00:00
|
|
|
if parts.Bootstrap {
|
|
|
|
t.Fatalf("unexpected bootstrap")
|
|
|
|
}
|
2014-06-16 21:36:12 +00:00
|
|
|
if parts.Expect != 0 {
|
|
|
|
t.Fatalf("bad: %v", parts.Expect)
|
|
|
|
}
|
2014-01-30 21:13:29 +00:00
|
|
|
m.Tags["bootstrap"] = "1"
|
|
|
|
valid, parts = isConsulServer(m)
|
|
|
|
if !valid || !parts.Bootstrap {
|
|
|
|
t.Fatalf("expected bootstrap")
|
|
|
|
}
|
2014-05-27 21:45:36 +00:00
|
|
|
if parts.Addr.String() != "127.0.0.1:10000" {
|
|
|
|
t.Fatalf("bad addr: %v", parts.Addr)
|
|
|
|
}
|
2014-05-27 22:07:31 +00:00
|
|
|
if parts.Version != 1 {
|
|
|
|
t.Fatalf("bad: %v", parts)
|
|
|
|
}
|
2014-06-16 21:36:12 +00:00
|
|
|
m.Tags["expect"] = "3"
|
|
|
|
delete(m.Tags, "bootstrap")
|
|
|
|
valid, parts = isConsulServer(m)
|
|
|
|
if !valid || parts.Expect != 3 {
|
|
|
|
t.Fatalf("bad: %v", parts.Expect)
|
|
|
|
}
|
2014-01-10 01:22:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsConsulNode(t *testing.T) {
|
|
|
|
m := serf.Member{
|
2014-01-30 21:13:29 +00:00
|
|
|
Tags: map[string]string{
|
|
|
|
"role": "node",
|
|
|
|
"dc": "east-aws",
|
|
|
|
},
|
2014-01-10 01:22:01 +00:00
|
|
|
}
|
|
|
|
valid, dc := isConsulNode(m)
|
|
|
|
if !valid || dc != "east-aws" {
|
2014-11-01 21:56:48 +00:00
|
|
|
t.Fatalf("bad: %v %v", valid, dc)
|
2014-01-10 01:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestByteConversion(t *testing.T) {
|
|
|
|
var val uint64 = 2 << 50
|
|
|
|
raw := uint64ToBytes(val)
|
|
|
|
if bytesToUint64(raw) != val {
|
|
|
|
t.Fatalf("no match")
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 23:31:03 +00:00
|
|
|
|
|
|
|
func TestGenerateUUID(t *testing.T) {
|
|
|
|
prev := generateUUID()
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
id := generateUUID()
|
|
|
|
if prev == id {
|
|
|
|
t.Fatalf("Should get a new ID!")
|
|
|
|
}
|
|
|
|
|
|
|
|
matched, err := regexp.MatchString(
|
|
|
|
"[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}", id)
|
|
|
|
if !matched || err != nil {
|
|
|
|
t.Fatalf("expected match %s %v %s", id, matched, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 00:59:11 +00:00
|
|
|
|
|
|
|
func TestRandomStagger(t *testing.T) {
|
|
|
|
intv := time.Minute
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
stagger := randomStagger(intv)
|
|
|
|
if stagger < 0 || stagger >= intv {
|
|
|
|
t.Fatalf("Bad: %v", stagger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|