Add tests for default communicator

This commit is contained in:
sophia 2022-06-27 15:08:35 -05:00
parent 22b2d52e00
commit 935c665412
3 changed files with 63 additions and 4 deletions

View File

@ -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,

View File

@ -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 {

View File

@ -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)
}
}
}