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

     1  package scheduler
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/helper"
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // Test that we properly create the bitmap even when the alloc set includes an
    12  // allocation with a higher count than the current min count and it is byte
    13  // aligned.
    14  // Ensure no regression from: https://github.com/hashicorp/nomad/issues/3008
    15  func TestBitmapFrom(t *testing.T) {
    16  	input := map[string]*structs.Allocation{
    17  		"8": {
    18  			JobID:     "foo",
    19  			TaskGroup: "bar",
    20  			Name:      "foo.bar[8]",
    21  		},
    22  	}
    23  	b := bitmapFrom(input, 1)
    24  	exp := uint(16)
    25  	if act := b.Size(); act != exp {
    26  		t.Fatalf("got %d; want %d", act, exp)
    27  	}
    28  
    29  	b = bitmapFrom(input, 8)
    30  	if act := b.Size(); act != exp {
    31  		t.Fatalf("got %d; want %d", act, exp)
    32  	}
    33  }
    34  
    35  func TestAllocSet_filterByTainted(t *testing.T) {
    36  	require := require.New(t)
    37  
    38  	nodes := map[string]*structs.Node{
    39  		"draining": {
    40  			ID:    "draining",
    41  			Drain: true,
    42  		},
    43  		"lost": {
    44  			ID:     "lost",
    45  			Status: structs.NodeStatusDown,
    46  		},
    47  		"nil": nil,
    48  		"normal": {
    49  			ID:     "normal",
    50  			Status: structs.NodeStatusReady,
    51  		},
    52  	}
    53  
    54  	batchJob := &structs.Job{
    55  		Type: structs.JobTypeBatch,
    56  	}
    57  
    58  	allocs := allocSet{
    59  		// Non-terminal alloc with migrate=true should migrate on a draining node
    60  		"migrating1": {
    61  			ID:                "migrating1",
    62  			ClientStatus:      structs.AllocClientStatusRunning,
    63  			DesiredTransition: structs.DesiredTransition{Migrate: helper.BoolToPtr(true)},
    64  			Job:               batchJob,
    65  			NodeID:            "draining",
    66  		},
    67  		// Non-terminal alloc with migrate=true should migrate on an unknown node
    68  		"migrating2": {
    69  			ID:                "migrating2",
    70  			ClientStatus:      structs.AllocClientStatusRunning,
    71  			DesiredTransition: structs.DesiredTransition{Migrate: helper.BoolToPtr(true)},
    72  			Job:               batchJob,
    73  			NodeID:            "nil",
    74  		},
    75  		"untainted1": {
    76  			ID:           "untainted1",
    77  			ClientStatus: structs.AllocClientStatusRunning,
    78  			Job:          batchJob,
    79  			NodeID:       "normal",
    80  		},
    81  		// Terminal allocs are always untainted
    82  		"untainted2": {
    83  			ID:           "untainted2",
    84  			ClientStatus: structs.AllocClientStatusComplete,
    85  			Job:          batchJob,
    86  			NodeID:       "normal",
    87  		},
    88  		// Terminal allocs are always untainted, even on draining nodes
    89  		"untainted3": {
    90  			ID:           "untainted3",
    91  			ClientStatus: structs.AllocClientStatusComplete,
    92  			Job:          batchJob,
    93  			NodeID:       "draining",
    94  		},
    95  		// Terminal allocs are always untainted, even on lost nodes
    96  		"untainted4": {
    97  			ID:           "untainted4",
    98  			ClientStatus: structs.AllocClientStatusComplete,
    99  			Job:          batchJob,
   100  			NodeID:       "lost",
   101  		},
   102  		// Non-terminal allocs on lost nodes are lost
   103  		"lost1": {
   104  			ID:           "lost1",
   105  			ClientStatus: structs.AllocClientStatusPending,
   106  			Job:          batchJob,
   107  			NodeID:       "lost",
   108  		},
   109  		// Non-terminal allocs on lost nodes are lost
   110  		"lost2": {
   111  			ID:           "lost2",
   112  			ClientStatus: structs.AllocClientStatusRunning,
   113  			Job:          batchJob,
   114  			NodeID:       "lost",
   115  		},
   116  	}
   117  
   118  	untainted, migrate, lost := allocs.filterByTainted(nodes)
   119  	require.Len(untainted, 4)
   120  	require.Contains(untainted, "untainted1")
   121  	require.Contains(untainted, "untainted2")
   122  	require.Contains(untainted, "untainted3")
   123  	require.Contains(untainted, "untainted4")
   124  	require.Len(migrate, 2)
   125  	require.Contains(migrate, "migrating1")
   126  	require.Contains(migrate, "migrating2")
   127  	require.Len(lost, 2)
   128  	require.Contains(lost, "lost1")
   129  	require.Contains(lost, "lost2")
   130  }