Commit b5616516 authored by Kamil Trzciński's avatar Kamil Trzciński

Added feature tests

parent 25ad4430
......@@ -195,12 +195,11 @@ dockerfiles:
mocks: FORCE
go get github.com/vektra/mockery/.../
rm -rf mocks/
mockery -dir=$(GOPATH)/src/github.com/ayufan/golang-kardianos-service -name=Interface
mockery -dir=./common -name=Network
mockery -dir=./common -name=Executor
mockery -dir=./common -name=ExecutorProvider
mockery -dir=./helpers/docker -name=Client
rm -rf ./mocks ./helpers/docker/mocks ./common/mocks ./helpers/service/mocks
find . -type f -name 'mock_*' -delete
mockery -dir=$(GOPATH)/src/github.com/ayufan/golang-kardianos-service -output=./helpers/service/mocks -name=Interface
mockery -dir=./common -all -inpkg
mockery -dir=./helpers/docker -all -inpkg
test-docker:
make test-docker-image IMAGE=centos:6 TYPE=rpm
......
......@@ -3,17 +3,18 @@ package helpers
import (
"archive/zip"
"bytes"
"github.com/Sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/mocks"
"io"
"os"
"github.com/Sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
)
const artifactsTestArchivedFile = "archive_file"
type testNetwork struct {
mocks.Network
common.MockNetwork
downloadState common.DownloadState
downloadCalled int
uploadState common.UploadState
......
package common
import (
"os"
"testing"
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func init() {
s := MockShell{}
s.On("GetName").Return("script-shell")
s.On("GenerateScript", mock.Anything, mock.Anything).Return("script", nil)
RegisterShell(&s)
}
func TestBuildRun(t *testing.T) {
e := MockExecutor{}
defer e.AssertExpectations(t)
p := MockExecutorProvider{}
defer p.AssertExpectations(t)
// Create executor only once
p.On("Create").Return(&e).Once()
// 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()
// Run script successfully
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
e.On("Run", mock.Anything).Return(nil)
RegisterExecutor("build-run-test", &p)
build := &Build{
GetBuildResponse: SuccessfulBuild,
Runner: &RunnerConfig{
RunnerSettings: RunnerSettings{
Executor: "build-run-test",
},
},
}
err := build.Run(&Config{}, &Trace{Writer: os.Stdout})
assert.NoError(t, err)
}
func TestRetryPrepare(t *testing.T) {
PreparationRetryInterval = 0
e := MockExecutor{}
defer e.AssertExpectations(t)
p := MockExecutorProvider{}
defer p.AssertExpectations(t)
// Create executor
p.On("Create").Return(&e).Times(3)
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
Return(errors.New("prepare failed")).Twice()
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
Return(nil).Once()
e.On("Cleanup").Return().Times(3)
// Succeed a build script
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
e.On("Run", mock.Anything).Return(nil)
e.On("Finish", nil).Return().Once()
RegisterExecutor("build-run-retry-prepare", &p)
build := &Build{
GetBuildResponse: SuccessfulBuild,
Runner: &RunnerConfig{
RunnerSettings: RunnerSettings{
Executor: "build-run-retry-prepare",
},
},
}
err := build.Run(&Config{}, &Trace{Writer: os.Stdout})
assert.NoError(t, err)
}
func TestPrepareFailure(t *testing.T) {
PreparationRetryInterval = 0
e := MockExecutor{}
defer e.AssertExpectations(t)
p := MockExecutorProvider{}
defer p.AssertExpectations(t)
// Create executor
p.On("Create").Return(&e).Times(3)
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
Return(errors.New("prepare failed")).Times(3)
e.On("Cleanup").Return().Times(3)
RegisterExecutor("build-run-prepare-failure", &p)
build := &Build{
GetBuildResponse: SuccessfulBuild,
Runner: &RunnerConfig{
RunnerSettings: RunnerSettings{
Executor: "build-run-prepare-failure",
},
},
}
err := build.Run(&Config{}, &Trace{Writer: os.Stdout})
assert.EqualError(t, err, "prepare failed")
}
func TestRunFailure(t *testing.T) {
e := MockExecutor{}
defer e.AssertExpectations(t)
p := MockExecutorProvider{}
defer p.AssertExpectations(t)
// Create executor
p.On("Create").Return(&e).Once()
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)
e.On("Cleanup").Return().Once()
// Fail a build script
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
e.On("Run", mock.Anything).Return(errors.New("build fail"))
e.On("Finish", errors.New("build fail")).Return().Once()
RegisterExecutor("build-run-run-failure", &p)
build := &Build{
GetBuildResponse: SuccessfulBuild,
Runner: &RunnerConfig{
RunnerSettings: RunnerSettings{
Executor: "build-run-run-failure",
},
},
}
err := build.Run(&Config{}, &Trace{Writer: os.Stdout})
assert.EqualError(t, err, "build fail")
}
......@@ -16,4 +16,5 @@ const ShutdownTimeout = 30
const DefaultOutputLimit = 4096 // 4MB in kilobytes
const ForceTraceSentInterval = 30 * time.Second
const PreparationRetries = 3
const PreparationRetryInterval = 3 * time.Second
var PreparationRetryInterval = 3 * time.Second
package common
import "github.com/stretchr/testify/mock"
type MockBuildTrace struct {
mock.Mock
}
func (m *MockBuildTrace) Success() {
m.Called()
}
func (m *MockBuildTrace) Fail(err error) {
m.Called(err)
}
func (m *MockBuildTrace) Aborted() chan interface{} {
ret := m.Called()
var r0 chan interface{}
if ret.Get(0) != nil {
r0 = ret.Get(0).(chan interface{})
}
return r0
}
func (m *MockBuildTrace) IsStdout() bool {
ret := m.Called()
r0 := ret.Get(0).(bool)
return r0
}
package common
import "github.com/stretchr/testify/mock"
type MockBuildTracePatch struct {
mock.Mock
}
func (m *MockBuildTracePatch) Patch() []byte {
ret := m.Called()
var r0 []byte
if ret.Get(0) != nil {
r0 = ret.Get(0).([]byte)
}
return r0
}
func (m *MockBuildTracePatch) Offset() int {
ret := m.Called()
r0 := ret.Get(0).(int)
return r0
}
func (m *MockBuildTracePatch) Limit() int {
ret := m.Called()
r0 := ret.Get(0).(int)
return r0
}
func (m *MockBuildTracePatch) SetNewOffset(newOffset int) {
m.Called(newOffset)
}
package common
import "github.com/stretchr/testify/mock"
import "github.com/codegangsta/cli"
type MockCommander struct {
mock.Mock
}
func (m *MockCommander) Execute(c *cli.Context) {
m.Called(c)
}
package mocks
package common
import "gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
import "github.com/stretchr/testify/mock"
type Executor struct {
type MockExecutor struct {
mock.Mock
}
func (m *Executor) Shell() *common.ShellScriptInfo {
func (m *MockExecutor) Shell() *ShellScriptInfo {
ret := m.Called()
var r0 *common.ShellScriptInfo
var r0 *ShellScriptInfo
if ret.Get(0) != nil {
r0 = ret.Get(0).(*common.ShellScriptInfo)
r0 = ret.Get(0).(*ShellScriptInfo)
}
return r0
}
func (m *Executor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
func (m *MockExecutor) Prepare(globalConfig *Config, config *RunnerConfig, build *Build) error {
ret := m.Called(globalConfig, config, build)
r0 := ret.Error(0)
return r0
}
func (m *Executor) Run(cmd common.ExecutorCommand) error {
func (m *MockExecutor) Run(cmd ExecutorCommand) error {
ret := m.Called(cmd)
r0 := ret.Error(0)
return r0
}
func (m *Executor) Finish(err error) {
func (m *MockExecutor) Finish(err error) {
m.Called(err)
}
func (m *Executor) Cleanup() {
func (m *MockExecutor) Cleanup() {
m.Called()
}
package common
import "github.com/stretchr/testify/mock"
type MockExecutorData struct {
mock.Mock
}
package mocks
package common
import "gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
import "github.com/stretchr/testify/mock"
type ExecutorProvider struct {
type MockExecutorProvider struct {
mock.Mock
}
func (m *ExecutorProvider) CanCreate() bool {
func (m *MockExecutorProvider) CanCreate() bool {
ret := m.Called()
r0 := ret.Get(0).(bool)
return r0
}
func (m *ExecutorProvider) Create() common.Executor {
func (m *MockExecutorProvider) Create() Executor {
ret := m.Called()
r0 := ret.Get(0).(common.Executor)
r0 := ret.Get(0).(Executor)
return r0
}
func (m *ExecutorProvider) Acquire(config *common.RunnerConfig) (common.ExecutorData, error) {
func (m *MockExecutorProvider) Acquire(config *RunnerConfig) (ExecutorData, error) {
ret := m.Called(config)
r0 := ret.Get(0).(common.ExecutorData)
r0 := ret.Get(0).(ExecutorData)
r1 := ret.Error(1)
return r0, r1
}
func (m *ExecutorProvider) Release(config *common.RunnerConfig, data common.ExecutorData) error {
func (m *MockExecutorProvider) Release(config *RunnerConfig, data ExecutorData) error {
ret := m.Called(config, data)
r0 := ret.Error(0)
return r0
}
func (m *ExecutorProvider) GetFeatures(features *common.FeaturesInfo) {
func (m *MockExecutorProvider) GetFeatures(features *FeaturesInfo) {
m.Called(features)
}
package mocks
package common
import "gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
import "github.com/stretchr/testify/mock"
import "io"
type Network struct {
type MockNetwork struct {
mock.Mock
}
func (m *Network) GetBuild(config common.RunnerConfig) (*common.GetBuildResponse, bool) {
func (m *MockNetwork) GetBuild(config RunnerConfig) (*GetBuildResponse, bool) {
ret := m.Called(config)
var r0 *common.GetBuildResponse
var r0 *GetBuildResponse
if ret.Get(0) != nil {
r0 = ret.Get(0).(*common.GetBuildResponse)
r0 = ret.Get(0).(*GetBuildResponse)
}
r1 := ret.Get(1).(bool)
return r0, r1
}
func (m *Network) RegisterRunner(config common.RunnerCredentials, description string, tags string) *common.RegisterRunnerResponse {
func (m *MockNetwork) RegisterRunner(config RunnerCredentials, description string, tags string) *RegisterRunnerResponse {
ret := m.Called(config, description, tags)
var r0 *common.RegisterRunnerResponse
var r0 *RegisterRunnerResponse
if ret.Get(0) != nil {
r0 = ret.Get(0).(*common.RegisterRunnerResponse)
r0 = ret.Get(0).(*RegisterRunnerResponse)
}
return r0
}
func (m *Network) DeleteRunner(config common.RunnerCredentials) bool {
func (m *MockNetwork) DeleteRunner(config RunnerCredentials) bool {
ret := m.Called(config)
r0 := ret.Get(0).(bool)
return r0
}
func (m *Network) VerifyRunner(config common.RunnerCredentials) bool {
func (m *MockNetwork) VerifyRunner(config RunnerCredentials) bool {
ret := m.Called(config)
r0 := ret.Get(0).(bool)
return r0
}
func (m *Network) UpdateBuild(config common.RunnerConfig, id int, state common.BuildState, trace *string) common.UpdateState {
func (m *MockNetwork) UpdateBuild(config RunnerConfig, id int, state BuildState, trace *string) UpdateState {
ret := m.Called(config, id, state, trace)
r0 := ret.Get(0).(common.UpdateState)
r0 := ret.Get(0).(UpdateState)
return r0
}
func (m *Network) PatchTrace(config common.RunnerConfig, buildCredentials *common.BuildCredentials, tracePart common.BuildTracePatch) common.UpdateState {
func (m *MockNetwork) PatchTrace(config RunnerConfig, buildCredentials *BuildCredentials, tracePart BuildTracePatch) UpdateState {
ret := m.Called(config, buildCredentials, tracePart)
r0 := ret.Get(0).(common.UpdateState)
r0 := ret.Get(0).(UpdateState)
return r0
}
func (m *Network) DownloadArtifacts(config common.BuildCredentials, artifactsFile string) common.DownloadState {
func (m *MockNetwork) DownloadArtifacts(config BuildCredentials, artifactsFile string) DownloadState {
ret := m.Called(config, artifactsFile)
r0 := ret.Get(0).(common.DownloadState)
r0 := ret.Get(0).(DownloadState)
return r0
}
func (m *Network) UploadRawArtifacts(config common.BuildCredentials, reader io.Reader, baseName string, expireIn string) common.UploadState {
func (m *MockNetwork) UploadRawArtifacts(config BuildCredentials, reader io.Reader, baseName string, expireIn string) UploadState {
ret := m.Called(config, reader, baseName, expireIn)
r0 := ret.Get(0).(common.UploadState)
r0 := ret.Get(0).(UploadState)
return r0
}
func (m *Network) UploadArtifacts(config common.BuildCredentials, artifactsFile string) common.UploadState {
func (m *MockNetwork) UploadArtifacts(config BuildCredentials, artifactsFile string) UploadState {
ret := m.Called(config, artifactsFile)
r0 := ret.Get(0).(common.UploadState)
r0 := ret.Get(0).(UploadState)
return r0
}
func (m *Network) ProcessBuild(config common.RunnerConfig, buildCredentials *common.BuildCredentials) common.BuildTrace {
func (m *MockNetwork) ProcessBuild(config RunnerConfig, buildCredentials *BuildCredentials) BuildTrace {
ret := m.Called(config, buildCredentials)
r0 := ret.Get(0).(common.BuildTrace)
r0 := ret.Get(0).(BuildTrace)
return r0
}
package common
import "github.com/stretchr/testify/mock"
type MockShell struct {
mock.Mock
}
func (m *MockShell) GetName() string {
ret := m.Called()
r0 := ret.Get(0).(string)
return r0
}
func (m *MockShell) GetSupportedOptions() []string {
ret := m.Called()
var r0 []string
if ret.Get(0) != nil {
r0 = ret.Get(0).([]string)
}
return r0
}
func (m *MockShell) GetFeatures(features *FeaturesInfo) {
m.Called(features)
}
func (m *MockShell) IsDefault() bool {
ret := m.Called()
r0 := ret.Get(0).(bool)
return r0
}
func (m *MockShell) GetConfiguration(info ShellScriptInfo) (*ShellConfiguration, error) {
ret := m.Called(info)
var r0 *ShellConfiguration
if ret.Get(0) != nil {
r0 = ret.Get(0).(*ShellConfiguration)
}
r1 := ret.Error(1)
return r0, r1
}
func (m *MockShell) GenerateScript(scriptType ShellScriptType, info ShellScriptInfo) (string, error) {
ret := m.Called(scriptType, info)
r0 := ret.Get(0).(string)
r1 := ret.Error(1)
return r0, r1
}
......@@ -6,8 +6,9 @@ import (
"github.com/fsouza/go-dockerclient"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/mocks"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/docker"
)
func TestParseDeviceStringOne(t *testing.T) {
......@@ -83,7 +84,7 @@ func TestSplitService(t *testing.T) {
}
func TestDockerForNamedImage(t *testing.T) {
var c mocks.Client
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
e := executor{client: &c}
......@@ -115,7 +116,7 @@ func TestDockerForNamedImage(t *testing.T) {
}
func TestDockerForExistingImage(t *testing.T) {
var c mocks.Client
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
e := executor{client: &c}
......@@ -144,7 +145,7 @@ func (e *executor) setPolicyMode(pullPolicy common.DockerPullPolicy) {
}
func TestDockerGetImageById(t *testing.T) {
var c mocks.Client
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
c.On("InspectImage", "ID").
......@@ -162,7 +163,7 @@ func TestDockerGetImageById(t *testing.T) {
}
func TestDockerUnknownPolicyMode(t *testing.T) {
var c mocks.Client
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
e := executor{client: &c}
......@@ -173,7 +174,7 @@ func TestDockerUnknownPolicyMode(t *testing.T) {
}
func TestDockerPolicyModeNever(t *testing.T) {
var c mocks.Client
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
c.On("InspectImage", "existing").
......@@ -197,7 +198,7 @@ func TestDockerPolicyModeNever(t *testing.T) {
}
func TestDockerPolicyModeIfNotPresentForExistingImage(t *testing.T) {
var c mocks.Client
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
e := executor{client: &c}
......@@ -213,7 +214,7 @@ func TestDockerPolicyModeIfNotPresentForExistingImage(t *testing.T) {
}
func TestDockerPolicyModeIfNotPresentForNotExistingImage(t *testing.T) {
var c mocks.Client
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
e := executor{client: &c}
......@@ -247,7 +248,7 @@ func TestDockerPolicyModeIfNotPresentForNotExistingImage(t *testing.T) {
}
func TestDockerPolicyModeAlwaysForExistingImage(t *testing.T) {
var c mocks.Client
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
e := executor{client: &c}
......@@ -272,7 +273,7 @@ func TestDockerPolicyModeAlwaysForExistingImage(t *testing.T) {
}
func TestDockerGetExistingDockerImageIfPullFails(t *testing.T) {
var c mocks.Client
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
e := executor{client: &c}
......
package mocks
package docker_helpers
import "github.com/stretchr/testify/mock"
import "github.com/fsouza/go-dockerclient"
type Client struct {
type MockClient struct {
mock.Mock
}
func (m *Client) InspectImage(name string) (*docker.Image, error) {
func (m *MockClient) InspectImage(name string) (*docker.Image, error) {
ret := m.Called(name)
var r0 *docker.Image
......@@ -19,21 +19,21 @@ func (m *Client) InspectImage(name string) (*docker.Image, error) {
return r0, r1
}
func (m *Client) PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error {
func (m *MockClient) PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error {
ret := m.Called(opts, auth)
r0 := ret.Error(0)
return r0
}
func (m *Client) ImportImage(opts docker.ImportImageOptions) error {
func (m *MockClient) ImportImage(opts docker.ImportImageOptions) error {
ret := m.Called(opts)
r0 := ret.Error(0)
return r0
}
func (m *Client) CreateContainer(opts docker.CreateContainerOptions) (*docker.Container, error) {
func (m *MockClient) CreateContainer(opts docker.CreateContainerOptions) (*docker.Container, error) {
ret := m.Called(opts)
var r0 *docker.Container
......@@ -44,14 +44,14 @@ func (m *Client) CreateContainer(opts docker.CreateContainerOptions) (*docker.Co
return r0, r1
}
func (m *Client) StartContainer(id string, hostConfig *docker.HostConfig) error {
func (m *MockClient) StartContainer(id string, hostConfig *docker.HostConfig) error {
ret := m.Called(id, hostConfig)
r0 := ret.Error(0)
return r0
}
func (m *Client) WaitContainer(id string) (int, error) {
func (m *MockClient) WaitContainer(id string) (int, error) {
ret := m.Called(id)
r0 := ret.Get(0).(int)
......@@ -59,14 +59,14 @@ func (m *Client) WaitContainer(id string) (int, error) {
return r0, r1