Skip to content

Commit

Permalink
chore: add lockfile status tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcStdt committed Jul 19, 2024
1 parent dc16779 commit 21fa37b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 21 additions & 15 deletions internal/core/services/queue_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -82,27 +88,27 @@ 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)
}

err = sc.processLauncher.Run(cmd)

if err != nil {
sc.setCommandQueue(cmd, domain.ScrollLockStatusError)
sc.setCommandQueue(cmd, domain.ScrollLockStatusError, changeStatus)
return err
}

//restart means we are never done!
if changeStatus && command.Run != domain.RunModeRestart {
lock.SetStatus(cmd, domain.ScrollLockStatusDone)
}
sc.setCommandQueue(cmd, domain.ScrollLockStatusDone)
sc.setCommandQueue(cmd, domain.ScrollLockStatusDone, changeStatus)

return nil

Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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))
}
Expand Down
49 changes: 36 additions & 13 deletions test/integration/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
}
}
})
}
}

0 comments on commit 21fa37b

Please sign in to comment.