packer-cn/packer/testing.go
Adrien Delorme ed091163be
HCL2 Parse packer.required_plugins block + packer init (#10304)
This adds the new `required_plugins` block to be nested under the packer block.

Example:
```hcl
packer {
  required_plugins {
    aws = {
      version = ">= 2.7.0"
      source = "azr/aws"
    }
    azure = ">= 2.7.0"
  }
}
```

For example on darwin_amd64 Packer will install those under :
* "${PACKER_HOME_DIR}/plugin/github.com/azr/amazon/packer-plugin-amazon_2.7.0_x5.0_darwin_amd64"
* "${PACKER_HOME_DIR}/plugin/github.com/hashicorp/azure/packer-plugin-azure_2.7.0_x5.0_darwin_amd64_x5"

+ docs
+ tests
2021-02-02 18:05:04 +01:00

80 lines
1.8 KiB
Go

package packer
import (
"bytes"
"io/ioutil"
"testing"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)
func TestCoreConfig(t *testing.T) *CoreConfig {
// Create some test components
components := ComponentFinder{
PluginConfig: &PluginConfig{
Builders: MapOfBuilder{
"test": func() (packersdk.Builder, error) { return &packersdk.MockBuilder{}, nil },
},
},
}
return &CoreConfig{
Components: components,
}
}
func TestCore(t *testing.T, c *CoreConfig) *Core {
core := NewCore(c)
err := core.Initialize()
if err != nil {
t.Fatalf("err: %s", err)
}
return core
}
func TestUi(t *testing.T) packersdk.Ui {
var buf bytes.Buffer
return &packersdk.BasicUi{
Reader: &buf,
Writer: ioutil.Discard,
ErrorWriter: ioutil.Discard,
}
}
// TestBuilder sets the builder with the name n to the component finder
// and returns the mock.
func TestBuilder(t *testing.T, c *CoreConfig, n string) *packersdk.MockBuilder {
var b packersdk.MockBuilder
c.Components.PluginConfig.Builders = MapOfBuilder{
n: func() (packersdk.Builder, error) { return &b, nil },
}
return &b
}
// TestProvisioner sets the prov. with the name n to the component finder
// and returns the mock.
func TestProvisioner(t *testing.T, c *CoreConfig, n string) *packersdk.MockProvisioner {
var b packersdk.MockProvisioner
c.Components.PluginConfig.Provisioners = MapOfProvisioner{
n: func() (packersdk.Provisioner, error) { return &b, nil },
}
return &b
}
// TestPostProcessor sets the prov. with the name n to the component finder
// and returns the mock.
func TestPostProcessor(t *testing.T, c *CoreConfig, n string) *MockPostProcessor {
var b MockPostProcessor
c.Components.PluginConfig.PostProcessors = MapOfPostProcessor{
n: func() (packersdk.PostProcessor, error) { return &b, nil },
}
return &b
}