mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-24 08:51:40 -04:00
The GetBuilds function, available on both HCL2 and legacy JSON configuration objects, used to return the Build interface. This typing by interface is not useful in this instance, since all the uses of `GetBuilds' are self-contained within Packer, and we're never using any other implementation for it than `*CoreBuild`. We've been relying on the dynamic type for all the builds being *CoreBuild in some places of the code, so to avoid potential surprises in the future, we'll change the signature now so it returns only concrete types.
149 lines
3.4 KiB
Go
149 lines
3.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package hcl2template
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/packer/builder/null"
|
|
"github.com/hashicorp/packer/packer"
|
|
)
|
|
|
|
func TestParse_source(t *testing.T) {
|
|
defaultParser := getBasicParser()
|
|
|
|
tests := []parseTest{
|
|
{"two basic sources",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/sources/basic.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Builds: Builds{
|
|
&BuildBlock{
|
|
Sources: []SourceUseBlock{
|
|
{
|
|
SourceRef: SourceRef{
|
|
Type: "null",
|
|
Name: "test",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Basedir: filepath.Join("testdata", "sources"),
|
|
Sources: map[SourceRef]SourceBlock{
|
|
{
|
|
Type: "virtualbox-iso",
|
|
Name: "ubuntu-1204",
|
|
}: {
|
|
Type: "virtualbox-iso",
|
|
Name: "ubuntu-1204",
|
|
},
|
|
{
|
|
Type: "null",
|
|
Name: "test",
|
|
}: {
|
|
Type: "null",
|
|
Name: "test",
|
|
},
|
|
},
|
|
},
|
|
false, false,
|
|
[]*packer.CoreBuild{
|
|
&packer.CoreBuild{
|
|
Type: "null.test",
|
|
BuilderType: "null",
|
|
Builder: &null.Builder{},
|
|
Provisioners: []packer.CoreBuildProvisioner{},
|
|
PostProcessors: [][]packer.CoreBuildPostProcessor{},
|
|
Prepared: true,
|
|
},
|
|
},
|
|
false,
|
|
},
|
|
{"untyped source",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/sources/untyped.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Basedir: filepath.Join("testdata", "sources"),
|
|
},
|
|
true, true,
|
|
nil,
|
|
false,
|
|
},
|
|
{"unnamed source",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/sources/unnamed.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Basedir: filepath.Join("testdata", "sources"),
|
|
},
|
|
true, true,
|
|
nil,
|
|
false,
|
|
},
|
|
{"unused source with unknown type fails",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/sources/nonexistent.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Builds: nil,
|
|
Basedir: filepath.Join("testdata", "sources"),
|
|
Sources: map[SourceRef]SourceBlock{
|
|
{Type: "nonexistent", Name: "ubuntu-1204"}: {Type: "nonexistent", Name: "ubuntu-1204"},
|
|
},
|
|
},
|
|
true, true,
|
|
[]*packer.CoreBuild{},
|
|
false,
|
|
},
|
|
{"used source with unknown type fails",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/sources/nonexistent_used.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Basedir: filepath.Join("testdata", "sources"),
|
|
Sources: map[SourceRef]SourceBlock{
|
|
{Type: "nonexistent", Name: "ubuntu-1204"}: {Type: "nonexistent", Name: "ubuntu-1204"},
|
|
},
|
|
Builds: Builds{
|
|
&BuildBlock{
|
|
Sources: []SourceUseBlock{
|
|
{
|
|
SourceRef: SourceRef{Type: "nonexistent", Name: "ubuntu-1204"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
true, true,
|
|
nil,
|
|
false,
|
|
},
|
|
{"duplicate source",
|
|
defaultParser,
|
|
parseTestArgs{"testdata/sources/duplicate.pkr.hcl", nil, nil},
|
|
&PackerConfig{
|
|
CorePackerVersionString: lockedVersion,
|
|
Basedir: filepath.Join("testdata", "sources"),
|
|
Sources: map[SourceRef]SourceBlock{
|
|
{
|
|
Type: "virtualbox-iso",
|
|
Name: "ubuntu-1204",
|
|
}: {
|
|
Type: "virtualbox-iso",
|
|
Name: "ubuntu-1204",
|
|
},
|
|
},
|
|
},
|
|
true, true,
|
|
nil,
|
|
false,
|
|
},
|
|
}
|
|
testParse(t, tests)
|
|
}
|