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