Skip to content

Commit

Permalink
command/validate: add option to eval datasources
Browse files Browse the repository at this point in the history
When packer validate is invoked, it does not try to evaluate the
datasources before attempting to decide if the template is valid.

In many cases, this works, but sometimes it will fail as the value is
unknown by the validation code.

Since the validation code for all the elements of a Packer template is
left to be implemented by plugins, we cannot rely on checking for
unknown values everywhere, especially since the unknown references are
replaced automatically by a value of the right type for the
configuration expected.

So, in order for such configurations to be validable, we add an extra
option to packer validate, that will let users evaluate the datasources
from a template.
  • Loading branch information
lbajolet-hashicorp committed Nov 14, 2022
1 parent 113bc5e commit 6519070
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
4 changes: 3 additions & 1 deletion command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,16 @@ type FixArgs struct {

func (va *ValidateArgs) AddFlagSets(flags *flag.FlagSet) {
flags.BoolVar(&va.SyntaxOnly, "syntax-only", false, "check syntax only")
flags.BoolVar(&va.EvaluateDatasources, "evaluate-datasources", false, "evaluate datasources for validation (HCL2 only, may incur costs)")

va.MetaArgs.AddFlagSets(flags)
}

// ValidateArgs represents a parsed cli line for a `packer validate`
type ValidateArgs struct {
MetaArgs
SyntaxOnly bool
SyntaxOnly bool
EvaluateDatasources bool
}

func (va *InspectArgs) AddFlagSets(flags *flag.FlagSet) {
Expand Down
17 changes: 17 additions & 0 deletions command/test-fixtures/hcl/local-ds-validate.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
data "null" "dep" {
input = "upload"
}

source "null" "test" {
communicator = "none"
}

build {
sources = ["sources.null.test"]

provisioner "file" {
source = "test-fixtures/hcl/force.pkr.hcl"
destination = "dest"
direction = "${data.null.dep.output}"
}
}
2 changes: 1 addition & 1 deletion command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *ValidateCommand) RunContext(ctx context.Context, cla *ValidateArgs) int
}

diags := packerStarter.Initialize(packer.InitializeOptions{
SkipDatasourcesExecution: true,
SkipDatasourcesExecution: !cla.EvaluateDatasources,
})
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
Expand Down
13 changes: 10 additions & 3 deletions command/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (

func TestValidateCommand(t *testing.T) {
tt := []struct {
path string
exitCode int
path string
exitCode int
extraArgs []string
}{
{path: filepath.Join(testFixture("validate"), "build.json")},
{path: filepath.Join(testFixture("validate"), "build.pkr.hcl")},
Expand All @@ -39,6 +40,11 @@ func TestValidateCommand(t *testing.T) {

// datasource could be unknown at that moment
{path: filepath.Join(testFixture("hcl", "data-source-validation.pkr.hcl")), exitCode: 0},

// datasource unknown at validation-time without datasource evaluation -> fail on provisioner
{path: filepath.Join(testFixture("hcl", "local-ds-validate.pkr.hcl")), exitCode: 1},
// datasource unknown at validation-time with datasource evaluation -> success
{path: filepath.Join(testFixture("hcl", "local-ds-validate.pkr.hcl")), exitCode: 0, extraArgs: []string{"--evaluate-datasources"}},
}

for _, tc := range tt {
Expand All @@ -47,7 +53,8 @@ func TestValidateCommand(t *testing.T) {
Meta: TestMetaFile(t),
}
tc := tc
args := []string{tc.path}
args := tc.extraArgs
args = append(args, tc.path)
if code := c.Run(args); code != tc.exitCode {
fatalCommand(t, c.Meta)
}
Expand Down

0 comments on commit 6519070

Please sign in to comment.