From 79e989d03ebb27d1d3b490e9ec804140f7656bb5 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet <105649352+lbajolet-hashicorp@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:30:02 -0500 Subject: [PATCH] hcl2: don't warn on excluded post-processor (#12187) * hcl2template: use && for chaining conditions For deciding if we need to run an error provisioner block, we checked using a nested if that we didn't need to avoid running it. This is unnecessary, and adds a superfluous level of inbrication to the code, so we chain both conditions with &&. * hcl2: don't warn on excluded post-processor When a post-processor is excluded through the -except command-line argument for packer build, we used to print a warning on HCL2 templates if this except statement did not match a build. However, since we can use the except option to avoid running post-processors, we should account for this as well when printing out a warning that there were no matches. This commit adds this counter to the except checks for the post-processors as well. --- command/build_test.go | 21 +++++++++++++++++++ .../hcl/test_except_manifest.pkr.hcl | 9 ++++++++ hcl2template/types.packer_config.go | 20 +++++++++--------- 3 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 command/test-fixtures/hcl/test_except_manifest.pkr.hcl diff --git a/command/build_test.go b/command/build_test.go index 8aa30c720..21a032d6e 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -1318,6 +1318,27 @@ func TestBuildCmd(t *testing.T) { return fmt.Errorf("error: missing context for error message") } + return nil + }, + }, + { + name: "hcl - exclude post-processor, expect no warning", + args: []string{ + "-except", "manifest", + testFixture("hcl", "test_except_manifest.pkr.hcl"), + }, + expectedCode: 0, + outputCheck: func(out, err string) error { + for _, stream := range []string{out, err} { + if strings.Contains(stream, "Warning: an 'except' option was passed, but did not match any build") { + return fmt.Errorf("Unexpected warning for build no match with except") + } + + if strings.Contains(stream, "Running post-processor:") { + return fmt.Errorf("Should not run post-processors, but found one") + } + } + return nil }, }, diff --git a/command/test-fixtures/hcl/test_except_manifest.pkr.hcl b/command/test-fixtures/hcl/test_except_manifest.pkr.hcl new file mode 100644 index 000000000..bf9f7ad29 --- /dev/null +++ b/command/test-fixtures/hcl/test_except_manifest.pkr.hcl @@ -0,0 +1,9 @@ +source "null" "test" { + communicator = "none" +} + +build { + sources = ["null.test"] + + post-processor "manifest" {} +} diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index ef67f33e7..3ac4b0c51 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -514,7 +514,7 @@ func (cfg *PackerConfig) getCoreBuildProvisioner(source SourceUseBlock, pb *Prov // getCoreBuildProvisioners takes a list of post processor block, starts // according provisioners and sends parsed HCL2 over to it. -func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceUseBlock, blocksList [][]*PostProcessorBlock, ectx *hcl.EvalContext) ([][]packer.CoreBuildPostProcessor, hcl.Diagnostics) { +func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceUseBlock, blocksList [][]*PostProcessorBlock, ectx *hcl.EvalContext, exceptMatches *int) ([][]packer.CoreBuildPostProcessor, hcl.Diagnostics) { var diags hcl.Diagnostics res := [][]packer.CoreBuildPostProcessor{} for _, blocks := range blocksList { @@ -533,6 +533,7 @@ func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceUseBlock, block for _, exceptGlob := range cfg.except { if exceptGlob.Match(name) { exclude = true + *exceptMatches = *exceptMatches + 1 break } } @@ -673,21 +674,20 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packersdk.Bu if moreDiags.HasErrors() { continue } - pps, moreDiags := cfg.getCoreBuildPostProcessors(srcUsage, build.PostProcessorsLists, cfg.EvalContext(BuildContext, variables)) + pps, moreDiags := cfg.getCoreBuildPostProcessors(srcUsage, build.PostProcessorsLists, cfg.EvalContext(BuildContext, variables), &opts.ExceptMatches) diags = append(diags, moreDiags...) if moreDiags.HasErrors() { continue } - if build.ErrorCleanupProvisionerBlock != nil { - if !build.ErrorCleanupProvisionerBlock.OnlyExcept.Skip(srcUsage.String()) { - errorCleanupProv, moreDiags := cfg.getCoreBuildProvisioner(srcUsage, build.ErrorCleanupProvisionerBlock, cfg.EvalContext(BuildContext, variables)) - diags = append(diags, moreDiags...) - if moreDiags.HasErrors() { - continue - } - pcb.CleanupProvisioner = errorCleanupProv + if build.ErrorCleanupProvisionerBlock != nil && + !build.ErrorCleanupProvisionerBlock.OnlyExcept.Skip(srcUsage.String()) { + errorCleanupProv, moreDiags := cfg.getCoreBuildProvisioner(srcUsage, build.ErrorCleanupProvisionerBlock, cfg.EvalContext(BuildContext, variables)) + diags = append(diags, moreDiags...) + if moreDiags.HasErrors() { + continue } + pcb.CleanupProvisioner = errorCleanupProv } pcb.Builder = builder