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