mirror of
https://github.com/hashicorp/packer.git
synced 2026-06-09 00:32:09 -04:00
Evaluating local variables used to be directly written to the PackerConfig while each variable was created. This was somewhat of an issue with testing, as we have a bunch of tests that relied on `PackerConfig.Variables` being set only when we actually write something. This is not really a concern for normal use, just for testing, but to limit the number of changes to the tests in hcl2template, I opted to change how variables' values are retained, so that evaluating a single variable returns a Variable in addition to hcl.Diagnostics, so we can reify the approach and only create the map of variables if there's something evaluated.
255 lines
5.3 KiB
Go
255 lines
5.3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package hcl2template
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
"github.com/hashicorp/packer/builder/null"
|
|
"github.com/hashicorp/packer/packer"
|
|
)
|
|
|
|
func TestParse_datasource(t *testing.T) {
|
|
defaultParser := getBasicParser()
|
|
|
|
tests := []parseTest{
|
|
{"two basic datasources",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/datasources/basic.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Builds: Builds{
|
|
&BuildBlock{
|
|
Sources: []SourceUseBlock{
|
|
{
|
|
SourceRef: SourceRef{
|
|
Type: "null",
|
|
Name: "test",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Sources: map[SourceRef]SourceBlock{
|
|
{
|
|
Type: "null",
|
|
Name: "test",
|
|
}: {
|
|
Type: "null",
|
|
Name: "test",
|
|
},
|
|
},
|
|
Basedir: filepath.Join("testdata", "datasources"),
|
|
Datasources: Datasources{
|
|
{
|
|
Type: "amazon-ami",
|
|
Name: "test",
|
|
}: {
|
|
Type: "amazon-ami",
|
|
DSName: "test",
|
|
},
|
|
},
|
|
},
|
|
false, false,
|
|
[]packersdk.Build{
|
|
&packer.CoreBuild{
|
|
Type: "null.test",
|
|
BuilderType: "null",
|
|
Builder: &null.Builder{},
|
|
Provisioners: []packer.CoreBuildProvisioner{},
|
|
PostProcessors: [][]packer.CoreBuildPostProcessor{},
|
|
Prepared: true,
|
|
},
|
|
},
|
|
false,
|
|
},
|
|
{"recursive datasources",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/datasources/recursive.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Builds: Builds{
|
|
&BuildBlock{
|
|
Sources: []SourceUseBlock{
|
|
{
|
|
SourceRef: SourceRef{
|
|
Type: "null",
|
|
Name: "test",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Sources: map[SourceRef]SourceBlock{
|
|
{
|
|
Type: "null",
|
|
Name: "test",
|
|
}: {
|
|
Type: "null",
|
|
Name: "test",
|
|
},
|
|
},
|
|
Basedir: filepath.Join("testdata", "datasources"),
|
|
Datasources: Datasources{
|
|
{
|
|
Type: "null",
|
|
Name: "foo",
|
|
}: {
|
|
Type: "null",
|
|
DSName: "foo",
|
|
},
|
|
{
|
|
Type: "null",
|
|
Name: "bar",
|
|
}: {
|
|
Type: "null",
|
|
DSName: "bar",
|
|
},
|
|
{
|
|
Type: "null",
|
|
Name: "baz",
|
|
}: {
|
|
Type: "null",
|
|
DSName: "baz",
|
|
Dependencies: []refString{
|
|
{"data", "null", "foo"},
|
|
{"data", "null", "bar"},
|
|
},
|
|
},
|
|
{
|
|
Type: "null",
|
|
Name: "bang",
|
|
}: {
|
|
Type: "null",
|
|
DSName: "bang",
|
|
Dependencies: []refString{
|
|
{"data", "null", "baz"},
|
|
},
|
|
},
|
|
{
|
|
Type: "null",
|
|
Name: "yummy",
|
|
}: {
|
|
Type: "null",
|
|
DSName: "yummy",
|
|
Dependencies: []refString{
|
|
{"data", "null", "bang"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
false, false,
|
|
[]packersdk.Build{
|
|
&packer.CoreBuild{
|
|
Type: "null.test",
|
|
BuilderType: "null",
|
|
Builder: &null.Builder{},
|
|
Provisioners: []packer.CoreBuildProvisioner{},
|
|
PostProcessors: [][]packer.CoreBuildPostProcessor{},
|
|
Prepared: true,
|
|
},
|
|
},
|
|
false,
|
|
},
|
|
{"untyped datasource",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/datasources/untyped.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Basedir: filepath.Join("testdata", "datasources"),
|
|
},
|
|
true, true,
|
|
nil,
|
|
false,
|
|
},
|
|
{"unnamed source",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/datasources/unnamed.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Basedir: filepath.Join("testdata", "datasources"),
|
|
},
|
|
true, true,
|
|
nil,
|
|
false,
|
|
},
|
|
{"nonexistent source",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/datasources/nonexistent.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Basedir: filepath.Join("testdata", "datasources"),
|
|
Datasources: Datasources{
|
|
{
|
|
Type: "nonexistent",
|
|
Name: "test",
|
|
}: {
|
|
Type: "nonexistent",
|
|
DSName: "test",
|
|
},
|
|
},
|
|
},
|
|
true, true,
|
|
nil,
|
|
false,
|
|
},
|
|
{"duplicate source",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/datasources/duplicate.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Basedir: filepath.Join("testdata", "datasources"),
|
|
Datasources: Datasources{
|
|
{
|
|
Type: "amazon-ami",
|
|
Name: "test",
|
|
}: {
|
|
Type: "amazon-ami",
|
|
DSName: "test",
|
|
},
|
|
},
|
|
},
|
|
true, true,
|
|
nil,
|
|
false,
|
|
},
|
|
{"cyclic dependency between data sources",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/datasources/dependency_cycle.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Basedir: filepath.Join("testdata", "datasources"),
|
|
Datasources: Datasources{
|
|
{
|
|
Type: "null",
|
|
Name: "gummy",
|
|
}: {
|
|
Type: "null",
|
|
DSName: "gummy",
|
|
Dependencies: []refString{
|
|
{"data", "null", "bear"},
|
|
},
|
|
},
|
|
{
|
|
Type: "null",
|
|
Name: "bear",
|
|
}: {
|
|
Type: "null",
|
|
DSName: "bear",
|
|
Dependencies: []refString{
|
|
{"data", "null", "gummy"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
true, true,
|
|
nil,
|
|
false,
|
|
},
|
|
}
|
|
|
|
testParse(t, tests)
|
|
}
|