Return exit code 1 when builder type is not found

This commit is contained in:
Moss 2019-12-10 18:55:18 +01:00
parent 5e81c6f44e
commit 7466c4fdca
3 changed files with 98 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 // 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) buildNames := c.Meta.BuildNames(core)
builds := make([]packer.Build, 0, len(buildNames)) builds := make([]packer.Build, 0, len(buildNames))
for _, n := range buildNames { for _, n := range buildNames {
@ -123,6 +127,7 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, args []string) int {
c.Ui.Error(fmt.Sprintf( c.Ui.Error(fmt.Sprintf(
"Failed to initialize build '%s': %s", "Failed to initialize build '%s': %s",
n, err)) n, err))
errors.m[n] = err
continue continue
} }
@ -200,11 +205,6 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, args []string) int {
sync.RWMutex sync.RWMutex
m map[string][]packer.Artifact m map[string][]packer.Artifact
}{m: make(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) limitParallel := semaphore.NewWeighted(cfg.ParallelBuilds)
for i := range builds { for i := range builds {
if err := buildCtx.Err(); err != nil { 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 // fileExists returns true if the filename is found
func fileExists(filename string) bool { func fileExists(filename string) bool {
if _, err := os.Stat(filename); err == nil { if _, err := os.Stat(filename); err == nil {
@ -182,6 +206,9 @@ func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {
if n == "file" { if n == "file" {
return &file.Builder{}, nil return &file.Builder{}, nil
} }
if n == "non-existing" {
return nil, fmt.Errorf("builder type not found")
}
return &null.Builder{}, nil return &null.Builder{}, nil
}, },
Provisioner: func(n string) (packer.Provisioner, error) { Provisioner: func(n string) (packer.Provisioner, error) {

View File

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