github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/exec/log_error_step_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("LogErrorStep", func() { 15 var ( 16 ctx context.Context 17 cancel func() 18 19 fakeStep *execfakes.FakeStep 20 21 fakeDelegate *execfakes.FakeBuildStepDelegate 22 fakeDelegateFactory *execfakes.FakeBuildStepDelegateFactory 23 24 repo *build.Repository 25 state *execfakes.FakeRunState 26 27 step Step 28 ) 29 30 BeforeEach(func() { 31 ctx, cancel = context.WithCancel(context.Background()) 32 33 fakeStep = new(execfakes.FakeStep) 34 fakeDelegate = new(execfakes.FakeBuildStepDelegate) 35 fakeDelegateFactory = new(execfakes.FakeBuildStepDelegateFactory) 36 fakeDelegateFactory.BuildStepDelegateReturns(fakeDelegate) 37 38 repo = build.NewRepository() 39 state = new(execfakes.FakeRunState) 40 state.ArtifactRepositoryReturns(repo) 41 42 step = LogError(fakeStep, fakeDelegateFactory) 43 }) 44 45 AfterEach(func() { 46 cancel() 47 }) 48 49 Describe("Run", func() { 50 var runOk bool 51 var runErr error 52 53 JustBeforeEach(func() { 54 runOk, runErr = step.Run(ctx, state) 55 }) 56 57 Context("when the inner step does not error", func() { 58 BeforeEach(func() { 59 fakeStep.RunReturns(true, nil) 60 }) 61 62 It("returns true", func() { 63 Expect(runOk).Should(BeTrue()) 64 }) 65 66 It("returns nil", func() { 67 Expect(runErr).To(BeNil()) 68 }) 69 70 It("does not log", func() { 71 Expect(fakeDelegate.ErroredCallCount()).To(Equal(0)) 72 }) 73 }) 74 75 Context("when the inner step has failed", func() { 76 BeforeEach(func() { 77 fakeStep.RunReturns(false, nil) 78 }) 79 80 It("returns false", func() { 81 Expect(runOk).Should(BeFalse()) 82 }) 83 84 It("returns nil", func() { 85 Expect(runErr).To(BeNil()) 86 }) 87 88 It("does not log", func() { 89 Expect(fakeDelegate.ErroredCallCount()).To(Equal(0)) 90 }) 91 }) 92 93 Context("when aborted", func() { 94 BeforeEach(func() { 95 fakeStep.RunReturns(false, context.Canceled) 96 }) 97 98 It("propagates the error", func() { 99 Expect(runErr).To(Equal(context.Canceled)) 100 }) 101 102 It("logs 'interrupted'", func() { 103 Expect(fakeDelegate.ErroredCallCount()).To(Equal(1)) 104 _, message := fakeDelegate.ErroredArgsForCall(0) 105 Expect(message).To(Equal("interrupted")) 106 }) 107 }) 108 109 Context("when timed out", func() { 110 BeforeEach(func() { 111 fakeStep.RunReturns(false, context.DeadlineExceeded) 112 }) 113 114 It("propagates the error", func() { 115 Expect(runErr).To(Equal(context.DeadlineExceeded)) 116 }) 117 118 It("logs 'timeout exceeded'", func() { 119 Expect(fakeDelegate.ErroredCallCount()).To(Equal(1)) 120 _, message := fakeDelegate.ErroredArgsForCall(0) 121 Expect(message).To(Equal("timeout exceeded")) 122 }) 123 }) 124 125 Context("when the inner step returns any other error", func() { 126 disaster := errors.New("disaster") 127 128 BeforeEach(func() { 129 fakeStep.RunReturns(false, disaster) 130 }) 131 132 It("propagates the error", func() { 133 Expect(runErr).To(Equal(disaster)) 134 }) 135 136 It("logs the error", func() { 137 Expect(fakeDelegate.ErroredCallCount()).To(Equal(1)) 138 _, message := fakeDelegate.ErroredArgsForCall(0) 139 Expect(message).To(Equal("disaster")) 140 }) 141 }) 142 }) 143 })