github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/helpers_test.go (about) 1 package commands_test 2 3 import ( 4 "errors" 5 "fmt" 6 "strconv" 7 8 "github.com/pf-qiu/concourse/v6/atc" 9 . "github.com/pf-qiu/concourse/v6/fly/commands" 10 "github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers" 11 "github.com/pf-qiu/concourse/v6/fly/rc/rcfakes" 12 "github.com/pf-qiu/concourse/v6/go-concourse/concourse" 13 fakes "github.com/pf-qiu/concourse/v6/go-concourse/concourse/concoursefakes" 14 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("Helper Functions", func() { 20 Describe("#GetBuild", func() { 21 var client *fakes.FakeClient 22 var team *fakes.FakeTeam 23 expectedBuildID := "123" 24 expectedBuildName := "5" 25 expectedJobName := "myjob" 26 expectedPipelineRef := atc.PipelineRef{} 27 expectedBuild := atc.Build{ 28 ID: 123, 29 Name: expectedBuildName, 30 Status: "Great Success", 31 JobName: expectedJobName, 32 APIURL: fmt.Sprintf("api/v1/builds/%s", expectedBuildID), 33 } 34 35 BeforeEach(func() { 36 client = new(fakes.FakeClient) 37 team = new(fakes.FakeTeam) 38 }) 39 40 Context("when passed a build id", func() { 41 Context("when no error is encountered while fetching build", func() { 42 Context("when build exists", func() { 43 BeforeEach(func() { 44 client.BuildReturns(expectedBuild, true, nil) 45 }) 46 47 It("returns the build", func() { 48 build, err := GetBuild(client, nil, "", expectedBuildID, expectedPipelineRef) 49 Expect(err).NotTo(HaveOccurred()) 50 Expect(build).To(Equal(expectedBuild)) 51 Expect(client.BuildCallCount()).To(Equal(1)) 52 Expect(client.BuildArgsForCall(0)).To(Equal(expectedBuildID)) 53 }) 54 }) 55 56 Context("when a build does not exist", func() { 57 BeforeEach(func() { 58 client.BuildReturns(atc.Build{}, false, nil) 59 }) 60 61 It("returns an error", func() { 62 _, err := GetBuild(client, nil, "", expectedBuildID, expectedPipelineRef) 63 Expect(err).To(HaveOccurred()) 64 Expect(err).To(MatchError("build not found")) 65 }) 66 }) 67 }) 68 69 Context("when an error is encountered while fetching build", func() { 70 BeforeEach(func() { 71 client.BuildReturns(atc.Build{}, false, errors.New("some-error")) 72 }) 73 74 It("return an error", func() { 75 _, err := GetBuild(client, nil, "", expectedBuildID, expectedPipelineRef) 76 Expect(err).To(MatchError("failed to get build some-error")) 77 }) 78 }) 79 }) 80 81 Context("when passed a pipeline and job name", func() { 82 Context("when no error was encountered while looking up for team job", func() { 83 Context("when job exists", func() { 84 Context("when the next build exists", func() { 85 BeforeEach(func() { 86 job := atc.Job{ 87 Name: expectedJobName, 88 NextBuild: &expectedBuild, 89 } 90 team.JobReturns(job, true, nil) 91 expectedPipelineRef = atc.PipelineRef{Name: "mypipeline"} 92 }) 93 94 It("returns the next build for that job", func() { 95 build, err := GetBuild(client, team, expectedJobName, "", expectedPipelineRef) 96 Expect(err).NotTo(HaveOccurred()) 97 Expect(build).To(Equal(expectedBuild)) 98 Expect(team.JobCallCount()).To(Equal(1)) 99 pipelineRef, jobName := team.JobArgsForCall(0) 100 Expect(pipelineRef).To(Equal(expectedPipelineRef)) 101 Expect(jobName).To(Equal(expectedJobName)) 102 }) 103 }) 104 105 Context("when the only the finished build exists", func() { 106 BeforeEach(func() { 107 job := atc.Job{ 108 Name: expectedJobName, 109 FinishedBuild: &expectedBuild, 110 } 111 team.JobReturns(job, true, nil) 112 expectedPipelineRef = atc.PipelineRef{Name: "mypipeline"} 113 }) 114 115 It("returns the finished build for that job", func() { 116 build, err := GetBuild(client, team, expectedJobName, "", expectedPipelineRef) 117 Expect(err).NotTo(HaveOccurred()) 118 Expect(build).To(Equal(expectedBuild)) 119 Expect(team.JobCallCount()).To(Equal(1)) 120 pipelineRef, jobName := team.JobArgsForCall(0) 121 Expect(pipelineRef).To(Equal(expectedPipelineRef)) 122 Expect(jobName).To(Equal(expectedJobName)) 123 }) 124 }) 125 126 Context("when no builds exist", func() { 127 BeforeEach(func() { 128 job := atc.Job{ 129 Name: expectedJobName, 130 } 131 team.JobReturns(job, true, nil) 132 expectedPipelineRef = atc.PipelineRef{Name: "mypipeline"} 133 }) 134 135 It("returns an error", func() { 136 _, err := GetBuild(client, team, expectedJobName, "", expectedPipelineRef) 137 Expect(err).To(HaveOccurred()) 138 }) 139 }) 140 }) 141 142 Context("when job does not exists", func() { 143 BeforeEach(func() { 144 team.JobReturns(atc.Job{}, false, nil) 145 expectedPipelineRef = atc.PipelineRef{Name: "mypipeline"} 146 }) 147 148 It("returns an error", func() { 149 _, err := GetBuild(client, team, expectedJobName, "", expectedPipelineRef) 150 Expect(err).To(MatchError("job not found")) 151 }) 152 }) 153 }) 154 155 Context("when an error was encountered while looking up for team job", func() { 156 BeforeEach(func() { 157 team.JobReturns(atc.Job{}, false, errors.New("some-error")) 158 }) 159 160 It("should return an error", func() { 161 _, err := GetBuild(client, team, expectedJobName, "", expectedPipelineRef) 162 Expect(err).To(HaveOccurred()) 163 Expect(err).To(MatchError("failed to get job some-error")) 164 }) 165 }) 166 167 }) 168 169 Context("when passed pipeline, job, and build names", func() { 170 Context("when the build exists", func() { 171 BeforeEach(func() { 172 team.JobBuildReturns(expectedBuild, true, nil) 173 expectedPipelineRef = atc.PipelineRef{Name: "mypipeline"} 174 }) 175 176 It("returns the build", func() { 177 build, err := GetBuild(client, team, expectedJobName, expectedBuildName, expectedPipelineRef) 178 Expect(err).NotTo(HaveOccurred()) 179 Expect(build).To(Equal(expectedBuild)) 180 Expect(team.JobBuildCallCount()).To(Equal(1)) 181 pipelineRef, jobName, buildName := team.JobBuildArgsForCall(0) 182 Expect(pipelineRef).To(Equal(expectedPipelineRef)) 183 Expect(buildName).To(Equal(expectedBuildName)) 184 Expect(jobName).To(Equal(expectedJobName)) 185 }) 186 }) 187 188 Context("when the build does not exist", func() { 189 BeforeEach(func() { 190 team.JobBuildReturns(atc.Build{}, false, nil) 191 expectedPipelineRef = atc.PipelineRef{Name: "mypipeline"} 192 }) 193 194 It("returns an error", func() { 195 _, err := GetBuild(client, team, expectedJobName, expectedBuildName, expectedPipelineRef) 196 Expect(err).To(MatchError("build not found")) 197 }) 198 }) 199 }) 200 201 Context("when nothing is passed", func() { 202 Context("when client.Builds does not return an error", func() { 203 var allBuilds [300]atc.Build 204 205 expectedOneOffBuild := atc.Build{ 206 ID: 150, 207 Name: expectedBuildName, 208 Status: "success", 209 JobName: "", 210 APIURL: fmt.Sprintf("api/v1/builds/%s", expectedBuildID), 211 } 212 213 Context("when a build was found", func() { 214 BeforeEach(func() { 215 for i := 300 - 1; i >= 0; i-- { 216 allBuilds[i] = atc.Build{ 217 ID: i, 218 Name: strconv.Itoa(i), 219 JobName: "some-job", 220 APIURL: fmt.Sprintf("api/v1/builds/%d", i), 221 } 222 } 223 224 allBuilds[150] = expectedOneOffBuild 225 226 client.BuildsStub = func(page concourse.Page) ([]atc.Build, concourse.Pagination, error) { 227 var builds []atc.Build 228 if page.To != 0 { 229 builds = allBuilds[page.To : page.To+page.Limit] 230 } else { 231 builds = allBuilds[0:page.Limit] 232 } 233 234 pagination := concourse.Pagination{ 235 Previous: &concourse.Page{ 236 Limit: page.Limit, 237 From: builds[0].ID, 238 }, 239 Next: &concourse.Page{ 240 Limit: page.Limit, 241 To: builds[len(builds)-1].ID, 242 }, 243 } 244 245 return builds, pagination, nil 246 } 247 }) 248 249 It("returns latest one off build", func() { 250 build, err := GetBuild(client, nil, "", "", expectedPipelineRef) 251 Expect(err).NotTo(HaveOccurred()) 252 Expect(build).To(Equal(expectedOneOffBuild)) 253 Expect(client.BuildsCallCount()).To(Equal(2)) 254 }) 255 }) 256 257 Context("when no builds were found ", func() { 258 BeforeEach(func() { 259 client.BuildsReturns([]atc.Build{}, concourse.Pagination{Next: nil}, nil) 260 }) 261 262 It("returns an error", func() { 263 _, err := GetBuild(client, nil, "", "", expectedPipelineRef) 264 Expect(err).To(HaveOccurred()) 265 Expect(err).To(MatchError("no builds match job")) 266 }) 267 }) 268 }) 269 270 Context("when client.Builds returns an error", func() { 271 BeforeEach(func() { 272 client.BuildsReturns(nil, concourse.Pagination{}, errors.New("some-error")) 273 }) 274 275 It("should return an error", func() { 276 _, err := GetBuild(client, nil, "", "", expectedPipelineRef) 277 Expect(err).To(HaveOccurred()) 278 Expect(err).To(MatchError("failed to get builds some-error")) 279 }) 280 }) 281 }) 282 }) 283 Describe("#GetLatestResourceVersions", func() { 284 var team *fakes.FakeTeam 285 var resourceVersions []atc.ResourceVersion 286 287 resource := flaghelpers.ResourceFlag{ 288 PipelineRef: atc.PipelineRef{ 289 Name: "mypipeline", 290 InstanceVars: atc.InstanceVars{"branch": "master"}, 291 }, 292 ResourceName: "myresource", 293 } 294 295 BeforeEach(func() { 296 team = new(fakes.FakeTeam) 297 resourceVersions = []atc.ResourceVersion{ 298 { 299 ID: 1, 300 Version: atc.Version{"version": "v1"}, 301 }, 302 { 303 ID: 2, 304 Version: atc.Version{"version": "v1"}, 305 }, 306 } 307 }) 308 309 When("resource versions exist", func() { 310 It("returns latest resource version", func() { 311 team.ResourceVersionsReturns(resourceVersions, concourse.Pagination{}, true, nil) 312 latestResourceVersion, err := GetLatestResourceVersion(team, resource, atc.Version{"version": "v1"}) 313 Expect(err).NotTo(HaveOccurred()) 314 Expect(latestResourceVersion.Version).To(Equal(atc.Version{"version": "v1"})) 315 Expect(latestResourceVersion.ID).To(Equal(1)) 316 }) 317 }) 318 319 When("call to resource versions returns an error", func() { 320 It("returns an error", func() { 321 team.ResourceVersionsReturns(nil, concourse.Pagination{}, false, errors.New("fake error")) 322 _, err := GetLatestResourceVersion(team, resource, atc.Version{"version": "v1"}) 323 Expect(err).To(MatchError("fake error")) 324 }) 325 }) 326 327 When("call to resource versions returns an empty array", func() { 328 It("returns an error", func() { 329 team.ResourceVersionsReturns([]atc.ResourceVersion{}, concourse.Pagination{}, true, nil) 330 _, err := GetLatestResourceVersion(team, resource, atc.Version{"version": "v2"}) 331 Expect(err).To(MatchError("could not find version matching {\"version\":\"v2\"}")) 332 }) 333 }) 334 }) 335 336 Describe("#GetTeam", func() { 337 var target *rcfakes.FakeTarget 338 var client *fakes.FakeClient 339 340 BeforeEach(func() { 341 target = new(rcfakes.FakeTarget) 342 client = new(fakes.FakeClient) 343 target.ClientReturns(client) 344 }) 345 346 It("gets the team", func() { 347 GetTeam(target, "team") 348 349 Expect(client.TeamCallCount()).To(Equal(1), "target.Client().Team should be called once") 350 Expect(client.TeamArgsForCall(0)).To(Equal("team")) 351 }) 352 353 It("returns the target default if no team is provided", func() { 354 GetTeam(target, "") 355 356 Expect(target.TeamCallCount()).To(Equal(1), "target.Team should be called once") 357 }) 358 }) 359 })