github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/internal/testing/apitests/tasks_test.go (about)

     1  package apitests
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  	"github.com/hashicorp/nomad/ci"
     9  	"github.com/hashicorp/nomad/helper/pointer"
    10  	"github.com/hashicorp/nomad/nomad/structs"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  // Verifies that reschedule policy is merged correctly
    15  func TestTaskGroup_Canonicalize_ReschedulePolicy(t *testing.T) {
    16  	ci.Parallel(t)
    17  
    18  	type testCase struct {
    19  		desc                 string
    20  		jobReschedulePolicy  *api.ReschedulePolicy
    21  		taskReschedulePolicy *api.ReschedulePolicy
    22  		expected             *api.ReschedulePolicy
    23  	}
    24  
    25  	testCases := []testCase{
    26  		{
    27  			desc:                 "Default",
    28  			jobReschedulePolicy:  nil,
    29  			taskReschedulePolicy: nil,
    30  			expected: &api.ReschedulePolicy{
    31  				Attempts:      pointer.Of(structs.DefaultBatchJobReschedulePolicy.Attempts),
    32  				Interval:      pointer.Of(structs.DefaultBatchJobReschedulePolicy.Interval),
    33  				Delay:         pointer.Of(structs.DefaultBatchJobReschedulePolicy.Delay),
    34  				DelayFunction: pointer.Of(structs.DefaultBatchJobReschedulePolicy.DelayFunction),
    35  				MaxDelay:      pointer.Of(structs.DefaultBatchJobReschedulePolicy.MaxDelay),
    36  				Unlimited:     pointer.Of(structs.DefaultBatchJobReschedulePolicy.Unlimited),
    37  			},
    38  		},
    39  		{
    40  			desc: "Empty job reschedule policy",
    41  			jobReschedulePolicy: &api.ReschedulePolicy{
    42  				Attempts:      pointer.Of(0),
    43  				Interval:      pointer.Of(time.Duration(0)),
    44  				Delay:         pointer.Of(time.Duration(0)),
    45  				MaxDelay:      pointer.Of(time.Duration(0)),
    46  				DelayFunction: pointer.Of(""),
    47  				Unlimited:     pointer.Of(false),
    48  			},
    49  			taskReschedulePolicy: nil,
    50  			expected: &api.ReschedulePolicy{
    51  				Attempts:      pointer.Of(0),
    52  				Interval:      pointer.Of(time.Duration(0)),
    53  				Delay:         pointer.Of(time.Duration(0)),
    54  				MaxDelay:      pointer.Of(time.Duration(0)),
    55  				DelayFunction: pointer.Of(""),
    56  				Unlimited:     pointer.Of(false),
    57  			},
    58  		},
    59  		{
    60  			desc: "Inherit from job",
    61  			jobReschedulePolicy: &api.ReschedulePolicy{
    62  				Attempts:      pointer.Of(1),
    63  				Interval:      pointer.Of(20 * time.Second),
    64  				Delay:         pointer.Of(20 * time.Second),
    65  				MaxDelay:      pointer.Of(10 * time.Minute),
    66  				DelayFunction: pointer.Of("constant"),
    67  				Unlimited:     pointer.Of(false),
    68  			},
    69  			taskReschedulePolicy: nil,
    70  			expected: &api.ReschedulePolicy{
    71  				Attempts:      pointer.Of(1),
    72  				Interval:      pointer.Of(20 * time.Second),
    73  				Delay:         pointer.Of(20 * time.Second),
    74  				MaxDelay:      pointer.Of(10 * time.Minute),
    75  				DelayFunction: pointer.Of("constant"),
    76  				Unlimited:     pointer.Of(false),
    77  			},
    78  		},
    79  		{
    80  			desc:                "Set in task",
    81  			jobReschedulePolicy: nil,
    82  			taskReschedulePolicy: &api.ReschedulePolicy{
    83  				Attempts:      pointer.Of(5),
    84  				Interval:      pointer.Of(2 * time.Minute),
    85  				Delay:         pointer.Of(20 * time.Second),
    86  				MaxDelay:      pointer.Of(10 * time.Minute),
    87  				DelayFunction: pointer.Of("constant"),
    88  				Unlimited:     pointer.Of(false),
    89  			},
    90  			expected: &api.ReschedulePolicy{
    91  				Attempts:      pointer.Of(5),
    92  				Interval:      pointer.Of(2 * time.Minute),
    93  				Delay:         pointer.Of(20 * time.Second),
    94  				MaxDelay:      pointer.Of(10 * time.Minute),
    95  				DelayFunction: pointer.Of("constant"),
    96  				Unlimited:     pointer.Of(false),
    97  			},
    98  		},
    99  		{
   100  			desc: "Merge from job",
   101  			jobReschedulePolicy: &api.ReschedulePolicy{
   102  				Attempts: pointer.Of(1),
   103  				Delay:    pointer.Of(20 * time.Second),
   104  				MaxDelay: pointer.Of(10 * time.Minute),
   105  			},
   106  			taskReschedulePolicy: &api.ReschedulePolicy{
   107  				Interval:      pointer.Of(5 * time.Minute),
   108  				DelayFunction: pointer.Of("constant"),
   109  				Unlimited:     pointer.Of(false),
   110  			},
   111  			expected: &api.ReschedulePolicy{
   112  				Attempts:      pointer.Of(1),
   113  				Interval:      pointer.Of(5 * time.Minute),
   114  				Delay:         pointer.Of(20 * time.Second),
   115  				MaxDelay:      pointer.Of(10 * time.Minute),
   116  				DelayFunction: pointer.Of("constant"),
   117  				Unlimited:     pointer.Of(false),
   118  			},
   119  		},
   120  		{
   121  			desc: "Override from group",
   122  			jobReschedulePolicy: &api.ReschedulePolicy{
   123  				Attempts: pointer.Of(1),
   124  				MaxDelay: pointer.Of(10 * time.Second),
   125  			},
   126  			taskReschedulePolicy: &api.ReschedulePolicy{
   127  				Attempts:      pointer.Of(5),
   128  				Delay:         pointer.Of(20 * time.Second),
   129  				MaxDelay:      pointer.Of(20 * time.Minute),
   130  				DelayFunction: pointer.Of("constant"),
   131  				Unlimited:     pointer.Of(false),
   132  			},
   133  			expected: &api.ReschedulePolicy{
   134  				Attempts:      pointer.Of(5),
   135  				Interval:      pointer.Of(structs.DefaultBatchJobReschedulePolicy.Interval),
   136  				Delay:         pointer.Of(20 * time.Second),
   137  				MaxDelay:      pointer.Of(20 * time.Minute),
   138  				DelayFunction: pointer.Of("constant"),
   139  				Unlimited:     pointer.Of(false),
   140  			},
   141  		},
   142  		{
   143  			desc: "Attempts from job, default interval",
   144  			jobReschedulePolicy: &api.ReschedulePolicy{
   145  				Attempts: pointer.Of(1),
   146  			},
   147  			taskReschedulePolicy: nil,
   148  			expected: &api.ReschedulePolicy{
   149  				Attempts:      pointer.Of(1),
   150  				Interval:      pointer.Of(structs.DefaultBatchJobReschedulePolicy.Interval),
   151  				Delay:         pointer.Of(structs.DefaultBatchJobReschedulePolicy.Delay),
   152  				DelayFunction: pointer.Of(structs.DefaultBatchJobReschedulePolicy.DelayFunction),
   153  				MaxDelay:      pointer.Of(structs.DefaultBatchJobReschedulePolicy.MaxDelay),
   154  				Unlimited:     pointer.Of(structs.DefaultBatchJobReschedulePolicy.Unlimited),
   155  			},
   156  		},
   157  	}
   158  
   159  	for _, tc := range testCases {
   160  		t.Run(tc.desc, func(t *testing.T) {
   161  			job := &api.Job{
   162  				ID:         pointer.Of("test"),
   163  				Reschedule: tc.jobReschedulePolicy,
   164  				Type:       pointer.Of(api.JobTypeBatch),
   165  			}
   166  			job.Canonicalize()
   167  			tg := &api.TaskGroup{
   168  				Name:             pointer.Of("foo"),
   169  				ReschedulePolicy: tc.taskReschedulePolicy,
   170  			}
   171  			tg.Canonicalize(job)
   172  			assert.Equal(t, tc.expected, tg.ReschedulePolicy)
   173  		})
   174  	}
   175  }