github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/worker/fetcher_test.go (about) 1 package worker_test 2 3 import ( 4 "context" 5 "errors" 6 "time" 7 8 "github.com/pf-qiu/concourse/v6/atc/db/dbfakes" 9 "github.com/pf-qiu/concourse/v6/atc/resource" 10 "github.com/pf-qiu/concourse/v6/atc/runtime" 11 "github.com/pf-qiu/concourse/v6/atc/worker" 12 13 "code.cloudfoundry.org/clock/fakeclock" 14 "code.cloudfoundry.org/lager" 15 "code.cloudfoundry.org/lager/lagertest" 16 "github.com/pf-qiu/concourse/v6/atc/db" 17 "github.com/pf-qiu/concourse/v6/atc/db/lock" 18 "github.com/pf-qiu/concourse/v6/atc/db/lock/lockfakes" 19 "github.com/pf-qiu/concourse/v6/atc/resource/resourcefakes" 20 "github.com/pf-qiu/concourse/v6/atc/worker/workerfakes" 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 ) 24 25 var _ = Describe("Fetcher", func() { 26 var ( 27 fakeClock *fakeclock.FakeClock 28 fakeLockFactory *lockfakes.FakeLockFactory 29 fetcher worker.Fetcher 30 ctx context.Context 31 cancel func() 32 33 fakeWorker *workerfakes.FakeWorker 34 fakeVolume *workerfakes.FakeVolume 35 fakeFetchSourceFactory *workerfakes.FakeFetchSourceFactory 36 fakeResource *resourcefakes.FakeResource 37 fakeUsedResourceCache *dbfakes.FakeUsedResourceCache 38 39 getResult worker.GetResult 40 fetchErr error 41 teamID = 123 42 43 volume worker.Volume 44 ) 45 46 BeforeEach(func() { 47 fakeClock = fakeclock.NewFakeClock(time.Unix(0, 123)) 48 fakeLockFactory = new(lockfakes.FakeLockFactory) 49 fakeFetchSourceFactory = new(workerfakes.FakeFetchSourceFactory) 50 51 fakeWorker = new(workerfakes.FakeWorker) 52 fakeWorker.NameReturns("some-worker") 53 54 fakeVolume = new(workerfakes.FakeVolume) 55 fakeVolume.HandleReturns("some-handle") 56 fakeResource = new(resourcefakes.FakeResource) 57 fakeUsedResourceCache = new(dbfakes.FakeUsedResourceCache) 58 59 fetcher = worker.NewFetcher( 60 fakeClock, 61 fakeLockFactory, 62 fakeFetchSourceFactory, 63 ) 64 65 ctx, cancel = context.WithCancel(context.Background()) 66 }) 67 68 JustBeforeEach(func() { 69 getResult, volume, fetchErr = fetcher.Fetch( 70 ctx, 71 lagertest.NewTestLogger("test"), 72 db.ContainerMetadata{}, 73 fakeWorker, 74 worker.ContainerSpec{ 75 TeamID: teamID, 76 }, 77 runtime.ProcessSpec{ 78 Args: []string{resource.ResourcesDir("get")}, 79 }, 80 fakeResource, 81 db.NewBuildStepContainerOwner(0, "some-plan-id", 0), 82 fakeUsedResourceCache, 83 "fake-lock-name", 84 ) 85 }) 86 87 Context("when getting source", func() { 88 var fakeFetchSource *workerfakes.FakeFetchSource 89 90 BeforeEach(func() { 91 fakeFetchSource = new(workerfakes.FakeFetchSource) 92 fakeFetchSourceFactory.NewFetchSourceReturns(fakeFetchSource) 93 94 fakeFetchSource.FindReturns(worker.GetResult{}, fakeVolume, false, nil) 95 }) 96 97 Describe("failing to get a lock", func() { 98 Context("when did not get a lock", func() { 99 BeforeEach(func() { 100 fakeLock := new(lockfakes.FakeLock) 101 callCount := 0 102 fakeLockFactory.AcquireStub = func(lager.Logger, lock.LockID) (lock.Lock, bool, error) { 103 callCount++ 104 fakeClock.Increment(worker.GetResourceLockInterval) 105 if callCount == 1 { 106 return nil, false, nil 107 } 108 return fakeLock, true, nil 109 } 110 }) 111 112 It("retries until it gets the lock", func() { 113 Expect(fakeLockFactory.AcquireCallCount()).To(Equal(2)) 114 }) 115 116 It("creates fetch source after lock is acquired", func() { 117 Expect(fakeFetchSource.CreateCallCount()).To(Equal(1)) 118 }) 119 }) 120 121 Context("when acquiring lock returns error", func() { 122 BeforeEach(func() { 123 fakeLock := new(lockfakes.FakeLock) 124 callCount := 0 125 fakeLockFactory.AcquireStub = func(lager.Logger, lock.LockID) (lock.Lock, bool, error) { 126 callCount++ 127 fakeClock.Increment(worker.GetResourceLockInterval) 128 if callCount == 1 { 129 return nil, false, errors.New("disaster") 130 } 131 return fakeLock, true, nil 132 } 133 }) 134 135 It("retries until it gets the lock", func() { 136 Expect(fakeLockFactory.AcquireCallCount()).To(Equal(2)) 137 }) 138 139 It("creates fetch source after lock is acquired", func() { 140 Expect(fakeFetchSource.CreateCallCount()).To(Equal(1)) 141 }) 142 }) 143 }) 144 145 Context("when getting lock succeeds", func() { 146 var fakeLock *lockfakes.FakeLock 147 148 BeforeEach(func() { 149 fakeLock = new(lockfakes.FakeLock) 150 fakeLockFactory.AcquireReturns(fakeLock, true, nil) 151 fakeFetchSource.CreateReturns(worker.GetResult{}, fakeVolume, nil) 152 }) 153 154 It("acquires a lock with source lock name", func() { 155 Expect(fakeLockFactory.AcquireCallCount()).To(Equal(1)) 156 _, lockID := fakeLockFactory.AcquireArgsForCall(0) 157 Expect(lockID).To(Equal(lock.NewTaskLockID("fake-lock-name"))) 158 }) 159 160 It("releases the lock", func() { 161 Expect(fakeLock.ReleaseCallCount()).To(Equal(1)) 162 }) 163 164 It("creates source", func() { 165 Expect(fakeFetchSource.CreateCallCount()).To(Equal(1)) 166 }) 167 168 It("returns the result and volume", func() { 169 Expect(getResult).To(Equal(worker.GetResult{})) 170 Expect(volume).ToNot(BeNil()) 171 }) 172 }) 173 174 Context("when finding fails", func() { 175 var disaster error 176 177 BeforeEach(func() { 178 disaster = errors.New("fail") 179 fakeFetchSource.FindReturns(worker.GetResult{}, nil, false, disaster) 180 }) 181 182 It("returns an error", func() { 183 Expect(fetchErr).To(HaveOccurred()) 184 Expect(fetchErr).To(Equal(disaster)) 185 }) 186 }) 187 188 Context("when cancelled", func() { 189 BeforeEach(func() { 190 cancel() 191 }) 192 193 It("returns the context err", func() { 194 Expect(fetchErr).To(Equal(context.Canceled)) 195 }) 196 }) 197 }) 198 })