2015-05-23 18:08:50 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
2020-11-19 14:54:31 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2015-05-23 18:08:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCoreConfig(t *testing.T) *CoreConfig {
|
|
|
|
// Create some test components
|
|
|
|
components := ComponentFinder{
|
2021-02-02 12:05:04 -05:00
|
|
|
PluginConfig: &PluginConfig{
|
|
|
|
Builders: MapOfBuilder{
|
|
|
|
"test": func() (packersdk.Builder, error) { return &packersdk.MockBuilder{}, nil },
|
|
|
|
},
|
2015-05-23 18:08:50 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &CoreConfig{
|
|
|
|
Components: components,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCore(t *testing.T, c *CoreConfig) *Core {
|
2020-07-24 04:58:03 -04:00
|
|
|
core := NewCore(c)
|
|
|
|
err := core.Initialize()
|
2015-05-23 18:08:50 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return core
|
|
|
|
}
|
2015-05-25 20:58:59 -04:00
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func TestUi(t *testing.T) packersdk.Ui {
|
2015-05-26 12:28:59 -04:00
|
|
|
var buf bytes.Buffer
|
2020-11-20 13:21:29 -05:00
|
|
|
return &packersdk.BasicUi{
|
2015-05-26 12:28:59 -04:00
|
|
|
Reader: &buf,
|
|
|
|
Writer: ioutil.Discard,
|
|
|
|
ErrorWriter: ioutil.Discard,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-25 20:58:59 -04:00
|
|
|
// TestBuilder sets the builder with the name n to the component finder
|
|
|
|
// and returns the mock.
|
2020-12-09 06:39:54 -05:00
|
|
|
func TestBuilder(t *testing.T, c *CoreConfig, n string) *packersdk.MockBuilder {
|
|
|
|
var b packersdk.MockBuilder
|
2015-05-25 20:58:59 -04:00
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
c.Components.PluginConfig.Builders = MapOfBuilder{
|
2020-12-01 16:42:11 -05:00
|
|
|
n: func() (packersdk.Builder, error) { return &b, nil },
|
2015-05-25 20:58:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &b
|
|
|
|
}
|
2015-05-26 12:14:29 -04:00
|
|
|
|
|
|
|
// TestProvisioner sets the prov. with the name n to the component finder
|
|
|
|
// and returns the mock.
|
2020-12-09 06:39:54 -05:00
|
|
|
func TestProvisioner(t *testing.T, c *CoreConfig, n string) *packersdk.MockProvisioner {
|
|
|
|
var b packersdk.MockProvisioner
|
2015-05-26 12:14:29 -04:00
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
c.Components.PluginConfig.Provisioners = MapOfProvisioner{
|
2020-12-01 17:48:55 -05:00
|
|
|
n: func() (packersdk.Provisioner, error) { return &b, nil },
|
2015-05-26 12:14:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &b
|
|
|
|
}
|
2015-05-26 12:28:59 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2021-02-02 12:05:04 -05:00
|
|
|
c.Components.PluginConfig.PostProcessors = MapOfPostProcessor{
|
2020-12-01 17:48:55 -05:00
|
|
|
n: func() (packersdk.PostProcessor, error) { return &b, nil },
|
2015-05-26 12:28:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &b
|
|
|
|
}
|