From c9504d50ba5bdf124ee90ba450c71f5200e8821e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 3 Dec 2014 10:01:00 -0800 Subject: [PATCH] command/push: send artifact true/false if we're post-processing --- command/push.go | 49 +++++++++++++++---- command/push_test.go | 43 +++++++++++++++- .../test-fixtures/push-builds/template.json | 15 ++++++ 3 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 command/test-fixtures/push-builds/template.json diff --git a/command/push.go b/command/push.go index f7c4f5ebe..c59c6cab9 100644 --- a/command/push.go +++ b/command/push.go @@ -7,8 +7,8 @@ import ( "path/filepath" "strings" - "github.com/hashicorp/atlas-go/v1" "github.com/hashicorp/atlas-go/archive" + "github.com/hashicorp/atlas-go/v1" "github.com/mitchellh/packer/packer" ) @@ -21,9 +21,14 @@ type PushCommand struct { client *atlas.Client // For tests: - uploadFn func(io.Reader, *uploadOpts) (<-chan struct{}, <-chan error, error) + uploadFn pushUploadFn } +// pushUploadFn is the callback type used for tests to stub out the uploading +// logic of the push command. +type pushUploadFn func( + io.Reader, *uploadOpts) (<-chan struct{}, <-chan error, error) + func (c *PushCommand) Run(args []string) int { var create bool var token string @@ -114,12 +119,32 @@ func (c *PushCommand) Run(args []string) int { } } + // Find the Atlas post-processors, if possible + var atlasPPs []packer.RawPostProcessorConfig + for _, list := range tpl.PostProcessors { + for _, pp := range list { + if pp.Type == "atlas" { + atlasPPs = append(atlasPPs, pp) + } + } + } + // Build the upload options var uploadOpts uploadOpts uploadOpts.Slug = tpl.Push.Name - uploadOpts.Builds = make(map[string]string) + uploadOpts.Builds = make(map[string]*uploadBuildInfo) for _, b := range tpl.Builders { - uploadOpts.Builds[b.Name] = b.Type + info := &uploadBuildInfo{Type: b.Type} + + // Determine if we're artifacting this build + for _, pp := range atlasPPs { + if !pp.Skip(b.Name) { + info.Artifact = true + break + } + } + + uploadOpts.Builds[b.Name] = info } // Create the build config if it doesn't currently exist. @@ -206,7 +231,7 @@ func (c *PushCommand) create(name string, create bool) error { // Otherwise, show an error if we're not creating. if !create { return fmt.Errorf( - "Push target doesn't exist: %s. Either create this online via\n" + + "Push target doesn't exist: %s. Either create this online via\n"+ "the website or pass the -create flag.", name) } @@ -242,10 +267,11 @@ func (c *PushCommand) upload( Name: bc.Name, Builds: make([]atlas.BuildConfigBuild, 0, len(opts.Builds)), } - for name, t := range opts.Builds { + for name, info := range opts.Builds { version.Builds = append(version.Builds, atlas.BuildConfigBuild{ - Name: name, - Type: t, + Name: name, + Type: info.Type, + Artifact: info.Artifact, }) } @@ -267,5 +293,10 @@ func (c *PushCommand) upload( type uploadOpts struct { URL string Slug string - Builds map[string]string + Builds map[string]*uploadBuildInfo +} + +type uploadBuildInfo struct { + Type string + Artifact bool } diff --git a/command/push_test.go b/command/push_test.go index 6ea1ec326..36e6d11c0 100644 --- a/command/push_test.go +++ b/command/push_test.go @@ -1,10 +1,10 @@ package command import ( - "fmt" "archive/tar" "bytes" "compress/gzip" + "fmt" "io" "path/filepath" "reflect" @@ -59,7 +59,46 @@ func TestPush(t *testing.T) { t.Fatalf("bad: %#v", actual) } - expectedBuilds := map[string]string{"dummy": "dummy"} + expectedBuilds := map[string]*uploadBuildInfo{ + "dummy": &uploadBuildInfo{ + Type: "dummy", + }, + } + if !reflect.DeepEqual(actualOpts.Builds, expectedBuilds) { + t.Fatalf("bad: %#v", actualOpts.Builds) + } +} + +func TestPush_builds(t *testing.T) { + var actualOpts *uploadOpts + uploadFn := func( + r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) { + actualOpts = opts + + doneCh := make(chan struct{}) + close(doneCh) + return doneCh, nil, nil + } + + c := &PushCommand{ + Meta: testMeta(t), + uploadFn: uploadFn, + } + + args := []string{filepath.Join(testFixture("push-builds"), "template.json")} + if code := c.Run(args); code != 0 { + fatalCommand(t, c.Meta) + } + + expectedBuilds := map[string]*uploadBuildInfo{ + "dummy": &uploadBuildInfo{ + Type: "dummy", + Artifact: true, + }, + "foo": &uploadBuildInfo{ + Type: "dummy", + }, + } if !reflect.DeepEqual(actualOpts.Builds, expectedBuilds) { t.Fatalf("bad: %#v", actualOpts.Builds) } diff --git a/command/test-fixtures/push-builds/template.json b/command/test-fixtures/push-builds/template.json new file mode 100644 index 000000000..35226ff1e --- /dev/null +++ b/command/test-fixtures/push-builds/template.json @@ -0,0 +1,15 @@ +{ + "builders": [ + {"type": "dummy"}, + {"type": "dummy", "name": "foo"} + ], + + "post-processors": [{ + "type": "atlas", + "only": ["dummy"] + }], + + "push": { + "name": "foo/bar" + } +}