2018-01-12 01:00:30 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
2020-06-07 13:16:11 +00:00
|
|
|
"os"
|
2018-01-12 01:00:30 +00:00
|
|
|
"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
|
|
|
|
2020-06-07 13:16:11 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
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"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"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()
|
2020-12-01 16:11:34 +00:00
|
|
|
err := server.State().BootstrapACLTokens(structs.MsgTypeTestSetup, 1, 0, token)
|
2018-01-12 01:00:30 +00:00
|
|
|
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()
|
2021-04-27 20:50:07 +00:00
|
|
|
|
|
|
|
// Setup default enterprise-specific settings, including license
|
|
|
|
defaultEnterpriseTestConfig(config)
|
|
|
|
|
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-10-08 18:27:52 +00:00
|
|
|
config.EnableEventBroker = 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)
|
|
|
|
|
2022-01-06 16:56:13 +00:00
|
|
|
// configure logger
|
2020-06-07 13:16:11 +00:00
|
|
|
level := hclog.Trace
|
|
|
|
if envLogLevel := os.Getenv("NOMAD_TEST_LOG_LEVEL"); envLogLevel != "" {
|
|
|
|
level = hclog.LevelFromString(envLogLevel)
|
|
|
|
}
|
|
|
|
opts := &hclog.LoggerOptions{
|
|
|
|
Level: level,
|
|
|
|
Output: testlog.NewPrefixWriter(t, config.NodeName+" "),
|
|
|
|
IncludeLocation: true,
|
|
|
|
}
|
|
|
|
config.Logger = hclog.NewInterceptLogger(opts)
|
|
|
|
config.LogOutput = opts.Output
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
// 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
|
|
|
|
|
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
|
|
|
|
|
api: implement fuzzy search API
This PR introduces the /v1/search/fuzzy API endpoint, used for fuzzy
searching objects in Nomad. The fuzzy search endpoint routes requests
to the Nomad Server leader, which implements the Search.FuzzySearch RPC
method.
Requests to the fuzzy search API are based on the api.FuzzySearchRequest
object, e.g.
{
"Text": "ed",
"Context": "all"
}
Responses from the fuzzy search API are based on the api.FuzzySearchResponse
object, e.g.
{
"Index": 27,
"KnownLeader": true,
"LastContact": 0,
"Matches": {
"tasks": [
{
"ID": "redis",
"Scope": [
"default",
"example",
"cache"
]
}
],
"evals": [],
"deployment": [],
"volumes": [],
"scaling_policy": [],
"images": [
{
"ID": "redis:3.2",
"Scope": [
"default",
"example",
"cache",
"redis"
]
}
]
},
"Truncations": {
"volumes": false,
"scaling_policy": false,
"evals": false,
"deployment": false
}
}
The API is tunable using the new server.search stanza, e.g.
server {
search {
fuzzy_enabled = true
limit_query = 200
limit_results = 1000
min_term_length = 5
}
}
These values can be increased or decreased, so as to provide more
search results or to reduce load on the Nomad Server. The fuzzy search
API can be disabled entirely by setting `fuzzy_enabled` to `false`.
2021-02-23 20:24:52 +00:00
|
|
|
// Enable fuzzy search API
|
|
|
|
config.SearchConfig = &structs.SearchConfig{
|
|
|
|
FuzzyEnabled: true,
|
|
|
|
LimitQuery: 20,
|
|
|
|
LimitResults: 100,
|
|
|
|
MinTermLength: 2,
|
|
|
|
}
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
// Invoke the callback if any
|
|
|
|
if cb != nil {
|
|
|
|
cb(config)
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
cCatalog := consul.NewMockCatalog(config.Logger)
|
|
|
|
cConfigs := consul.NewMockConfigsAPI(config.Logger)
|
|
|
|
cACLs := consul.NewMockACLsAPI(config.Logger)
|
2019-12-06 20:46:46 +00:00
|
|
|
|
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
|
2020-07-28 20:12:08 +00:00
|
|
|
server, err := NewServer(config, cCatalog, cConfigs, cACLs)
|
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
|
|
|
}
|
|
|
|
|
2021-06-08 17:47:04 +00:00
|
|
|
func TestJoin(t testing.T, servers ...*Server) {
|
|
|
|
for i := 0; i < len(servers)-1; i++ {
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
servers[i].config.SerfConfig.MemberlistConfig.BindPort)
|
|
|
|
|
|
|
|
for j := i + 1; j < len(servers); j++ {
|
|
|
|
num, err := servers[j].Join([]string{addr})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if num != 1 {
|
|
|
|
t.Fatalf("bad: %d", num)
|
|
|
|
}
|
2018-01-12 01:00:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|