Browse Source

Adds a check for the minimum version as well.

pull/1353/head
James Phillips 9 years ago
parent
commit
9f85f7ee1a
  1. 15
      consul/util.go
  2. 14
      consul/util_test.go

15
consul/util.go

@ -96,8 +96,8 @@ func ensurePath(path string, dir bool) error {
} }
// CanServersUnderstandProtocol checks to see if all the servers in the given // CanServersUnderstandProtocol checks to see if all the servers in the given
// list understand the given protocol version or higher. If there are no servers // list understand the given protocol version. If there are no servers in the
// in the list then this will return false. // list then this will return false.
func CanServersUnderstandProtocol(members []serf.Member, version uint8) (bool, error) { func CanServersUnderstandProtocol(members []serf.Member, version uint8) (bool, error) {
numServers, numWhoGrok := 0, 0 numServers, numWhoGrok := 0, 0
for _, m := range members { for _, m := range members {
@ -106,13 +106,18 @@ func CanServersUnderstandProtocol(members []serf.Member, version uint8) (bool, e
} }
numServers++ numServers++
vsn_str := m.Tags["vsn_max"] vsn_min, err := strconv.Atoi(m.Tags["vsn_min"])
vsn, err := strconv.Atoi(vsn_str)
if err != nil { if err != nil {
return false, err return false, err
} }
if vsn >= int(version) { vsn_max, err := strconv.Atoi(m.Tags["vsn_max"])
if err != nil {
return false, err
}
v := int(version)
if (v >= vsn_min) && (v <= vsn_max) {
numWhoGrok++ numWhoGrok++
} }
} }

14
consul/util_test.go

@ -136,6 +136,7 @@ func TestUtil_CanServersUnderstandProtocol(t *testing.T) {
// Add a non-server member. // Add a non-server member.
members = append(members, serf.Member{ members = append(members, serf.Member{
Tags: map[string]string{ Tags: map[string]string{
"vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
"vsn_max": fmt.Sprintf("%d", ProtocolVersionMax), "vsn_max": fmt.Sprintf("%d", ProtocolVersionMax),
}, },
}) })
@ -155,6 +156,7 @@ func TestUtil_CanServersUnderstandProtocol(t *testing.T) {
members = append(members, serf.Member{ members = append(members, serf.Member{
Tags: map[string]string{ Tags: map[string]string{
"role": "consul", "role": "consul",
"vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
"vsn_max": fmt.Sprintf("%d", ProtocolVersionMax), "vsn_max": fmt.Sprintf("%d", ProtocolVersionMax),
}, },
}) })
@ -185,6 +187,7 @@ func TestUtil_CanServersUnderstandProtocol(t *testing.T) {
members = append(members, serf.Member{ members = append(members, serf.Member{
Tags: map[string]string{ Tags: map[string]string{
"role": "consul", "role": "consul",
"vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
"vsn_max": fmt.Sprintf("%d", ProtocolVersionMax-1), "vsn_max": fmt.Sprintf("%d", ProtocolVersionMax-1),
}, },
}) })
@ -200,6 +203,17 @@ func TestUtil_CanServersUnderstandProtocol(t *testing.T) {
t.Fatalf("bad: %v != %v", 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) { func TestIsConsulServer(t *testing.T) {

Loading…
Cancel
Save