2015-06-03 11:35:48 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2015-07-24 00:30:07 +00:00
|
|
|
"reflect"
|
2015-06-03 11:35:48 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
version "github.com/hashicorp/go-version"
|
2017-09-29 16:58:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2015-06-03 11:35:48 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsNomadServer(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2015-06-03 11:35:48 +00:00
|
|
|
m := serf.Member{
|
2017-09-07 23:56:15 +00:00
|
|
|
Name: "foo",
|
|
|
|
Addr: net.IP([]byte{127, 0, 0, 1}),
|
|
|
|
Status: serf.StatusAlive,
|
2015-06-03 11:35:48 +00:00
|
|
|
Tags: map[string]string{
|
2017-12-18 21:16:23 +00:00
|
|
|
"role": "nomad",
|
|
|
|
"region": "aws",
|
|
|
|
"dc": "east-aws",
|
|
|
|
"rpc_addr": "1.1.1.1",
|
|
|
|
"port": "10000",
|
|
|
|
"vsn": "1",
|
|
|
|
"raft_vsn": "2",
|
|
|
|
"build": "0.7.0+ent",
|
2018-09-20 00:13:37 +00:00
|
|
|
"nonvoter": "1",
|
2015-06-03 11:35:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
valid, parts := isNomadServer(m)
|
|
|
|
if !valid || parts.Region != "aws" ||
|
|
|
|
parts.Datacenter != "east-aws" || parts.Port != 10000 {
|
|
|
|
t.Fatalf("bad: %v %v", valid, parts)
|
|
|
|
}
|
|
|
|
if parts.Name != "foo" {
|
|
|
|
t.Fatalf("bad: %v", parts)
|
|
|
|
}
|
|
|
|
if parts.Bootstrap {
|
|
|
|
t.Fatalf("unexpected bootstrap")
|
|
|
|
}
|
|
|
|
if parts.Expect != 0 {
|
|
|
|
t.Fatalf("bad: %v", parts.Expect)
|
|
|
|
}
|
2017-09-07 23:56:15 +00:00
|
|
|
if parts.Status != serf.StatusAlive {
|
|
|
|
t.Fatalf("bad: %v", parts.Status)
|
|
|
|
}
|
2017-12-18 21:16:23 +00:00
|
|
|
if parts.RaftVersion != 2 {
|
|
|
|
t.Fatalf("bad: %v", parts.RaftVersion)
|
|
|
|
}
|
|
|
|
if parts.RPCAddr.String() != "1.1.1.1:10000" {
|
|
|
|
t.Fatalf("bad: %v", parts.RPCAddr.String())
|
|
|
|
}
|
2017-09-07 23:56:15 +00:00
|
|
|
if seg := parts.Build.Segments(); len(seg) != 3 {
|
|
|
|
t.Fatalf("bad: %v", parts.Build)
|
|
|
|
} else if seg[0] != 0 && seg[1] != 7 && seg[2] != 0 {
|
|
|
|
t.Fatalf("bad: %v", parts.Build)
|
|
|
|
}
|
2018-09-20 00:13:37 +00:00
|
|
|
if !parts.NonVoter {
|
|
|
|
t.Fatalf("should be nonvoter")
|
|
|
|
}
|
2015-06-03 11:35:48 +00:00
|
|
|
|
|
|
|
m.Tags["bootstrap"] = "1"
|
|
|
|
valid, parts = isNomadServer(m)
|
|
|
|
if !valid || !parts.Bootstrap {
|
|
|
|
t.Fatalf("expected bootstrap")
|
|
|
|
}
|
|
|
|
if parts.Addr.String() != "127.0.0.1:10000" {
|
|
|
|
t.Fatalf("bad addr: %v", parts.Addr)
|
|
|
|
}
|
2016-05-28 01:14:34 +00:00
|
|
|
if parts.MajorVersion != 1 {
|
2015-06-03 11:35:48 +00:00
|
|
|
t.Fatalf("bad: %v", parts)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Tags["expect"] = "3"
|
|
|
|
delete(m.Tags, "bootstrap")
|
|
|
|
valid, parts = isNomadServer(m)
|
|
|
|
if !valid || parts.Expect != 3 {
|
|
|
|
t.Fatalf("bad: %v", parts.Expect)
|
|
|
|
}
|
2018-09-20 00:13:37 +00:00
|
|
|
|
|
|
|
delete(m.Tags, "nonvoter")
|
|
|
|
valid, parts = isNomadServer(m)
|
|
|
|
if !valid || parts.NonVoter {
|
|
|
|
t.Fatalf("should be a voter")
|
|
|
|
}
|
2015-06-03 11:35:48 +00:00
|
|
|
}
|
2015-06-05 22:14:08 +00:00
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
func TestServersMeetMinimumVersion(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
makeMember := func(version string) serf.Member {
|
|
|
|
return serf.Member{
|
|
|
|
Name: "foo",
|
|
|
|
Addr: net.IP([]byte{127, 0, 0, 1}),
|
|
|
|
Tags: map[string]string{
|
|
|
|
"role": "nomad",
|
|
|
|
"region": "aws",
|
|
|
|
"dc": "east-aws",
|
|
|
|
"port": "10000",
|
|
|
|
"build": version,
|
|
|
|
"vsn": "1",
|
|
|
|
},
|
|
|
|
Status: serf.StatusAlive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
members []serf.Member
|
|
|
|
ver *version.Version
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
// One server, meets reqs
|
|
|
|
{
|
|
|
|
members: []serf.Member{
|
|
|
|
makeMember("0.7.5"),
|
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.7.5")),
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
// One server in dev, meets reqs
|
|
|
|
{
|
|
|
|
members: []serf.Member{
|
|
|
|
makeMember("0.8.5-dev"),
|
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.7.5")),
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
// One server with meta, meets reqs
|
|
|
|
{
|
|
|
|
members: []serf.Member{
|
|
|
|
makeMember("0.7.5+ent"),
|
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.7.5")),
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
// One server, doesn't meet reqs
|
|
|
|
{
|
|
|
|
members: []serf.Member{
|
|
|
|
makeMember("0.7.5"),
|
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.8.0")),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
// Multiple servers, meets req version
|
|
|
|
{
|
|
|
|
members: []serf.Member{
|
|
|
|
makeMember("0.7.5"),
|
|
|
|
makeMember("0.8.0"),
|
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.7.5")),
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
// Multiple servers, doesn't meet req version
|
|
|
|
{
|
|
|
|
members: []serf.Member{
|
|
|
|
makeMember("0.7.5"),
|
|
|
|
makeMember("0.8.0"),
|
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.8.0")),
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2019-03-04 09:49:32 +00:00
|
|
|
result := ServersMeetMinimumVersion(tc.members, tc.ver, false)
|
2017-09-07 23:56:15 +00:00
|
|
|
if result != tc.expected {
|
|
|
|
t.Fatalf("bad: %v, %v, %v", result, tc.ver.String(), tc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:30:07 +00:00
|
|
|
func TestShuffleStrings(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2015-07-24 00:30:07 +00:00
|
|
|
// Generate input
|
|
|
|
inp := make([]string, 10)
|
|
|
|
for idx := range inp {
|
2017-09-29 16:58:48 +00:00
|
|
|
inp[idx] = uuid.Generate()
|
2015-07-24 00:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the input
|
|
|
|
orig := make([]string, len(inp))
|
|
|
|
copy(orig, inp)
|
|
|
|
|
|
|
|
// Shuffle
|
|
|
|
shuffleStrings(inp)
|
|
|
|
|
|
|
|
// Ensure order is not the same
|
|
|
|
if reflect.DeepEqual(inp, orig) {
|
|
|
|
t.Fatalf("shuffle failed")
|
|
|
|
}
|
|
|
|
}
|
2015-08-05 00:13:40 +00:00
|
|
|
|
|
|
|
func TestMaxUint64(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2015-08-05 00:13:40 +00:00
|
|
|
if maxUint64(1, 2) != 2 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if maxUint64(2, 2) != 2 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if maxUint64(2, 1) != 2 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|