Merge pull request #8475 from hashicorp/fix_8472

Return exit code 1 when builder type is not found
This commit is contained in:
Megan Marsh 2019-12-10 10:42:33 -08:00 committed by GitHub
commit 8073a5381c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 5 deletions

View File

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

View File

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

View File

@ -0,0 +1,41 @@
{
"builders": [
{
"name": "chocolate",
"type": "file",
"content": "chocolate",
"target": "chocolate.txt"
},
{
"name": "vanilla",
"type": "non-existing",
"content": "vanilla",
"target": "vanilla.txt"
}
],
"post-processors": [
[
{
"only": [
"vanilla"
],
"name": "tomato",
"type": "shell-local",
"inline": [
"echo tomato > tomato.txt"
]
}
],
[
{
"only": [
"chocolate"
],
"type": "shell-local",
"inline": [
"echo unnamed > unnamed.txt"
]
}
]
]
}