From 0deaa5d2a5682b8cd4901158f91ec184106b3eec Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 6 Jul 2020 17:01:55 +0200 Subject: [PATCH] add a complete tests for builder variables + only/except --- command/build_test.go | 67 +++++++++++++++++++ command/command_test.go | 6 +- .../test-fixtures/hcl/complete/build.pkr.hcl | 54 +++++++++++++++ .../hcl/complete/sources.pkr.hcl | 3 + 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 command/test-fixtures/hcl/complete/build.pkr.hcl create mode 100644 command/test-fixtures/hcl/complete/sources.pkr.hcl diff --git a/command/build_test.go b/command/build_test.go index e464b3401..651ae988e 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -20,6 +20,17 @@ import ( shell_local "github.com/hashicorp/packer/provisioner/shell-local" ) +var ( + spaghettiCarbonara = `spaghetti +carbonara +` + lasagna = `lasagna +tomato +mozza +cooking... +` +) + func TestBuild(t *testing.T) { tc := []struct { name string @@ -232,6 +243,62 @@ func TestBuild(t *testing.T) { expected: []string{"chocolate.txt", "vanilla.txt"}, }, }, + + // complete + { + name: "hcl - complete", + args: []string{ + testFixture("hcl", "complete"), + }, + fileCheck: fileCheck{ + expectedContent: map[string]string{ + "NULL.spaghetti_carbonara.txt": spaghettiCarbonara, + "NULL.lasagna.txt": lasagna, + }, + }, + }, + + { + name: "hcl - complete - except carbonara", + args: []string{ + "-except", "recipes.null.spaghetti_carbonara", + testFixture("hcl", "complete"), + }, + fileCheck: fileCheck{ + notExpected: []string{"NULL.spaghetti_carbonara.txt"}, + expectedContent: map[string]string{ + "NULL.lasagna.txt": lasagna, + }, + }, + }, + + { + name: "hcl - complete - only lasagna", + args: []string{ + "-only", "*lasagna", + testFixture("hcl", "complete"), + }, + fileCheck: fileCheck{ + notExpected: []string{"NULL.spaghetti_carbonara.txt"}, + expectedContent: map[string]string{ + "NULL.lasagna.txt": lasagna, + }, + }, + }, + + { + name: "hcl - complete - only recipes", + args: []string{ + "-only", "recipes.*", + testFixture("hcl", "complete"), + }, + fileCheck: fileCheck{ + expectedContent: map[string]string{ + "NULL.spaghetti_carbonara.txt": spaghettiCarbonara, + "NULL.lasagna.txt": lasagna, + }, + }, + }, } for _, tt := range tc { diff --git a/command/command_test.go b/command/command_test.go index 31f1e3d54..34d044292 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -27,8 +27,10 @@ func outputCommand(t *testing.T, m Meta) (string, string) { return out.String(), err.String() } -func testFixture(n string) string { - return filepath.Join(fixturesDir, n) +func testFixture(n ...string) string { + paths := []string{fixturesDir} + paths = append(paths, n...) + return filepath.Join(paths...) } func testMeta(t *testing.T) Meta { diff --git a/command/test-fixtures/hcl/complete/build.pkr.hcl b/command/test-fixtures/hcl/complete/build.pkr.hcl new file mode 100644 index 000000000..ceb0a58d3 --- /dev/null +++ b/command/test-fixtures/hcl/complete/build.pkr.hcl @@ -0,0 +1,54 @@ + +build { + source "source.null.base" { + name = "tiramisu" + } +} + +build { + name = "recipes" + source "source.null.base" { + name = "spaghetti_carbonara" + } + source "source.null.base" { + name = "lasagna" + } + + provisioner "shell-local" { + name = "add_spaghetti" + inline = [ "echo spaghetti > ${upper(build.ID)}.${source.name}.txt" ] + only = ["null.spaghetti_carbonara"] + } + + post-processor "shell-local" { + name = "carbonara_it" + inline = [ "echo carbonara >> ${upper(build.ID)}.${source.name}.txt" ] + except = ["null.lasagna"] + } + + + provisioner "shell-local" { + name = "add_lasagna" + inline = [ "echo lasagna > ${upper(build.ID)}.${source.name}.txt" ] + only = ["null.lasagna"] + } + + provisioner "shell-local" { + name = "add_tomato" + inline = [ "echo tomato >> ${upper(build.ID)}.${source.name}.txt" ] + except = ["null.spaghetti_carbonara"] + } + + provisioner "shell-local" { + name = "add_mozza" + inline = [ "echo mozza >> ${upper(build.ID)}.${source.name}.txt" ] + except = ["null.spaghetti_carbonara"] + } + + post-processor "shell-local" { + name = "cook" + inline = [ "echo 'cooking...' >> ${upper(build.ID)}.${source.name}.txt" ] + except = ["null.spaghetti_carbonara"] + } + +} diff --git a/command/test-fixtures/hcl/complete/sources.pkr.hcl b/command/test-fixtures/hcl/complete/sources.pkr.hcl new file mode 100644 index 000000000..baeae65fb --- /dev/null +++ b/command/test-fixtures/hcl/complete/sources.pkr.hcl @@ -0,0 +1,3 @@ +source "null" "base" { + communicator = "none" +}