github.com/hashicorp/packer@v1.14.3/command/hcl2_upgrade_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package command
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  )
    13  
    14  func Test_hcl2_upgrade(t *testing.T) {
    15  
    16  	tc := []struct {
    17  		folder    string
    18  		flags     []string
    19  		exitCode  int
    20  		exitEarly bool
    21  	}{
    22  		{folder: "unknown_builder", flags: []string{}, exitCode: 1}, // warn for unknown components not tracked in knownPluginPrefixes
    23  		{folder: "complete", flags: []string{"-with-annotations"}, exitCode: 0},
    24  		{folder: "without-annotations", flags: []string{}, exitCode: 0},
    25  		{folder: "minimal", flags: []string{"-with-annotations"}, exitCode: 0},
    26  		{folder: "source-name", flags: []string{"-with-annotations"}, exitCode: 0},
    27  		{folder: "error-cleanup-provisioner", flags: []string{"-with-annotations"}, exitCode: 0},
    28  		{folder: "aws-access-config", flags: []string{}, exitCode: 0},
    29  		{folder: "escaping", flags: []string{}, exitCode: 0},
    30  		{folder: "vsphere_linux_options_and_network_interface", flags: []string{}, exitCode: 0}, //do not warn for known uninstalled plugins components
    31  		{folder: "nonexistent", flags: []string{}, exitCode: 1, exitEarly: true},
    32  		{folder: "placeholders", flags: []string{}, exitCode: 0},
    33  		{folder: "ami_test", flags: []string{}, exitCode: 0},
    34  		{folder: "azure_shg", flags: []string{}, exitCode: 0},
    35  		{folder: "variables-only", flags: []string{}, exitCode: 0},
    36  		{folder: "variables-with-variables", flags: []string{}, exitCode: 0},
    37  		{folder: "complete-variables-with-template-engine", flags: []string{}, exitCode: 0},
    38  		{folder: "undeclared-variables", flags: []string{}, exitCode: 0},
    39  		{folder: "varfile-with-no-variables-block", flags: []string{}, exitCode: 0},
    40  		{folder: "bundled-plugin-used", flags: []string{}, exitCode: 0},
    41  	}
    42  
    43  	for _, tc := range tc {
    44  		t.Run(tc.folder, func(t *testing.T) {
    45  			inputPath := filepath.Join(testFixture("hcl2_upgrade", tc.folder, "input.json"))
    46  			outputPath := inputPath + ".pkr.hcl"
    47  			expectedPath := filepath.Join(testFixture("hcl2_upgrade", tc.folder, "expected.pkr.hcl"))
    48  			args := []string{"hcl2_upgrade"}
    49  			if len(tc.flags) > 0 {
    50  				args = append(args, tc.flags...)
    51  			}
    52  			args = append(args, inputPath)
    53  			p := helperCommand(t, args...)
    54  			err := p.Run()
    55  			defer os.Remove(outputPath)
    56  			if err != nil {
    57  				t.Logf("run returned an error: %s", err)
    58  			}
    59  			actualExitCode := p.ProcessState.ExitCode()
    60  			if tc.exitCode != actualExitCode {
    61  				t.Fatalf("unexpected exit code: %d found; expected %d ", actualExitCode, tc.exitCode)
    62  			}
    63  			if tc.exitEarly {
    64  				return
    65  			}
    66  			expected := string(mustBytes(os.ReadFile(expectedPath)))
    67  			actual := string(mustBytes(os.ReadFile(outputPath)))
    68  
    69  			if diff := cmp.Diff(expected, actual); diff != "" {
    70  				t.Fatalf("unexpected output: %s", diff)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func mustBytes(b []byte, e error) []byte {
    77  	if e != nil {
    78  		panic(e)
    79  	}
    80  	return b
    81  }