github.com/bigcommerce/nomad@v0.9.3-bc/nomad/testing.go (about)

     1  package nomad
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"net"
     7  	"sync/atomic"
     8  	"time"
     9  
    10  	testing "github.com/mitchellh/go-testing-interface"
    11  
    12  	"github.com/hashicorp/consul/lib/freeport"
    13  	"github.com/hashicorp/nomad/command/agent/consul"
    14  	"github.com/hashicorp/nomad/helper/pluginutils/catalog"
    15  	"github.com/hashicorp/nomad/helper/pluginutils/singleton"
    16  	"github.com/hashicorp/nomad/helper/testlog"
    17  	"github.com/hashicorp/nomad/nomad/mock"
    18  	"github.com/hashicorp/nomad/nomad/structs"
    19  )
    20  
    21  var (
    22  	nodeNumber uint32 = 0
    23  )
    24  
    25  func TestACLServer(t testing.T, cb func(*Config)) (*Server, *structs.ACLToken) {
    26  	server := TestServer(t, func(c *Config) {
    27  		c.ACLEnabled = true
    28  		if cb != nil {
    29  			cb(c)
    30  		}
    31  	})
    32  	token := mock.ACLManagementToken()
    33  	err := server.State().BootstrapACLTokens(1, 0, token)
    34  	if err != nil {
    35  		t.Fatalf("failed to bootstrap ACL token: %v", err)
    36  	}
    37  	return server, token
    38  }
    39  
    40  func TestServer(t testing.T, cb func(*Config)) *Server {
    41  	// Setup the default settings
    42  	config := DefaultConfig()
    43  	config.Logger = testlog.HCLogger(t)
    44  	config.Build = "0.8.0+unittest"
    45  	config.DevMode = true
    46  	nodeNum := atomic.AddUint32(&nodeNumber, 1)
    47  	config.NodeName = fmt.Sprintf("nomad-%03d", nodeNum)
    48  
    49  	// Tighten the Serf timing
    50  	config.SerfConfig.MemberlistConfig.BindAddr = "127.0.0.1"
    51  	config.SerfConfig.MemberlistConfig.SuspicionMult = 2
    52  	config.SerfConfig.MemberlistConfig.RetransmitMult = 2
    53  	config.SerfConfig.MemberlistConfig.ProbeTimeout = 50 * time.Millisecond
    54  	config.SerfConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
    55  	config.SerfConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
    56  
    57  	// Tighten the Raft timing
    58  	config.RaftConfig.LeaderLeaseTimeout = 50 * time.Millisecond
    59  	config.RaftConfig.HeartbeatTimeout = 50 * time.Millisecond
    60  	config.RaftConfig.ElectionTimeout = 50 * time.Millisecond
    61  	config.RaftTimeout = 500 * time.Millisecond
    62  
    63  	// Disable Vault
    64  	f := false
    65  	config.VaultConfig.Enabled = &f
    66  
    67  	// Squelch output when -v isn't specified
    68  	config.LogOutput = testlog.NewWriter(t)
    69  
    70  	// Tighten the autopilot timing
    71  	config.AutopilotConfig.ServerStabilizationTime = 100 * time.Millisecond
    72  	config.ServerHealthInterval = 50 * time.Millisecond
    73  	config.AutopilotInterval = 100 * time.Millisecond
    74  
    75  	// Set the plugin loaders
    76  	config.PluginLoader = catalog.TestPluginLoader(t)
    77  	config.PluginSingletonLoader = singleton.NewSingletonLoader(config.Logger, config.PluginLoader)
    78  
    79  	// Invoke the callback if any
    80  	if cb != nil {
    81  		cb(config)
    82  	}
    83  
    84  	// Enable raft as leader if we have bootstrap on
    85  	config.RaftConfig.StartAsLeader = !config.DevDisableBootstrap
    86  
    87  	catalog := consul.NewMockCatalog(config.Logger)
    88  
    89  	for i := 10; i >= 0; i-- {
    90  		// Get random ports
    91  		ports := freeport.GetT(t, 2)
    92  		config.RPCAddr = &net.TCPAddr{
    93  			IP:   []byte{127, 0, 0, 1},
    94  			Port: ports[0],
    95  		}
    96  		config.SerfConfig.MemberlistConfig.BindPort = ports[1]
    97  
    98  		// Create server
    99  		server, err := NewServer(config, catalog)
   100  		if err == nil {
   101  			return server
   102  		} else if i == 0 {
   103  			t.Fatalf("err: %v", err)
   104  		} else {
   105  			if server != nil {
   106  				server.Shutdown()
   107  			}
   108  			wait := time.Duration(rand.Int31n(2000)) * time.Millisecond
   109  			time.Sleep(wait)
   110  		}
   111  	}
   112  
   113  	return nil
   114  }
   115  
   116  func TestJoin(t testing.T, s1 *Server, other ...*Server) {
   117  	addr := fmt.Sprintf("127.0.0.1:%d",
   118  		s1.config.SerfConfig.MemberlistConfig.BindPort)
   119  	for _, s2 := range other {
   120  		if num, err := s2.Join([]string{addr}); err != nil {
   121  			t.Fatalf("err: %v", err)
   122  		} else if num != 1 {
   123  			t.Fatalf("bad: %d", num)
   124  		}
   125  	}
   126  }