|
|
|
@ -2,6 +2,7 @@ package consul
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"regexp"
|
|
|
|
|
"testing"
|
|
|
|
@ -118,6 +119,103 @@ func TestIsPrivateIP(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUtil_CanServersUnderstandProtocol(t *testing.T) {
|
|
|
|
|
var members []serf.Member
|
|
|
|
|
|
|
|
|
|
// All empty list cases should return false.
|
|
|
|
|
for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
|
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if grok {
|
|
|
|
|
t.Fatalf("empty list should always return false")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add a non-server member.
|
|
|
|
|
members = append(members, serf.Member{
|
|
|
|
|
Tags: map[string]string{
|
|
|
|
|
"vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
|
|
|
|
|
"vsn_max": fmt.Sprintf("%d", ProtocolVersionMax),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Make sure it doesn't get counted.
|
|
|
|
|
for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
|
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if grok {
|
|
|
|
|
t.Fatalf("non-server members should not be counted")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add a server member.
|
|
|
|
|
members = append(members, serf.Member{
|
|
|
|
|
Tags: map[string]string{
|
|
|
|
|
"role": "consul",
|
|
|
|
|
"vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
|
|
|
|
|
"vsn_max": fmt.Sprintf("%d", ProtocolVersionMax),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Now it should report that it understands.
|
|
|
|
|
for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
|
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if !grok {
|
|
|
|
|
t.Fatalf("server should grok")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Nobody should understand anything from the future.
|
|
|
|
|
for v := uint8(ProtocolVersionMax + 1); v <= uint8(ProtocolVersionMax+10); v++ {
|
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if grok {
|
|
|
|
|
t.Fatalf("server should not grok")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add an older server.
|
|
|
|
|
members = append(members, serf.Member{
|
|
|
|
|
Tags: map[string]string{
|
|
|
|
|
"role": "consul",
|
|
|
|
|
"vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
|
|
|
|
|
"vsn_max": fmt.Sprintf("%d", ProtocolVersionMax-1),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// The servers should no longer understand the max version.
|
|
|
|
|
for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
|
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
expected := v < ProtocolVersionMax
|
|
|
|
|
if grok != expected {
|
|
|
|
|
t.Fatalf("bad: %v != %v", grok, expected)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try a version that's too low for the minimum.
|
|
|
|
|
{
|
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if grok {
|
|
|
|
|
t.Fatalf("server should not grok")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsConsulServer(t *testing.T) {
|
|
|
|
|
m := serf.Member{
|
|
|
|
|
Name: "foo",
|
|
|
|
|