Skip to content
Snippets Groups Projects
Unverified Commit b8b4b4cc authored by Kamil Trzciński's avatar Kamil Trzciński :speech_balloon:
Browse files

Fix context canceling and returned message to upstream

parent fbfc08bd
No related branches found
No related tags found
1 merge request!559Use contexes everywhere
Pipeline #
Showing
with 164 additions and 116 deletions
package common
import (
"context"
"errors"
"fmt"
"net/url"
......@@ -9,7 +10,6 @@ import (
"strconv"
"strings"
"time"
"context"
"github.com/Sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
......@@ -59,7 +59,6 @@ const (
type Build struct {
JobResponse `yaml:",inline"`
Trace JobTrace
SystemInterrupt chan os.Signal `json:"-" yaml:"-"`
RootDir string `json:"-" yaml:"-"`
BuildDir string `json:"-" yaml:"-"`
......@@ -250,6 +249,22 @@ func (b *Build) GetBuildTimeout() time.Duration {
return time.Duration(buildTimeout) * time.Second
}
func (b *Build) handleError(err error) error {
switch err {
case context.Canceled:
b.CurrentState = BuildRunRuntimeCanceled
return &BuildError{Inner: errors.New("canceled")}
case context.DeadlineExceeded:
b.CurrentState = BuildRunRuntimeTimedout
return &BuildError{Inner: fmt.Errorf("execution took longer than %v seconds", b.GetBuildTimeout())}
default:
b.CurrentState = BuildRunRuntimeFinished
return err
}
}
func (b *Build) run(ctx context.Context, executor Executor) (err error) {
b.CurrentState = BuildRunRuntimeRunning
......@@ -266,13 +281,8 @@ func (b *Build) run(ctx context.Context, executor Executor) (err error) {
// Wait for signals: cancel, timeout, abort or finish
b.Log().Debugln("Waiting for signals...")
select {
case <-b.Trace.Aborted():
err = &BuildError{Inner: errors.New("canceled")}
b.CurrentState = BuildRunRuntimeCanceled
case <-ctx.Done():
err = &BuildError{Inner: fmt.Errorf("execution took longer than %v seconds", b.GetBuildTimeout())}
b.CurrentState = BuildRunRuntimeTimedout
err = b.handleError(ctx.Err())
case signal := <-b.SystemInterrupt:
err = fmt.Errorf("aborted: %v", signal)
......@@ -311,6 +321,8 @@ func (b *Build) retryCreateExecutor(options ExecutorPrepareOptions, provider Exe
}
if _, ok := err.(*BuildError); ok {
break
} else if options.Context.Err() != nil {
return nil, b.handleError(options.Context.Err())
}
logger.SoftErrorln("Preparation failed:", err)
......@@ -357,16 +369,16 @@ func (b *Build) Run(globalConfig *Config, trace JobTrace) (err error) {
context, cancel := context.WithTimeout(context.Background(), b.GetBuildTimeout())
defer cancel()
trace.SetCancelFunc(cancel)
options := ExecutorPrepareOptions{
Config: b.Runner,
Build: b,
Trace: b.Trace,
Trace: trace,
User: globalConfig.User,
Context: context,
}
b.Trace = trace
provider := GetExecutor(b.Runner.Executor)
if provider == nil {
return errors.New("executor not found")
......
......@@ -21,6 +21,6 @@ const DefaultArtifactDownloadAttempts = 1
const DefaultRestoreCacheAttempts = 1
const KubernetesPollInterval = 3
const KubernetesPollTimeout = 180
const AfterScriptTimeout = 5 * time.Second
const AfterScriptTimeout = 5 * time.Minute
var PreparationRetryInterval = 3 * time.Second
......@@ -16,8 +16,8 @@ func (m *MockExecutor) Shell() *ShellScriptInfo {
return r0
}
func (m *MockExecutor) Prepare(globalConfig *Config, config *RunnerConfig, build *Build) error {
ret := m.Called(globalConfig, config, build)
func (m *MockExecutor) Prepare(options ExecutorPrepareOptions) error {
ret := m.Called(options)
r0 := ret.Error(0)
......@@ -36,18 +36,13 @@ func (m *MockExecutor) Finish(err error) {
func (m *MockExecutor) Cleanup() {
m.Called()
}
func (m *MockExecutor) GetCurrentStage() ExecutorStage {
ret := m.Called()
var r0 ExecutorStage
if ret.Get(0) != nil {
r0 = ret.Get(0).(ExecutorStage)
}
r0 := ret.Get(0).(ExecutorStage)
return r0
}
func (m *MockExecutor) SetCurrentStage(stage ExecutorStage) {
m.Called(stage)
}
......@@ -2,27 +2,22 @@ package common
import "github.com/stretchr/testify/mock"
type MockBuildTrace struct {
import "context"
type MockJobTrace struct {
mock.Mock
}
func (m *MockBuildTrace) Success() {
func (m *MockJobTrace) Success() {
m.Called()
}
func (m *MockBuildTrace) Fail(err error) {
func (m *MockJobTrace) 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 *MockJobTrace) SetCancelFunc(cancelFunc context.CancelFunc) {
m.Called(cancelFunc)
}
func (m *MockBuildTrace) IsStdout() bool {
func (m *MockJobTrace) IsStdout() bool {
ret := m.Called()
r0 := ret.Get(0).(bool)
......
......@@ -2,11 +2,11 @@ package common
import "github.com/stretchr/testify/mock"
type MockBuildTracePatch struct {
type MockJobTracePatch struct {
mock.Mock
}
func (m *MockBuildTracePatch) Patch() []byte {
func (m *MockJobTracePatch) Patch() []byte {
ret := m.Called()
var r0 []byte
......@@ -16,24 +16,24 @@ func (m *MockBuildTracePatch) Patch() []byte {
return r0
}
func (m *MockBuildTracePatch) Offset() int {
func (m *MockJobTracePatch) Offset() int {
ret := m.Called()
r0 := ret.Get(0).(int)
return r0
}
func (m *MockBuildTracePatch) Limit() int {
func (m *MockJobTracePatch) Limit() int {
ret := m.Called()
r0 := ret.Get(0).(int)
return r0
}
func (m *MockBuildTracePatch) SetNewOffset(newOffset int) {
func (m *MockJobTracePatch) SetNewOffset(newOffset int) {
m.Called(newOffset)
}
func (m *MockBuildTracePatch) ValidateRange() bool {
func (m *MockJobTracePatch) ValidateRange() bool {
ret := m.Called()
r0 := ret.Get(0).(bool)
......
......@@ -8,7 +8,7 @@ type MockNetwork struct {
mock.Mock
}
func (m *MockNetwork) RegisterRunner(config RunnerCredentials, description string, tags string, runUntagged, locked bool) *RegisterRunnerResponse {
func (m *MockNetwork) RegisterRunner(config RunnerCredentials, description string, tags string, runUntagged bool, locked bool) *RegisterRunnerResponse {
ret := m.Called(config, description, tags, runUntagged, locked)
var r0 *RegisterRunnerResponse
......@@ -50,8 +50,8 @@ func (m *MockNetwork) UpdateJob(config RunnerConfig, jobCredentials *JobCredenti
return r0
}
func (m *MockNetwork) PatchTrace(config RunnerConfig, buildCredentials *JobCredentials, tracePart JobTracePatch) UpdateState {
ret := m.Called(config, buildCredentials, tracePart)
func (m *MockNetwork) PatchTrace(config RunnerConfig, jobCredentials *JobCredentials, tracePart JobTracePatch) UpdateState {
ret := m.Called(config, jobCredentials, tracePart)
r0 := ret.Get(0).(UpdateState)
......@@ -78,8 +78,8 @@ func (m *MockNetwork) UploadArtifacts(config JobCredentials, artifactsFile strin
return r0
}
func (m *MockNetwork) ProcessJob(config RunnerConfig, jobCredentials *JobCredentials) JobTrace {
ret := m.Called(config, jobCredentials)
func (m *MockNetwork) ProcessJob(config RunnerConfig, buildCredentials *JobCredentials) JobTrace {
ret := m.Called(config, buildCredentials)
r0 := ret.Get(0).(JobTrace)
......
package common
import (
"context"
"io"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/url"
......@@ -246,7 +247,7 @@ type JobTrace interface {
io.Writer
Success()
Fail(err error)
Aborted() chan interface{}
SetCancelFunc(cancelFunc context.CancelFunc)
IsStdout() bool
}
......
package common
import (
"context"
"io"
"os"
)
type Trace struct {
Writer io.Writer
Abort chan interface{}
Writer io.Writer
CancelFunc context.CancelFunc
}
func (s *Trace) Write(p []byte) (n int, err error) {
......@@ -23,8 +24,8 @@ func (s *Trace) Success() {
func (s *Trace) Fail(err error) {
}
func (s *Trace) Aborted() chan interface{} {
return s.Abort
func (s *Trace) SetCancelFunc(cancelFunc context.CancelFunc) {
s.CancelFunc = cancelFunc
}
func (s *Trace) IsStdout() bool {
......
......@@ -863,7 +863,7 @@ func (s *executor) watchContainer(ctx context.Context, id string, input io.Reade
// Copy any output to the build trace
go func() {
_, err := stdcopy.StdCopy(s.BuildTrace, s.BuildTrace, hijacked.Reader)
_, err := stdcopy.StdCopy(s.Trace, s.Trace, hijacked.Reader)
if err != nil {
attachCh <- err
}
......@@ -1211,6 +1211,6 @@ func (s *executor) waitForServiceContainer(service *types.Container, timeout tim
buffer.WriteString("\n")
buffer.WriteString(helpers.ANSI_YELLOW + "*********" + helpers.ANSI_RESET + "\n")
buffer.WriteString("\n")
io.Copy(s.BuildTrace, &buffer)
io.Copy(s.Trace, &buffer)
return err
}
......@@ -168,11 +168,11 @@ func TestDockerCommandBuildCancel(t *testing.T) {
},
}
trace := &common.Trace{Writer: os.Stdout, Abort: make(chan interface{}, 1)}
trace := &common.Trace{Writer: os.Stdout}
abortTimer := time.AfterFunc(time.Second, func() {
t.Log("Interrupt")
trace.Abort <- true
trace.CancelFunc()
})
defer abortTimer.Stop()
......
......@@ -52,8 +52,8 @@ func (s *sshExecutor) Prepare(options common.ExecutorPrepareOptions) error {
// Create SSH command
s.sshCommand = ssh.Client{
Config: *s.Config.SSH,
Stdout: s.BuildTrace,
Stderr: s.BuildTrace,
Stdout: s.Trace,
Stderr: s.Trace,
}
s.sshCommand.Host = containerData.NetworkSettings.IPAddress
......
......@@ -2,6 +2,7 @@ package docker
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
......@@ -20,8 +21,6 @@ import (
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/docker"
"golang.org/x/net/context"
)
// ImagePullOptions contains the RegistryAuth which is inferred from the docker
......@@ -140,16 +139,17 @@ func testServiceFromNamedImage(t *testing.T, description, imageName, serviceName
}
e.Build.JobInfo.ProjectID = 0
e.Build.Runner.Token = "abcdef1234567890"
e.Context, _ = context.WithCancel(context.Background())
c.On("ImagePullBlocking", context.TODO(), imageName, options).
c.On("ImagePullBlocking", e.Context, imageName, options).
Return(nil).
Once()
c.On("ImageInspectWithRaw", context.TODO(), imageName).
c.On("ImageInspectWithRaw", e.Context, imageName).
Return(types.ImageInspect{}, nil, nil).
Twice()
c.On("ContainerRemove", context.TODO(), containerName, types.ContainerRemoveOptions{RemoveVolumes: true, Force: true}).
c.On("ContainerRemove", e.Context, containerName, types.ContainerRemoveOptions{RemoveVolumes: true, Force: true}).
Return(nil).
Once()
......@@ -157,11 +157,11 @@ func testServiceFromNamedImage(t *testing.T, description, imageName, serviceName
"1": {Name: containerName},
}
c.On("NetworkList", context.TODO(), types.NetworkListOptions{}).
c.On("NetworkList", e.Context, types.NetworkListOptions{}).
Return([]types.NetworkResource{{ID: networkID, Name: "network-name", Containers: networkContainersMap}}, nil).
Once()
c.On("NetworkDisconnect", context.TODO(), networkID, containerName, true).
c.On("NetworkDisconnect", e.Context, networkID, containerName, true).
Return(nil).
Once()
......@@ -169,7 +169,7 @@ func testServiceFromNamedImage(t *testing.T, description, imageName, serviceName
Return(container.ContainerCreateCreatedBody{ID: containerName}, nil).
Once()
c.On("ContainerStart", context.TODO(), mock.Anything, mock.Anything).
c.On("ContainerStart", e.Context, mock.Anything, mock.Anything).
Return(nil).
Once()
......@@ -192,17 +192,18 @@ func TestDockerForNamedImage(t *testing.T) {
validSHA := "real@sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"
e := executor{client: &c}
e.Context, _ = context.WithCancel(context.Background())
options := buildImagePullOptions(e, "test")
c.On("ImagePullBlocking", context.TODO(), "test:latest", options).
c.On("ImagePullBlocking", e.Context, "test:latest", options).
Return(os.ErrNotExist).
Once()
c.On("ImagePullBlocking", context.TODO(), "tagged:tag", options).
c.On("ImagePullBlocking", e.Context, "tagged:tag", options).
Return(os.ErrNotExist).
Once()
c.On("ImagePullBlocking", context.TODO(), validSHA, options).
c.On("ImagePullBlocking", e.Context, validSHA, options).
Return(os.ErrNotExist).
Once()
......@@ -224,13 +225,14 @@ func TestDockerForExistingImage(t *testing.T) {
defer c.AssertExpectations(t)
e := executor{client: &c}
e.Context, _ = context.WithCancel(context.Background())
options := buildImagePullOptions(e, "existing")
c.On("ImagePullBlocking", context.TODO(), "existing:latest", options).
c.On("ImagePullBlocking", e.Context, "existing:latest", options).
Return(nil).
Once()
c.On("ImageInspectWithRaw", context.TODO(), "existing").
c.On("ImageInspectWithRaw", e.Context, "existing").
Return(types.ImageInspect{}, nil, nil).
Once()
......@@ -253,14 +255,15 @@ func TestDockerGetImageById(t *testing.T) {
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
c.On("ImageInspectWithRaw", context.TODO(), "ID").
Return(types.ImageInspect{ID: "ID"}, nil, nil).
Once()
// Use default policy
e := executor{client: &c}
e.Context, _ = context.WithCancel(context.Background())
e.setPolicyMode("")
c.On("ImageInspectWithRaw", e.Context, "ID").
Return(types.ImageInspect{ID: "ID"}, nil, nil).
Once()
image, err := e.getDockerImage("ID")
assert.NoError(t, err)
assert.NotNil(t, image)
......@@ -272,6 +275,7 @@ func TestDockerUnknownPolicyMode(t *testing.T) {
defer c.AssertExpectations(t)
e := executor{client: &c}
e.Context, _ = context.WithCancel(context.Background())
e.setPolicyMode("unknown")
_, err := e.getDockerImage("not-existing")
......@@ -282,17 +286,18 @@ func TestDockerPolicyModeNever(t *testing.T) {
var c docker_helpers.MockClient
defer c.AssertExpectations(t)
c.On("ImageInspectWithRaw", context.TODO(), "existing").
e := executor{client: &c}
e.Context, _ = context.WithCancel(context.Background())
e.setPolicyMode(common.PullPolicyNever)
c.On("ImageInspectWithRaw", e.Context, "existing").
Return(types.ImageInspect{ID: "existing"}, nil, nil).
Once()
c.On("ImageInspectWithRaw", context.TODO(), "not-existing").
c.On("ImageInspectWithRaw", e.Context, "not-existing").
Return(types.ImageInspect{}, nil, os.ErrNotExist).
Once()
e := executor{client: &c}
e.setPolicyMode(common.PullPolicyNever)
image, err := e.getDockerImage("existing")
assert.NoError(t, err)
assert.Equal(t, "existing", image.ID)
......@@ -306,9 +311,10 @@ func TestDockerPolicyModeIfNotPresentForExistingImage(t *testing.T) {
defer c.AssertExpectations(t)
e := executor{client: &c}
e.Context, _ = context.WithCancel(context.Background())
e.setPolicyMode(common.PullPolicyIfNotPresent)
c.On("ImageInspectWithRaw", context.TODO(), "existing").
c.On("ImageInspectWithRaw", e.Context, "existing").
Return(types.ImageInspect{}, nil, nil).
Once()
......@@ -322,18 +328,19 @@ func TestDockerPolicyModeIfNotPresentForNotExistingImage(t *testing.T) {
defer c.AssertExpectations(t)
e := executor{client: &c}
e.Context, _ = context.WithCancel(context.Background())
e.setPolicyMode(common.PullPolicyIfNotPresent)
c.On("ImageInspectWithRaw", context.TODO(), "not-existing").
c.On("ImageInspectWithRaw", e.Context, "not-existing").
Return(types.ImageInspect{}, nil, os.ErrNotExist).
Once()
options := buildImagePullOptions(e, "not-existing")
c.On("ImagePullBlocking", context.TODO(), "not-existing:latest", options).
c.On("ImagePullBlocking", e.Context, "not-existing:latest", options).
Return(nil).
Once()
c.On("ImageInspectWithRaw", context.TODO(), "not-existing").
c.On("ImageInspectWithRaw", e.Context, "not-existing").
Return(types.ImageInspect{}, nil, nil).
Once()
......@@ -341,7 +348,7 @@ func TestDockerPolicyModeIfNotPresentForNotExistingImage(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, image)
c.On("ImageInspectWithRaw", context.TODO(), "not-existing").
c.On("ImageInspectWithRaw", e.Context, "not-existing").
Return(types.ImageInspect{}, nil, nil).
Once()
......@@ -356,18 +363,19 @@ func TestDockerPolicyModeAlwaysForExistingImage(t *testing.T) {
defer c.AssertExpectations(t)
e := executor{client: &c}
e.Context, _ = context.WithCancel(context.Background())
e.setPolicyMode(common.PullPolicyAlways)
c.On("ImageInspectWithRaw", context.TODO(), "existing").
c.On("ImageInspectWithRaw", e.Context, "existing").
Return(types.ImageInspect{}, nil, nil).
Once()
options := buildImagePullOptions(e, "existing:latest")
c.On("ImagePullBlocking", context.TODO(), "existing:latest", options).
c.On("ImagePullBlocking", e.Context, "existing:latest", options).
Return(nil).
Once()
c.On("ImageInspectWithRaw", context.TODO(), "existing").
c.On("ImageInspectWithRaw", e.Context, "existing").
Return(types.ImageInspect{}, nil, nil).
Once()
......@@ -381,14 +389,15 @@ func TestDockerPolicyModeAlwaysForLocalOnlyImage(t *testing.T) {
defer c.AssertExpectations(t)
e := executor{client: &c}
e.Context, _ = context.WithCancel(context.Background())
e.setPolicyMode(common.PullPolicyAlways)
c.On("ImageInspectWithRaw", context.TODO(), "existing").
c.On("ImageInspectWithRaw", e.Context, "existing").
Return(types.ImageInspect{}, nil, nil).
Once()
options := buildImagePullOptions(e, "existing:lastest")
c.On("ImagePullBlocking", context.TODO(), "existing:latest", options).
c.On("ImagePullBlocking", e.Context, "existing:latest", options).
Return(fmt.Errorf("not found")).
Once()
......@@ -402,14 +411,15 @@ func TestDockerGetExistingDockerImageIfPullFails(t *testing.T) {
defer c.AssertExpectations(t)
e := executor{client: &c}
e.Context, _ = context.WithCancel(context.Background())
e.setPolicyMode(common.PullPolicyAlways)
c.On("ImageInspectWithRaw", context.TODO(), "to-pull").
c.On("ImageInspectWithRaw", e.Context, "to-pull").
Return(types.ImageInspect{}, nil, nil).
Once()
options := buildImagePullOptions(e, "to-pull")
c.On("ImagePullBlocking", context.TODO(), "to-pull:latest", options).
c.On("ImagePullBlocking", e.Context, "to-pull:latest", options).
Return(os.ErrNotExist).
Once()
......@@ -417,11 +427,11 @@ func TestDockerGetExistingDockerImageIfPullFails(t *testing.T) {
assert.Error(t, err)
assert.Nil(t, image, "Forces to authorize pulling")
c.On("ImageInspectWithRaw", context.TODO(), "not-existing").
c.On("ImageInspectWithRaw", e.Context, "not-existing").
Return(types.ImageInspect{}, nil, os.ErrNotExist).
Once()
c.On("ImagePullBlocking", context.TODO(), "not-existing:latest", options).
c.On("ImagePullBlocking", e.Context, "not-existing:latest", options).
Return(os.ErrNotExist).
Once()
......@@ -700,31 +710,31 @@ func testDeniesDockerImage(t *testing.T, e executor, imageName string, setClient
}
func addFindsLocalImageExpectations(c *docker_helpers.MockClient, imageName string) {
c.On("ImageInspectWithRaw", context.TODO(), imageName).
c.On("ImageInspectWithRaw", mock.Anything, imageName).
Return(types.ImageInspect{ID: "this-image"}, nil, nil).
Once()
}
func addPullsRemoteImageExpectations(c *docker_helpers.MockClient, imageName string) {
c.On("ImageInspectWithRaw", context.TODO(), imageName).
c.On("ImageInspectWithRaw", mock.Anything, imageName).
Return(types.ImageInspect{ID: "not-this-image"}, nil, nil).
Once()
c.On("ImagePullBlocking", context.TODO(), imageName, mock.AnythingOfType("types.ImagePullOptions")).
c.On("ImagePullBlocking", mock.Anything, imageName, mock.AnythingOfType("types.ImagePullOptions")).
Return(nil).
Once()
c.On("ImageInspectWithRaw", context.TODO(), imageName).
c.On("ImageInspectWithRaw", mock.Anything, imageName).
Return(types.ImageInspect{ID: "this-image"}, nil, nil).
Once()
}
func addDeniesPullExpectations(c *docker_helpers.MockClient, imageName string) {
c.On("ImageInspectWithRaw", context.TODO(), imageName).
c.On("ImageInspectWithRaw", mock.Anything, imageName).
Return(types.ImageInspect{ID: "image"}, nil, nil).
Once()
c.On("ImagePullBlocking", context.TODO(), imageName, mock.AnythingOfType("types.ImagePullOptions")).
c.On("ImagePullBlocking", mock.Anything, imageName, mock.AnythingOfType("types.ImagePullOptions")).
Return(fmt.Errorf("deny pulling")).
Once()
}
......@@ -734,6 +744,7 @@ func TestPullPolicyWhenAlwaysIsSet(t *testing.T) {
gitlabImage := "registry.gitlab.tld:1234/image/name:version"
e := getAuthConfigTestExecutor(t, false)
e.Context, _ = context.WithCancel(context.Background())
e.Config.Docker.PullPolicy = common.PullPolicyAlways
testGetDockerImage(t, e, remoteImage, addPullsRemoteImageExpectations)
......@@ -748,6 +759,7 @@ func TestPullPolicyWhenIfNotPresentIsSet(t *testing.T) {
gitlabImage := "registry.gitlab.tld:1234/image/name:version"
e := getAuthConfigTestExecutor(t, false)
e.Context, _ = context.WithCancel(context.Background())
e.Config.Docker.PullPolicy = common.PullPolicyIfNotPresent
testGetDockerImage(t, e, remoteImage, addFindsLocalImageExpectations)
......@@ -760,6 +772,7 @@ func TestDockerWatchOn_1_12_4(t *testing.T) {
}
e := executor{}
e.Context, _ = context.WithCancel(context.Background())
e.Build = &common.Build{
Runner: &common.RunnerConfig{},
}
......@@ -773,7 +786,7 @@ func TestDockerWatchOn_1_12_4(t *testing.T) {
PullPolicy: common.PullPolicyAlways,
}
e.BuildTrace = &common.Trace{Writer: os.Stdout}
e.Trace = &common.Trace{Writer: os.Stdout}
err := e.connectDocker()
assert.NoError(t, err)
......@@ -782,14 +795,13 @@ func TestDockerWatchOn_1_12_4(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, container)
abort := make(chan interface{})
input := bytes.NewBufferString("echo 'script'")
finished := make(chan bool, 1)
wg := &sync.WaitGroup{}
wg.Add(1) // Avoid a race where assert.NoError() is called too late in the goroutine
go func() {
err = e.watchContainer(container.ID, input, abort)
err = e.watchContainer(e.Context, container.ID, input)
assert.NoError(t, err)
t.Log(err)
finished <- true
......@@ -802,7 +814,7 @@ func TestDockerWatchOn_1_12_4(t *testing.T) {
t.Error("Container script not finished")
}
err = e.removeContainer(container.ID)
err = e.removeContainer(e.Context, container.ID)
assert.NoError(t, err)
wg.Wait()
}
......
......@@ -20,7 +20,7 @@ type AbstractExecutor struct {
common.BuildLogger
Config common.RunnerConfig
Build *common.Build
BuildTrace common.JobTrace
Trace common.JobTrace
BuildShell *common.ShellConfiguration
currentStage common.ExecutorStage
Context context.Context
......@@ -77,8 +77,8 @@ func (e *AbstractExecutor) Prepare(options common.ExecutorPrepareOptions) error
e.Context = options.Context
e.Config = *options.Config
e.Build = options.Build
e.BuildTrace = options.Trace
e.BuildLogger = common.NewBuildLogger(options.Build.Trace, options.Build.Log())
e.Trace = options.Trace
e.BuildLogger = common.NewBuildLogger(options.Trace, options.Build.Log())
err := e.startBuild()
if err != nil {
......
......@@ -296,7 +296,7 @@ func (s *executor) runInContainer(ctx context.Context, name, command string) <-c
go func() {
defer close(errc)
status, err := waitForPodRunning(ctx, s.kubeClient, s.pod, s.BuildTrace, s.Config.Kubernetes)
status, err := waitForPodRunning(ctx, s.kubeClient, s.pod, s.Trace, s.Config.Kubernetes)
if err != nil {
errc <- err
......@@ -321,8 +321,8 @@ func (s *executor) runInContainer(ctx context.Context, name, command string) <-c
ContainerName: name,
Command: s.BuildShell.DockerCommand,
In: strings.NewReader(command),
Out: s.BuildTrace,
Err: s.BuildTrace,
Out: s.Trace,
Err: s.Trace,
Stdin: true,
Config: config,
Client: s.kubeClient,
......
......@@ -2,6 +2,7 @@ package kubernetes
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
......@@ -146,7 +147,7 @@ func TestCleanup(t *testing.T) {
},
},
}
ex.AbstractExecutor.BuildTrace = buildTrace
ex.AbstractExecutor.Trace = buildTrace
ex.AbstractExecutor.BuildLogger = common.NewBuildLogger(buildTrace, logrus.WithFields(logrus.Fields{}))
ex.Cleanup()
if test.Error && !errored {
......@@ -822,11 +823,11 @@ func TestKubernetesBuildCancel(t *testing.T) {
}
build.Image.Name = "docker:git"
trace := &common.Trace{Writer: os.Stdout, Abort: make(chan interface{}, 1)}
trace := &common.Trace{Writer: os.Stdout}
abortTimer := time.AfterFunc(time.Second, func() {
t.Log("Interrupt")
trace.Abort <- true
trace.CancelFunc()
})
defer abortTimer.Stop()
......@@ -887,12 +888,10 @@ type FakeBuildTrace struct {
testWriter
}
func (f FakeBuildTrace) Success() {}
func (f FakeBuildTrace) Fail(error) {}
func (f FakeBuildTrace) Notify(func()) {}
func (f FakeBuildTrace) Aborted() chan interface{} {
return make(chan interface{})
}
func (f FakeBuildTrace) Success() {}
func (f FakeBuildTrace) Fail(error) {}
func (f FakeBuildTrace) Notify(func()) {}
func (f FakeBuildTrace) SetCancelFunc(cancelFunc context.CancelFunc) {}
func (f FakeBuildTrace) IsStdout() bool {
return false
}
......@@ -63,8 +63,8 @@ func (s *executor) verifyMachine(vmName string) error {
// Create SSH command
sshCommand := ssh.Client{
Config: *s.Config.SSH,
Stdout: s.BuildTrace,
Stderr: s.BuildTrace,
Stdout: s.Trace,
Stderr: s.Trace,
ConnectRetries: 30,
}
sshCommand.Host = ipAddr
......@@ -278,8 +278,8 @@ func (s *executor) Prepare(options common.ExecutorPrepareOptions) error {
s.Debugln("Starting SSH command...")
s.sshCommand = ssh.Client{
Config: *s.Config.SSH,
Stdout: s.BuildTrace,
Stderr: s.BuildTrace,
Stdout: s.Trace,
Stderr: s.Trace,
}
s.sshCommand.Host = ipAddr
......
package parallels_test
import (
"context"
"os"
"testing"
"time"
......@@ -187,11 +188,11 @@ func TestParallelsBuildCancel(t *testing.T) {
},
}
trace := &common.Trace{Writer: os.Stdout, Abort: make(chan interface{}, 1)}
trace := &common.Trace{Writer: os.Stdout}
abortTimer := time.AfterFunc(time.Second, func() {
t.Log("Interrupt")
trace.Abort <- true
trace.CancelFunc()
})
defer abortTimer.Stop()
......
......@@ -78,8 +78,8 @@ func (s *executor) Run(cmd common.ExecutorCommand) error {
// Fill process environment variables
c.Env = append(os.Environ(), s.BuildShell.Environment...)
c.Stdout = s.BuildTrace
c.Stderr = s.BuildTrace
c.Stdout = s.Trace
c.Stderr = s.Trace
if s.BuildShell.PassFile {
scriptDir, err := ioutil.TempDir("", "build_script")
......
......@@ -127,14 +127,15 @@ func TestBuildCancel(t *testing.T) {
build, cleanup := newBuild(t, longRunningBuild, shell)
defer cleanup()
cancelChan := make(chan interface{}, 1)
trace := &common.Trace{Writer: os.Stdout}
cancelTimer := time.AfterFunc(time.Second, func() {
t.Log("Cancel")
cancelChan <- true
trace.CancelFunc()
})
defer cancelTimer.Stop()
err = runBuildWithTrace(t, build, &common.Trace{Writer: os.Stdout, Abort: cancelChan})
err = runBuildWithTrace(t, build, trace)
assert.EqualError(t, err, "canceled")
assert.IsType(t, err, &common.BuildError{})
})
......
......@@ -33,8 +33,8 @@ func (s *executor) Prepare(options common.ExecutorPrepareOptions) error {
// Create SSH command
s.sshCommand = ssh.Client{
Config: *s.Config.SSH,
Stdout: s.BuildTrace,
Stderr: s.BuildTrace,
Stdout: s.Trace,
Stderr: s.Trace,
}
s.Debugln("Connecting to SSH server...")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment