2023-03-02 15:37:05 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-10 15:53:29 -07:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-02 15:37:05 -05:00
|
|
|
|
2019-12-17 11:25:56 +01:00
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
|
|
import (
|
2020-02-06 11:49:21 +01:00
|
|
|
"path/filepath"
|
2019-12-17 11:25:56 +01:00
|
|
|
"testing"
|
|
|
|
|
|
2020-12-17 13:29:25 -08:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2022-09-23 16:59:17 -04:00
|
|
|
"github.com/hashicorp/packer/builder/null"
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2019-12-17 11:25:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestParse_source(t *testing.T) {
|
|
|
|
|
defaultParser := getBasicParser()
|
|
|
|
|
|
|
|
|
|
tests := []parseTest{
|
|
|
|
|
{"two basic sources",
|
|
|
|
|
defaultParser,
|
2020-03-12 14:27:56 +01:00
|
|
|
parseTestArgs{"testdata/sources/basic.pkr.hcl", nil, nil},
|
2019-12-17 11:25:56 +01:00
|
|
|
&PackerConfig{
|
2021-02-02 18:05:04 +01:00
|
|
|
CorePackerVersionString: lockedVersion,
|
2022-09-08 10:12:12 -04:00
|
|
|
Builds: Builds{
|
2022-09-23 16:59:17 -04:00
|
|
|
&BuildBlock{
|
|
|
|
|
Sources: []SourceUseBlock{
|
|
|
|
|
{
|
|
|
|
|
SourceRef: SourceRef{
|
|
|
|
|
Type: "null",
|
|
|
|
|
Name: "test",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-09-08 10:12:12 -04:00
|
|
|
},
|
|
|
|
|
Basedir: filepath.Join("testdata", "sources"),
|
2020-05-25 17:09:37 +02:00
|
|
|
Sources: map[SourceRef]SourceBlock{
|
2020-01-28 16:49:16 -05:00
|
|
|
{
|
2019-12-17 11:25:56 +01:00
|
|
|
Type: "virtualbox-iso",
|
|
|
|
|
Name: "ubuntu-1204",
|
|
|
|
|
}: {
|
|
|
|
|
Type: "virtualbox-iso",
|
|
|
|
|
Name: "ubuntu-1204",
|
|
|
|
|
},
|
2022-09-23 16:59:17 -04:00
|
|
|
{
|
|
|
|
|
Type: "null",
|
|
|
|
|
Name: "test",
|
|
|
|
|
}: {
|
|
|
|
|
Type: "null",
|
|
|
|
|
Name: "test",
|
|
|
|
|
},
|
2019-12-17 11:25:56 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
false, false,
|
2022-09-23 16:59:17 -04:00
|
|
|
[]packersdk.Build{
|
|
|
|
|
&packer.CoreBuild{
|
|
|
|
|
Type: "null.test",
|
|
|
|
|
Builder: &null.Builder{},
|
|
|
|
|
Provisioners: []packer.CoreBuildProvisioner{},
|
|
|
|
|
PostProcessors: [][]packer.CoreBuildPostProcessor{},
|
|
|
|
|
Prepared: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-12-17 11:25:56 +01:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{"untyped source",
|
|
|
|
|
defaultParser,
|
2020-03-12 14:27:56 +01:00
|
|
|
parseTestArgs{"testdata/sources/untyped.pkr.hcl", nil, nil},
|
2020-02-06 11:49:21 +01:00
|
|
|
&PackerConfig{
|
2021-02-02 18:05:04 +01:00
|
|
|
CorePackerVersionString: lockedVersion,
|
|
|
|
|
Basedir: filepath.Join("testdata", "sources"),
|
2020-02-06 11:49:21 +01:00
|
|
|
},
|
2019-12-17 11:25:56 +01:00
|
|
|
true, true,
|
|
|
|
|
nil,
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{"unnamed source",
|
|
|
|
|
defaultParser,
|
2020-03-12 14:27:56 +01:00
|
|
|
parseTestArgs{"testdata/sources/unnamed.pkr.hcl", nil, nil},
|
2020-02-06 11:49:21 +01:00
|
|
|
&PackerConfig{
|
2021-02-02 18:05:04 +01:00
|
|
|
CorePackerVersionString: lockedVersion,
|
|
|
|
|
Basedir: filepath.Join("testdata", "sources"),
|
2020-02-06 11:49:21 +01:00
|
|
|
},
|
2019-12-17 11:25:56 +01:00
|
|
|
true, true,
|
|
|
|
|
nil,
|
|
|
|
|
false,
|
|
|
|
|
},
|
2021-02-02 18:05:04 +01:00
|
|
|
{"unused source with unknown type fails",
|
2019-12-17 11:25:56 +01:00
|
|
|
defaultParser,
|
2021-10-11 15:48:51 +02:00
|
|
|
parseTestArgs{"testdata/sources/nonexistent.pkr.hcl", nil, nil},
|
2020-02-06 11:49:21 +01:00
|
|
|
&PackerConfig{
|
2021-02-02 18:05:04 +01:00
|
|
|
CorePackerVersionString: lockedVersion,
|
2022-09-23 16:59:17 -04:00
|
|
|
Builds: nil,
|
|
|
|
|
Basedir: filepath.Join("testdata", "sources"),
|
2021-02-02 18:05:04 +01:00
|
|
|
Sources: map[SourceRef]SourceBlock{
|
2021-10-11 15:48:51 +02:00
|
|
|
{Type: "nonexistent", Name: "ubuntu-1204"}: {Type: "nonexistent", Name: "ubuntu-1204"},
|
2021-02-02 18:05:04 +01:00
|
|
|
},
|
|
|
|
|
},
|
2022-09-23 16:59:17 -04:00
|
|
|
true, true,
|
2021-02-02 18:05:04 +01:00
|
|
|
[]packersdk.Build{},
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{"used source with unknown type fails",
|
|
|
|
|
defaultParser,
|
2021-10-11 15:48:51 +02:00
|
|
|
parseTestArgs{"testdata/sources/nonexistent_used.pkr.hcl", nil, nil},
|
2021-02-02 18:05:04 +01:00
|
|
|
&PackerConfig{
|
|
|
|
|
CorePackerVersionString: lockedVersion,
|
|
|
|
|
Basedir: filepath.Join("testdata", "sources"),
|
|
|
|
|
Sources: map[SourceRef]SourceBlock{
|
2021-10-11 15:48:51 +02:00
|
|
|
{Type: "nonexistent", Name: "ubuntu-1204"}: {Type: "nonexistent", Name: "ubuntu-1204"},
|
2021-02-02 18:05:04 +01:00
|
|
|
},
|
|
|
|
|
Builds: Builds{
|
|
|
|
|
&BuildBlock{
|
|
|
|
|
Sources: []SourceUseBlock{
|
|
|
|
|
{
|
2021-10-11 15:48:51 +02:00
|
|
|
SourceRef: SourceRef{Type: "nonexistent", Name: "ubuntu-1204"},
|
2021-02-02 18:05:04 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-02-06 11:49:21 +01:00
|
|
|
},
|
2019-12-17 11:25:56 +01:00
|
|
|
true, true,
|
|
|
|
|
nil,
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{"duplicate source",
|
|
|
|
|
defaultParser,
|
2020-03-12 14:27:56 +01:00
|
|
|
parseTestArgs{"testdata/sources/duplicate.pkr.hcl", nil, nil},
|
2019-12-17 11:25:56 +01:00
|
|
|
&PackerConfig{
|
2021-02-02 18:05:04 +01:00
|
|
|
CorePackerVersionString: lockedVersion,
|
|
|
|
|
Basedir: filepath.Join("testdata", "sources"),
|
2020-05-25 17:09:37 +02:00
|
|
|
Sources: map[SourceRef]SourceBlock{
|
2020-01-28 16:49:16 -05:00
|
|
|
{
|
2019-12-17 11:25:56 +01:00
|
|
|
Type: "virtualbox-iso",
|
|
|
|
|
Name: "ubuntu-1204",
|
|
|
|
|
}: {
|
|
|
|
|
Type: "virtualbox-iso",
|
|
|
|
|
Name: "ubuntu-1204",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
true, true,
|
|
|
|
|
nil,
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
testParse(t, tests)
|
|
|
|
|
}
|