vaguerent/internal/core/machine_test.go
2022-04-25 12:26:40 -05:00

94 lines
2.2 KiB
Go

package core
import (
"context"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant/internal/plugin"
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
"github.com/stretchr/testify/require"
)
func TestMachineSetValidId(t *testing.T) {
tm, _ := TestMinimalMachine(t)
// Set valid id
tm.SetID("something")
newId, err := tm.ID()
if err != nil {
t.Errorf("Failed to get id")
}
require.Equal(t, newId, "something")
// Ensure new id is save to db
dbTarget, err := tm.Client().GetTarget(tm.ctx,
&vagrant_server.GetTargetRequest{
Target: tm.Ref().(*vagrant_plugin_sdk.Ref_Target),
},
)
if err != nil {
t.Errorf("Failed to get target")
}
require.Equal(t, dbTarget.Target.Uuid, "something")
}
func TestMachineSetEmptyId(t *testing.T) {
tm, _ := TestMinimalMachine(t)
oldId := tm.target.ResourceId
// Set empty id
tm.SetID("")
newId, err := tm.ID()
if err != nil {
t.Errorf("Failed to get id")
}
require.Equal(t, newId, "")
// Ensure machine is deleted from the db by checking for the old id
dbTarget, err := tm.Client().GetTarget(tm.ctx,
&vagrant_server.GetTargetRequest{
Target: &vagrant_plugin_sdk.Ref_Target{
ResourceId: oldId,
Project: tm.target.Project,
Name: tm.target.Name,
},
},
)
require.Nil(t, dbTarget)
require.Error(t, err)
// Also check new id
dbTarget, err = tm.Client().GetTarget(tm.ctx,
&vagrant_server.GetTargetRequest{
Target: &vagrant_plugin_sdk.Ref_Target{
ResourceId: "",
Project: tm.target.Project,
Name: tm.target.Name,
},
},
)
require.Nil(t, dbTarget)
require.Error(t, err)
}
func TestMachineConfigedGuest(t *testing.T) {
pluginManager := plugin.NewManager(
context.Background(),
hclog.New(&hclog.LoggerOptions{}),
)
tp := TestProject(t,
WithPluginManager(pluginManager),
)
tm, _ := TestMachine(t, tp,
WithTestTargetConfig(&vagrant_plugin_sdk.Vagrantfile_MachineConfig{
ConfigVm: &vagrant_plugin_sdk.Vagrantfile_ConfigVM{Guest: "myguest"},
}),
)
guest, err := tm.Guest()
require.NoError(t, err)
require.NotNil(t, guest)
require.NotNil(t, tm.guest)
}