From b04b8b3857e31bda3ad7556c3225c7d5323b885b Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Tue, 19 Feb 2019 15:25:41 +0100 Subject: [PATCH 01/10] test building with `-except=""` --- command/build_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/command/build_test.go b/command/build_test.go index 12f9709d2..dd9bb1f43 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -102,6 +102,30 @@ func TestBuildOnlyFileMultipleFlags(t *testing.T) { } } +func TestBuildEverything(t *testing.T) { + c := &BuildCommand{ + Meta: testMetaFile(t), + } + + args := []string{ + `-except=""`, + filepath.Join(testFixture("build-only"), "template.json"), + } + + defer cleanup() + + if code := c.Run(args); code != 0 { + fatalCommand(t, c.Meta) + } + + for _, f := range []string{"chocolate.txt", "vanilla.txt", "tomato.txt", + "apple.txt", "cherry.txt", "pear.txt", "peach.txt"} { + if !fileExists(f) { + t.Errorf("Expected to find %s", f) + } + } +} + func TestBuildExceptFileCommaFlags(t *testing.T) { c := &BuildCommand{ Meta: testMetaFile(t), From de336ef15e539705bb2ba121045f6c101718d618 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Tue, 19 Feb 2019 16:40:42 +0100 Subject: [PATCH 02/10] TestBuildOnlyFileMultipleFlags: check that tomato.txt is not created --- command/build_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/build_test.go b/command/build_test.go index dd9bb1f43..53043a2d4 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -89,7 +89,7 @@ func TestBuildOnlyFileMultipleFlags(t *testing.T) { fatalCommand(t, c.Meta) } - for _, f := range []string{"vanilla.txt"} { + for _, f := range []string{"vanilla.txt", "tomato.txt"} { if fileExists(f) { t.Errorf("Expected NOT to find %s", f) } From 7e2d86731e737d89b9697a51bf95e8ee04663dc5 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 20 Feb 2019 11:03:17 +0100 Subject: [PATCH 03/10] tests: add a post-processor that creates an unnamed.txt file --- command/build_test.go | 15 +++++++++------ command/test-fixtures/build-only/template.json | 9 +++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/command/build_test.go b/command/build_test.go index 53043a2d4..2a2ba1734 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -28,7 +28,7 @@ func TestBuildOnlyFileCommaFlags(t *testing.T) { } for _, f := range []string{"chocolate.txt", "vanilla.txt", - "apple.txt", "peach.txt", "pear.txt"} { + "apple.txt", "peach.txt", "pear.txt", "unnamed.txt"} { if !fileExists(f) { t.Errorf("Expected to find %s", f) } @@ -62,7 +62,8 @@ func TestBuildStdin(t *testing.T) { fatalCommand(t, c.Meta) } - for _, f := range []string{"vanilla.txt", "cherry.txt", "chocolate.txt"} { + for _, f := range []string{"vanilla.txt", "cherry.txt", "chocolate.txt", + "unnamed.txt"} { if !fileExists(f) { t.Errorf("Expected to find %s", f) } @@ -95,7 +96,7 @@ func TestBuildOnlyFileMultipleFlags(t *testing.T) { } } for _, f := range []string{"chocolate.txt", "cherry.txt", - "apple.txt", "peach.txt", "pear.txt"} { + "apple.txt", "peach.txt", "pear.txt", "unnamed.txt"} { if !fileExists(f) { t.Errorf("Expected to find %s", f) } @@ -108,7 +109,7 @@ func TestBuildEverything(t *testing.T) { } args := []string{ - `-except=""`, + `-except=`, filepath.Join(testFixture("build-only"), "template.json"), } @@ -119,7 +120,7 @@ func TestBuildEverything(t *testing.T) { } for _, f := range []string{"chocolate.txt", "vanilla.txt", "tomato.txt", - "apple.txt", "cherry.txt", "pear.txt", "peach.txt"} { + "apple.txt", "cherry.txt", "pear.txt", "peach.txt", "unnamed.txt"} { if !fileExists(f) { t.Errorf("Expected to find %s", f) } @@ -142,7 +143,8 @@ func TestBuildExceptFileCommaFlags(t *testing.T) { fatalCommand(t, c.Meta) } - for _, f := range []string{"chocolate.txt", "vanilla.txt", "tomato.txt"} { + for _, f := range []string{"chocolate.txt", "vanilla.txt", "tomato.txt", + "unnamed.txt"} { if fileExists(f) { t.Errorf("Expected NOT to find %s", f) } @@ -198,4 +200,5 @@ func cleanup() { os.RemoveAll("peach.txt") os.RemoveAll("pear.txt") os.RemoveAll("tomato.txt") + os.RemoveAll("unnamed.txt") } diff --git a/command/test-fixtures/build-only/template.json b/command/test-fixtures/build-only/template.json index 9ca20adca..01e6bfd13 100644 --- a/command/test-fixtures/build-only/template.json +++ b/command/test-fixtures/build-only/template.json @@ -48,6 +48,15 @@ "type": "shell-local", "inline": [ "touch tomato.txt" ] } + ], + [ + { + "only": [ + "chocolate" + ], + "type": "shell-local", + "inline": [ "touch unnamed.txt" ] + } ] ] } \ No newline at end of file From 0f7065f6b1f0653dfa4c2b7a9889be32731ddb8b Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 20 Feb 2019 11:07:18 +0100 Subject: [PATCH 04/10] post-processor except: don't match empty names --- packer/core.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packer/core.go b/packer/core.go index a059f90c9..d88e21e5c 100644 --- a/packer/core.go +++ b/packer/core.go @@ -186,7 +186,7 @@ func (c *Core) Build(n string) (Build, error) { // -except skips post-processor & build foundExcept := false for _, except := range c.except { - if except == rawP.Name { + if except != "" && except == rawP.Name { foundExcept = true } } From 3167d5d52cccef2f3e537807904bd80b6934ec62 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 20 Feb 2019 11:47:39 +0100 Subject: [PATCH 05/10] document post-processor execpt more --- website/source/docs/commands/build.html.md | 11 ++--- website/source/docs/commands/validate.html.md | 9 ++-- .../docs/templates/post-processors.html.md | 42 ++++++++++++++----- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/website/source/docs/commands/build.html.md b/website/source/docs/commands/build.html.md index 61ee8c3a9..605dcef7f 100644 --- a/website/source/docs/commands/build.html.md +++ b/website/source/docs/commands/build.html.md @@ -27,11 +27,12 @@ artifacts that are created will be outputted at the end of the build. This will allow the user to inspect state and so on. - `-except=foo,bar,baz` - Run all the builds and post-processors except those - with the given comma-separated names. Build and post-processor names by - default are their type, unless a specific `name` attribute is specified - within the configuration. Any post-processor following a skipped - post-processor will not run. Because post-processors can be nested in - arrays a differ post-processor chain can still run. + with the given comma-separated names. Build names by default are their + type, unless a specific `name` attribute is specified within the + configuration. Any post-processor following a skipped post-processor will + not run. Because post-processors can be nested in arrays a different + post-processor chain can still run. A post-processor with an empty name + will be ignored. - `-force` - Forces a builder to run when artifacts from a previous build prevent a build from running. The exact behavior of a forced build is left diff --git a/website/source/docs/commands/validate.html.md b/website/source/docs/commands/validate.html.md index 6f1a54c82..fd7b4f144 100644 --- a/website/source/docs/commands/validate.html.md +++ b/website/source/docs/commands/validate.html.md @@ -33,10 +33,11 @@ Errors validating build 'vmware'. 1 error(s) occurred: - `-syntax-only` - Only the syntax of the template is checked. The configuration is not validated. -- `-except=foo,bar,baz` - Builds all the builds except those with the given - comma-separated names. Build names by default are the names of their - builders, unless a specific `name` attribute is specified within the - configuration. +- `-except=foo,bar,baz` - Builds all the builds and post-processors except + those with the given comma-separated names. Build names by default are the + names of their builders, unless a specific `name` attribute is specified + within the configuration. A post-processor with an empty name will be + ignored. - `-only=foo,bar,baz` - Only build the builds with the given comma-separated names. Build names by default are the names of their builders, unless a diff --git a/website/source/docs/templates/post-processors.html.md b/website/source/docs/templates/post-processors.html.md index b656dd72a..cd4ce1fd8 100644 --- a/website/source/docs/templates/post-processors.html.md +++ b/website/source/docs/templates/post-processors.html.md @@ -38,7 +38,8 @@ defined builders and send it through the post-processors. This means that if you have one post-processor defined and two builders defined in a template, the post-processor will run twice (once for each builder), by default. There are ways, which will be covered later, to control what builders post-processors -apply to, if you wish. +apply to, if you wish. It is also possible to prevent a post-processor from +running. ## Post-Processor Definition @@ -136,21 +137,40 @@ post-processor requested that the input be kept, so it will keep it around. ## Run on Specific Builds -You can use the `only` or `except` configurations to run a post-processor only -with specific builds. These two configurations do what you expect: `only` will -only run the post-processor on the specified builds and `except` will run the -post-processor on anything other than the specified builds. +You can use the `only` or `except` fields to run a post-processor only with +specific builds. These two fields do what you expect: `only` will only run the +post-processor on the specified builds and `except` will run the post-processor +on anything other than the specified builds. A sequence of post-processors will +execute until a skipped post-processor. An example of `only` being used is shown below, but the usage of `except` is effectively the same. `only` and `except` can only be specified on "detailed" -configurations. If you have a sequence of post-processors to run, `only` and -`except` will only affect that single post-processor in the sequence. +fields. If you have a sequence of post-processors to run, `only` and `except` +will affect that post-processor and stop the sequence. + +The `-except` option can specifically skip a named post processor. ``` json -{ - "type": "vagrant", - "only": ["virtualbox-iso"] -} +[ + { + // can also be skipped when running `packer build -except vbox` + "name": "vbox", + "type": "vagrant", + "only": ["virtualbox-iso"] + }, + { + "type": "compress" // will only be executed when vbox is + } +], +[ + "compress", // will run even if vbox is skipped, from the same start as vbox. + { + "type": "upload", + "endpoint": "http://example.com" + } + // this list of post processors will execute + // using build, not another post-processor. +] ``` The values within `only` or `except` are *build names*, not builder types. If From 5eccbc702d6eecba499b078baa6c449980a6b713 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 20 Feb 2019 11:03:39 +0100 Subject: [PATCH 06/10] name a post-processor to it's type if it is not named --- template/parse.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/template/parse.go b/template/parse.go index 9f37929a3..de54f091c 100644 --- a/template/parse.go +++ b/template/parse.go @@ -144,6 +144,11 @@ func (r *rawTemplate) Template() (*Template, error) { continue } + // The name defaults to the type if it isn't set + if pp.Name == "" { + pp.Name = pp.Type + } + // Set the configuration delete(c, "except") delete(c, "only") From b37dee1a673cb272a6705c824231bcc3309e2496 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 20 Feb 2019 12:34:52 +0100 Subject: [PATCH 07/10] Update post-processor docs --- website/source/docs/commands/build.html.md | 12 ++++++------ website/source/docs/commands/validate.html.md | 8 ++++---- .../source/docs/templates/post-processors.html.md | 5 +++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/website/source/docs/commands/build.html.md b/website/source/docs/commands/build.html.md index 605dcef7f..7e559b974 100644 --- a/website/source/docs/commands/build.html.md +++ b/website/source/docs/commands/build.html.md @@ -27,12 +27,12 @@ artifacts that are created will be outputted at the end of the build. This will allow the user to inspect state and so on. - `-except=foo,bar,baz` - Run all the builds and post-processors except those - with the given comma-separated names. Build names by default are their - type, unless a specific `name` attribute is specified within the - configuration. Any post-processor following a skipped post-processor will - not run. Because post-processors can be nested in arrays a different - post-processor chain can still run. A post-processor with an empty name - will be ignored. + with the given comma-separated names. Build and post-processor names by + default are their type, unless a specific `name` attribute is specified + within the configuration. Any post-processor following a skipped + post-processor will not run. Because post-processors can be nested in + arrays a different post-processor chain can still run. A post-processor + with an empty name will be ignored. - `-force` - Forces a builder to run when artifacts from a previous build prevent a build from running. The exact behavior of a forced build is left diff --git a/website/source/docs/commands/validate.html.md b/website/source/docs/commands/validate.html.md index fd7b4f144..ead5a4fa9 100644 --- a/website/source/docs/commands/validate.html.md +++ b/website/source/docs/commands/validate.html.md @@ -34,10 +34,10 @@ Errors validating build 'vmware'. 1 error(s) occurred: configuration is not validated. - `-except=foo,bar,baz` - Builds all the builds and post-processors except - those with the given comma-separated names. Build names by default are the - names of their builders, unless a specific `name` attribute is specified - within the configuration. A post-processor with an empty name will be - ignored. + those with the given comma-separated names. Build and post-processor names + by default are the names of their builders, unless a specific `name` + attribute is specified within the configuration. A post-processor with an + empty name will be ignored. - `-only=foo,bar,baz` - Only build the builds with the given comma-separated names. Build names by default are the names of their builders, unless a diff --git a/website/source/docs/templates/post-processors.html.md b/website/source/docs/templates/post-processors.html.md index cd4ce1fd8..8b041d73b 100644 --- a/website/source/docs/templates/post-processors.html.md +++ b/website/source/docs/templates/post-processors.html.md @@ -148,12 +148,13 @@ effectively the same. `only` and `except` can only be specified on "detailed" fields. If you have a sequence of post-processors to run, `only` and `except` will affect that post-processor and stop the sequence. -The `-except` option can specifically skip a named post processor. +The `-except` option can specifically skip a named post processor. The `-only` +option *ignores* post-processors. ``` json [ { - // can also be skipped when running `packer build -except vbox` + // can be skipped when running `packer build -except vbox` "name": "vbox", "type": "vagrant", "only": ["virtualbox-iso"] From b6b3cc901ce23d0b043da73d8cb16dd370888309 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 20 Feb 2019 12:48:52 +0100 Subject: [PATCH 08/10] template/parse_test.go: use diff for easier eye debug --- template/parse_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/template/parse_test.go b/template/parse_test.go index 8881783f6..510a67122 100644 --- a/template/parse_test.go +++ b/template/parse_test.go @@ -4,10 +4,11 @@ package template import ( "path/filepath" - "reflect" "strings" "testing" "time" + + "github.com/google/go-cmp/cmp" ) func TestParse(t *testing.T) { @@ -321,7 +322,7 @@ func TestParse(t *testing.T) { }, } - for _, tc := range cases { + for i, tc := range cases { path, _ := filepath.Abs(fixtureDir(tc.File)) tpl, err := ParseFile(fixtureDir(tc.File)) if (err != nil) != tc.Err { @@ -334,8 +335,8 @@ func TestParse(t *testing.T) { if tpl != nil { tpl.RawContents = nil } - if !reflect.DeepEqual(tpl, tc.Result) { - t.Fatalf("bad: %s\n\n%#v\n\n%#v", tc.File, tpl, tc.Result) + if diff := cmp.Diff(tpl, tc.Result); diff != "" { + t.Fatalf("[%d]bad: %v", i, diff) } } } From c12e9eea3a6edc669e5820784a4217e48ff2de8c Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 20 Feb 2019 12:49:21 +0100 Subject: [PATCH 09/10] template/parse_test.go: name the post-processors to their type as it's now the default --- template/parse_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/template/parse_test.go b/template/parse_test.go index 510a67122..85609d629 100644 --- a/template/parse_test.go +++ b/template/parse_test.go @@ -152,6 +152,7 @@ func TestParse(t *testing.T) { PostProcessors: [][]*PostProcessor{ { { + Name: "foo", Type: "foo", Config: map[string]interface{}{ "foo": "bar", @@ -169,6 +170,7 @@ func TestParse(t *testing.T) { PostProcessors: [][]*PostProcessor{ { { + Name: "foo", Type: "foo", KeepInputArtifact: true, }, @@ -184,6 +186,7 @@ func TestParse(t *testing.T) { PostProcessors: [][]*PostProcessor{ { { + Name: "foo", Type: "foo", OnlyExcept: OnlyExcept{ Only: []string{"bar"}, @@ -201,6 +204,7 @@ func TestParse(t *testing.T) { PostProcessors: [][]*PostProcessor{ { { + Name: "foo", Type: "foo", OnlyExcept: OnlyExcept{ Except: []string{"bar"}, @@ -218,6 +222,7 @@ func TestParse(t *testing.T) { PostProcessors: [][]*PostProcessor{ { { + Name: "foo", Type: "foo", }, }, @@ -232,6 +237,7 @@ func TestParse(t *testing.T) { PostProcessors: [][]*PostProcessor{ { { + Name: "foo", Type: "foo", }, }, @@ -246,11 +252,13 @@ func TestParse(t *testing.T) { PostProcessors: [][]*PostProcessor{ { { + Name: "foo", Type: "foo", }, }, { { + Name: "bar", Type: "bar", }, }, @@ -265,9 +273,11 @@ func TestParse(t *testing.T) { PostProcessors: [][]*PostProcessor{ { { + Name: "foo", Type: "foo", }, { + Name: "bar", Type: "bar", }, }, From 944c00900e0bd5d584dc6aa4b884021368d4d1a3 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 20 Feb 2019 12:54:57 +0100 Subject: [PATCH 10/10] parse_test.go: still display file name in case of error --- template/parse_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template/parse_test.go b/template/parse_test.go index 85609d629..7ba17b2a7 100644 --- a/template/parse_test.go +++ b/template/parse_test.go @@ -346,7 +346,7 @@ func TestParse(t *testing.T) { tpl.RawContents = nil } if diff := cmp.Diff(tpl, tc.Result); diff != "" { - t.Fatalf("[%d]bad: %v", i, diff) + t.Fatalf("[%d]bad: %s\n%v", i, tc.File, diff) } } }