github.com/emate/nomad@v0.8.2-wo-binpacking/api/allocations_test.go (about)

     1  package api
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"testing"
     7  
     8  	"time"
     9  
    10  	"github.com/hashicorp/nomad/helper"
    11  	"github.com/hashicorp/nomad/helper/uuid"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestAllocations_List(t *testing.T) {
    16  	t.Parallel()
    17  	c, s := makeClient(t, nil, nil)
    18  	defer s.Stop()
    19  	a := c.Allocations()
    20  
    21  	// Querying when no allocs exist returns nothing
    22  	allocs, qm, err := a.List(nil)
    23  	if err != nil {
    24  		t.Fatalf("err: %s", err)
    25  	}
    26  	if qm.LastIndex != 0 {
    27  		t.Fatalf("bad index: %d", qm.LastIndex)
    28  	}
    29  	if n := len(allocs); n != 0 {
    30  		t.Fatalf("expected 0 allocs, got: %d", n)
    31  	}
    32  
    33  	// TODO: do something that causes an allocation to actually happen
    34  	// so we can query for them.
    35  	return
    36  
    37  	//job := &Job{
    38  	//ID:   helper.StringToPtr("job1"),
    39  	//Name: helper.StringToPtr("Job #1"),
    40  	//Type: helper.StringToPtr(JobTypeService),
    41  	//}
    42  	//eval, _, err := c.Jobs().Register(job, nil)
    43  	//if err != nil {
    44  	//t.Fatalf("err: %s", err)
    45  	//}
    46  
    47  	//// List the allocations again
    48  	//allocs, qm, err = a.List(nil)
    49  	//if err != nil {
    50  	//t.Fatalf("err: %s", err)
    51  	//}
    52  	//if qm.LastIndex == 0 {
    53  	//t.Fatalf("bad index: %d", qm.LastIndex)
    54  	//}
    55  
    56  	//// Check that we got the allocation back
    57  	//if len(allocs) == 0 || allocs[0].EvalID != eval {
    58  	//t.Fatalf("bad: %#v", allocs)
    59  	//}
    60  }
    61  
    62  func TestAllocations_PrefixList(t *testing.T) {
    63  	t.Parallel()
    64  	c, s := makeClient(t, nil, nil)
    65  	defer s.Stop()
    66  	a := c.Allocations()
    67  
    68  	// Querying when no allocs exist returns nothing
    69  	allocs, qm, err := a.PrefixList("")
    70  	if err != nil {
    71  		t.Fatalf("err: %s", err)
    72  	}
    73  	if qm.LastIndex != 0 {
    74  		t.Fatalf("bad index: %d", qm.LastIndex)
    75  	}
    76  	if n := len(allocs); n != 0 {
    77  		t.Fatalf("expected 0 allocs, got: %d", n)
    78  	}
    79  
    80  	// TODO: do something that causes an allocation to actually happen
    81  	// so we can query for them.
    82  	return
    83  
    84  	//job := &Job{
    85  	//ID:   helper.StringToPtr("job1"),
    86  	//Name: helper.StringToPtr("Job #1"),
    87  	//Type: helper.StringToPtr(JobTypeService),
    88  	//}
    89  
    90  	//eval, _, err := c.Jobs().Register(job, nil)
    91  	//if err != nil {
    92  	//t.Fatalf("err: %s", err)
    93  	//}
    94  
    95  	//// List the allocations by prefix
    96  	//allocs, qm, err = a.PrefixList("foobar")
    97  	//if err != nil {
    98  	//t.Fatalf("err: %s", err)
    99  	//}
   100  	//if qm.LastIndex == 0 {
   101  	//t.Fatalf("bad index: %d", qm.LastIndex)
   102  	//}
   103  
   104  	//// Check that we got the allocation back
   105  	//if len(allocs) == 0 || allocs[0].EvalID != eval {
   106  	//t.Fatalf("bad: %#v", allocs)
   107  	//}
   108  }
   109  
   110  func TestAllocations_CreateIndexSort(t *testing.T) {
   111  	t.Parallel()
   112  	allocs := []*AllocationListStub{
   113  		{CreateIndex: 2},
   114  		{CreateIndex: 1},
   115  		{CreateIndex: 5},
   116  	}
   117  	sort.Sort(AllocIndexSort(allocs))
   118  
   119  	expect := []*AllocationListStub{
   120  		{CreateIndex: 5},
   121  		{CreateIndex: 2},
   122  		{CreateIndex: 1},
   123  	}
   124  	if !reflect.DeepEqual(allocs, expect) {
   125  		t.Fatalf("\n\n%#v\n\n%#v", allocs, expect)
   126  	}
   127  }
   128  
   129  func TestAllocations_RescheduleInfo(t *testing.T) {
   130  	t.Parallel()
   131  	// Create a job, task group and alloc
   132  	job := &Job{
   133  		Name:      helper.StringToPtr("foo"),
   134  		Namespace: helper.StringToPtr(DefaultNamespace),
   135  		ID:        helper.StringToPtr("bar"),
   136  		ParentID:  helper.StringToPtr("lol"),
   137  		TaskGroups: []*TaskGroup{
   138  			{
   139  				Name: helper.StringToPtr("bar"),
   140  				Tasks: []*Task{
   141  					{
   142  						Name: "task1",
   143  					},
   144  				},
   145  			},
   146  		},
   147  	}
   148  	job.Canonicalize()
   149  
   150  	alloc := &Allocation{
   151  		ID:        uuid.Generate(),
   152  		Namespace: DefaultNamespace,
   153  		EvalID:    uuid.Generate(),
   154  		Name:      "foo-bar[1]",
   155  		NodeID:    uuid.Generate(),
   156  		TaskGroup: *job.TaskGroups[0].Name,
   157  		JobID:     *job.ID,
   158  		Job:       job,
   159  	}
   160  
   161  	type testCase struct {
   162  		desc              string
   163  		reschedulePolicy  *ReschedulePolicy
   164  		rescheduleTracker *RescheduleTracker
   165  		time              time.Time
   166  		expAttempted      int
   167  		expTotal          int
   168  	}
   169  
   170  	testCases := []testCase{
   171  		{
   172  			desc:         "no reschedule policy",
   173  			expAttempted: 0,
   174  			expTotal:     0,
   175  		},
   176  		{
   177  			desc: "no reschedule events",
   178  			reschedulePolicy: &ReschedulePolicy{
   179  				Attempts: helper.IntToPtr(3),
   180  				Interval: helper.TimeToPtr(15 * time.Minute),
   181  			},
   182  			expAttempted: 0,
   183  			expTotal:     3,
   184  		},
   185  		{
   186  			desc: "all reschedule events within interval",
   187  			reschedulePolicy: &ReschedulePolicy{
   188  				Attempts: helper.IntToPtr(3),
   189  				Interval: helper.TimeToPtr(15 * time.Minute),
   190  			},
   191  			time: time.Now(),
   192  			rescheduleTracker: &RescheduleTracker{
   193  				Events: []*RescheduleEvent{
   194  					{
   195  						RescheduleTime: time.Now().Add(-5 * time.Minute).UTC().UnixNano(),
   196  					},
   197  				},
   198  			},
   199  			expAttempted: 1,
   200  			expTotal:     3,
   201  		},
   202  		{
   203  			desc: "some reschedule events outside interval",
   204  			reschedulePolicy: &ReschedulePolicy{
   205  				Attempts: helper.IntToPtr(3),
   206  				Interval: helper.TimeToPtr(15 * time.Minute),
   207  			},
   208  			time: time.Now(),
   209  			rescheduleTracker: &RescheduleTracker{
   210  				Events: []*RescheduleEvent{
   211  					{
   212  						RescheduleTime: time.Now().Add(-45 * time.Minute).UTC().UnixNano(),
   213  					},
   214  					{
   215  						RescheduleTime: time.Now().Add(-30 * time.Minute).UTC().UnixNano(),
   216  					},
   217  					{
   218  						RescheduleTime: time.Now().Add(-10 * time.Minute).UTC().UnixNano(),
   219  					},
   220  					{
   221  						RescheduleTime: time.Now().Add(-5 * time.Minute).UTC().UnixNano(),
   222  					},
   223  				},
   224  			},
   225  			expAttempted: 2,
   226  			expTotal:     3,
   227  		},
   228  	}
   229  
   230  	for _, tc := range testCases {
   231  		t.Run(tc.desc, func(t *testing.T) {
   232  			require := require.New(t)
   233  			alloc.RescheduleTracker = tc.rescheduleTracker
   234  			job.TaskGroups[0].ReschedulePolicy = tc.reschedulePolicy
   235  			attempted, total := alloc.RescheduleInfo(tc.time)
   236  			require.Equal(tc.expAttempted, attempted)
   237  			require.Equal(tc.expTotal, total)
   238  		})
   239  	}
   240  
   241  }
   242  
   243  func TestAllocations_ShouldMigrate(t *testing.T) {
   244  	t.Parallel()
   245  	require.True(t, DesiredTransition{Migrate: helper.BoolToPtr(true)}.ShouldMigrate())
   246  	require.False(t, DesiredTransition{}.ShouldMigrate())
   247  	require.False(t, DesiredTransition{Migrate: helper.BoolToPtr(false)}.ShouldMigrate())
   248  }