2018-01-12 01:00:30 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2019-01-15 19:46:12 +00:00
|
|
|
testing "github.com/mitchellh/go-testing-interface"
|
2019-12-04 00:15:11 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-01-15 19:46:12 +00:00
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/consul"
|
2019-12-04 00:15:11 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/freeport"
|
2019-01-23 14:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/catalog"
|
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/singleton"
|
2018-01-12 01:00:30 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2020-03-06 15:09:10 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2018-01-12 01:00:30 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2020-03-06 15:09:10 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2018-01-12 01:00:30 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-09-04 01:42:41 +00:00
|
|
|
"github.com/hashicorp/nomad/version"
|
2018-01-12 01:00:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
nodeNumber uint32 = 0
|
|
|
|
)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
func TestACLServer(t testing.T, cb func(*Config)) (*Server, *structs.ACLToken, func()) {
|
|
|
|
server, cleanup := TestServer(t, func(c *Config) {
|
2018-01-12 01:00:30 +00:00
|
|
|
c.ACLEnabled = true
|
|
|
|
if cb != nil {
|
|
|
|
cb(c)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
token := mock.ACLManagementToken()
|
|
|
|
err := server.State().BootstrapACLTokens(1, 0, token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to bootstrap ACL token: %v", err)
|
|
|
|
}
|
2019-12-04 00:15:11 +00:00
|
|
|
return server, token, cleanup
|
2018-01-12 01:00:30 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
func TestServer(t testing.T, cb func(*Config)) (*Server, func()) {
|
2018-01-12 01:00:30 +00:00
|
|
|
// Setup the default settings
|
|
|
|
config := DefaultConfig()
|
2018-09-15 23:23:13 +00:00
|
|
|
config.Logger = testlog.HCLogger(t)
|
2019-09-04 01:42:41 +00:00
|
|
|
config.Build = version.Version + "+unittest"
|
2018-01-12 01:00:30 +00:00
|
|
|
config.DevMode = true
|
2020-03-02 15:29:24 +00:00
|
|
|
config.BootstrapExpect = 1
|
2018-01-12 01:00:30 +00:00
|
|
|
nodeNum := atomic.AddUint32(&nodeNumber, 1)
|
|
|
|
config.NodeName = fmt.Sprintf("nomad-%03d", nodeNum)
|
|
|
|
|
|
|
|
// Tighten the Serf timing
|
|
|
|
config.SerfConfig.MemberlistConfig.BindAddr = "127.0.0.1"
|
|
|
|
config.SerfConfig.MemberlistConfig.SuspicionMult = 2
|
|
|
|
config.SerfConfig.MemberlistConfig.RetransmitMult = 2
|
|
|
|
config.SerfConfig.MemberlistConfig.ProbeTimeout = 50 * time.Millisecond
|
|
|
|
config.SerfConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
|
|
|
|
config.SerfConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
|
|
|
|
|
|
|
|
// Tighten the Raft timing
|
|
|
|
config.RaftConfig.LeaderLeaseTimeout = 50 * time.Millisecond
|
|
|
|
config.RaftConfig.HeartbeatTimeout = 50 * time.Millisecond
|
|
|
|
config.RaftConfig.ElectionTimeout = 50 * time.Millisecond
|
|
|
|
config.RaftTimeout = 500 * time.Millisecond
|
|
|
|
|
|
|
|
// Disable Vault
|
|
|
|
f := false
|
|
|
|
config.VaultConfig.Enabled = &f
|
|
|
|
|
|
|
|
// Squelch output when -v isn't specified
|
|
|
|
config.LogOutput = testlog.NewWriter(t)
|
|
|
|
|
2018-02-15 23:48:32 +00:00
|
|
|
// Tighten the autopilot timing
|
|
|
|
config.AutopilotConfig.ServerStabilizationTime = 100 * time.Millisecond
|
|
|
|
config.ServerHealthInterval = 50 * time.Millisecond
|
|
|
|
config.AutopilotInterval = 100 * time.Millisecond
|
|
|
|
|
2018-09-27 01:14:36 +00:00
|
|
|
// Set the plugin loaders
|
|
|
|
config.PluginLoader = catalog.TestPluginLoader(t)
|
|
|
|
config.PluginSingletonLoader = singleton.NewSingletonLoader(config.Logger, config.PluginLoader)
|
|
|
|
|
2019-09-04 11:54:54 +00:00
|
|
|
// Disable consul autojoining: tests typically join servers directly
|
|
|
|
config.ConsulConfig.ServerAutoJoin = &f
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
// Invoke the callback if any
|
|
|
|
if cb != nil {
|
|
|
|
cb(config)
|
|
|
|
}
|
|
|
|
|
2018-09-15 23:23:13 +00:00
|
|
|
catalog := consul.NewMockCatalog(config.Logger)
|
2018-01-12 01:00:30 +00:00
|
|
|
|
2019-12-06 20:46:46 +00:00
|
|
|
acls := consul.NewMockACLsAPI(config.Logger)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
for i := 10; i >= 0; i-- {
|
2019-12-04 00:15:11 +00:00
|
|
|
// Get random ports, need to cleanup later
|
|
|
|
ports := freeport.MustTake(2)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
config.RPCAddr = &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: ports[0],
|
|
|
|
}
|
|
|
|
config.SerfConfig.MemberlistConfig.BindPort = ports[1]
|
|
|
|
|
|
|
|
// Create server
|
2019-12-06 20:46:46 +00:00
|
|
|
server, err := NewServer(config, catalog, acls)
|
2018-01-12 01:00:30 +00:00
|
|
|
if err == nil {
|
2019-12-04 00:15:11 +00:00
|
|
|
return server, func() {
|
|
|
|
ch := make(chan error)
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
|
|
|
|
// Shutdown server
|
|
|
|
err := server.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
ch <- errors.Wrap(err, "failed to shutdown server")
|
|
|
|
}
|
|
|
|
|
|
|
|
freeport.Return(ports)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case e := <-ch:
|
|
|
|
if e != nil {
|
|
|
|
t.Fatal(e.Error())
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Minute):
|
|
|
|
t.Fatal("timed out while shutting down server")
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 01:00:30 +00:00
|
|
|
} else if i == 0 {
|
2019-12-04 00:15:11 +00:00
|
|
|
freeport.Return(ports)
|
2018-01-12 01:00:30 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else {
|
|
|
|
if server != nil {
|
2019-12-04 00:15:11 +00:00
|
|
|
_ = server.Shutdown()
|
|
|
|
freeport.Return(ports)
|
2018-01-12 01:00:30 +00:00
|
|
|
}
|
|
|
|
wait := time.Duration(rand.Int31n(2000)) * time.Millisecond
|
|
|
|
time.Sleep(wait)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
return nil, nil
|
2018-01-12 01:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJoin(t testing.T, s1 *Server, other ...*Server) {
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfConfig.MemberlistConfig.BindPort)
|
|
|
|
for _, s2 := range other {
|
|
|
|
if num, err := s2.Join([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if num != 1 {
|
|
|
|
t.Fatalf("bad: %d", num)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 15:09:10 +00:00
|
|
|
|
|
|
|
// CreateTestPlugin is a helper that generates the node + fingerprint results necessary to
|
|
|
|
// create a CSIPlugin by directly inserting into the state store. It's exported for use in
|
|
|
|
// other test packages
|
|
|
|
func CreateTestCSIPlugin(s *state.StateStore, id string) func() {
|
|
|
|
// Create some nodes
|
|
|
|
ns := make([]*structs.Node, 3)
|
|
|
|
for i := range ns {
|
|
|
|
n := mock.Node()
|
|
|
|
ns[i] = n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install healthy plugin fingerprinting results
|
|
|
|
ns[0].CSIControllerPlugins = map[string]*structs.CSIInfo{
|
|
|
|
id: {
|
|
|
|
PluginID: id,
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
Healthy: true,
|
|
|
|
HealthDescription: "healthy",
|
|
|
|
RequiresControllerPlugin: true,
|
|
|
|
RequiresTopologies: false,
|
|
|
|
ControllerInfo: &structs.CSIControllerInfo{
|
|
|
|
SupportsReadOnlyAttach: true,
|
|
|
|
SupportsAttachDetach: true,
|
|
|
|
SupportsListVolumes: true,
|
|
|
|
SupportsListVolumesAttachedNodes: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install healthy plugin fingerprinting results
|
|
|
|
for _, n := range ns[1:] {
|
|
|
|
n.CSINodePlugins = map[string]*structs.CSIInfo{
|
|
|
|
id: {
|
|
|
|
PluginID: id,
|
2020-03-18 19:29:03 +00:00
|
|
|
AllocID: uuid.Generate(),
|
2020-03-06 15:09:10 +00:00
|
|
|
Healthy: true,
|
|
|
|
HealthDescription: "healthy",
|
|
|
|
RequiresControllerPlugin: true,
|
|
|
|
RequiresTopologies: false,
|
|
|
|
NodeInfo: &structs.CSINodeInfo{
|
|
|
|
ID: n.ID,
|
|
|
|
MaxVolumes: 64,
|
|
|
|
RequiresNodeStageVolume: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert them into the state store
|
|
|
|
index := uint64(999)
|
|
|
|
for _, n := range ns {
|
|
|
|
index++
|
|
|
|
s.UpsertNode(index, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return cleanup function that deletes the nodes
|
|
|
|
return func() {
|
|
|
|
ids := make([]string, len(ns))
|
|
|
|
for i, n := range ns {
|
|
|
|
ids[i] = n.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
index++
|
|
|
|
s.DeleteNode(index, ids)
|
|
|
|
}
|
|
|
|
}
|