add some tests for packer_plugin_path
This commit is contained in:
parent
a163ff7388
commit
69e7d39fd3
144
config_test.go
144
config_test.go
|
@ -12,8 +12,106 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/hashicorp/packer/packer"
|
||||
"github.com/hashicorp/packer/packer/plugin"
|
||||
)
|
||||
|
||||
func newConfig() config {
|
||||
var conf config
|
||||
conf.PluginMinPort = 10000
|
||||
conf.PluginMaxPort = 25000
|
||||
conf.Builders = packer.MapOfBuilder{}
|
||||
conf.PostProcessors = packer.MapOfPostProcessor{}
|
||||
conf.Provisioners = packer.MapOfProvisioner{}
|
||||
|
||||
return conf
|
||||
}
|
||||
func TestDiscoverReturnsIfMagicCookieSet(t *testing.T) {
|
||||
config := newConfig()
|
||||
|
||||
os.Setenv(plugin.MagicCookieKey, plugin.MagicCookieValue)
|
||||
defer os.Unsetenv(plugin.MagicCookieKey)
|
||||
|
||||
err := config.Discover()
|
||||
if err != nil {
|
||||
t.Fatalf("Should not have errored: %s", err)
|
||||
}
|
||||
|
||||
if len(config.Builders) != 0 {
|
||||
t.Fatalf("Should not have tried to find builders")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvVarPackerPluginPath(t *testing.T) {
|
||||
// Create a temporary directory to store plugins in
|
||||
dir, _, cleanUpFunc, err := generateFakePlugins("custom_plugin_dir",
|
||||
[]string{"packer-provisioner-partyparrot"})
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating fake custom plugins: %s", err)
|
||||
}
|
||||
|
||||
defer cleanUpFunc()
|
||||
|
||||
// Add temp dir to path.
|
||||
os.Setenv("PACKER_PLUGIN_PATH", dir)
|
||||
defer os.Unsetenv("PACKER_PLUGIN_PATH")
|
||||
|
||||
config := newConfig()
|
||||
|
||||
err = config.Discover()
|
||||
if err != nil {
|
||||
t.Fatalf("Should not have errored: %s", err)
|
||||
}
|
||||
|
||||
if len(config.Provisioners) == 0 {
|
||||
t.Fatalf("Should have found partyparrot provisioner")
|
||||
}
|
||||
if _, ok := config.Provisioners["partyparrot"]; !ok {
|
||||
t.Fatalf("Should have found partyparrot provisioner.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvVarPackerPluginPath_MultiplePaths(t *testing.T) {
|
||||
// Create a temporary directory to store plugins in
|
||||
dir, _, cleanUpFunc, err := generateFakePlugins("custom_plugin_dir",
|
||||
[]string{"packer-provisioner-partyparrot"})
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating fake custom plugins: %s", err)
|
||||
}
|
||||
|
||||
defer cleanUpFunc()
|
||||
|
||||
pathsep := ":"
|
||||
if runtime.GOOS == "windows" {
|
||||
pathsep = ";"
|
||||
}
|
||||
|
||||
// Create a second dir to look in that will be empty
|
||||
decoyDir, err := ioutil.TempDir("", "decoy")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create a temporary test dir.")
|
||||
}
|
||||
|
||||
pluginPath := dir + pathsep + decoyDir
|
||||
|
||||
// Add temp dir to path.
|
||||
os.Setenv("PACKER_PLUGIN_PATH", pluginPath)
|
||||
defer os.Unsetenv("PACKER_PLUGIN_PATH")
|
||||
|
||||
config := newConfig()
|
||||
|
||||
err = config.Discover()
|
||||
if err != nil {
|
||||
t.Fatalf("Should not have errored: %s", err)
|
||||
}
|
||||
|
||||
if len(config.Provisioners) == 0 {
|
||||
t.Fatalf("Should have found partyparrot provisioner")
|
||||
}
|
||||
if _, ok := config.Provisioners["partyparrot"]; !ok {
|
||||
t.Fatalf("Should have found partyparrot provisioner.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeConfig(t *testing.T) {
|
||||
|
||||
packerConfig := `
|
||||
|
@ -107,18 +205,13 @@ func TestLoadExternalComponentsFromConfig_onlyProvisioner(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
/* generateFakePackerConfigData creates a collection of mock plugins along with a basic packerconfig.
|
||||
The return packerConfigData is a valid packerconfig file that can be used for configuring external plugins, cleanUpFunc is a function that should be called for cleaning up any generated mock data.
|
||||
This function will only clean up if there is an error, on successful runs the caller
|
||||
is responsible for cleaning up the data via cleanUpFunc().
|
||||
*/
|
||||
func generateFakePackerConfigData() (packerConfigData string, cleanUpFunc func(), err error) {
|
||||
dir, err := ioutil.TempDir("", "random-testdata")
|
||||
func generateFakePlugins(dirname string, pluginNames []string) (string, []string, func(), error) {
|
||||
dir, err := ioutil.TempDir("", dirname)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("failed to create temporary test directory: %v", err)
|
||||
return "", nil, nil, fmt.Errorf("failed to create temporary test directory: %v", err)
|
||||
}
|
||||
|
||||
cleanUpFunc = func() {
|
||||
cleanUpFunc := func() {
|
||||
os.RemoveAll(dir)
|
||||
}
|
||||
|
||||
|
@ -127,19 +220,36 @@ func generateFakePackerConfigData() (packerConfigData string, cleanUpFunc func()
|
|||
suffix = ".exe"
|
||||
}
|
||||
|
||||
plugins := [...]string{
|
||||
filepath.Join(dir, "packer-builder-cloud-xyz"+suffix),
|
||||
filepath.Join(dir, "packer-provisioner-super-shell"+suffix),
|
||||
filepath.Join(dir, "packer-post-processor-noop"+suffix),
|
||||
}
|
||||
for _, plugin := range plugins {
|
||||
_, err := os.Create(plugin)
|
||||
plugins := make([]string, len(pluginNames))
|
||||
for i, plugin := range pluginNames {
|
||||
plug := filepath.Join(dir, plugin+suffix)
|
||||
plugins[i] = plug
|
||||
_, err := os.Create(plug)
|
||||
if err != nil {
|
||||
cleanUpFunc()
|
||||
return "", nil, fmt.Errorf("failed to create temporary plugin file (%s): %v", plugin, err)
|
||||
return "", nil, nil, fmt.Errorf("failed to create temporary plugin file (%s): %v", plug, err)
|
||||
}
|
||||
}
|
||||
|
||||
return dir, plugins, cleanUpFunc, nil
|
||||
}
|
||||
|
||||
/* generateFakePackerConfigData creates a collection of mock plugins along with a basic packerconfig.
|
||||
The return packerConfigData is a valid packerconfig file that can be used for configuring external plugins, cleanUpFunc is a function that should be called for cleaning up any generated mock data.
|
||||
This function will only clean up if there is an error, on successful runs the caller
|
||||
is responsible for cleaning up the data via cleanUpFunc().
|
||||
*/
|
||||
func generateFakePackerConfigData() (packerConfigData string, cleanUpFunc func(), err error) {
|
||||
_, plugins, cleanUpFunc, err := generateFakePlugins("random-testdata",
|
||||
[]string{"packer-builder-cloud-xyz",
|
||||
"packer-provisioner-super-shell",
|
||||
"packer-post-processor-noop"})
|
||||
|
||||
if err != nil {
|
||||
cleanUpFunc()
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
packerConfigData = fmt.Sprintf(`
|
||||
{
|
||||
"PluginMinPort": 10,
|
||||
|
|
Loading…
Reference in New Issue