bugfix: broken unit tests

This commit is contained in:
jamesread 2023-10-03 23:21:21 +01:00
parent 6d21bbe03a
commit 2b4a3ab137
3 changed files with 25 additions and 11 deletions

View File

@ -1,7 +1,6 @@
package executor package executor
import ( import (
pb "github.com/OliveTin/OliveTin/gen/grpc"
acl "github.com/OliveTin/OliveTin/internal/acl" acl "github.com/OliveTin/OliveTin/internal/acl"
config "github.com/OliveTin/OliveTin/internal/config" config "github.com/OliveTin/OliveTin/internal/config"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -13,6 +12,7 @@ import (
"os/exec" "os/exec"
"runtime" "runtime"
"time" "time"
"sync"
) )
// Executor represents a helper class for executing commands. It's main method // Executor represents a helper class for executing commands. It's main method
@ -99,11 +99,14 @@ func (e *Executor) AddListener(m listener) {
} }
// ExecRequest processes an ExecutionRequest // ExecRequest processes an ExecutionRequest
func (e *Executor) ExecRequest(req *ExecutionRequest) *pb.StartActionResponse { func (e *Executor) ExecRequest(req *ExecutionRequest) (*sync.WaitGroup, string) {
req.executor = e
// req.UUID is now set by the client, so that they can track the request // req.UUID is now set by the client, so that they can track the request
// from start to finish. This means that a malicious client could send // from start to finish. This means that a malicious client could send
// duplicate UUIDs (or just random strings), but this is the only way. // duplicate UUIDs (or just random strings), but this is the only way.
req.executor = e req.uuid = uuid.New().String()
req.logEntry = &InternalLogEntry{ req.logEntry = &InternalLogEntry{
DatetimeStarted: time.Now().Format("2006-01-02 15:04:05"), DatetimeStarted: time.Now().Format("2006-01-02 15:04:05"),
ActionTitle: req.ActionName, ActionTitle: req.ActionName,
@ -121,11 +124,15 @@ func (e *Executor) ExecRequest(req *ExecutionRequest) *pb.StartActionResponse {
listener.OnExecutionStarted(req.ActionName) listener.OnExecutionStarted(req.ActionName)
} }
go e.execChain(req) wg := new(sync.WaitGroup)
wg.Add(1);
return &pb.StartActionResponse{ go func() {
ExecutionUuid: req.UUID, e.execChain(req)
} defer wg.Done();
}()
return wg, req.uuid;
} }
func (e *Executor) execChain(req *ExecutionRequest) { func (e *Executor) execChain(req *ExecutionRequest) {

View File

@ -42,11 +42,12 @@ func TestCreateExecutorAndExec(t *testing.T) {
}, },
} }
e.ExecRequest(&req)
assert.NotNil(t, e, "Create an executor") assert.NotNil(t, e, "Create an executor")
assert.NotNil(t, e.ExecRequest(&req), "Execute a request") wg, _ := e.ExecRequest(&req)
wg.Wait()
assert.Equal(t, int32(0), req.logEntry.ExitCode, "Exit code is zero") assert.Equal(t, int32(0), req.logEntry.ExitCode, "Exit code is zero")
} }
@ -59,7 +60,8 @@ func TestExecNonExistant(t *testing.T) {
Cfg: cfg, Cfg: cfg,
} }
e.ExecRequest(&req) wg, _ := e.ExecRequest(&req)
wg.Wait()
assert.Equal(t, int32(-1337), req.logEntry.ExitCode, "Log entry is set to an internal error code") assert.Equal(t, int32(-1337), req.logEntry.ExitCode, "Log entry is set to an internal error code")
assert.Equal(t, "", req.logEntry.ActionIcon, "Log entry icon wasnt found") assert.Equal(t, "", req.logEntry.ActionIcon, "Log entry icon wasnt found")

View File

@ -42,7 +42,12 @@ func (api *oliveTinAPI) StartAction(ctx ctx.Context, req *pb.StartActionRequest)
Cfg: cfg, Cfg: cfg,
} }
return api.executor.ExecRequest(&execReq), nil _, uuid := api.executor.ExecRequest(&execReq);
return &pb.StartActionResponse {
ExecutionUuid: uuid,
}, nil;
} }
func (api *oliveTinAPI) ExecutionStatus(ctx ctx.Context, req *pb.ExecutionStatusRequest) (*pb.ExecutionStatusResponse, error) { func (api *oliveTinAPI) ExecutionStatus(ctx ctx.Context, req *pb.ExecutionStatusRequest) (*pb.ExecutionStatusResponse, error) {