github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/e2e/eval_priority/eval_priority.go (about)

     1  package eval_priority
     2  
     3  import (
     4  	"github.com/hashicorp/nomad/api"
     5  	"github.com/hashicorp/nomad/e2e/e2eutil"
     6  	"github.com/hashicorp/nomad/e2e/framework"
     7  	"github.com/hashicorp/nomad/helper/uuid"
     8  )
     9  
    10  type EvalPriorityTest struct {
    11  	framework.TC
    12  	jobIDs []string
    13  }
    14  
    15  func init() {
    16  	framework.AddSuites(&framework.TestSuite{
    17  		Component:   "EvalPriority",
    18  		CanRunLocal: true,
    19  		Cases: []framework.TestCase{
    20  			new(EvalPriorityTest),
    21  		},
    22  	})
    23  }
    24  
    25  func (tc *EvalPriorityTest) BeforeAll(f *framework.F) {
    26  	e2eutil.WaitForLeader(f.T(), tc.Nomad())
    27  	e2eutil.WaitForNodesReady(f.T(), tc.Nomad(), 1)
    28  }
    29  
    30  func (tc *EvalPriorityTest) AfterEach(f *framework.F) {
    31  	for _, id := range tc.jobIDs {
    32  		_, _, err := tc.Nomad().Jobs().Deregister(id, true, nil)
    33  		f.NoError(err)
    34  	}
    35  	tc.jobIDs = []string{}
    36  
    37  	_, err := e2eutil.Command("nomad", "system", "gc")
    38  	f.NoError(err)
    39  }
    40  
    41  // TestEvalPrioritySet performs a test which registers, updates, and
    42  // deregsiters a job setting the eval priority on every call.
    43  func (tc *EvalPriorityTest) TestEvalPrioritySet(f *framework.F) {
    44  
    45  	// Generate a jobID and attempt to register the job using the eval
    46  	// priority. In case there is a problem found here and the job registers,
    47  	// we need to ensure it gets cleaned up.
    48  	jobID := "test-eval-priority-" + uuid.Generate()[0:8]
    49  	f.NoError(e2eutil.RegisterWithArgs(jobID, "eval_priority/inputs/thirteen_job_priority.nomad",
    50  		"-eval-priority=80"))
    51  	tc.jobIDs = append(tc.jobIDs, jobID)
    52  
    53  	// Wait for the deployment to finish.
    54  	f.NoError(e2eutil.WaitForLastDeploymentStatus(jobID, "default", "successful", nil))
    55  
    56  	// Pull the job evaluation list from the API and ensure that this didn't
    57  	// error and contains two evals.
    58  	//
    59  	// Eval 1: the job registration eval.
    60  	// Eval 2: the deployment watcher eval.
    61  	registerEvals, _, err := tc.Nomad().Jobs().Evaluations(jobID, nil)
    62  	f.NoError(err)
    63  	f.Len(registerEvals, 2, "job expected to have two evals")
    64  
    65  	// seenEvals tracks the evaluations we have tested for priority quality
    66  	// against our expected value. This allows us to easily perform multiple
    67  	// checks with confidence.
    68  	seenEvals := map[string]bool{}
    69  
    70  	// All evaluations should have the priority set to the overridden priority.
    71  	for _, eval := range registerEvals {
    72  		f.Equal(80, eval.Priority)
    73  		seenEvals[eval.ID] = true
    74  	}
    75  
    76  	// Update the job image and set an eval priority higher than the job
    77  	// priority.
    78  	f.NoError(e2eutil.RegisterWithArgs(jobID, "eval_priority/inputs/thirteen_job_priority.nomad",
    79  		"-eval-priority=7", "-var", "image=busybox:1.34"))
    80  	f.NoError(e2eutil.WaitForLastDeploymentStatus(jobID, "default", "successful",
    81  		&e2eutil.WaitConfig{Retries: 200}))
    82  
    83  	// Pull the latest list of evaluations for the job which will include those
    84  	// as a result of the job update.
    85  	updateEvals, _, err := tc.Nomad().Jobs().Evaluations(jobID, nil)
    86  	f.NoError(err)
    87  	f.NotNil(updateEvals, "expected non-nil evaluation list response")
    88  	f.NotEmpty(updateEvals, "expected non-empty evaluation list response")
    89  
    90  	// Iterate the evals, ignoring those we have already seen and check their
    91  	// priority is as expected.
    92  	for _, eval := range updateEvals {
    93  		if ok := seenEvals[eval.ID]; ok {
    94  			continue
    95  		}
    96  		f.Equal(7, eval.Priority)
    97  		seenEvals[eval.ID] = true
    98  	}
    99  
   100  	// Deregister the job using an increased priority.
   101  	deregOpts := api.DeregisterOptions{EvalPriority: 100, Purge: true}
   102  	deregEvalID, _, err := tc.Nomad().Jobs().DeregisterOpts(jobID, &deregOpts, nil)
   103  	f.NoError(err)
   104  	f.NotEmpty(deregEvalID, "expected non-empty evaluation ID")
   105  
   106  	// Detail the deregistration evaluation and check its priority.
   107  	evalInfo, _, err := tc.Nomad().Evaluations().Info(deregEvalID, nil)
   108  	f.NoError(err)
   109  	f.Equal(100, evalInfo.Priority)
   110  
   111  	// If the job was successfully purged, clear the test suite state.
   112  	if err == nil {
   113  		tc.jobIDs = []string{}
   114  	}
   115  }
   116  
   117  // TestEvalPriorityNotSet performs a test which registers, updates, and
   118  // deregsiters a job never setting the eval priority.
   119  func (tc *EvalPriorityTest) TestEvalPriorityNotSet(f *framework.F) {
   120  
   121  	// Generate a jobID and attempt to register the job using the eval
   122  	// priority. In case there is a problem found here and the job registers,
   123  	// we need to ensure it gets cleaned up.
   124  	jobID := "test-eval-priority-" + uuid.Generate()[0:8]
   125  	f.NoError(e2eutil.Register(jobID, "eval_priority/inputs/thirteen_job_priority.nomad"))
   126  	tc.jobIDs = append(tc.jobIDs, jobID)
   127  
   128  	// Wait for the deployment to finish.
   129  	f.NoError(e2eutil.WaitForLastDeploymentStatus(jobID, "default", "successful", nil))
   130  
   131  	// Pull the job evaluation list from the API and ensure that this didn't
   132  	// error and contains two evals.
   133  	//
   134  	// Eval 1: the job registration eval.
   135  	// Eval 2: the deployment watcher eval.
   136  	registerEvals, _, err := tc.Nomad().Jobs().Evaluations(jobID, nil)
   137  	f.NoError(err)
   138  	f.Len(registerEvals, 2, "job expected to have two evals")
   139  
   140  	// seenEvals tracks the evaluations we have tested for priority quality
   141  	// against our expected value. This allows us to easily perform multiple
   142  	// checks with confidence.
   143  	seenEvals := map[string]bool{}
   144  
   145  	// All evaluations should have the priority set to the job priority.
   146  	for _, eval := range registerEvals {
   147  		f.Equal(13, eval.Priority)
   148  		seenEvals[eval.ID] = true
   149  	}
   150  
   151  	// Update the job image without setting an eval priority.
   152  	f.NoError(e2eutil.RegisterWithArgs(jobID, "eval_priority/inputs/thirteen_job_priority.nomad",
   153  		"-var", "image=busybox:1.34"))
   154  	f.NoError(e2eutil.WaitForLastDeploymentStatus(jobID, "default", "successful",
   155  		&e2eutil.WaitConfig{Retries: 200}))
   156  
   157  	// Pull the latest list of evaluations for the job which will include those
   158  	// as a result of the job update.
   159  	updateEvals, _, err := tc.Nomad().Jobs().Evaluations(jobID, nil)
   160  	f.NoError(err)
   161  	f.NotNil(updateEvals, "expected non-nil evaluation list response")
   162  	f.NotEmpty(updateEvals, "expected non-empty evaluation list response")
   163  
   164  	// Iterate the evals, ignoring those we have already seen and check their
   165  	// priority is as expected.
   166  	for _, eval := range updateEvals {
   167  		if ok := seenEvals[eval.ID]; ok {
   168  			continue
   169  		}
   170  		f.Equal(13, eval.Priority)
   171  		seenEvals[eval.ID] = true
   172  	}
   173  
   174  	// Deregister the job without setting an eval priority.
   175  	deregOpts := api.DeregisterOptions{Purge: true}
   176  	deregEvalID, _, err := tc.Nomad().Jobs().DeregisterOpts(jobID, &deregOpts, nil)
   177  	f.NoError(err)
   178  	f.NotEmpty(deregEvalID, "expected non-empty evaluation ID")
   179  
   180  	// Detail the deregistration evaluation and check its priority.
   181  	evalInfo, _, err := tc.Nomad().Evaluations().Info(deregEvalID, nil)
   182  	f.NoError(err)
   183  	f.Equal(13, evalInfo.Priority)
   184  
   185  	// If the job was successfully purged, clear the test suite state.
   186  	if err == nil {
   187  		tc.jobIDs = []string{}
   188  	}
   189  }