Worker: add CommandLineRunner interface for executing CLIs
Not yet used, but interface is there + mocked for testing.
This commit is contained in:
parent
6b5b3eacdd
commit
77f1e02c75
@ -23,6 +23,7 @@ package worker
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
@ -42,11 +43,12 @@ type CommandListener interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CommandExecutor struct {
|
type CommandExecutor struct {
|
||||||
|
cli CommandLineRunner
|
||||||
listener CommandListener
|
listener CommandListener
|
||||||
|
timeService TimeService
|
||||||
|
|
||||||
// registry maps a command name to a function that runs that command.
|
// registry maps a command name to a function that runs that command.
|
||||||
registry map[string]commandCallable
|
registry map[string]commandCallable
|
||||||
|
|
||||||
timeService TimeService
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ CommandRunner = (*CommandExecutor)(nil)
|
var _ CommandRunner = (*CommandExecutor)(nil)
|
||||||
@ -58,8 +60,15 @@ type TimeService interface {
|
|||||||
After(duration time.Duration) <-chan time.Time
|
After(duration time.Duration) <-chan time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCommandExecutor(listener CommandListener, timeService TimeService) *CommandExecutor {
|
//go:generate go run github.com/golang/mock/mockgen -destination mocks/cli_runner.gen.go -package mocks gitlab.com/blender/flamenco-ng-poc/internal/worker CommandLineRunner
|
||||||
|
// CommandLineRunner is an interface around exec.CommandContext().
|
||||||
|
type CommandLineRunner interface {
|
||||||
|
CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCommandExecutor(cli CommandLineRunner, listener CommandListener, timeService TimeService) *CommandExecutor {
|
||||||
ce := &CommandExecutor{
|
ce := &CommandExecutor{
|
||||||
|
cli: cli,
|
||||||
listener: listener,
|
listener: listener,
|
||||||
timeService: timeService,
|
timeService: timeService,
|
||||||
}
|
}
|
||||||
|
@ -25,19 +25,38 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/benbjohnson/clock"
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"gitlab.com/blender/flamenco-ng-poc/internal/worker/mocks"
|
"gitlab.com/blender/flamenco-ng-poc/internal/worker/mocks"
|
||||||
"gitlab.com/blender/flamenco-ng-poc/pkg/api"
|
"gitlab.com/blender/flamenco-ng-poc/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MockCommandExecutor struct {
|
||||||
|
ce *CommandExecutor
|
||||||
|
cli *mocks.MockCommandLineRunner
|
||||||
|
listener *mocks.MockCommandListener
|
||||||
|
clock *clock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCommandExecutor(t *testing.T, mockCtrl *gomock.Controller) *MockCommandExecutor {
|
||||||
|
cli := mocks.NewMockCommandLineRunner(mockCtrl)
|
||||||
|
listener := mocks.NewMockCommandListener(mockCtrl)
|
||||||
|
clock := mockedClock(t)
|
||||||
|
ce := NewCommandExecutor(cli, listener, clock)
|
||||||
|
|
||||||
|
return &MockCommandExecutor{
|
||||||
|
ce: ce,
|
||||||
|
cli: cli,
|
||||||
|
listener: listener,
|
||||||
|
clock: clock,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCommandEcho(t *testing.T) {
|
func TestCommandEcho(t *testing.T) {
|
||||||
mockCtrl := gomock.NewController(t)
|
mockCtrl := gomock.NewController(t)
|
||||||
defer mockCtrl.Finish()
|
defer mockCtrl.Finish()
|
||||||
|
tce := testCommandExecutor(t, mockCtrl)
|
||||||
listener := mocks.NewMockCommandListener(mockCtrl)
|
|
||||||
clock := mockedClock(t)
|
|
||||||
ce := NewCommandExecutor(listener, clock)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
message := "понављај за мном"
|
message := "понављај за мном"
|
||||||
@ -47,19 +66,16 @@ func TestCommandEcho(t *testing.T) {
|
|||||||
Parameters: map[string]interface{}{"message": message},
|
Parameters: map[string]interface{}{"message": message},
|
||||||
}
|
}
|
||||||
|
|
||||||
listener.EXPECT().LogProduced(gomock.Any(), taskID, "echo: \"понављај за мном\"")
|
tce.listener.EXPECT().LogProduced(gomock.Any(), taskID, "echo: \"понављај за мном\"")
|
||||||
|
|
||||||
err := ce.Run(ctx, taskID, cmd)
|
err := tce.ce.Run(ctx, taskID, cmd)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandSleep(t *testing.T) {
|
func TestCommandSleep(t *testing.T) {
|
||||||
mockCtrl := gomock.NewController(t)
|
mockCtrl := gomock.NewController(t)
|
||||||
defer mockCtrl.Finish()
|
defer mockCtrl.Finish()
|
||||||
|
tce := testCommandExecutor(t, mockCtrl)
|
||||||
listener := mocks.NewMockCommandListener(mockCtrl)
|
|
||||||
clock := mockedClock(t)
|
|
||||||
ce := NewCommandExecutor(listener, clock)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
taskID := "90e9d656-e201-4ef0-b6b0-c80684fafa27"
|
taskID := "90e9d656-e201-4ef0-b6b0-c80684fafa27"
|
||||||
@ -68,9 +84,9 @@ func TestCommandSleep(t *testing.T) {
|
|||||||
Parameters: map[string]interface{}{"duration_in_seconds": 47},
|
Parameters: map[string]interface{}{"duration_in_seconds": 47},
|
||||||
}
|
}
|
||||||
|
|
||||||
listener.EXPECT().LogProduced(gomock.Any(), taskID, "slept 47s")
|
tce.listener.EXPECT().LogProduced(gomock.Any(), taskID, "slept 47s")
|
||||||
|
|
||||||
timeBefore := clock.Now()
|
timeBefore := tce.clock.Now()
|
||||||
|
|
||||||
// Run the test in a goroutine, as we also need to actually increase the
|
// Run the test in a goroutine, as we also need to actually increase the
|
||||||
// mocked clock at the same time; without that, the command will sleep
|
// mocked clock at the same time; without that, the command will sleep
|
||||||
@ -78,7 +94,7 @@ func TestCommandSleep(t *testing.T) {
|
|||||||
runDone := make(chan struct{})
|
runDone := make(chan struct{})
|
||||||
var err error
|
var err error
|
||||||
go func() {
|
go func() {
|
||||||
err = ce.Run(ctx, taskID, cmd)
|
err = tce.ce.Run(ctx, taskID, cmd)
|
||||||
close(runDone)
|
close(runDone)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -89,12 +105,12 @@ loop:
|
|||||||
case <-runDone:
|
case <-runDone:
|
||||||
break loop
|
break loop
|
||||||
default:
|
default:
|
||||||
clock.Add(timeStepSize)
|
tce.clock.Add(timeStepSize)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
timeAfter := clock.Now()
|
timeAfter := tce.clock.Now()
|
||||||
// Within the step size is precise enough. We're testing our implementation, not the precision of `time.After()`.
|
// Within the step size is precise enough. We're testing our implementation, not the precision of `time.After()`.
|
||||||
assert.WithinDuration(t, timeBefore.Add(47*time.Second), timeAfter, timeStepSize)
|
assert.WithinDuration(t, timeBefore.Add(47*time.Second), timeAfter, timeStepSize)
|
||||||
}
|
}
|
||||||
|
55
internal/worker/mocks/cli_runner.gen.go
Normal file
55
internal/worker/mocks/cli_runner.gen.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
|
// Source: gitlab.com/blender/flamenco-ng-poc/internal/worker (interfaces: CommandLineRunner)
|
||||||
|
|
||||||
|
// Package mocks is a generated GoMock package.
|
||||||
|
package mocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
exec "os/exec"
|
||||||
|
reflect "reflect"
|
||||||
|
|
||||||
|
gomock "github.com/golang/mock/gomock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockCommandLineRunner is a mock of CommandLineRunner interface.
|
||||||
|
type MockCommandLineRunner struct {
|
||||||
|
ctrl *gomock.Controller
|
||||||
|
recorder *MockCommandLineRunnerMockRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockCommandLineRunnerMockRecorder is the mock recorder for MockCommandLineRunner.
|
||||||
|
type MockCommandLineRunnerMockRecorder struct {
|
||||||
|
mock *MockCommandLineRunner
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockCommandLineRunner creates a new mock instance.
|
||||||
|
func NewMockCommandLineRunner(ctrl *gomock.Controller) *MockCommandLineRunner {
|
||||||
|
mock := &MockCommandLineRunner{ctrl: ctrl}
|
||||||
|
mock.recorder = &MockCommandLineRunnerMockRecorder{mock}
|
||||||
|
return mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||||
|
func (m *MockCommandLineRunner) EXPECT() *MockCommandLineRunnerMockRecorder {
|
||||||
|
return m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommandContext mocks base method.
|
||||||
|
func (m *MockCommandLineRunner) CommandContext(arg0 context.Context, arg1 string, arg2 ...string) *exec.Cmd {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
varargs := []interface{}{arg0, arg1}
|
||||||
|
for _, a := range arg2 {
|
||||||
|
varargs = append(varargs, a)
|
||||||
|
}
|
||||||
|
ret := m.ctrl.Call(m, "CommandContext", varargs...)
|
||||||
|
ret0, _ := ret[0].(*exec.Cmd)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommandContext indicates an expected call of CommandContext.
|
||||||
|
func (mr *MockCommandLineRunnerMockRecorder) CommandContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
varargs := append([]interface{}{arg0, arg1}, arg2...)
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CommandContext", reflect.TypeOf((*MockCommandLineRunner)(nil).CommandContext), varargs...)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user