github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/exec/on_success_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 Success 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  		onSuccessStep 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  		onSuccessStep = exec.OnSuccess(step, hook)
    42  
    43  		stepOk = false
    44  		stepErr = nil
    45  	})
    46  
    47  	JustBeforeEach(func() {
    48  		stepOk, stepErr = onSuccessStep.Run(ctx, state)
    49  	})
    50  
    51  	AfterEach(func() {
    52  		cancel()
    53  	})
    54  
    55  	Context("when the step succeeds", func() {
    56  		BeforeEach(func() {
    57  			step.RunReturns(true, nil)
    58  		})
    59  
    60  		It("runs the hook", func() {
    61  			Expect(step.RunCallCount()).To(Equal(1))
    62  			Expect(hook.RunCallCount()).To(Equal(1))
    63  		})
    64  
    65  		It("runs the hook with the run state", func() {
    66  			Expect(hook.RunCallCount()).To(Equal(1))
    67  
    68  			_, argsState := hook.RunArgsForCall(0)
    69  			Expect(argsState).To(Equal(state))
    70  		})
    71  
    72  		It("propagates the context to the hook", func() {
    73  			runCtx, _ := hook.RunArgsForCall(0)
    74  			Expect(runCtx).To(Equal(ctx))
    75  		})
    76  
    77  		It("returns nil", func() {
    78  			Expect(stepErr).ToNot(HaveOccurred())
    79  		})
    80  	})
    81  
    82  	Context("when the step errors", func() {
    83  		disaster := errors.New("disaster")
    84  
    85  		BeforeEach(func() {
    86  			step.RunReturns(false, disaster)
    87  		})
    88  
    89  		It("does not run the hook", func() {
    90  			Expect(step.RunCallCount()).To(Equal(1))
    91  			Expect(hook.RunCallCount()).To(Equal(0))
    92  		})
    93  
    94  		It("returns the error", func() {
    95  			Expect(stepErr).To(Equal(disaster))
    96  		})
    97  	})
    98  
    99  	Context("when the step fails", func() {
   100  		BeforeEach(func() {
   101  			step.RunReturns(false, nil)
   102  		})
   103  
   104  		It("does not run the hook", func() {
   105  			Expect(step.RunCallCount()).To(Equal(1))
   106  			Expect(hook.RunCallCount()).To(Equal(0))
   107  		})
   108  
   109  		It("returns nil", func() {
   110  			Expect(stepErr).To(BeNil())
   111  		})
   112  	})
   113  
   114  	It("propagates the context to the step", func() {
   115  		runCtx, _ := step.RunArgsForCall(0)
   116  		Expect(runCtx).To(Equal(ctx))
   117  	})
   118  
   119  	Context("when step fails", func() {
   120  		BeforeEach(func() {
   121  			step.RunReturns(false, nil)
   122  		})
   123  
   124  		It("returns false", func() {
   125  			Expect(stepOk).To(BeFalse())
   126  		})
   127  	})
   128  
   129  	Context("when step succeeds and hook succeeds", func() {
   130  		BeforeEach(func() {
   131  			step.RunReturns(true, nil)
   132  			hook.RunReturns(true, nil)
   133  		})
   134  
   135  		It("returns true", func() {
   136  			Expect(stepOk).To(BeTrue())
   137  		})
   138  	})
   139  
   140  	Context("when step succeeds and hook fails", func() {
   141  		BeforeEach(func() {
   142  			step.RunReturns(false, nil)
   143  			hook.RunReturns(false, nil)
   144  		})
   145  
   146  		It("returns false", func() {
   147  			Expect(stepOk).To(BeFalse())
   148  		})
   149  	})
   150  })