github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/exec/on_abort_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 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  		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  		onAbortStep = exec.OnAbort(step, hook)
    42  
    43  		stepOk = false
    44  		stepErr = nil
    45  	})
    46  
    47  	AfterEach(func() {
    48  		cancel()
    49  	})
    50  
    51  	JustBeforeEach(func() {
    52  		stepOk, stepErr = onAbortStep.Run(ctx, state)
    53  	})
    54  
    55  	Context("when the step is aborted", func() {
    56  		BeforeEach(func() {
    57  			step.RunReturns(false, context.Canceled)
    58  		})
    59  
    60  		It("runs the abort hook", func() {
    61  			Expect(stepErr).To(Equal(context.Canceled))
    62  			Expect(hook.RunCallCount()).To(Equal(1))
    63  		})
    64  	})
    65  
    66  	Context("when the step succeeds", func() {
    67  		BeforeEach(func() {
    68  			step.RunReturns(true, nil)
    69  		})
    70  
    71  		It("is successful", func() {
    72  			Expect(stepOk).To(BeTrue())
    73  		})
    74  
    75  		It("does not run the abort hook", func() {
    76  			Expect(hook.RunCallCount()).To(Equal(0))
    77  		})
    78  	})
    79  
    80  	Context("when the step fails", func() {
    81  		BeforeEach(func() {
    82  			step.RunReturns(false, nil)
    83  		})
    84  
    85  		It("is not successful", func() {
    86  			Expect(stepOk).ToNot(BeTrue())
    87  		})
    88  
    89  		It("does not run the abort hook", func() {
    90  			Expect(step.RunCallCount()).To(Equal(1))
    91  			Expect(hook.RunCallCount()).To(Equal(0))
    92  		})
    93  	})
    94  
    95  	Context("when the step errors", func() {
    96  		disaster := errors.New("disaster")
    97  
    98  		BeforeEach(func() {
    99  			step.RunReturns(false, disaster)
   100  		})
   101  
   102  		It("returns the error", func() {
   103  			Expect(stepErr).To(Equal(disaster))
   104  		})
   105  
   106  		It("does not run the abort hook", func() {
   107  			Expect(step.RunCallCount()).To(Equal(1))
   108  			Expect(hook.RunCallCount()).To(Equal(0))
   109  		})
   110  	})
   111  })