github.com/hashicorp/packer@v1.14.3/acctest/plugin/component_acc_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  // component_acc_test.go should contain acceptance tests for plugin components
     5  // to make sure all component types can be discovered and started.
     6  package plugin
     7  
     8  import (
     9  	_ "embed"
    10  	"fmt"
    11  	"io"
    12  	"os"
    13  	"os/exec"
    14  	"testing"
    15  
    16  	"github.com/hashicorp/packer-plugin-sdk/acctest"
    17  	"github.com/hashicorp/packer/hcl2template/addrs"
    18  )
    19  
    20  //go:embed test-fixtures/basic-amazon-ami-datasource.pkr.hcl
    21  var basicAmazonAmiDatasourceHCL2Template string
    22  
    23  func TestAccInitAndBuildBasicAmazonAmiDatasource(t *testing.T) {
    24  	plugin := addrs.Plugin{
    25  		Source: "github.com/hashicorp/amazon",
    26  	}
    27  	testCase := &acctest.PluginTestCase{
    28  		Name: "amazon-ami_basic_datasource_test",
    29  		Setup: func() error {
    30  			return cleanupPluginInstallation(plugin)
    31  		},
    32  		Template: basicAmazonAmiDatasourceHCL2Template,
    33  		Type:     "amazon-ami",
    34  		Init:     true,
    35  		CheckInit: func(initCommand *exec.Cmd, logfile string) error {
    36  			if initCommand.ProcessState != nil {
    37  				if initCommand.ProcessState.ExitCode() != 0 {
    38  					return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
    39  				}
    40  			}
    41  			logs, err := os.Open(logfile)
    42  			if err != nil {
    43  				return fmt.Errorf("Unable find %s", logfile)
    44  			}
    45  			defer logs.Close()
    46  
    47  			logsBytes, err := io.ReadAll(logs)
    48  			if err != nil {
    49  				return fmt.Errorf("Unable to read %s", logfile)
    50  			}
    51  			initOutput := string(logsBytes)
    52  			return checkPluginInstallation(initOutput, plugin)
    53  		},
    54  		Check: func(buildCommand *exec.Cmd, logfile string) error {
    55  			if buildCommand.ProcessState != nil {
    56  				if buildCommand.ProcessState.ExitCode() != 0 {
    57  					return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
    58  				}
    59  			}
    60  			return nil
    61  		},
    62  	}
    63  	acctest.TestPlugin(t, testCase)
    64  }