From 7466c4fdca6e525a6a9e4d6d7d92f2ab86f58697 Mon Sep 17 00:00:00 2001 From: Moss Date: Tue, 10 Dec 2019 18:55:18 +0100 Subject: [PATCH 1/2] Return exit code 1 when builder type is not found --- command/build.go | 10 +-- command/build_test.go | 27 ++++++++ .../test-fixtures/build-only/not-found.json | 66 +++++++++++++++++++ 3 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 command/test-fixtures/build-only/not-found.json diff --git a/command/build.go b/command/build.go index 7f582d38a..8dd245458 100644 --- a/command/build.go +++ b/command/build.go @@ -115,6 +115,10 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, args []string) int { } // Get the builds we care about + var errors = struct { + sync.RWMutex + m map[string]error + }{m: make(map[string]error)} buildNames := c.Meta.BuildNames(core) builds := make([]packer.Build, 0, len(buildNames)) for _, n := range buildNames { @@ -123,6 +127,7 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, args []string) int { c.Ui.Error(fmt.Sprintf( "Failed to initialize build '%s': %s", n, err)) + errors.m[n] = err continue } @@ -200,11 +205,6 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, args []string) int { sync.RWMutex m map[string][]packer.Artifact }{m: make(map[string][]packer.Artifact)} - var errors = struct { - sync.RWMutex - m map[string]error - }{m: make(map[string]error)} - limitParallel := semaphore.NewWeighted(cfg.ParallelBuilds) for i := range builds { if err := buildCtx.Err(); err != nil { diff --git a/command/build_test.go b/command/build_test.go index d96ce6998..b68ef068c 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -166,6 +166,30 @@ func TestBuildExceptFileCommaFlags(t *testing.T) { } } +func TestBuildExceptNonExistingBuilder(t *testing.T) { + c := &BuildCommand{ + Meta: testMetaFile(t), + } + + args := []string{ + "-parallel=false", + `-except=`, + filepath.Join(testFixture("build-only"), "not-found.json"), + } + + defer cleanup() + + if code := c.Run(args); code != 1 { + t.Errorf("Expected to find exit code 1, found %d", code) + } + if !fileExists("chocolate.txt") { + t.Errorf("Expected to find chocolate.txt") + } + if fileExists("vanilla.txt") { + t.Errorf("NOT expected to find vanilla.tx") + } +} + // fileExists returns true if the filename is found func fileExists(filename string) bool { if _, err := os.Stat(filename); err == nil { @@ -182,6 +206,9 @@ func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig { if n == "file" { return &file.Builder{}, nil } + if n == "non-existing" { + return nil, fmt.Errorf("builder type not found") + } return &null.Builder{}, nil }, Provisioner: func(n string) (packer.Provisioner, error) { diff --git a/command/test-fixtures/build-only/not-found.json b/command/test-fixtures/build-only/not-found.json new file mode 100644 index 000000000..5f6ec3238 --- /dev/null +++ b/command/test-fixtures/build-only/not-found.json @@ -0,0 +1,66 @@ +{ + "builders": [ + { + "name": "chocolate", + "type": "file", + "content": "chocolate", + "target": "chocolate.txt" + }, + { + "name": "vanilla", + "type": "non-existing", + "content": "vanilla", + "target": "vanilla.txt" + } + ], + "post-processors": [ + [ + { + "name": "apple", + "type": "shell-local", + "inline": [ + "echo apple > apple.txt" + ] + }, + { + "name": "peach", + "type": "shell-local", + "inline": [ + "echo peach > peach.txt" + ] + } + ], + [ + { + "name": "pear", + "type": "shell-local", + "inline": [ + "echo pear > pear.txt" + ] + } + ], + [ + { + "only": [ + "vanilla" + ], + "name": "tomato", + "type": "shell-local", + "inline": [ + "echo tomato > tomato.txt" + ] + } + ], + [ + { + "only": [ + "chocolate" + ], + "type": "shell-local", + "inline": [ + "echo unnamed > unnamed.txt" + ] + } + ] + ] +} \ No newline at end of file From a0329ef1964ee07b819843eaadaa432b2169e4ca Mon Sep 17 00:00:00 2001 From: Moss Date: Tue, 10 Dec 2019 18:59:56 +0100 Subject: [PATCH 2/2] Simplify testing template --- .../test-fixtures/build-only/not-found.json | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/command/test-fixtures/build-only/not-found.json b/command/test-fixtures/build-only/not-found.json index 5f6ec3238..4f327d82f 100644 --- a/command/test-fixtures/build-only/not-found.json +++ b/command/test-fixtures/build-only/not-found.json @@ -14,31 +14,6 @@ } ], "post-processors": [ - [ - { - "name": "apple", - "type": "shell-local", - "inline": [ - "echo apple > apple.txt" - ] - }, - { - "name": "peach", - "type": "shell-local", - "inline": [ - "echo peach > peach.txt" - ] - } - ], - [ - { - "name": "pear", - "type": "shell-local", - "inline": [ - "echo pear > pear.txt" - ] - } - ], [ { "only": [