2016-12-27 15:47:13 -05:00
|
|
|
package converge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-12-27 15:47:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"module_dirs": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"source": "from",
|
|
|
|
"destination": "/opt/converge",
|
|
|
|
},
|
|
|
|
},
|
2016-12-27 17:32:19 -05:00
|
|
|
"module": "/opt/converge/test.hcl",
|
2016-12-27 15:47:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_Impl(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Provisioner{}
|
|
|
|
if _, ok := raw.(packer.Provisioner); !ok {
|
|
|
|
t.Fatal("must be a Provisioner")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare(t *testing.T) {
|
|
|
|
t.Run("defaults", func(t *testing.T) {
|
2016-12-27 17:48:11 -05:00
|
|
|
t.Run("working_directory", func(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "working_directory")
|
|
|
|
|
|
|
|
if err := p.Prepare(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.WorkingDirectory != "/tmp" {
|
|
|
|
t.Fatalf("unexpected module directory: %s", p.config.WorkingDirectory)
|
|
|
|
}
|
|
|
|
})
|
2016-12-27 15:47:13 -05:00
|
|
|
|
2016-12-27 17:48:11 -05:00
|
|
|
t.Run("execute_command", func(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
2016-12-27 15:47:13 -05:00
|
|
|
|
2016-12-27 17:48:11 -05:00
|
|
|
delete(config, "execute_command")
|
2016-12-27 15:47:13 -05:00
|
|
|
|
2016-12-27 17:48:11 -05:00
|
|
|
if err := p.Prepare(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ExecuteCommand == "" {
|
|
|
|
t.Fatal("execute command unexpectedly blank")
|
|
|
|
}
|
|
|
|
})
|
2016-12-28 09:45:19 -05:00
|
|
|
|
|
|
|
t.Run("bootstrap_command", func(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "bootstrap_command")
|
|
|
|
|
|
|
|
if err := p.Prepare(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.BootstrapCommand == "" {
|
|
|
|
t.Fatal("bootstrap command unexpectedly blank")
|
|
|
|
}
|
|
|
|
})
|
2016-12-27 15:47:13 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("validate", func(t *testing.T) {
|
|
|
|
t.Run("module dir", func(t *testing.T) {
|
|
|
|
t.Run("missing source", func(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
delete(config["module_dirs"].([]map[string]interface{})[0], "source")
|
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error")
|
|
|
|
} else if err.Error() != "Source (\"source\" key) is required in Converge module dir #0" {
|
|
|
|
t.Errorf("bad error message: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("missing destination", func(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
delete(config["module_dirs"].([]map[string]interface{})[0], "destination")
|
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error")
|
|
|
|
} else if err.Error() != "Destination (\"destination\" key) is required in Converge module dir #0" {
|
|
|
|
t.Errorf("bad error message: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-12-27 17:32:19 -05:00
|
|
|
t.Run("no module specified", func(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
delete(config, "module")
|
2016-12-27 15:47:13 -05:00
|
|
|
|
2016-12-27 17:32:19 -05:00
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error")
|
|
|
|
} else if err.Error() != "Converge requires a module to provision the system" {
|
|
|
|
t.Errorf("bad error message: %s", err)
|
|
|
|
}
|
2016-12-27 15:47:13 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|