Add tests for setting machine id
This commit is contained in:
parent
294293097c
commit
63a4b76c21
@ -244,6 +244,7 @@ func (m *Machine) SyncedFolders() (folders []*core.MachineSyncedFolder, err erro
|
|||||||
func (m *Machine) SaveMachine() (err error) {
|
func (m *Machine) SaveMachine() (err error) {
|
||||||
m.logger.Debug("saving machine to db", "machine", m.machine.Id)
|
m.logger.Debug("saving machine to db", "machine", m.machine.Id)
|
||||||
m.target.Record, err = ptypes.MarshalAny(m.machine)
|
m.target.Record, err = ptypes.MarshalAny(m.machine)
|
||||||
|
m.target.ResourceId = m.machine.Id
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
71
internal/core/machine_test.go
Normal file
71
internal/core/machine_test.go
Normal 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)
|
||||||
|
}
|
||||||
@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
"github.com/hashicorp/vagrant-plugin-sdk/component"
|
"github.com/hashicorp/vagrant-plugin-sdk/component"
|
||||||
componentmocks "github.com/hashicorp/vagrant-plugin-sdk/component/mocks"
|
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/datadir"
|
||||||
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
|
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
|
||||||
"github.com/hashicorp/vagrant/internal/factory"
|
"github.com/hashicorp/vagrant/internal/factory"
|
||||||
@ -55,6 +56,19 @@ func TestTarget(t testing.T, opts ...BasisOption) (target *Target, err error) {
|
|||||||
return
|
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
|
// 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
|
// can be used for testing. Additional options can be given to provide your own
|
||||||
// factories, configuration, etc.
|
// factories, configuration, etc.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user