Merge pull request #4998 from pivotal-michael-stergianis/atc-helpers-test-coverage

Increased coverage for atc/helpers GetBuild
This commit is contained in:
Taylor Silva 2020-01-14 14:23:18 -05:00 committed by GitHub
commit 405cd5f1a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 159 additions and 102 deletions

View File

@ -37,99 +37,128 @@ var _ = Describe("Helper Functions", func() {
})
Context("when passed a build id", func() {
Context("when build exists", func() {
BeforeEach(func() {
client.BuildReturns(expectedBuild, true, nil)
Context("when no error is encountered while fetching build", func() {
Context("when build exists", func() {
BeforeEach(func() {
client.BuildReturns(expectedBuild, true, nil)
})
It("returns the build", func() {
build, err := GetBuild(client, nil, "", expectedBuildID, "")
Expect(err).NotTo(HaveOccurred())
Expect(build).To(Equal(expectedBuild))
Expect(client.BuildCallCount()).To(Equal(1))
Expect(client.BuildArgsForCall(0)).To(Equal(expectedBuildID))
})
})
It("returns the build", func() {
build, err := GetBuild(client, nil, "", expectedBuildID, "")
Expect(err).NotTo(HaveOccurred())
Expect(build).To(Equal(expectedBuild))
Expect(client.BuildCallCount()).To(Equal(1))
Expect(client.BuildArgsForCall(0)).To(Equal(expectedBuildID))
Context("when a build does not exist", func() {
BeforeEach(func() {
client.BuildReturns(atc.Build{}, false, nil)
})
It("returns an error", func() {
_, err := GetBuild(client, nil, "", expectedBuildID, "")
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("build not found"))
})
})
})
Context("when a build does not exist", func() {
Context("when an error is encountered while fetching build", func() {
BeforeEach(func() {
client.BuildReturns(atc.Build{}, false, nil)
client.BuildReturns(atc.Build{}, false, errors.New("some-error"))
})
It("returns an error", func() {
It("return an error", func() {
_, err := GetBuild(client, nil, "", expectedBuildID, "")
Expect(err).To(MatchError("build not found"))
Expect(err).To(MatchError("failed to get build some-error"))
})
})
})
Context("when passed a pipeline and job name", func() {
Context("when job exists", func() {
Context("when the next build exists", func() {
BeforeEach(func() {
job := atc.Job{
Name: expectedJobName,
NextBuild: &expectedBuild,
}
team.JobReturns(job, true, nil)
Context("when no error was encountered while looking up for team job", func() {
Context("when job exists", func() {
Context("when the next build exists", func() {
BeforeEach(func() {
job := atc.Job{
Name: expectedJobName,
NextBuild: &expectedBuild,
}
team.JobReturns(job, true, nil)
})
It("returns the next build for that job", func() {
build, err := GetBuild(client, team, expectedJobName, "", expectedPipelineName)
Expect(err).NotTo(HaveOccurred())
Expect(build).To(Equal(expectedBuild))
Expect(team.JobCallCount()).To(Equal(1))
pipelineName, jobName := team.JobArgsForCall(0)
Expect(pipelineName).To(Equal(expectedPipelineName))
Expect(jobName).To(Equal(expectedJobName))
})
})
It("returns the next build for that job", func() {
build, err := GetBuild(client, team, expectedJobName, "", expectedPipelineName)
Expect(err).NotTo(HaveOccurred())
Expect(build).To(Equal(expectedBuild))
Expect(team.JobCallCount()).To(Equal(1))
pipelineName, jobName := team.JobArgsForCall(0)
Expect(pipelineName).To(Equal(expectedPipelineName))
Expect(jobName).To(Equal(expectedJobName))
Context("when the only the finished build exists", func() {
BeforeEach(func() {
job := atc.Job{
Name: expectedJobName,
FinishedBuild: &expectedBuild,
}
team.JobReturns(job, true, nil)
})
It("returns the finished build for that job", func() {
build, err := GetBuild(client, team, expectedJobName, "", expectedPipelineName)
Expect(err).NotTo(HaveOccurred())
Expect(build).To(Equal(expectedBuild))
Expect(team.JobCallCount()).To(Equal(1))
pipelineName, jobName := team.JobArgsForCall(0)
Expect(pipelineName).To(Equal(expectedPipelineName))
Expect(jobName).To(Equal(expectedJobName))
})
})
Context("when no builds exist", func() {
BeforeEach(func() {
job := atc.Job{
Name: expectedJobName,
}
team.JobReturns(job, true, nil)
})
It("returns an error", func() {
_, err := GetBuild(client, team, expectedJobName, "", expectedPipelineName)
Expect(err).To(HaveOccurred())
})
})
})
Context("when the only the finished build exists", func() {
Context("when job does not exists", func() {
BeforeEach(func() {
job := atc.Job{
Name: expectedJobName,
FinishedBuild: &expectedBuild,
}
team.JobReturns(job, true, nil)
})
It("returns the finished build for that job", func() {
build, err := GetBuild(client, team, expectedJobName, "", expectedPipelineName)
Expect(err).NotTo(HaveOccurred())
Expect(build).To(Equal(expectedBuild))
Expect(team.JobCallCount()).To(Equal(1))
pipelineName, jobName := team.JobArgsForCall(0)
Expect(pipelineName).To(Equal(expectedPipelineName))
Expect(jobName).To(Equal(expectedJobName))
})
})
Context("when no builds exist", func() {
BeforeEach(func() {
job := atc.Job{
Name: expectedJobName,
}
team.JobReturns(job, true, nil)
team.JobReturns(atc.Job{}, false, nil)
})
It("returns an error", func() {
_, err := GetBuild(client, team, expectedJobName, "", expectedPipelineName)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("job not found"))
})
})
})
Context("when job does not exists", func() {
Context("when an error was encountered while looking up for team job", func() {
BeforeEach(func() {
team.JobReturns(atc.Job{}, false, nil)
team.JobReturns(atc.Job{}, false, errors.New("some-error"))
})
It("returns an error", func() {
_, err := GetBuild(client, team, expectedJobName, "", expectedPipelineName)
Expect(err).To(MatchError("job not found"))
It("should return an error", func() {
_, err := GetBuild(client, team, expectedJobName, "", "")
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("failed to get job some-error"))
})
})
})
Context("when passed pipeline, job, and build names", func() {
@ -163,56 +192,84 @@ var _ = Describe("Helper Functions", func() {
})
Context("when nothing is passed", func() {
var allBuilds [300]atc.Build
Context("when client.Builds does not return an error", func() {
var allBuilds [300]atc.Build
expectedOneOffBuild := atc.Build{
ID: 150,
Name: expectedBuildName,
Status: "success",
JobName: "",
APIURL: fmt.Sprintf("api/v1/builds/%s", expectedBuildID),
}
BeforeEach(func() {
for i := 300 - 1; i >= 0; i-- {
allBuilds[i] = atc.Build{
ID: i,
Name: strconv.Itoa(i),
JobName: "some-job",
APIURL: fmt.Sprintf("api/v1/builds/%d", i),
}
expectedOneOffBuild := atc.Build{
ID: 150,
Name: expectedBuildName,
Status: "success",
JobName: "",
APIURL: fmt.Sprintf("api/v1/builds/%s", expectedBuildID),
}
allBuilds[150] = expectedOneOffBuild
Context("when a build was found", func() {
BeforeEach(func() {
for i := 300 - 1; i >= 0; i-- {
allBuilds[i] = atc.Build{
ID: i,
Name: strconv.Itoa(i),
JobName: "some-job",
APIURL: fmt.Sprintf("api/v1/builds/%d", i),
}
}
client.BuildsStub = func(page concourse.Page) ([]atc.Build, concourse.Pagination, error) {
var builds []atc.Build
if page.Since != 0 {
builds = allBuilds[page.Since : page.Since+page.Limit]
} else {
builds = allBuilds[0:page.Limit]
}
allBuilds[150] = expectedOneOffBuild
pagination := concourse.Pagination{
Previous: &concourse.Page{
Limit: page.Limit,
Until: builds[0].ID,
},
Next: &concourse.Page{
Limit: page.Limit,
Since: builds[len(builds)-1].ID,
},
}
client.BuildsStub = func(page concourse.Page) ([]atc.Build, concourse.Pagination, error) {
var builds []atc.Build
if page.Since != 0 {
builds = allBuilds[page.Since : page.Since+page.Limit]
} else {
builds = allBuilds[0:page.Limit]
}
return builds, pagination, nil
}
pagination := concourse.Pagination{
Previous: &concourse.Page{
Limit: page.Limit,
Until: builds[0].ID,
},
Next: &concourse.Page{
Limit: page.Limit,
Since: builds[len(builds)-1].ID,
},
}
return builds, pagination, nil
}
})
It("returns latest one off build", func() {
build, err := GetBuild(client, nil, "", "", "")
Expect(err).NotTo(HaveOccurred())
Expect(build).To(Equal(expectedOneOffBuild))
Expect(client.BuildsCallCount()).To(Equal(2))
})
})
Context("when no builds were found ", func() {
BeforeEach(func() {
client.BuildsReturns([]atc.Build{}, concourse.Pagination{Next: nil}, nil)
})
It("returns an error", func() {
_, err := GetBuild(client, nil, "", "", "")
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("no builds match job"))
})
})
})
It("returns latest one off build", func() {
build, err := GetBuild(client, nil, "", "", "")
Expect(err).NotTo(HaveOccurred())
Expect(build).To(Equal(expectedOneOffBuild))
Expect(client.BuildsCallCount()).To(Equal(2))
Context("when client.Builds returns an error", func() {
BeforeEach(func() {
client.BuildsReturns(nil, concourse.Pagination{}, errors.New("some-error"))
})
It("should return an error", func() {
_, err := GetBuild(client, nil, "", "", "")
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("failed to get builds some-error"))
})
})
})
})