2019-01-25 15:32:44 -05:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2019-01-25 15:32:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Builder{}
|
2020-12-01 16:42:11 -05:00
|
|
|
if _, ok := raw.(packersdk.Builder); !ok {
|
2019-01-25 15:32:44 -05:00
|
|
|
t.Fatalf("Builder should be a builder")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_Prepare_ValidateSource(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
config map[string]interface{}
|
|
|
|
errExpected bool
|
|
|
|
reason string
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"global_id": "a3559ec",
|
|
|
|
},
|
|
|
|
errExpected: true,
|
|
|
|
reason: "Need to set SSH communicator.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"global_id": "a3559ec",
|
|
|
|
"communicator": "ssh",
|
|
|
|
},
|
|
|
|
errExpected: false,
|
|
|
|
reason: "Shouldn't fail because we've set global_id",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"communicator": "ssh",
|
|
|
|
},
|
|
|
|
errExpected: true,
|
|
|
|
reason: "Should fail because we must set source_path or global_id",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"source_path": "./mybox",
|
|
|
|
"communicator": "ssh",
|
|
|
|
},
|
|
|
|
errExpected: false,
|
|
|
|
reason: "Source path is set; we should be fine",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"source_path": "./mybox",
|
|
|
|
"communicator": "ssh",
|
|
|
|
"global_id": "a3559ec",
|
|
|
|
},
|
|
|
|
errExpected: true,
|
|
|
|
reason: "Both source path and global are set: we should error.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"communicator": "ssh",
|
|
|
|
"global_id": "a3559ec",
|
|
|
|
"teardown_method": "suspend",
|
|
|
|
},
|
|
|
|
errExpected: false,
|
|
|
|
reason: "Valid argument for teardown method",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"communicator": "ssh",
|
|
|
|
"global_id": "a3559ec",
|
|
|
|
"teardown_method": "surspernd",
|
|
|
|
},
|
|
|
|
errExpected: true,
|
|
|
|
reason: "Inalid argument for teardown method",
|
|
|
|
},
|
2020-07-29 04:26:09 -04:00
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"communicator": "ssh",
|
|
|
|
"source_path": "./my.box",
|
|
|
|
},
|
|
|
|
errExpected: true,
|
|
|
|
reason: "Should fail because path does not exist",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"communicator": "ssh",
|
|
|
|
"source_path": "file://my.box",
|
|
|
|
},
|
|
|
|
errExpected: true,
|
|
|
|
reason: "Should fail because path does not exist",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"communicator": "ssh",
|
|
|
|
"source_path": "http://my.box",
|
|
|
|
},
|
|
|
|
errExpected: false,
|
|
|
|
reason: "Should pass because path is not local",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"communicator": "ssh",
|
|
|
|
"source_path": "https://my.box",
|
|
|
|
},
|
|
|
|
errExpected: false,
|
|
|
|
reason: "Should pass because path is not local",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"communicator": "ssh",
|
|
|
|
"source_path": "smb://my.box",
|
|
|
|
},
|
|
|
|
errExpected: false,
|
|
|
|
reason: "Should pass because path is not local",
|
|
|
|
},
|
2019-01-25 15:32:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2019-12-17 05:57:09 -05:00
|
|
|
_, _, err := (&Builder{}).Prepare(tc.config)
|
2019-01-25 15:32:44 -05:00
|
|
|
if (err != nil) != tc.errExpected {
|
|
|
|
t.Fatalf("Unexpected behavior from test case %#v; %s.", tc.config, tc.reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|