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