From 7fae5b61fed27ab1ec1c76592ec31cb6a9d4616b Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Thu, 5 Sep 2024 15:58:50 -0400 Subject: [PATCH] packer_test: add base tests for DAG eval Since we introduce the DAG with this series of commits, only on locals and data sources, we need to make sure that the behaviour is what we expect. Therefore, this commit adds a basic test with Packer build, and packer validate, to evaluate a template with both locals and data sources depending on one another. This is rejected with the sequential evaluation methods, as we process the different types one-by-one, whereas the DAG allows us to mix the order between the two, while still rejecting circular dependencies (and doing that before they even get evaluated), and self-references. --- packer_test/dag_tests/mix_data_locals_test.go | 26 +++++++++++++++++++ packer_test/dag_tests/suite_test.go | 23 ++++++++++++++++ .../templates/mixed_data_local.pkr.hcl | 23 ++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 packer_test/dag_tests/mix_data_locals_test.go create mode 100644 packer_test/dag_tests/suite_test.go create mode 100644 packer_test/dag_tests/templates/mixed_data_local.pkr.hcl diff --git a/packer_test/dag_tests/mix_data_locals_test.go b/packer_test/dag_tests/mix_data_locals_test.go new file mode 100644 index 00000000000..8ec19711d54 --- /dev/null +++ b/packer_test/dag_tests/mix_data_locals_test.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + + "github.com/hashicorp/packer/packer_test/lib" +) + +func (ts *PackerDAGTestSuite) TestWithBothDataLocalMixedOrder() { + pluginDir, cleanup := ts.MakePluginDir() + defer cleanup() + + for _, cmd := range []string{"build", "validate"} { + ts.Run(fmt.Sprintf("%s: evaluating with DAG - success expected", cmd), func() { + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs(cmd, "./templates/mixed_data_local.pkr.hcl"). + Assert(lib.MustSucceed()) + }) + + ts.Run(fmt.Sprintf("%s: evaluating sequentially - failure expected", cmd), func() { + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs(cmd, "--use-sequential-evaluation", "./templates/mixed_data_local.pkr.hcl"). + Assert(lib.MustFail()) + }) + } +} diff --git a/packer_test/dag_tests/suite_test.go b/packer_test/dag_tests/suite_test.go new file mode 100644 index 00000000000..f9cdce19bfd --- /dev/null +++ b/packer_test/dag_tests/suite_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "testing" + + "github.com/hashicorp/packer/packer_test/lib" + "github.com/stretchr/testify/suite" +) + +type PackerDAGTestSuite struct { + *lib.PackerTestSuite +} + +func Test_PackerDAGSuite(t *testing.T) { + baseSuite, cleanup := lib.PackerCoreSuite(t) + defer cleanup() + + ts := &PackerDAGTestSuite{ + baseSuite, + } + + suite.Run(t, ts) +} diff --git a/packer_test/dag_tests/templates/mixed_data_local.pkr.hcl b/packer_test/dag_tests/templates/mixed_data_local.pkr.hcl new file mode 100644 index 00000000000..a0447538eb2 --- /dev/null +++ b/packer_test/dag_tests/templates/mixed_data_local.pkr.hcl @@ -0,0 +1,23 @@ +data "null" "head" { + input = "foo" +} + +locals { + loc = "${data.null.head.output}" +} + +data "null" "tail" { + input = "${local.loc}" +} + +locals { + last = "final - ${data.null.tail.output}" +} + +source "null" "test" { + communicator = "none" +} + +build { + sources = ["null.test"] +}