1package exec_test
2
3import (
4	"context"
5	"errors"
6
7	"github.com/concourse/concourse/atc/exec"
8	"github.com/concourse/concourse/atc/exec/build"
9	"github.com/concourse/concourse/atc/exec/execfakes"
10	. "github.com/onsi/ginkgo"
11	. "github.com/onsi/gomega"
12)
13
14var _ = Describe("On Abort Step", func() {
15	var (
16		ctx    context.Context
17		cancel func()
18
19		step *execfakes.FakeStep
20		hook *execfakes.FakeStep
21
22		repo  *build.Repository
23		state *execfakes.FakeRunState
24
25		onAbortStep exec.Step
26
27		stepErr error
28	)
29
30	BeforeEach(func() {
31		ctx, cancel = context.WithCancel(context.Background())
32
33		step = &execfakes.FakeStep{}
34		hook = &execfakes.FakeStep{}
35
36		repo = build.NewRepository()
37		state = new(execfakes.FakeRunState)
38		state.ArtifactRepositoryReturns(repo)
39
40		onAbortStep = exec.OnAbort(step, hook)
41
42		stepErr = nil
43	})
44
45	AfterEach(func() {
46		cancel()
47	})
48
49	JustBeforeEach(func() {
50		stepErr = onAbortStep.Run(ctx, state)
51	})
52
53	Context("when the step is aborted", func() {
54		BeforeEach(func() {
55			step.RunReturns(context.Canceled)
56		})
57
58		It("runs the abort hook", func() {
59			Expect(stepErr).To(Equal(context.Canceled))
60			Expect(hook.RunCallCount()).To(Equal(1))
61		})
62	})
63
64	Context("when the step succeeds", func() {
65		BeforeEach(func() {
66			step.SucceededReturns(true)
67		})
68
69		It("is successful", func() {
70			Expect(onAbortStep.Succeeded()).To(BeTrue())
71		})
72
73		It("does not run the abort hook", func() {
74			Expect(hook.RunCallCount()).To(Equal(0))
75		})
76	})
77
78	Context("when the step fails", func() {
79		BeforeEach(func() {
80			step.SucceededReturns(false)
81		})
82
83		It("is not successful", func() {
84			Expect(onAbortStep.Succeeded()).ToNot(BeTrue())
85		})
86
87		It("does not run the abort hook", func() {
88			Expect(step.RunCallCount()).To(Equal(1))
89			Expect(hook.RunCallCount()).To(Equal(0))
90		})
91	})
92
93	Context("when the step errors", func() {
94		disaster := errors.New("disaster")
95
96		BeforeEach(func() {
97			step.RunReturns(disaster)
98		})
99
100		It("returns the error", func() {
101			Expect(stepErr).To(Equal(disaster))
102		})
103
104		It("does not run the abort hook", func() {
105			Expect(step.RunCallCount()).To(Equal(1))
106			Expect(hook.RunCallCount()).To(Equal(0))
107		})
108	})
109})
110