Add tests for setting machine id

This commit is contained in:
sophia 2021-12-22 16:49:43 -06:00 committed by Paul Hinze
parent 294293097c
commit 63a4b76c21
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
3 changed files with 86 additions and 0 deletions

View File

@ -244,6 +244,7 @@ func (m *Machine) SyncedFolders() (folders []*core.MachineSyncedFolder, err erro
func (m *Machine) SaveMachine() (err error) {
m.logger.Debug("saving machine to db", "machine", m.machine.Id)
m.target.Record, err = ptypes.MarshalAny(m.machine)
m.target.ResourceId = m.machine.Id
if err != nil {
return nil
}

View File

@ -0,0 +1,71 @@
package core
import (
"testing"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
"github.com/stretchr/testify/require"
)
func TestMachineSetValidId(t *testing.T) {
tm, _ := TestMachine(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.ResourceId, "something")
}
func TestMachineSetEmptyId(t *testing.T) {
tm, _ := TestMachine(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)
}

View File

@ -12,6 +12,7 @@ import (
"github.com/hashicorp/vagrant-plugin-sdk/component"
componentmocks "github.com/hashicorp/vagrant-plugin-sdk/component/mocks"
"github.com/hashicorp/vagrant-plugin-sdk/core"
"github.com/hashicorp/vagrant-plugin-sdk/datadir"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant/internal/factory"
@ -55,6 +56,19 @@ func TestTarget(t testing.T, opts ...BasisOption) (target *Target, err error) {
return
}
// TestMachine returns a fully in-memory and side-effect free Machine that
// can be used for testing. Additional options can be given to provide your own
// factories, configuration, etc.
func TestMachine(t testing.T, opts ...BasisOption) (machine *Machine, err error) {
tt, _ := TestTarget(t)
specialized, err := tt.Specialize((*core.Machine)(nil))
if err != nil {
return nil, err
}
machine = specialized.(*Machine)
return
}
// TestProject returns a fully in-memory and side-effect free Project that
// can be used for testing. Additional options can be given to provide your own
// factories, configuration, etc.