mirror of
https://github.com/hashicorp/packer.git
synced 2026-10-13 17:52:10 -04:00
* Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at https://hashi.co/license-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package hcl2template
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
)
|
|
|
|
func TestPackerConfig_ParseProvisionerBlock(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
inputFile string
|
|
expectError bool
|
|
expectedErrorMessage string
|
|
}{
|
|
{
|
|
"success - provisioner is valid",
|
|
"fixtures/well_formed_provisioner.pkr.hcl",
|
|
false,
|
|
"",
|
|
},
|
|
{
|
|
"failure - provisioner override is malformed",
|
|
"fixtures/malformed_override.pkr.hcl",
|
|
true,
|
|
"provisioner's override block must be an HCL object",
|
|
},
|
|
{
|
|
"failure - provisioner override.test is malformed",
|
|
"fixtures/malformed_override_innards.pkr.hcl",
|
|
true,
|
|
"provisioner's override.'test' block must be an HCL object",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
cfg := PackerConfig{parser: getBasicParser()}
|
|
f, diags := cfg.parser.ParseHCLFile(test.inputFile)
|
|
if diags.HasErrors() {
|
|
t.Errorf("failed to parse input file %s", test.inputFile)
|
|
for _, d := range diags {
|
|
t.Errorf("%s", d)
|
|
}
|
|
return
|
|
}
|
|
provBlock := f.OutermostBlockAtPos(hcl.Pos{
|
|
Line: 1,
|
|
Column: 1,
|
|
Byte: 0,
|
|
})
|
|
_, diags = cfg.parser.decodeProvisioner(provBlock, nil)
|
|
|
|
if !diags.HasErrors() {
|
|
if !test.expectError {
|
|
return
|
|
}
|
|
|
|
t.Fatalf("unexpected success")
|
|
}
|
|
|
|
if !test.expectError {
|
|
for _, d := range diags {
|
|
t.Errorf("%s", d)
|
|
}
|
|
}
|
|
|
|
gotExpectedErr := false
|
|
for _, d := range diags {
|
|
if d.Summary == test.expectedErrorMessage {
|
|
gotExpectedErr = true
|
|
}
|
|
|
|
t.Logf("got error (expected): '%s'", d.Summary)
|
|
}
|
|
|
|
if !gotExpectedErr {
|
|
t.Errorf("never got expected error: '%s'", test.expectedErrorMessage)
|
|
}
|
|
})
|
|
}
|
|
}
|