github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/e2e/spread/spread.go (about)

     1  package spread
     2  
     3  import (
     4  	"github.com/hashicorp/nomad/e2e/framework"
     5  	"github.com/stretchr/testify/require"
     6  
     7  	"github.com/hashicorp/nomad/e2e/e2eutil"
     8  	"github.com/hashicorp/nomad/helper/uuid"
     9  )
    10  
    11  type SpreadTest struct {
    12  	framework.TC
    13  	jobIds []string
    14  }
    15  
    16  func init() {
    17  	framework.AddSuites(&framework.TestSuite{
    18  		Component:   "Spread",
    19  		CanRunLocal: true,
    20  		Cases: []framework.TestCase{
    21  			new(SpreadTest),
    22  		},
    23  	})
    24  }
    25  
    26  func (tc *SpreadTest) BeforeAll(f *framework.F) {
    27  	// Ensure cluster has leader before running tests
    28  	e2eutil.WaitForLeader(f.T(), tc.Nomad())
    29  	e2eutil.WaitForNodesReady(f.T(), tc.Nomad(), 4)
    30  }
    31  
    32  func (tc *SpreadTest) TestEvenSpread(f *framework.F) {
    33  	nomadClient := tc.Nomad()
    34  	uuid := uuid.Generate()
    35  	jobId := "spread" + uuid[0:8]
    36  	tc.jobIds = append(tc.jobIds, jobId)
    37  	allocs := e2eutil.RegisterAndWaitForAllocs(f.T(), nomadClient, "spread/input/even_spread.nomad", jobId, "")
    38  
    39  	jobAllocs := nomadClient.Allocations()
    40  	dcToAllocs := make(map[string]int)
    41  	require := require.New(f.T())
    42  	// Verify spread score and alloc distribution
    43  	for _, allocStub := range allocs {
    44  		alloc, _, err := jobAllocs.Info(allocStub.ID, nil)
    45  		require.Nil(err)
    46  		require.NotEmpty(alloc.Metrics.ScoreMetaData)
    47  
    48  		node, _, err := nomadClient.Nodes().Info(alloc.NodeID, nil)
    49  		require.Nil(err)
    50  		dcToAllocs[node.Datacenter]++
    51  	}
    52  
    53  	expectedDcToAllocs := make(map[string]int)
    54  	expectedDcToAllocs["dc1"] = 3
    55  	expectedDcToAllocs["dc2"] = 3
    56  	require.Equal(expectedDcToAllocs, dcToAllocs)
    57  }
    58  
    59  func (tc *SpreadTest) TestMultipleSpreads(f *framework.F) {
    60  	nomadClient := tc.Nomad()
    61  	uuid := uuid.Generate()
    62  	jobId := "spread" + uuid[0:8]
    63  	tc.jobIds = append(tc.jobIds, jobId)
    64  	allocs := e2eutil.RegisterAndWaitForAllocs(f.T(), nomadClient, "spread/input/multiple_spread.nomad", jobId, "")
    65  
    66  	jobAllocs := nomadClient.Allocations()
    67  	dcToAllocs := make(map[string]int)
    68  	rackToAllocs := make(map[string]int)
    69  
    70  	require := require.New(f.T())
    71  	// Verify spread score and alloc distribution
    72  	for _, allocStub := range allocs {
    73  		alloc, _, err := jobAllocs.Info(allocStub.ID, nil)
    74  
    75  		require.Nil(err)
    76  		require.NotEmpty(alloc.Metrics.ScoreMetaData)
    77  
    78  		node, _, err := nomadClient.Nodes().Info(alloc.NodeID, nil)
    79  		require.Nil(err)
    80  		dcToAllocs[node.Datacenter]++
    81  		rack := node.Meta["rack"]
    82  		if rack != "" {
    83  			rackToAllocs[rack]++
    84  		}
    85  	}
    86  
    87  	expectedDcToAllocs := make(map[string]int)
    88  	expectedDcToAllocs["dc1"] = 5
    89  	expectedDcToAllocs["dc2"] = 5
    90  	require.Equal(expectedDcToAllocs, dcToAllocs)
    91  
    92  	expectedRackToAllocs := make(map[string]int)
    93  	expectedRackToAllocs["r1"] = 7
    94  	expectedRackToAllocs["r2"] = 3
    95  	require.Equal(expectedRackToAllocs, rackToAllocs)
    96  
    97  }
    98  
    99  func (tc *SpreadTest) AfterEach(f *framework.F) {
   100  	nomadClient := tc.Nomad()
   101  	jobs := nomadClient.Jobs()
   102  	// Stop all jobs in test
   103  	for _, id := range tc.jobIds {
   104  		jobs.Deregister(id, true, nil)
   105  	}
   106  	// Garbage collect
   107  	nomadClient.System().GarbageCollect()
   108  }