5ba134ac5b
hcl2_upgrade transforms a JSON build-file in a HCL2 build-file. This starts a validated Packer core and from that core we generate an HCL 'block' per plugin/configuration. So for a builder, a provisioner, a post-processor or a variable. The contents of each block is just transformed as is and basically all fields are HCL2-ified. A generated field can be valid in JSON but invalid on HCL2; for example JSON templating (in mapstructure) allows to set arrays of strings - like `x = ["a", "b"]` - with single strings - like `x="a"` -, HCL does not allow this. Since JSON does not make the distinction between variables and locals, everything will be a variable. So variables that use other variables will not work. hcl2_upgrade tries to transform go templating interpolation calls to HCL2 calls when possible, leaving the go templating calls like they are in case it cannot. Work: * transpiler * tests * update hcl v2 library so that output looks great. * update docs
52 lines
1022 B
Go
52 lines
1022 B
Go
package command
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
func Test_hcl2_upgrade(t *testing.T) {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("Getwd: %v", err)
|
|
}
|
|
_ = cwd
|
|
|
|
tc := []struct {
|
|
folder string
|
|
}{
|
|
{"hcl2_upgrade_basic"},
|
|
}
|
|
|
|
for _, tc := range tc {
|
|
t.Run(tc.folder, func(t *testing.T) {
|
|
inputPath := filepath.Join(testFixture(tc.folder, "input.json"))
|
|
outputPath := inputPath + ".pkr.hcl"
|
|
expectedPath := filepath.Join(testFixture(tc.folder, "expected.pkr.hcl"))
|
|
p := helperCommand(t, "hcl2_upgrade", inputPath)
|
|
bs, err := p.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%v %s", err, bs)
|
|
}
|
|
expected := mustBytes(ioutil.ReadFile(expectedPath))
|
|
actual := mustBytes(ioutil.ReadFile(outputPath))
|
|
|
|
if diff := cmp.Diff(expected, actual); diff != "" {
|
|
t.Fatalf("unexpected output: %s", diff)
|
|
}
|
|
os.Remove(outputPath)
|
|
})
|
|
}
|
|
}
|
|
|
|
func mustBytes(b []byte, e error) []byte {
|
|
if e != nil {
|
|
panic(e)
|
|
}
|
|
return b
|
|
}
|