Skip to content
Snippets Groups Projects
Commit 16d0bd1c authored by Alessio Caiazza's avatar Alessio Caiazza Committed by Alessio Caiazza
Browse files

Refactor RunSingleCommand tests

parent a134b254
Branches
Tags
No related merge requests found
package commands
import (
"github.com/urfave/cli"
"os"
"os/signal"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/tevino/abool"
"github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/network"
"os/signal"
"syscall"
)
type RunSingleCommand struct {
......
......@@ -20,70 +20,62 @@ func init() {
common.RegisterShell(&s)
}
func TestSingleRunnerSigquit(t *testing.T) {
assert := assert.New(t)
executorName := "test-sigquit"
type jobSimulation func(mock.Arguments)
// mocking the whole stack
e := common.MockExecutor{}
defer e.AssertExpectations(t)
p := common.MockExecutorProvider{}
defer p.AssertExpectations(t)
mockNetwork := common.MockNetwork{}
defer mockNetwork.AssertExpectations(t)
//Network
jobData := common.JobResponse{}
_, cancel := context.WithCancel(context.Background())
defer cancel()
jobTrace := common.Trace{Writer: ioutil.Discard, CancelFunc: cancel}
mockNetwork.On("RequestJob", mock.Anything).Return(&jobData, true)
mockNetwork.On("ProcessJob", mock.Anything, mock.Anything).Return(&jobTrace).Run(func(_ mock.Arguments) {
func TestSingleRunnerSigquit(t *testing.T) {
job := func(_ mock.Arguments) {
err := syscall.Kill(syscall.Getpid(), syscall.SIGQUIT)
assert.NoError(err)
assert.NoError(t, err)
// simulate some real work while while sigquit get handled
time.Sleep(time.Second)
})
}
// Create executor only once
p.On("Create").Return(&e).Once()
p.On("Acquire", mock.Anything).Return(&common.MockExecutorData{}, nil).Once()
p.On("Release", mock.Anything, mock.Anything).Return(nil).Once()
single, cleanup := mockingExecutionStack(t, "test-sigquit", 1, job)
defer cleanup()
// We run everything once
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
e.On("Finish", nil).Return().Once()
e.On("Cleanup").Return().Once()
single.Execute(nil)
}
// Run script successfully
e.On("Shell").Return(&common.ShellScriptInfo{Shell: "script-shell"})
e.On("Run", mock.Anything).Return(nil)
func TestSingleRunnerMaxBuilds(t *testing.T) {
maxBuilds := 7
common.RegisterExecutor(executorName, &p)
single, cleanup := mockingExecutionStack(t, "test-max-build", maxBuilds, nil)
defer cleanup()
single := newRunSingleCommand(executorName, &mockNetwork)
single.MaxBuilds = maxBuilds
single.Execute(nil)
}
func TestSingleRunnerMaxBuilds(t *testing.T) {
executorName := "test-max-build"
maxBuilds := 7
func newRunSingleCommand(executorName string, network common.Network) *RunSingleCommand {
return &RunSingleCommand{
network: network,
RunnerConfig: common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Executor: executorName,
},
RunnerCredentials: common.RunnerCredentials{
URL: "http://example.com",
Token: "_test_token_",
},
},
}
}
func mockingExecutionStack(t *testing.T, executorName string, maxBuilds int, job jobSimulation) (*RunSingleCommand, func()) {
// mocking the whole stack
e := common.MockExecutor{}
defer e.AssertExpectations(t)
p := common.MockExecutorProvider{}
defer p.AssertExpectations(t)
mockNetwork := common.MockNetwork{}
defer mockNetwork.AssertExpectations(t)
//Network
jobData := common.JobResponse{}
_, cancel := context.WithCancel(context.Background())
defer cancel()
jobTrace := common.Trace{Writer: ioutil.Discard, CancelFunc: cancel}
mockNetwork.On("RequestJob", mock.Anything).Return(&jobData, true).Times(maxBuilds)
mockNetwork.On("ProcessJob", mock.Anything, mock.Anything).Return(&jobTrace).Times(maxBuilds)
processJob := mockNetwork.On("ProcessJob", mock.Anything, mock.Anything).Return(&jobTrace).Times(maxBuilds)
if job != nil {
processJob.Run(job)
}
//ExecutorProvider
p.On("Create").Return(&e).Times(maxBuilds)
......@@ -101,22 +93,10 @@ func TestSingleRunnerMaxBuilds(t *testing.T) {
common.RegisterExecutor(executorName, &p)
single := newRunSingleCommand(executorName, &mockNetwork)
single.MaxBuilds = maxBuilds
single.Execute(nil)
}
func newRunSingleCommand(executorName string, network common.Network) *RunSingleCommand {
return &RunSingleCommand{
network: network,
RunnerConfig: common.RunnerConfig{
RunnerSettings: common.RunnerSettings{
Executor: executorName,
},
RunnerCredentials: common.RunnerCredentials{
URL: "http://example.com",
Token: "_test_token_",
},
},
return newRunSingleCommand(executorName, &mockNetwork), func() {
e.AssertExpectations(t)
p.AssertExpectations(t)
mockNetwork.AssertExpectations(t)
cancel()
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment