github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/exec/artifact_output_step_test.go (about)

     1  package exec_test
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     8  	"github.com/pf-qiu/concourse/v6/atc/db/dbfakes"
     9  	"github.com/pf-qiu/concourse/v6/atc/exec"
    10  	"github.com/pf-qiu/concourse/v6/atc/exec/build"
    11  	"github.com/pf-qiu/concourse/v6/atc/runtime/runtimefakes"
    12  	"github.com/pf-qiu/concourse/v6/atc/worker/workerfakes"
    13  	"github.com/pf-qiu/concourse/v6/vars"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("ArtifactOutputStep", func() {
    19  	var (
    20  		ctx    context.Context
    21  		cancel func()
    22  
    23  		state exec.RunState
    24  
    25  		step             exec.Step
    26  		stepOk           bool
    27  		stepErr          error
    28  		plan             atc.Plan
    29  		fakeBuild        *dbfakes.FakeBuild
    30  		fakeWorkerClient *workerfakes.FakeClient
    31  
    32  		artifactName string
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		ctx, cancel = context.WithCancel(context.Background())
    37  
    38  		state = exec.NewRunState(noopStepper, vars.StaticVariables{}, false)
    39  
    40  		fakeBuild = new(dbfakes.FakeBuild)
    41  		fakeBuild.TeamIDReturns(4)
    42  
    43  		fakeWorkerClient = new(workerfakes.FakeClient)
    44  
    45  		artifactName = "some-artifact-name"
    46  	})
    47  
    48  	AfterEach(func() {
    49  		cancel()
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		plan = atc.Plan{ArtifactOutput: &atc.ArtifactOutputPlan{Name: artifactName}}
    54  
    55  		step = exec.NewArtifactOutputStep(plan, fakeBuild, fakeWorkerClient)
    56  		stepOk, stepErr = step.Run(ctx, state)
    57  	})
    58  
    59  	Context("when the source does not exist", func() {
    60  		It("returns the error", func() {
    61  			Expect(stepErr).To(HaveOccurred())
    62  		})
    63  	})
    64  
    65  	Context("when the artifact exists", func() {
    66  		Context("when the source is not a worker.Volume", func() {
    67  			BeforeEach(func() {
    68  				fakeArtifact := new(runtimefakes.FakeArtifact)
    69  				state.ArtifactRepository().RegisterArtifact(build.ArtifactName(artifactName), fakeArtifact)
    70  			})
    71  			It("returns the error", func() {
    72  				Expect(stepErr).To(HaveOccurred())
    73  			})
    74  		})
    75  
    76  		Context("when the source is a worker.Volume", func() {
    77  			var fakeWorkerVolume *workerfakes.FakeVolume
    78  			var fakeArtifact *runtimefakes.FakeArtifact
    79  
    80  			BeforeEach(func() {
    81  				fakeWorkerVolume = new(workerfakes.FakeVolume)
    82  				fakeWorkerVolume.HandleReturns("some-volume-handle")
    83  
    84  				fakeArtifact = new(runtimefakes.FakeArtifact)
    85  				fakeArtifact.IDReturns("some-artifact-id")
    86  
    87  				fakeWorkerClient.FindVolumeReturns(fakeWorkerVolume, true, nil)
    88  
    89  				state.ArtifactRepository().RegisterArtifact(build.ArtifactName(artifactName), fakeArtifact)
    90  			})
    91  
    92  			Context("when initializing the artifact fails", func() {
    93  				BeforeEach(func() {
    94  					fakeWorkerVolume.InitializeArtifactReturns(nil, errors.New("nope"))
    95  				})
    96  				It("returns the error", func() {
    97  					Expect(stepErr).To(HaveOccurred())
    98  				})
    99  			})
   100  
   101  			Context("when initializing the artifact succeeds", func() {
   102  				var fakeWorkerArtifact *dbfakes.FakeWorkerArtifact
   103  
   104  				BeforeEach(func() {
   105  					fakeWorkerArtifact = new(dbfakes.FakeWorkerArtifact)
   106  					fakeWorkerArtifact.IDReturns(0)
   107  
   108  					fakeWorkerVolume.InitializeArtifactReturns(fakeWorkerArtifact, nil)
   109  				})
   110  
   111  				It("calls workerClient -> FindVolume with the correct arguments", func() {
   112  					_, actualTeamId, actualBuildArtifactID := fakeWorkerClient.FindVolumeArgsForCall(0)
   113  					Expect(actualTeamId).To(Equal(4))
   114  					Expect(actualBuildArtifactID).To(Equal("some-artifact-id"))
   115  				})
   116  
   117  				It("succeeds", func() {
   118  					Expect(stepOk).To(BeTrue())
   119  				})
   120  			})
   121  		})
   122  	})
   123  })