github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/nomad/testing.go (about)

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