single_test.go 3.01 KB
Newer Older
1 2 3 4 5
package commands

import (
	"context"
	"io/ioutil"
6
	"os"
7 8 9 10 11 12
	"syscall"
	"testing"
	"time"

	"github.com/stretchr/testify/mock"

13
	"gitlab.com/gitlab-org/gitlab-runner/common"
14 15 16 17 18 19 20 21 22
)

func init() {
	s := common.MockShell{}
	s.On("GetName").Return("script-shell")
	s.On("GenerateScript", mock.Anything, mock.Anything).Return("script", nil)
	common.RegisterShell(&s)
}

23
type jobSimulation func(mock.Arguments)
24

25
func TestSingleRunnerSigquit(t *testing.T) {
26 27
	var sendQuitSignal func()

28
	job := func(_ mock.Arguments) {
29
		sendQuitSignal()
30 31
		// simulate some real work while while sigquit get handled
		time.Sleep(time.Second)
32
	}
33

34 35
	single, cleanup := mockingExecutionStack(t, "test-sigquit", 1, job)
	defer cleanup()
36

37 38 39 40
	sendQuitSignal = func() {
		single.interruptSignals <- syscall.SIGQUIT
	}

41 42
	single.Execute(nil)
}
43

44 45
func TestSingleRunnerMaxBuilds(t *testing.T) {
	maxBuilds := 7
46

47 48
	single, cleanup := mockingExecutionStack(t, "test-max-build", maxBuilds, nil)
	defer cleanup()
49 50 51 52

	single.Execute(nil)
}

53 54 55 56 57 58 59 60 61 62 63 64
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_",
			},
		},
65
		interruptSignals: make(chan os.Signal),
66 67
	}
}
68

69
func mockingExecutionStack(t *testing.T, executorName string, maxBuilds int, job jobSimulation) (*RunSingleCommand, func()) {
70 71 72 73 74 75 76 77 78
	// mocking the whole stack
	e := common.MockExecutor{}
	p := common.MockExecutorProvider{}
	mockNetwork := common.MockNetwork{}

	//Network
	jobData := common.JobResponse{}
	_, cancel := context.WithCancel(context.Background())
	jobTrace := common.Trace{Writer: ioutil.Discard, CancelFunc: cancel}
79
	mockNetwork.On("RequestJob", mock.Anything, mock.Anything).Return(&jobData, true).Times(maxBuilds)
80 81 82 83
	processJob := mockNetwork.On("ProcessJob", mock.Anything, mock.Anything).Return(&jobTrace).Times(maxBuilds)
	if job != nil {
		processJob.Run(job)
	}
84 85

	//ExecutorProvider
Kamil Trzciński's avatar
Kamil Trzciński committed
86 87 88 89
	p.On("CanCreate").Return(true).Once()
	p.On("GetDefaultShell").Return("bash").Once()
	p.On("GetFeatures", mock.Anything).Return(nil).Times(maxBuilds + 1)

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	p.On("Create").Return(&e).Times(maxBuilds)
	p.On("Acquire", mock.Anything).Return(&common.MockExecutorData{}, nil).Times(maxBuilds)
	p.On("Release", mock.Anything, mock.Anything).Return(nil).Times(maxBuilds)

	//Executor
	e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil).Times(maxBuilds)
	e.On("Finish", nil).Return().Times(maxBuilds)
	e.On("Cleanup").Return().Times(maxBuilds)

	// Run script successfully
	e.On("Shell").Return(&common.ShellScriptInfo{Shell: "script-shell"})
	e.On("Run", mock.Anything).Return(nil)

	common.RegisterExecutor(executorName, &p)

105 106 107
	single := newRunSingleCommand(executorName, &mockNetwork)
	single.MaxBuilds = maxBuilds
	cleanup := func() {
108 109 110 111
		e.AssertExpectations(t)
		p.AssertExpectations(t)
		mockNetwork.AssertExpectations(t)
		cancel()
112
	}
113 114

	return single, cleanup
115
}