add a complete tests for builder variables + only/except

This commit is contained in:
Adrien Delorme 2020-07-06 17:01:55 +02:00
parent 8e923977f0
commit 0deaa5d2a5
4 changed files with 128 additions and 2 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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"]
}
}

View File

@ -0,0 +1,3 @@
source "null" "base" {
communicator = "none"
}