diff --git a/internal/core/helpers_test.go b/internal/core/helpers_test.go index 7a70cc3ad..508b858e2 100644 --- a/internal/core/helpers_test.go +++ b/internal/core/helpers_test.go @@ -62,6 +62,19 @@ func testGuestConfig(name string) *component.ConfigData { } } +// Set communicator name in vm configuration +func testCommunicatorConfig(name string) *component.ConfigData { + return &component.ConfigData{ + Data: map[string]interface{}{ + "vm": &component.ConfigData{ + Data: map[string]interface{}{ + "communicator": name, + }, + }, + }, + } +} + // Generate a synced folder plugin func syncedFolderPlugin(t *testing.T, name string) *plugin.Plugin { return plugin.TestPlugin(t, diff --git a/internal/core/target.go b/internal/core/target.go index 4278afde9..858029808 100644 --- a/internal/core/target.go +++ b/internal/core/target.go @@ -145,11 +145,12 @@ func (t *Target) Communicate() (c core.Communicator, err error) { c = i.(core.Communicator) return } - rawCommunicatorName, err := t.vagrantfile.GetValue("vm", "communicator") - if err != nil { - return nil, err - } communicatorName := "" + rawCommunicatorName, err := t.vagrantfile.GetValue("vm", "communicator") + // If there is an error getting the communicator, default to using the ssh communicator + if err != nil { + communicatorName = "ssh" + } if rawCommunicatorName == nil { communicatorName = "ssh" } else { diff --git a/internal/core/target_test.go b/internal/core/target_test.go index e67b838a4..4f3b7bd8b 100644 --- a/internal/core/target_test.go +++ b/internal/core/target_test.go @@ -3,7 +3,9 @@ package core import ( "testing" + "github.com/hashicorp/vagrant-plugin-sdk/component" "github.com/hashicorp/vagrant-plugin-sdk/core" + "github.com/hashicorp/vagrant/internal/plugin" "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server" "github.com/stretchr/testify/require" ) @@ -64,3 +66,46 @@ func TestTargetSpecializeBad(t *testing.T) { t.Errorf("Should not specialize to an unsupported type") } } + +func TestTargetConfigedCommunicator(t *testing.T) { + type test struct { + config *component.ConfigData + errors bool + } + + tests := []test{ + {config: testCommunicatorConfig("winrm"), errors: false}, + {config: testSyncedFolderConfig([]*testSyncedFolder{}), errors: false}, + {config: testCommunicatorConfig("idontexist"), errors: true}, + } + communicatorMockSSH := BuildTestCommunicatorPlugin("ssh") + communicatorMockWinRM := BuildTestCommunicatorPlugin("winrm") + + pluginManager := plugin.TestManager(t, + plugin.TestPlugin(t, + communicatorMockSSH, + plugin.WithPluginName("ssh"), + plugin.WithPluginTypes(component.CommunicatorType), + ), + plugin.TestPlugin(t, + communicatorMockWinRM, + plugin.WithPluginName("winrm"), + plugin.WithPluginTypes(component.CommunicatorType), + ), + ) + + for _, tc := range tests { + tp := TestProject(t, WithPluginManager(pluginManager)) + tm := TestMachine(t, tp, + WithTestTargetConfig(tc.config), + ) + comm, err := tm.Communicate() + if tc.errors { + require.Error(t, err) + require.Nil(t, comm) + } else { + require.NoError(t, err) + require.NotNil(t, comm) + } + } +}