Add more tests for guest detection

This commit is contained in:
sophia 2022-02-24 12:14:23 -06:00 committed by Paul Hinze
parent 736e080add
commit 06cfbb79bf
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 61 additions and 4 deletions

View File

@ -75,6 +75,13 @@ func TestMachineSetEmptyId(t *testing.T) {
require.Error(t, err)
}
func seededGuestMock() *coremocks.Guest {
guestMock := &coremocks.Guest{}
guestMock.On("Seeds").Return(sdkcore.NewSeeds(), nil)
guestMock.On("Seed", mock.AnythingOfType("")).Return(nil)
return guestMock
}
func TestMachineConfigedGuest(t *testing.T) {
type test struct {
config *vagrant_plugin_sdk.Vagrantfile_ConfigVM
@ -86,10 +93,7 @@ func TestMachineConfigedGuest(t *testing.T) {
{config: &vagrant_plugin_sdk.Vagrantfile_ConfigVM{Guest: "idontexist"}, errors: true},
}
guestMock := &coremocks.Guest{}
guestMock.On("Seeds").Return(sdkcore.NewSeeds(), nil)
guestMock.On("Seed", mock.AnythingOfType("")).Return(nil)
guestMock := seededGuestMock()
pluginManager := plugin.TestManager(t,
plugin.TestPlugin(t,
plugin.WithPluginName("myguest"),
@ -115,3 +119,46 @@ func TestMachineConfigedGuest(t *testing.T) {
}
}
}
func TestMachineNoConfigGuest(t *testing.T) {
guestMock := seededGuestMock()
guestMock.On("Detect", mock.AnythingOfType("*core.Machine")).Return(true, nil)
detectingPlugin := plugin.TestPlugin(t,
plugin.WithPluginName("myguest"),
plugin.WithPluginComponents(component.GuestType, guestMock))
notGuestMock := seededGuestMock()
notGuestMock.On("Detect", mock.AnythingOfType("*core.Machine")).Return(false, nil)
nonDetectingPlugin := plugin.TestPlugin(t,
plugin.WithPluginName("mynondetectingguest"),
plugin.WithPluginComponents(component.GuestType, notGuestMock))
type test struct {
plugins []*plugin.Plugin
errors bool
}
tests := []test{
{plugins: []*plugin.Plugin{detectingPlugin}, errors: false},
{plugins: []*plugin.Plugin{detectingPlugin, nonDetectingPlugin}, errors: false},
{plugins: []*plugin.Plugin{nonDetectingPlugin}, errors: true},
{plugins: []*plugin.Plugin{}, errors: true},
}
for _, tc := range tests {
pluginManager := plugin.TestManager(t, tc.plugins...)
tp := TestProject(t, WithPluginManager(pluginManager))
tm, _ := TestMachine(t, tp, WithTestTargetMinimalConfig())
guest, err := tm.Guest()
if tc.errors {
require.Error(t, err)
require.Nil(t, guest)
require.Nil(t, tm.guest)
} else {
require.NoError(t, err)
require.NotNil(t, guest)
require.NotNil(t, tm.guest)
}
}
}

View File

@ -86,11 +86,21 @@ func TestMinimalMachine(t testing.T) (machine *Machine, err error) {
return nil, err
}
machine = specialized.(*Machine)
WithTestTargetMinimalConfig()(machine)
return
}
type TestMachineOption func(*Machine) error
func WithTestTargetMinimalConfig() TestMachineOption {
return func(m *Machine) (err error) {
m.target.Configuration = &vagrant_plugin_sdk.Vagrantfile_MachineConfig{
ConfigVm: &vagrant_plugin_sdk.Vagrantfile_ConfigVM{},
}
return
}
}
func WithTestTargetConfig(config *vagrant_plugin_sdk.Vagrantfile_MachineConfig) TestMachineOption {
return func(m *Machine) (err error) {
m.target.Configuration = config