github.com/hashicorp/packer@v1.14.3/datasource/hcp-packer-version/data_acc_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package hcp_packer_version
     5  
     6  import (
     7  	_ "embed"
     8  	"fmt"
     9  	"os"
    10  	"os/exec"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/hashicorp/packer-plugin-sdk/acctest"
    15  	"github.com/hashicorp/packer/internal/hcp/env"
    16  )
    17  
    18  //go:embed test-fixtures/template.pkr.hcl
    19  var testDatasourceBasic string
    20  
    21  //go:embed test-fixtures/hcp-setup-build.pkr.hcl
    22  var testHCPBuild string
    23  
    24  // Acceptance tests for data sources.
    25  //
    26  // Your HCP credentials must be provided through your runtime
    27  // environment because the template this test uses does not set them.
    28  func TestAccDatasource_HCPPackerVersion(t *testing.T) {
    29  	if os.Getenv(env.HCPClientID) == "" && os.Getenv(env.HCPClientSecret) == "" {
    30  		t.Skipf("Acceptance tests skipped unless envs %q and %q are set", env.HCPClientID, env.HCPClientSecret)
    31  		return
    32  	}
    33  
    34  	tmpFile := filepath.Join(t.TempDir(), "hcp-target-file")
    35  	testSetup := acctest.PluginTestCase{
    36  		Template: fmt.Sprintf(testHCPBuild, tmpFile),
    37  		Check: func(buildCommand *exec.Cmd, logfile string) error {
    38  			if buildCommand.ProcessState != nil {
    39  				if buildCommand.ProcessState.ExitCode() != 0 {
    40  					return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
    41  				}
    42  			}
    43  			return nil
    44  		},
    45  	}
    46  	acctest.TestPlugin(t, &testSetup)
    47  
    48  	testCase := acctest.PluginTestCase{
    49  		Name:     "hcp_packer_version_datasource_basic_test",
    50  		Template: fmt.Sprintf(testDatasourceBasic, filepath.Dir(tmpFile)),
    51  		Setup: func() error {
    52  			if _, err := os.Stat(tmpFile); os.IsNotExist(err) {
    53  				return err
    54  			}
    55  			return nil
    56  		},
    57  		// TODO have acc test write version id to a file and check it to make
    58  		// sure it isn't empty.
    59  		Check: func(buildCommand *exec.Cmd, logfile string) error {
    60  			if buildCommand.ProcessState != nil {
    61  				if buildCommand.ProcessState.ExitCode() != 0 {
    62  					return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
    63  				}
    64  			}
    65  			return nil
    66  		},
    67  	}
    68  	acctest.TestPlugin(t, &testCase)
    69  }