Skip to content

Commit

Permalink
Backfill tests for pkg/in
Browse files Browse the repository at this point in the history
As with pkg/check, there are a few changes.

- Fixed trying to read error message from nil errors on 404s
- Some changes to wording
- Added counterfeiter-generated fakes for eventstream. Due to a known
  bug, I had to manually edit the fake file to use a non-vendor import
  path in the generated code. This also arose in bbc3538.
  See: See: maxbrunsfeld/counterfeiter#75

[#4]

Signed-off-by: Jacques Chester <jchester@pivotal.io>
  • Loading branch information
jchester authored and jchesterpivotal committed Aug 19, 2018
1 parent 53f0cb5 commit 87011ce
Show file tree
Hide file tree
Showing 3 changed files with 297 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/in/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (i inner) In() (*config.InResponse, error) {
return nil, fmt.Errorf("error while fetching build '%s': '%s", i.inRequest.Version.BuildId, err.Error())
}
if !found {
return nil, fmt.Errorf("build '%s' not found: '%s", i.inRequest.Version.BuildId, err.Error())
return nil, fmt.Errorf("server could not find '%s/%s' while retrieving build '%s'", i.inRequest.Source.Pipeline, i.inRequest.Source.Job, i.inRequest.Version.BuildId)
}

buildFile, err := os.Create(filepath.Join(i.inRequest.WorkingDirectory, "build.json"))
Expand All @@ -52,7 +52,7 @@ func (i inner) In() (*config.InResponse, error) {
return nil, fmt.Errorf("error while fetching resources for build '%s': '%s", i.inRequest.Version.BuildId, err.Error())
}
if !found {
return nil, fmt.Errorf("build '%s' not found while fetching resources: '%s", i.inRequest.Version.BuildId, err.Error())
return nil, fmt.Errorf("build '%s' not found while fetching resources", i.inRequest.Version.BuildId)
}

resFile, err := os.Create(filepath.Join(i.inRequest.WorkingDirectory, "resources.json"))
Expand All @@ -69,7 +69,7 @@ func (i inner) In() (*config.InResponse, error) {
return nil, fmt.Errorf("error while fetching plan for build '%s': '%s", i.inRequest.Version.BuildId, err.Error())
}
if !found {
return nil, fmt.Errorf("build '%s' not found while fetching plan: '%s", i.inRequest.Version.BuildId, err.Error())
return nil, fmt.Errorf("build '%s' not found while fetching plan", i.inRequest.Version.BuildId)
}

planFile, err := os.Create(filepath.Join(i.inRequest.WorkingDirectory, "plan.json"))
Expand Down
149 changes: 149 additions & 0 deletions pkg/in/in_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package in_test

import (
"testing"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/onsi/gomega"
fakes "github.com/concourse/go-concourse/concourse/concoursefakes"

"github.com/jchesterpivotal/concourse-build-resource/pkg/config"
"github.com/jchesterpivotal/concourse-build-resource/pkg/in"
"github.com/concourse/atc"
"os"
"github.com/concourse/go-concourse/concourse/eventstream/eventstreamfakes"
"io"
"fmt"
)

func TestInPkg(t *testing.T) {
gt := gomega.NewGomegaWithT(t)

err := os.Mkdir("build", os.ModeDir|os.ModePerm)
if err != nil {
gt.Expect(err).NotTo(gomega.MatchError("build: file exists"))
}

spec.Run(t, "pkg/in", func(t *testing.T, when spec.G, it spec.S) {
when("given valid inputs", func() {
gt := gomega.NewGomegaWithT(t)
faketeam := new(fakes.FakeTeam)
fakeclient := new(fakes.FakeClient)
fakeclient.TeamReturns(faketeam)
fakeeventstream := &eventstreamfakes.FakeEventStream{}
var response *config.InResponse
var err error

it.Before(func() {
fakeclient.BuildReturns(atc.Build{ID: 111}, true, nil)
fakeclient.BuildResourcesReturns(atc.BuildInputsOutputs{}, true, nil)
fakeclient.BuildPlanReturns(atc.PublicBuildPlan{}, true, nil)
fakeeventstream.NextEventReturns(nil, io.EOF)
fakeclient.BuildEventsReturns(fakeeventstream, nil)

inner := in.NewInnerUsingClient(&config.InRequest{
Source: config.Source{},
Version: config.Version{BuildId: "111"},
Params: config.InParams{},
WorkingDirectory: "build",
}, fakeclient)
response, err = inner.In()
gt.Expect(err).NotTo(gomega.HaveOccurred())
})

it("returns the version it was given", func() {
gt.Expect(response.Version).To(gomega.Equal(config.Version{BuildId: "111"}))
})

it("returns no metadata", func() {
gt.Expect(response.Metadata).To(gomega.BeEmpty())
})

it("writes out the build.json file", func() {
gt.Expect("build/build.json").To(gomega.BeAnExistingFile())
})

it("writes out the resources.json file", func() {
gt.Expect("build/resources.json").To(gomega.BeAnExistingFile())
})

it("writes out the plan.json file", func() {
gt.Expect("build/plan.json").To(gomega.BeAnExistingFile())
})

it("writes out the events.log", func() {
gt.Expect("build/events.log").To(gomega.BeAnExistingFile())
})
}, spec.Nested())

when("something goes wrong", func() {
gt := gomega.NewGomegaWithT(t)
faketeam := new(fakes.FakeTeam)
fakeclient := new(fakes.FakeClient)
fakeclient.TeamReturns(faketeam)
var response *config.InResponse
var err error

when("data cannot be retrieved due to an error", func() {
it.Before(func() {
fakeclient.BuildReturns(atc.Build{ID: 111}, false, fmt.Errorf("test error"))

inner := in.NewInnerUsingClient(&config.InRequest{
Source: config.Source{},
Version: config.Version{BuildId: "111"},
Params: config.InParams{},
WorkingDirectory: "build",
}, fakeclient)
response, err = inner.In()
})

it("returns an error", func() {
gt.Expect(err.Error()).To(gomega.ContainSubstring("error while fetching build"))
gt.Expect(response).To(gomega.BeNil())
})
}, spec.Nested())

when("the team, pipeline or job are not found", func() {
it.Before(func() {
fakeclient.BuildReturns(atc.Build{ID: 111}, false, nil)

inner := in.NewInnerUsingClient(&config.InRequest{
Source: config.Source{Pipeline: "pipeline", Job: "job"},
Version: config.Version{BuildId: "111"},
Params: config.InParams{},
WorkingDirectory: "build",
}, fakeclient)
response, err = inner.In()
})

it("returns an error", func() {
gt.Expect(err.Error()).To(gomega.ContainSubstring("server could not find 'pipeline/job' while retrieving build '111'"))
gt.Expect(response).To(gomega.BeNil())
})
}, spec.Nested())
}, spec.Nested())

when("build ID is defined, but is not a valid number", func() {
gt := gomega.NewGomegaWithT(t)
faketeam := new(fakes.FakeTeam)
fakeclient := new(fakes.FakeClient)
fakeclient.TeamReturns(faketeam)
var response *config.InResponse
var err error

it.Before(func() {
fakeclient.BuildReturns(atc.Build{ID: 111}, true, nil)

inner := in.NewInnerUsingClient(&config.InRequest{Version: config.Version{BuildId: "not numerical"}}, fakeclient)
response, err = inner.In()
})

it("returns an error", func() {
gt.Expect(err.Error()).To(gomega.ContainSubstring("could not convert build id 'not numerical' to an int:"))
gt.Expect(response).To(gomega.BeNil())
})
}, spec.Nested())
}, spec.Report(report.Terminal{}))

gt.Expect(os.RemoveAll("build")).To(gomega.Succeed())
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 87011ce

Please sign in to comment.