github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/helper/raftutil/sample_test.go (about)

     1  package raftutil
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/helper/uuid"
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  	"github.com/hashicorp/nomad/scheduler"
     9  	"github.com/kr/pretty"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // TestSampleInvariant illustrates how to find offending log entry for an invariant
    14  func TestSampleInvariant(t *testing.T) {
    15  	t.Skip("not a real test")
    16  
    17  	path := "/tmp/nomad-datadir/server/raft"
    18  	ns := "default"
    19  	parentID := "myjob"
    20  
    21  	fsm, err := NewFSM(path)
    22  	require.NoError(t, err)
    23  
    24  	state := fsm.State()
    25  	for {
    26  		idx, _, err := fsm.ApplyNext()
    27  		if err == ErrNoMoreLogs {
    28  			break
    29  		}
    30  		require.NoError(t, err)
    31  
    32  		// Test invariant for each entry
    33  
    34  		// For example, test job summary numbers against running jobs
    35  		summary, err := state.JobSummaryByID(nil, ns, parentID)
    36  		require.NoError(t, err)
    37  
    38  		if summary == nil {
    39  			// job hasn't been created yet
    40  			continue
    41  		}
    42  
    43  		summaryCount := summary.Children.Running + summary.Children.Pending + summary.Children.Dead
    44  		jobCountByParent := 0
    45  
    46  		iter, err := state.Jobs(nil)
    47  		require.NoError(t, err)
    48  		for {
    49  			rawJob := iter.Next()
    50  			if rawJob == nil {
    51  				break
    52  			}
    53  			job := rawJob.(*structs.Job)
    54  			if job.Namespace == ns && job.ParentID == parentID {
    55  				jobCountByParent++
    56  			}
    57  		}
    58  
    59  		require.Equalf(t, summaryCount, jobCountByParent, "job summary at idx=%v", idx)
    60  
    61  	}
    62  
    63  	// any post-assertion follow
    64  }
    65  
    66  // TestSchedulerLogic illustrates how to test how to test the scheduler
    67  // logic for handling an eval
    68  func TestSchedulerLogic(t *testing.T) {
    69  	t.Skip("not a real test")
    70  
    71  	path := "/tmp/nomad-datadir/server/raft"
    72  	ns := "default"
    73  	jobID := "myjob"
    74  	testIdx := uint64(3234)
    75  
    76  	fsm, err := NewFSM(path)
    77  	require.NoError(t, err)
    78  
    79  	_, _, err = fsm.ApplyUntil(testIdx)
    80  	require.NoError(t, err)
    81  
    82  	state := fsm.State()
    83  
    84  	job, err := state.JobByID(nil, ns, jobID)
    85  	require.NoError(t, err)
    86  
    87  	// Create an eval and schedule it!
    88  	// Create a mock evaluation to register the job
    89  	eval := &structs.Evaluation{
    90  		Namespace:   ns,
    91  		ID:          uuid.Generate(),
    92  		Priority:    job.Priority,
    93  		TriggeredBy: structs.EvalTriggerJobRegister,
    94  		JobID:       job.ID,
    95  		Status:      structs.EvalStatusPending,
    96  	}
    97  
    98  	// Process the evaluation
    99  	h := scheduler.NewHarnessWithState(t, state)
   100  	err = h.Process(scheduler.NewServiceScheduler, eval)
   101  	require.NoError(t, err)
   102  
   103  	require.Len(t, h.Plans, 1)
   104  	pretty.Println(h.Plans[0])
   105  
   106  }