github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/exec/on_failure_test.go (about) 1 package exec_test 2 3 import ( 4 "context" 5 "errors" 6 7 "github.com/pf-qiu/concourse/v6/atc/exec" 8 "github.com/pf-qiu/concourse/v6/atc/exec/build" 9 "github.com/pf-qiu/concourse/v6/atc/exec/execfakes" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("On Failure 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 onFailureStep exec.Step 26 27 stepOk bool 28 stepErr error 29 ) 30 31 BeforeEach(func() { 32 ctx, cancel = context.WithCancel(context.Background()) 33 34 step = &execfakes.FakeStep{} 35 hook = &execfakes.FakeStep{} 36 37 repo = build.NewRepository() 38 state = new(execfakes.FakeRunState) 39 state.ArtifactRepositoryReturns(repo) 40 41 onFailureStep = exec.OnFailure(step, hook) 42 }) 43 44 AfterEach(func() { 45 cancel() 46 }) 47 48 JustBeforeEach(func() { 49 stepOk, stepErr = onFailureStep.Run(ctx, state) 50 }) 51 52 Context("when the step fails", func() { 53 BeforeEach(func() { 54 step.RunReturns(false, nil) 55 }) 56 57 It("runs the failure hook", func() { 58 Expect(step.RunCallCount()).To(Equal(1)) 59 Expect(hook.RunCallCount()).To(Equal(1)) 60 }) 61 62 It("runs the hook with the run state", func() { 63 Expect(hook.RunCallCount()).To(Equal(1)) 64 65 _, argsState := hook.RunArgsForCall(0) 66 Expect(argsState).To(Equal(state)) 67 }) 68 69 It("propagates the context to the hook", func() { 70 runCtx, _ := hook.RunArgsForCall(0) 71 Expect(runCtx).To(Equal(ctx)) 72 }) 73 74 It("does not error", func() { 75 Expect(stepErr).ToNot(HaveOccurred()) 76 }) 77 }) 78 79 Context("when the step errors", func() { 80 disaster := errors.New("disaster") 81 82 BeforeEach(func() { 83 step.RunReturns(false, disaster) 84 }) 85 86 It("does not run the failure hook", func() { 87 Expect(step.RunCallCount()).To(Equal(1)) 88 Expect(hook.RunCallCount()).To(Equal(0)) 89 }) 90 91 It("returns the error", func() { 92 Expect(stepErr).To(Equal(disaster)) 93 }) 94 }) 95 96 Context("when the step succeeds", func() { 97 BeforeEach(func() { 98 step.RunReturns(true, nil) 99 }) 100 101 It("does not run the failure hook", func() { 102 Expect(step.RunCallCount()).To(Equal(1)) 103 Expect(hook.RunCallCount()).To(Equal(0)) 104 }) 105 106 It("returns nil", func() { 107 Expect(stepErr).To(BeNil()) 108 }) 109 }) 110 111 It("propagates the context to the step", func() { 112 runCtx, _ := step.RunArgsForCall(0) 113 Expect(runCtx).To(Equal(ctx)) 114 }) 115 116 Context("when step fails and hook fails", func() { 117 BeforeEach(func() { 118 step.RunReturns(false, nil) 119 hook.RunReturns(false, nil) 120 }) 121 122 It("fails", func() { 123 Expect(stepOk).To(BeFalse()) 124 }) 125 }) 126 127 Context("when step fails and hook succeeds", func() { 128 BeforeEach(func() { 129 step.RunReturns(false, nil) 130 hook.RunReturns(true, nil) 131 }) 132 133 It("fails", func() { 134 Expect(stepOk).To(BeFalse()) 135 }) 136 }) 137 138 Context("when step succeeds", func() { 139 BeforeEach(func() { 140 step.RunReturns(true, nil) 141 }) 142 143 It("succeeds", func() { 144 Expect(stepOk).To(BeTrue()) 145 }) 146 }) 147 })