@@ -20,9 +20,11 @@ import (
2020 "encoding/base64"
2121 "encoding/json"
2222 "fmt"
23+ "reflect"
2324 "testing"
2425
2526 "github.com/agiledragon/gomonkey/v2"
27+ log "github.com/sirupsen/logrus"
2628 "github.com/stretchr/testify/assert"
2729 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2830 "k8s.io/apimachinery/pkg/runtime"
@@ -289,6 +291,13 @@ func TestNewWorkflowByRun(t *testing.T) {
289291 }
290292 _ , err = newMockWorkflowByRun (run2 )
291293 assert .Nil (t , err )
294+
295+ run2 .ID = "1445"
296+ _ , err = newMockWorkflowByRun (run2 )
297+ assert .Nil (t , err )
298+
299+ _ , ok := wfMap .Load (run2 .ID )
300+ assert .True (t , ok )
292301}
293302
294303func TestCreateRunByJson (t * testing.T ) {
@@ -363,5 +372,118 @@ func TestCreateRun(t *testing.T) {
363372 }
364373 _ , err = CreateRun (ctx , & createRunRequest , map [string ]string {})
365374 assert .Nil (t , err )
375+ }
376+
377+ func TestStopRun (t * testing.T ) {
378+ driver .InitMockDB ()
379+ run := getMockRunWithoutRuntime ()
380+ ctx := & logger.RequestContext {UserName : MockRootUser }
381+ runID , err := models .CreateRun (ctx .Logging (), & run )
382+ assert .Nil (t , err )
383+
384+ wfMap .Store (runID , & pipeline.Workflow {})
385+
386+ var wf * pipeline.Workflow
387+ patch := gomonkey .ApplyMethod (reflect .TypeOf (wf ), "Stop" , func (* pipeline.Workflow , bool ) {
388+ return
389+ })
390+ defer patch .Reset ()
391+
392+ var r * models.Run
393+ patch2 := gomonkey .ApplyMethod (reflect .TypeOf (r ), "Encode" , func (* models.Run ) error {
394+ return nil
395+ })
396+ defer patch2 .Reset ()
366397
398+ patch3 := gomonkey .ApplyFunc (GetRunByID , func (logEntry * log.Entry , userName string , runID string ) (models.Run , error ) {
399+ return run , nil
400+ })
401+ defer patch3 .Reset ()
402+
403+ req := UpdateRunRequest {StopForce : false }
404+ err = StopRun (ctx .Logging (), "root" , runID , req )
405+ assert .Nil (t , err )
406+ }
407+
408+ func TestStartWf (t * testing.T ) {
409+ run := models.Run {
410+ ID : "run=00001" ,
411+ }
412+ wfptr := & pipeline.Workflow {}
413+ patch := gomonkey .ApplyMethod (reflect .TypeOf (wfptr ), "NewWorkflowRuntime" , func (* pipeline.Workflow ) error {
414+ return nil
415+ })
416+ defer patch .Reset ()
417+
418+ patch2 := gomonkey .ApplyFunc (models .UpdateRunStatus , func (logEntry * log.Entry , runID string , status string ) error {
419+ return nil
420+ })
421+ defer patch2 .Reset ()
422+
423+ patch3 := gomonkey .ApplyMethod (reflect .TypeOf (wfptr ), "Start" , func (* pipeline.Workflow ) {
424+ return
425+ })
426+ defer patch3 .Reset ()
427+
428+ StartWf (run , wfptr )
429+ _ , ok := wfMap .Load (run .ID )
430+ assert .True (t , ok )
431+ }
432+
433+ func TestRestartWf (t * testing.T ) {
434+ run := models.Run {
435+ ID : "run=00001" ,
436+ }
437+ wfptr := & pipeline.Workflow {}
438+
439+ patch := gomonkey .ApplyFunc (newWorkflowByRun , func (run models.Run ) (* pipeline.Workflow , error ) {
440+ return & pipeline.Workflow {}, nil
441+ })
442+ defer patch .Reset ()
443+
444+ patch2 := gomonkey .ApplyFunc (models .UpdateRunStatus , func (logEntry * log.Entry , runID string , status string ) error {
445+ return nil
446+ })
447+ defer patch2 .Reset ()
448+
449+ patch3 := gomonkey .ApplyMethod (reflect .TypeOf (wfptr ), "Restart" , func (* pipeline.Workflow , * schema.DagView , schema.PostProcessView ) {
450+ return
451+ })
452+ defer patch3 .Reset ()
453+
454+ patch4 := gomonkey .ApplyFunc (models .GetRunJobsOfRun , func (logEntry * log.Entry , runID string ) ([]models.RunJob , error ) {
455+ return nil , nil
456+ })
457+ defer patch4 .Reset ()
458+
459+ patch5 := gomonkey .ApplyFunc (models .GetRunDagsOfRun , func (logEntry * log.Entry , runID string ) ([]models.RunDag , error ) {
460+ return nil , nil
461+ })
462+ defer patch5 .Reset ()
463+
464+ var r * models.Run
465+ patch6 := gomonkey .ApplyMethod (reflect .TypeOf (r ), "Encode" , func (* models.Run ) error {
466+ return nil
467+ })
468+ defer patch6 .Reset ()
469+
470+ patch7 := gomonkey .ApplyFunc (models .CreateRun , func (logEntry * log.Entry , run * models.Run ) (string , error ) {
471+ return "" , nil
472+ })
473+ defer patch7 .Reset ()
474+
475+ patch8 := gomonkey .ApplyFunc (models .CreateRunDag , func (logEntry * log.Entry , runDag * models.RunDag ) (int64 , error ) {
476+ return 234 , nil
477+ })
478+ defer patch8 .Reset ()
479+
480+ patch9 := gomonkey .ApplyMethod (reflect .TypeOf (r ), "InitRuntime" , func (_ * models.Run , jobs []models.RunJob , dags []models.RunDag ) error {
481+ return nil
482+ })
483+ defer patch9 .Reset ()
484+
485+ id , err := RestartWf (run , false )
486+ assert .Nil (t , err )
487+ _ , ok := wfMap .Load (id )
488+ assert .True (t , ok )
367489}
0 commit comments