@@ -23,6 +23,7 @@ import (
2323 "github.com/chaitin/MonkeyCode/backend/db"
2424 "github.com/chaitin/MonkeyCode/backend/db/enttest"
2525 "github.com/chaitin/MonkeyCode/backend/domain"
26+ "github.com/chaitin/MonkeyCode/backend/pkg/delayqueue"
2627 "github.com/chaitin/MonkeyCode/backend/pkg/taskflow"
2728)
2829
@@ -337,6 +338,97 @@ func TestHostUsecase_markRecycledTasksFinished(t *testing.T) {
337338 }
338339}
339340
341+ func TestHostUsecase_DeleteVMFinishesBoundTasks (t * testing.T ) {
342+ t .Parallel ()
343+
344+ ctx := context .Background ()
345+ client := enttest .Open (t , "sqlite3" , "file:host-usecase-delete-vm-finish-task-test?mode=memory&cache=shared&_fk=1" )
346+ defer client .Close ()
347+
348+ userID := uuid .New ()
349+ if _ , err := client .User .Create ().
350+ SetID (userID ).
351+ SetName ("tester" ).
352+ SetRole (consts .UserRoleIndividual ).
353+ SetStatus (consts .UserStatusActive ).
354+ Save (ctx ); err != nil {
355+ t .Fatalf ("create user: %v" , err )
356+ }
357+
358+ hostID := "host-1"
359+ if _ , err := client .Host .Create ().
360+ SetID (hostID ).
361+ SetUserID (userID ).
362+ SetHostname ("host" ).
363+ Save (ctx ); err != nil {
364+ t .Fatalf ("create host: %v" , err )
365+ }
366+
367+ vmID := "vm-1"
368+ if _ , err := client .VirtualMachine .Create ().
369+ SetID (vmID ).
370+ SetHostID (hostID ).
371+ SetUserID (userID ).
372+ SetName ("vm" ).
373+ SetEnvironmentID ("env-1" ).
374+ Save (ctx ); err != nil {
375+ t .Fatalf ("create vm: %v" , err )
376+ }
377+
378+ taskID := uuid .New ()
379+ if _ , err := client .Task .Create ().
380+ SetID (taskID ).
381+ SetUserID (userID ).
382+ SetKind (consts .TaskTypeDevelop ).
383+ SetContent ("content" ).
384+ SetStatus (consts .TaskStatusProcessing ).
385+ Save (ctx ); err != nil {
386+ t .Fatalf ("create task: %v" , err )
387+ }
388+ if _ , err := client .TaskVirtualMachine .Create ().
389+ SetID (uuid .New ()).
390+ SetTaskID (taskID ).
391+ SetVirtualmachineID (vmID ).
392+ Save (ctx ); err != nil {
393+ t .Fatalf ("bind task vm: %v" , err )
394+ }
395+
396+ i := do .New ()
397+ do .ProvideValue (i , client )
398+ redisServer := miniredis .RunT (t )
399+ redisClient := redis .NewClient (& redis.Options {Addr : redisServer .Addr ()})
400+ t .Cleanup (func () { _ = redisClient .Close () })
401+ logger := slog .New (slog .NewTextHandler (io .Discard , nil ))
402+ do .ProvideValue (i , & config.Config {})
403+ do .ProvideValue (i , logger )
404+ do .ProvideValue (i , redisClient )
405+ hostRepo , err := repo .NewHostRepo (i )
406+ if err != nil {
407+ t .Fatalf ("new host repo: %v" , err )
408+ }
409+ u := & HostUsecase {
410+ repo : hostRepo ,
411+ taskflow : & preinsertTaskflowStub {vm : & preinsertVMCreateStub {db : client }},
412+ logger : logger ,
413+ vmexpireQueue : delayqueue .NewVMExpireQueue (redisClient , logger ),
414+ }
415+
416+ if err := u .DeleteVM (ctx , userID , hostID , vmID ); err != nil {
417+ t .Fatalf ("DeleteVM() error = %v" , err )
418+ }
419+
420+ gotTask , err := client .Task .Get (ctx , taskID )
421+ if err != nil {
422+ t .Fatalf ("query task: %v" , err )
423+ }
424+ if gotTask .Status != consts .TaskStatusFinished {
425+ t .Fatalf ("task status = %s, want %s" , gotTask .Status , consts .TaskStatusFinished )
426+ }
427+ if gotTask .CompletedAt .IsZero () {
428+ t .Fatal ("expected task completed_at to be set" )
429+ }
430+ }
431+
340432func TestHostUsecase_CreateVMPreinsertsVirtualMachineBeforeTaskflowCreate (t * testing.T ) {
341433 t .Parallel ()
342434
0 commit comments