From 21fa37b094dfa47d5d9ffff4d4d9c7d2745b531b Mon Sep 17 00:00:00 2001 From: Marc Date: Sat, 20 Jul 2024 00:00:19 +0200 Subject: [PATCH] chore: add lockfile status tests --- cmd/serve.go | 2 +- internal/core/services/queue_manager.go | 36 ++++++++++-------- test/integration/example_test.go | 49 ++++++++++++++++++------- 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 33c8672..e7c3590 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -148,7 +148,7 @@ to interact and monitor the Scroll Application`, } //start scroll.init process //initialize if nothing is there - err = queueManager.AddItem(currentScroll.Init, false) + err = queueManager.AddItem(currentScroll.Init, true) if err != nil { return err } diff --git a/internal/core/services/queue_manager.go b/internal/core/services/queue_manager.go index b52ccae..57d30cc 100644 --- a/internal/core/services/queue_manager.go +++ b/internal/core/services/queue_manager.go @@ -41,34 +41,40 @@ func NewQueueManager( } } -func (sc *QueueManager) setCommandQueue(commandName string, status domain.ScrollLockStatus) { +func (sc *QueueManager) setCommandQueue(commandName string, status domain.ScrollLockStatus, changeStatus bool) { sc.mu.Lock() defer sc.mu.Unlock() sc.commandQueue[commandName] = &QueueItem{ status: status, - changeStatus: true, + changeStatus: changeStatus, } } -func (sc *QueueManager) workItem(cmd string, changeStatus bool) error { +func (sc *QueueManager) workItem(cmd string) error { - logger.Log().Debug("Running command", - zap.String("cmd", cmd), - zap.Bool("changeStatus", changeStatus), - ) + queueItem := sc.commandQueue[cmd] + if queueItem == nil { + return fmt.Errorf("command %s not found", cmd) + } command, err := sc.scrollService.GetCommand(cmd) - if err != nil { return err } + changeStatus := queueItem.changeStatus + + logger.Log().Debug("Running command", + zap.String("cmd", cmd), + zap.Bool("changeStatus", changeStatus), + ) + lock, err := sc.scrollService.GetLock() if err != nil { return err } - sc.setCommandQueue(cmd, domain.ScrollLockStatusWaiting) + sc.setCommandQueue(cmd, domain.ScrollLockStatusWaiting, changeStatus) if changeStatus { lock.SetStatus(cmd, domain.ScrollLockStatusWaiting) } @@ -82,11 +88,11 @@ func (sc *QueueManager) workItem(cmd string, changeStatus bool) error { //if done and should be done once, skip if status == domain.ScrollLockStatusDone && command.Run == domain.RunModeOnce { - sc.setCommandQueue(cmd, domain.ScrollLockStatusDone) + sc.setCommandQueue(cmd, domain.ScrollLockStatusDone, changeStatus) return nil } - sc.setCommandQueue(cmd, domain.ScrollLockStatusRunning) + sc.setCommandQueue(cmd, domain.ScrollLockStatusRunning, changeStatus) if changeStatus { lock.SetStatus(cmd, domain.ScrollLockStatusRunning) } @@ -94,7 +100,7 @@ func (sc *QueueManager) workItem(cmd string, changeStatus bool) error { err = sc.processLauncher.Run(cmd) if err != nil { - sc.setCommandQueue(cmd, domain.ScrollLockStatusError) + sc.setCommandQueue(cmd, domain.ScrollLockStatusError, changeStatus) return err } @@ -102,7 +108,7 @@ func (sc *QueueManager) workItem(cmd string, changeStatus bool) error { if changeStatus && command.Run != domain.RunModeRestart { lock.SetStatus(cmd, domain.ScrollLockStatusDone) } - sc.setCommandQueue(cmd, domain.ScrollLockStatusDone) + sc.setCommandQueue(cmd, domain.ScrollLockStatusDone, changeStatus) return nil @@ -162,7 +168,7 @@ func (sc *QueueManager) QueueLockFile() error { for cmd, status := range lock.Statuses { sc.commandQueue[cmd] = &QueueItem{ status: status, - changeStatus: false, + changeStatus: true, } } @@ -230,7 +236,7 @@ func (sc *QueueManager) RunQueue() { } if dependenciesReady { - err := sc.workItem(cmd, item.changeStatus) + err := sc.workItem(cmd) if err != nil { logger.Log().Error("Error running command", zap.String("command", cmd), zap.Error(err)) } diff --git a/test/integration/example_test.go b/test/integration/example_test.go index 703c44f..3d91541 100644 --- a/test/integration/example_test.go +++ b/test/integration/example_test.go @@ -17,10 +17,11 @@ import ( ) type ServiceConfig struct { - ServiceName string - ExamplePath string - TestAddress string - TestName string + ServiceName string + ExamplePath string + TestAddress string + TestName string + LockFileStatus []string } func TestExamples(t *testing.T) { @@ -30,16 +31,18 @@ func TestExamples(t *testing.T) { configs := []ServiceConfig{ { - ServiceName: "minecraft", - ExamplePath: "../../examples/minecraft/.scroll/scroll.yaml", - TestAddress: "localhost:25565", - TestName: "Minecraft", + ServiceName: "minecraft", + ExamplePath: "../../examples/minecraft/.scroll/scroll.yaml", + TestAddress: "localhost:25565", + TestName: "Minecraft", + LockFileStatus: []string{"start", "install"}, }, { - ServiceName: "nginx", - ExamplePath: "../../examples/nginx/.scroll/scroll.yaml", - TestAddress: "localhost:80", - TestName: "Nginx", + ServiceName: "nginx", + ExamplePath: "../../examples/nginx/.scroll/scroll.yaml", + TestAddress: "localhost:80", + TestName: "Nginx", + LockFileStatus: []string{"start"}, }, // Add more services here } @@ -133,7 +136,7 @@ func TestExamples(t *testing.T) { timeout := time.After(4 * time.Minute) tick := time.Tick(1 * time.Second) - err := queueManager.AddItem("start", false) + err := queueManager.AddItem("start", true) if err != nil { doneStarting <- err return @@ -165,6 +168,26 @@ func TestExamples(t *testing.T) { if err != nil { t.Error("Failed to test to server: ", err) } + + lock, err := scrollService.GetLock() + + if err != nil { + t.Error(err) + return + + } + + if len(lock.Statuses) != len(config.LockFileStatus) { + t.Errorf("Lock file statuses count mismatch, expected: %d, got: %d", len(config.LockFileStatus), len(lock.Statuses)) + } + + for _, status := range config.LockFileStatus { + if _, ok := lock.Statuses[status]; !ok { + t.Errorf( + "Lock file status %s not found, expected: %v, got: %v", status, config.LockFileStatus, lock.Statuses, + ) + } + } }) } }