From 5371f66599cda4600992dac3c4da969fb143b53d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 20 Sep 2013 11:20:05 -0700 Subject: [PATCH] packer: verify only one of 'only' or 'except' specified [GH-438] --- packer/template.go | 5 ++++ packer/template_test.go | 62 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/packer/template.go b/packer/template.go index cada49fd3..b398b54cc 100644 --- a/packer/template.go +++ b/packer/template.go @@ -554,6 +554,11 @@ func (t *TemplateOnlyExcept) Skip(name string) bool { // Validates the only/except parameters. func (t *TemplateOnlyExcept) Validate(b map[string]RawBuilderConfig) (e []error) { + if len(t.Only) > 0 && len(t.Except) > 0 { + e = append(e, + fmt.Errorf("Only one of 'only' or 'except' may be specified.")) + } + if len(t.Only) > 0 { for _, n := range t.Only { if _, ok := b[n]; !ok { diff --git a/packer/template_test.go b/packer/template_test.go index 62b1f54fa..aa531ad26 100644 --- a/packer/template_test.go +++ b/packer/template_test.go @@ -690,7 +690,67 @@ func TestTemplate_Build(t *testing.T) { } } -func TestTemplateBuild_exeptPPInvalid(t *testing.T) { +func TestTemplateBuild_exceptOnlyPP(t *testing.T) { + data := ` + { + "builders": [ + { + "name": "test1", + "type": "test-builder" + }, + { + "name": "test2", + "type": "test-builder" + } + ], + + "post-processors": [ + { + "type": "test-pp", + "except": ["test1"], + "only": ["test1"] + } + ] + } + ` + + _, err := ParseTemplate([]byte(data)) + if err == nil { + t.Fatal("should have error") + } +} + +func TestTemplateBuild_exceptOnlyProv(t *testing.T) { + data := ` + { + "builders": [ + { + "name": "test1", + "type": "test-builder" + }, + { + "name": "test2", + "type": "test-builder" + } + ], + + "provisioners": [ + { + "type": "test-prov", + "except": ["test1"], + "only": ["test1"] + } + ] + } + ` + + _, err := ParseTemplate([]byte(data)) + if err == nil { + t.Fatal("should have error") + } +} + +func TestTemplateBuild_exceptPPInvalid(t *testing.T) { data := ` { "builders": [