github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/exec/try_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("Try Step", func() {
    15  	var (
    16  		ctx    context.Context
    17  		cancel func()
    18  
    19  		runStep *execfakes.FakeStep
    20  
    21  		repo  *build.Repository
    22  		state *execfakes.FakeRunState
    23  
    24  		step Step
    25  
    26  		stepOk  bool
    27  		stepErr error
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		ctx, cancel = context.WithCancel(context.Background())
    32  
    33  		runStep = new(execfakes.FakeStep)
    34  
    35  		repo = build.NewRepository()
    36  		state = new(execfakes.FakeRunState)
    37  		state.ArtifactRepositoryReturns(repo)
    38  
    39  		step = Try(runStep)
    40  	})
    41  
    42  	JustBeforeEach(func() {
    43  		stepOk, stepErr = step.Run(ctx, state)
    44  	})
    45  
    46  	AfterEach(func() {
    47  		cancel()
    48  	})
    49  
    50  	Context("when the inner step fails", func() {
    51  		BeforeEach(func() {
    52  			runStep.RunReturns(false, nil)
    53  		})
    54  
    55  		It("succeeds anyway", func() {
    56  			Expect(stepErr).NotTo(HaveOccurred())
    57  			Expect(stepOk).To(BeTrue())
    58  		})
    59  	})
    60  
    61  	Context("when interrupted", func() {
    62  		BeforeEach(func() {
    63  			runStep.RunReturns(false, context.Canceled)
    64  		})
    65  
    66  		It("propagates the error and does not succeed", func() {
    67  			Expect(stepErr).To(Equal(context.Canceled))
    68  			Expect(stepOk).To(BeFalse())
    69  		})
    70  	})
    71  
    72  	Context("when the inner step returns any other error", func() {
    73  		BeforeEach(func() {
    74  			runStep.RunReturns(false, errors.New("some error"))
    75  		})
    76  
    77  		It("swallows the error", func() {
    78  			Expect(stepErr).NotTo(HaveOccurred())
    79  			Expect(stepOk).To(BeTrue())
    80  		})
    81  	})
    82  })