Files
Packer-Cn/hcl2template/types.source_test.go
T

149 lines
3.5 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
2023-08-10 15:53:29 -07:00
// SPDX-License-Identifier: BUSL-1.1
2019-12-17 11:25:56 +01:00
package hcl2template
import (
"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,
parseTestArgs{"testdata/sources/basic.pkr.hcl", nil, nil},
2019-12-17 11:25:56 +01:00
&PackerConfig{
CorePackerVersionString: lockedVersion,
Builds: Builds{
2022-09-23 16:59:17 -04:00
&BuildBlock{
Sources: []SourceUseBlock{
{
SourceRef: SourceRef{
Type: "null",
Name: "test",
},
},
},
},
},
Basedir: filepath.Join("testdata", "sources"),
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,
parseTestArgs{"testdata/sources/untyped.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "sources"),
},
2019-12-17 11:25:56 +01:00
true, true,
nil,
false,
},
{"unnamed source",
defaultParser,
parseTestArgs{"testdata/sources/unnamed.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "sources"),
},
2019-12-17 11:25:56 +01:00
true, true,
nil,
false,
},
{"unused source with unknown type fails",
2019-12-17 11:25:56 +01:00
defaultParser,
parseTestArgs{"testdata/sources/nonexistent.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
2022-09-23 16:59:17 -04:00
Builds: nil,
Basedir: filepath.Join("testdata", "sources"),
Sources: map[SourceRef]SourceBlock{
{Type: "nonexistent", Name: "ubuntu-1204"}: {Type: "nonexistent", Name: "ubuntu-1204"},
},
},
2022-09-23 16:59:17 -04:00
true, true,
[]packersdk.Build{},
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"},
},
},
},
},
},
2019-12-17 11:25:56 +01:00
true, true,
nil,
false,
},
{"duplicate source",
defaultParser,
parseTestArgs{"testdata/sources/duplicate.pkr.hcl", nil, nil},
2019-12-17 11:25:56 +01:00
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "sources"),
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)
}