Fix up some of the testing
This commit is contained in:
parent
6104e29129
commit
6d88f3a54f
@ -670,6 +670,7 @@ func (b *Basis) Save() (err error) {
|
||||
if err != nil {
|
||||
b.logger.Trace("failed to save basis",
|
||||
"error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
b.basis = result.Basis
|
||||
|
||||
@ -165,6 +165,7 @@ func TestRemoveMissingBox(t *testing.T) {
|
||||
testBoxPath := generateTestBox(t, td, bc.basis)
|
||||
// Insert test box into the collection
|
||||
box, err := bc.Add(path.NewPath(testBoxPath), "test/box", "1.2.3", "", true)
|
||||
require.NoError(t, err)
|
||||
boxPath, _ := box.Directory()
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, box)
|
||||
|
||||
@ -19,7 +19,7 @@ import (
|
||||
func hashicorpBionicBoxData() *vagrant_server.Box {
|
||||
testMetadata, _ := structpb.NewStruct(make(map[string]interface{}))
|
||||
return &vagrant_server.Box{
|
||||
Id: "123",
|
||||
ResourceId: "123",
|
||||
Provider: "virtualbox",
|
||||
Version: "0.0.282",
|
||||
Directory: "/tmp/boxes",
|
||||
@ -32,7 +32,7 @@ func hashicorpBionicBoxData() *vagrant_server.Box {
|
||||
func testboxBoxData() *vagrant_server.Box {
|
||||
testMetadata, _ := structpb.NewStruct(make(map[string]interface{}))
|
||||
return &vagrant_server.Box{
|
||||
Id: "123",
|
||||
ResourceId: "123",
|
||||
Provider: "virtualbox",
|
||||
Version: "1.2.3",
|
||||
Directory: "/tmp/boxes",
|
||||
|
||||
@ -237,7 +237,9 @@ func (m *Machine) MachineState() (state *core.MachineState, err error) {
|
||||
// SetMachineState implements core.Machine
|
||||
func (m *Machine) SetMachineState(state *core.MachineState) (err error) {
|
||||
var st *vagrant_plugin_sdk.Args_Target_Machine_State
|
||||
mapstructure.Decode(state, &st)
|
||||
if err := mapstructure.Decode(state, &st); err != nil {
|
||||
return err
|
||||
}
|
||||
m.machine.State = st
|
||||
|
||||
switch st.Id {
|
||||
|
||||
@ -280,8 +280,8 @@ func TestMachineSetState(t *testing.T) {
|
||||
// Set MachineState
|
||||
desiredState := &core.MachineState{ID: tc.id}
|
||||
tm.SetMachineState(desiredState)
|
||||
require.Equal(t, tm.machine.State.Id, tc.id)
|
||||
require.Equal(t, tm.target.State, tc.state)
|
||||
require.Equal(t, tc.id, tm.machine.State.Id)
|
||||
require.Equal(t, tc.state, tm.target.State)
|
||||
|
||||
// Ensure new id is save to db
|
||||
dbTarget, err := tm.Client().GetTarget(tm.ctx,
|
||||
@ -289,10 +289,8 @@ func TestMachineSetState(t *testing.T) {
|
||||
Target: tm.Ref().(*vagrant_plugin_sdk.Ref_Target),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get target")
|
||||
}
|
||||
require.Equal(t, dbTarget.Target.State, tc.state)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.state, dbTarget.Target.State)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -91,9 +91,7 @@ func TestTargetIndexSet(t *testing.T) {
|
||||
func TestTargetIndexAll(t *testing.T) {
|
||||
tp := TestMinimalProject(t)
|
||||
ti, err := tp.TargetIndex()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// No Targets
|
||||
targets, err := ti.All()
|
||||
|
||||
@ -95,6 +95,7 @@ func TestBasis(t testing.T, opts ...BasisOption) (b *Basis) {
|
||||
td, err := ioutil.TempDir("", "core")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { os.RemoveAll(td) })
|
||||
name := filepath.Base(td)
|
||||
|
||||
mkSubdir := func(root, sub string) string {
|
||||
sd := filepath.Join(root, sub)
|
||||
@ -131,12 +132,12 @@ func TestBasis(t testing.T, opts ...BasisOption) (b *Basis) {
|
||||
WithFactory(factory),
|
||||
WithClient(client),
|
||||
WithBasisDataDir(projDir),
|
||||
WithBasisRef(&vagrant_plugin_sdk.Ref_Basis{Name: "test-basis"}),
|
||||
WithBasisRef(&vagrant_plugin_sdk.Ref_Basis{Name: name, Path: td}),
|
||||
}
|
||||
|
||||
b, err = factory.NewBasis("", append(defaultOpts, opts...)...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, b.Save())
|
||||
return
|
||||
}
|
||||
|
||||
@ -1,27 +1,39 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/go-testing-interface"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
// "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/plugin"
|
||||
"github.com/mitchellh/go-testing-interface"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// 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.
|
||||
func TestProject(t testing.T, opts ...BasisOption) *Project {
|
||||
path := testTempDir(t)
|
||||
name := filepath.Base(path)
|
||||
|
||||
b := TestBasis(t, opts...)
|
||||
p, err := b.factory.NewProject(
|
||||
[]ProjectOption{
|
||||
WithBasis(b),
|
||||
WithProjectName("test-project"),
|
||||
WithProjectRef(
|
||||
&vagrant_plugin_sdk.Ref_Project{
|
||||
Basis: b.Ref().(*vagrant_plugin_sdk.Ref_Basis),
|
||||
Name: name,
|
||||
Path: path,
|
||||
},
|
||||
),
|
||||
}...,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, p.Save())
|
||||
|
||||
return p
|
||||
}
|
||||
@ -29,18 +41,35 @@ func TestProject(t testing.T, opts ...BasisOption) *Project {
|
||||
// TestMinimalProject uses a minimal basis to setup the most basic project
|
||||
// that will work for testing
|
||||
func TestMinimalProject(t testing.T) *Project {
|
||||
path := testTempDir(t)
|
||||
name := filepath.Base(path)
|
||||
|
||||
pluginManager := plugin.TestManager(t)
|
||||
b := TestBasis(t, WithPluginManager(pluginManager))
|
||||
p, err := b.factory.NewProject(
|
||||
[]ProjectOption{
|
||||
WithBasis(b),
|
||||
WithProjectName("test-project"),
|
||||
WithProjectRef(
|
||||
&vagrant_plugin_sdk.Ref_Project{
|
||||
Basis: b.Ref().(*vagrant_plugin_sdk.Ref_Basis),
|
||||
Name: name,
|
||||
Path: path,
|
||||
},
|
||||
),
|
||||
}...,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, p.Save())
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func testTempDir(t testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
dir, err := ioutil.TempDir("", "vagrant-test")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { os.RemoveAll(dir) })
|
||||
return dir
|
||||
}
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/imdario/mergo"
|
||||
|
||||
@ -11,55 +11,40 @@ import (
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/core"
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
|
||||
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
|
||||
"github.com/hashicorp/vagrant/internal/server/ptypes"
|
||||
"github.com/mitchellh/go-testing-interface"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestTarget returns a fully in-memory and side-effect free Target that
|
||||
// can be used for testing. Additional options can be given to provide your own
|
||||
// factories, configuration, etc.
|
||||
func TestTarget(t testing.T, p *Project, st *vagrant_server.Target, opts ...TestTargetOption) (target *Target) {
|
||||
testingTarget := ptypes.TestTarget(t, st)
|
||||
testingTarget.Project = p.Ref().(*vagrant_plugin_sdk.Ref_Project)
|
||||
_, err := p.basis.client.UpsertTarget(
|
||||
context.Background(),
|
||||
&vagrant_server.UpsertTargetRequest{
|
||||
Project: p.Ref().(*vagrant_plugin_sdk.Ref_Project),
|
||||
Target: testingTarget,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
if st.Name == "" {
|
||||
st.Name = filepath.Base(testTempDir(t))
|
||||
}
|
||||
|
||||
target, err = p.factory.NewTarget(
|
||||
target, err := p.factory.NewTarget(
|
||||
[]TargetOption{
|
||||
WithProject(p),
|
||||
WithTargetRef(
|
||||
&vagrant_plugin_sdk.Ref_Target{
|
||||
Project: p.Ref().(*vagrant_plugin_sdk.Ref_Project),
|
||||
Name: testingTarget.Name,
|
||||
Project: p.Ref().(*vagrant_plugin_sdk.Ref_Project),
|
||||
Name: st.Name,
|
||||
ResourceId: st.ResourceId,
|
||||
},
|
||||
),
|
||||
}...,
|
||||
)
|
||||
|
||||
if err = p.Reload(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mergo.Merge(target.target, st))
|
||||
|
||||
for _, opt := range opts {
|
||||
if oerr := opt(target); oerr != nil {
|
||||
err = multierror.Append(err, oerr)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = p.Reload(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, target.Save())
|
||||
require.NoError(t, p.Reload())
|
||||
|
||||
return
|
||||
}
|
||||
@ -68,39 +53,20 @@ func TestTarget(t testing.T, p *Project, st *vagrant_server.Target, opts ...Test
|
||||
// that will work for testing
|
||||
func TestMinimalTarget(t testing.T) (target *Target) {
|
||||
tp := TestMinimalProject(t)
|
||||
_, err := tp.basis.client.UpsertTarget(
|
||||
context.Background(),
|
||||
&vagrant_server.UpsertTargetRequest{
|
||||
Project: tp.Ref().(*vagrant_plugin_sdk.Ref_Project),
|
||||
Target: &vagrant_server.Target{
|
||||
Name: "test-target",
|
||||
Project: tp.Ref().(*vagrant_plugin_sdk.Ref_Project),
|
||||
},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
target, err = tp.factory.NewTarget(
|
||||
target, err := tp.factory.NewTarget(
|
||||
[]TargetOption{
|
||||
WithProject(tp),
|
||||
WithTargetRef(
|
||||
&vagrant_plugin_sdk.Ref_Target{
|
||||
Project: tp.Ref().(*vagrant_plugin_sdk.Ref_Project),
|
||||
Name: "test-target",
|
||||
Name: filepath.Base(testTempDir(t)),
|
||||
},
|
||||
),
|
||||
}...,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = tp.Reload(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, target.Save())
|
||||
require.NoError(t, tp.Reload())
|
||||
|
||||
return
|
||||
}
|
||||
@ -111,9 +77,7 @@ func TestMinimalTarget(t testing.T) (target *Target) {
|
||||
func TestMachine(t testing.T, tp *Project, opts ...TestTargetOption) (machine *Machine) {
|
||||
tt := TestTarget(t, tp, &vagrant_server.Target{})
|
||||
specialized, err := tt.Specialize((*core.Machine)(nil))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
machine = specialized.(*Machine)
|
||||
for _, opt := range opts {
|
||||
@ -121,9 +85,7 @@ func TestMachine(t testing.T, tp *Project, opts ...TestTargetOption) (machine *M
|
||||
err = multierror.Append(err, oerr)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
return
|
||||
}
|
||||
@ -134,9 +96,8 @@ func TestMinimalMachine(t testing.T) (machine *Machine) {
|
||||
tp := TestMinimalProject(t)
|
||||
tt := TestTarget(t, tp, &vagrant_server.Target{})
|
||||
specialized, err := tt.Specialize((*core.Machine)(nil))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
machine = specialized.(*Machine)
|
||||
return
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package state
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-ozzo/ozzo-validation/v4"
|
||||
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
|
||||
@ -394,7 +395,7 @@ func (s *State) ProjectList() ([]*vagrant_plugin_sdk.Ref_Project, error) {
|
||||
func (s *State) ProjectFind(p *vagrant_server.Project) (*vagrant_server.Project, error) {
|
||||
project, err := s.ProjectFromProtoFuzzy(p)
|
||||
if err != nil {
|
||||
return nil, saveErrorToStatus("project", err)
|
||||
return nil, lookupErrorToStatus("project", fmt.Errorf("%w (%#v)", err, p))
|
||||
}
|
||||
|
||||
return project.ToProto(), nil
|
||||
|
||||
@ -408,6 +408,11 @@ func (s *State) TargetPut(
|
||||
return nil, saveErrorToStatus("target", ErrMissingProtoParent)
|
||||
}
|
||||
|
||||
// NOTE: this does not get set when updating to
|
||||
// the UNKNOWN value since it's a zero
|
||||
// value and thus ignored with soft decode
|
||||
target.State = t.State
|
||||
|
||||
if err := s.upsertFull(target); err != nil {
|
||||
return nil, saveErrorToStatus("target", err)
|
||||
}
|
||||
|
||||
@ -247,6 +247,26 @@ func TestTarget_Update(t *testing.T) {
|
||||
require.ErrorContains(result.Error, "ResourceId:")
|
||||
})
|
||||
|
||||
t.Run("Updates the state", func(t *testing.T) {
|
||||
require, db := requireAndDB(t)
|
||||
|
||||
target := Target{
|
||||
Name: "default",
|
||||
Project: testProject(t, db),
|
||||
State: vagrant_server.Operation_NOT_CREATED,
|
||||
}
|
||||
result := db.Save(&target)
|
||||
require.NoError(result.Error)
|
||||
require.Equal(vagrant_server.Operation_NOT_CREATED, target.State)
|
||||
target.State = vagrant_server.Operation_UNKNOWN
|
||||
result = db.Save(&target)
|
||||
require.NoError(result.Error)
|
||||
result = db.First(&target, &Target{Model: Model{ID: target.ID}})
|
||||
require.NoError(result.Error)
|
||||
require.Equal(vagrant_server.Operation_UNKNOWN, target.State)
|
||||
|
||||
})
|
||||
|
||||
t.Run("Adds subtarget", func(t *testing.T) {
|
||||
require, db := requireAndDB(t)
|
||||
|
||||
@ -359,7 +379,6 @@ func TestTarget_State(t *testing.T) {
|
||||
s := TestState(t)
|
||||
defer s.Close()
|
||||
|
||||
// Set
|
||||
_, err := s.TargetGet(&vagrant_plugin_sdk.Ref_Target{
|
||||
ResourceId: "foo",
|
||||
})
|
||||
@ -367,6 +386,36 @@ func TestTarget_State(t *testing.T) {
|
||||
require.Equal(codes.NotFound, status.Code(err))
|
||||
})
|
||||
|
||||
t.Run("Simple update", func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
s := TestState(t)
|
||||
defer s.Close()
|
||||
|
||||
resp, err := s.TargetPut(&vagrant_server.Target{
|
||||
Name: "default",
|
||||
Project: testProject(t, s.db).ToProtoRef(),
|
||||
State: vagrant_server.Operation_NOT_CREATED,
|
||||
})
|
||||
require.NoError(err)
|
||||
require.Equal(vagrant_server.Operation_NOT_CREATED, resp.State)
|
||||
target, err := s.TargetGet(&vagrant_plugin_sdk.Ref_Target{
|
||||
ResourceId: resp.ResourceId,
|
||||
})
|
||||
require.NoError(err)
|
||||
require.Equal(vagrant_server.Operation_NOT_CREATED, target.State)
|
||||
|
||||
target.State = vagrant_server.Operation_UNKNOWN
|
||||
resp, err = s.TargetPut(target)
|
||||
require.NoError(err)
|
||||
require.Equal(vagrant_server.Operation_UNKNOWN, resp.State)
|
||||
|
||||
target, err = s.TargetGet(&vagrant_plugin_sdk.Ref_Target{
|
||||
ResourceId: resp.ResourceId,
|
||||
})
|
||||
require.NoError(err)
|
||||
require.Equal(vagrant_server.Operation_UNKNOWN, target.State)
|
||||
})
|
||||
|
||||
t.Run("Put and Get", func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ func TestState(t testing.T) *State {
|
||||
t.Cleanup(func() {
|
||||
t.Log(buf.String())
|
||||
})
|
||||
result, err := New(l, testDB(t))
|
||||
result, err := New(l, TestDB(t))
|
||||
require.NoError(t, err)
|
||||
return result
|
||||
}
|
||||
@ -83,7 +83,7 @@ func TestState(t testing.T) *State {
|
||||
// return New(hclog.L(), db)
|
||||
// }
|
||||
|
||||
func testDB(t testing.T) *gorm.DB {
|
||||
func TestDB(t testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
|
||||
db, err := gorm.Open(sqlite.Open(""), &gorm.Config{
|
||||
@ -106,7 +106,7 @@ func testDB(t testing.T) *gorm.DB {
|
||||
}
|
||||
|
||||
func requireAndDB(t testing.T) (*require.Assertions, *gorm.DB) {
|
||||
db := testDB(t)
|
||||
db := TestDB(t)
|
||||
require := require.New(t)
|
||||
if err := db.AutoMigrate(models...); err != nil {
|
||||
require.NoError(err)
|
||||
|
||||
@ -13,6 +13,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/vagrant/internal/server"
|
||||
pb "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
|
||||
"github.com/hashicorp/vagrant/internal/server/singleprocess/state"
|
||||
"github.com/hashicorp/vagrant/internal/serverclient"
|
||||
)
|
||||
|
||||
@ -26,7 +27,7 @@ func TestServer(t testing.T, opts ...Option) *serverclient.VagrantClient {
|
||||
// with server.TestServer. It is easier to just use TestServer directly.
|
||||
func TestImpl(t testing.T, opts ...Option) pb.VagrantServer {
|
||||
impl, err := New(append(
|
||||
[]Option{WithDB(testDB(t))},
|
||||
[]Option{WithDB(state.TestDB(t))},
|
||||
opts...,
|
||||
)...)
|
||||
require.NoError(t, err)
|
||||
@ -155,7 +156,7 @@ func TestBasis(t testing.T, client pb.VagrantClient, ref *pb.Basis) {
|
||||
func testDB(t testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
d, err := db.DB()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user