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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package command
     5  
     6  import (
     7  	"bytes"
     8  	"testing"
     9  
    10  	packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
    11  	"github.com/hashicorp/packer/builder/file"
    12  	"github.com/hashicorp/packer/builder/null"
    13  	hcppackerimagedatasource "github.com/hashicorp/packer/datasource/hcp-packer-image"
    14  	hcppackeriterationdatasource "github.com/hashicorp/packer/datasource/hcp-packer-iteration"
    15  	nulldatasource "github.com/hashicorp/packer/datasource/null"
    16  	"github.com/hashicorp/packer/packer"
    17  	"github.com/hashicorp/packer/post-processor/manifest"
    18  	shell_local_pp "github.com/hashicorp/packer/post-processor/shell-local"
    19  	filep "github.com/hashicorp/packer/provisioner/file"
    20  	"github.com/hashicorp/packer/provisioner/shell"
    21  	shell_local "github.com/hashicorp/packer/provisioner/shell-local"
    22  )
    23  
    24  // Utils to use in other tests
    25  
    26  // TestMetaFile creates a Meta object that includes a file builder
    27  func TestMetaFile(t *testing.T) Meta {
    28  	var out, err bytes.Buffer
    29  	return Meta{
    30  		CoreConfig: testCoreConfigBuilder(t),
    31  		Ui: &packersdk.BasicUi{
    32  			Writer:      &out,
    33  			ErrorWriter: &err,
    34  		},
    35  	}
    36  }
    37  
    38  // GetStdoutAndErrFromTestMeta extracts stdout/stderr from a Meta created by TestMetaFile
    39  func GetStdoutAndErrFromTestMeta(t *testing.T, m Meta) (string, string) {
    40  	ui := m.Ui.(*packersdk.BasicUi)
    41  	out := ui.Writer.(*bytes.Buffer)
    42  	err := ui.ErrorWriter.(*bytes.Buffer)
    43  	return out.String(), err.String()
    44  }
    45  
    46  // testCoreConfigBuilder creates a packer CoreConfig that has a file builder
    47  // available. This allows us to test a builder that writes files to disk.
    48  func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {
    49  	components := packer.ComponentFinder{
    50  		PluginConfig: &packer.PluginConfig{
    51  			Builders: packer.MapOfBuilder{
    52  				"file": func() (packersdk.Builder, error) { return &file.Builder{}, nil },
    53  				"null": func() (packersdk.Builder, error) { return &null.Builder{}, nil },
    54  			},
    55  			Provisioners: packer.MapOfProvisioner{
    56  				"shell-local": func() (packersdk.Provisioner, error) { return &shell_local.Provisioner{}, nil },
    57  				"shell":       func() (packersdk.Provisioner, error) { return &shell.Provisioner{}, nil },
    58  				"file":        func() (packersdk.Provisioner, error) { return &filep.Provisioner{}, nil },
    59  			},
    60  			PostProcessors: packer.MapOfPostProcessor{
    61  				"shell-local": func() (packersdk.PostProcessor, error) { return &shell_local_pp.PostProcessor{}, nil },
    62  				"manifest":    func() (packersdk.PostProcessor, error) { return &manifest.PostProcessor{}, nil },
    63  			},
    64  			DataSources: packer.MapOfDatasource{
    65  				"mock":                 func() (packersdk.Datasource, error) { return &packersdk.MockDatasource{}, nil },
    66  				"null":                 func() (packersdk.Datasource, error) { return &nulldatasource.Datasource{}, nil },
    67  				"hcp-packer-image":     func() (packersdk.Datasource, error) { return &hcppackerimagedatasource.Datasource{}, nil },
    68  				"hcp-packer-iteration": func() (packersdk.Datasource, error) { return &hcppackeriterationdatasource.Datasource{}, nil },
    69  			},
    70  		},
    71  	}
    72  	return &packer.CoreConfig{
    73  		Components: components,
    74  	}
    75  }