add FindDatastore driver test

This commit is contained in:
sylviamoss 2020-09-22 17:42:35 +02:00
parent b881a59bc8
commit 7cd6ee2b3b
2 changed files with 102 additions and 1 deletions

View File

@ -1,6 +1,10 @@
package driver
import "testing"
import (
"testing"
"github.com/vmware/govmomi/simulator"
)
func TestDatastoreIsoPath(t *testing.T) {
tc := []struct {
@ -87,3 +91,84 @@ func TestDatastoreIsoPath(t *testing.T) {
}
}
}
func TestVCenterDriver_FindDatastore(t *testing.T) {
sim, err := NewVCenterSimulator()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
defer sim.Close()
_, datastore := sim.ChooseSimulatorPreCreatedDatastore()
_, host := sim.ChooseSimulatorPreCreatedHost()
tc := []struct {
name string
datastore string
host string
fail bool
errMessage string
}{
{
name: "should find datastore when name is provided",
datastore: datastore.Name,
fail: false,
},
{
name: "should find datastore when only host is provided",
host: host.Name,
fail: false,
},
{
name: "should not find invalid datastore",
datastore: "invalid",
fail: true,
},
{
name: "should not find invalid host",
host: "invalid",
fail: true,
},
}
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
ds, err := sim.driver.FindDatastore(c.datastore, c.host)
if c.fail {
if err == nil {
t.Fatalf("expected to fail")
}
if c.errMessage != "" && err.Error() != c.errMessage {
t.Fatalf("unexpected error message %s", err.Error())
}
} else {
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
if ds == nil {
t.Fatalf("expected to find datastore")
}
}
})
}
}
func TestVCenterDriver_MultipleDatastoreError(t *testing.T) {
model := simulator.ESX()
model.Datastore = 2
sim, err := NewCustomVCenterSimulator(model)
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
defer sim.Close()
_, host := sim.ChooseSimulatorPreCreatedHost()
_, err = sim.driver.FindDatastore("", host.Name)
if err == nil {
t.Fatalf("expected to fail")
}
if err.Error() != "Host has multiple datastores. Specify it explicitly" {
t.Fatalf("unexpected error message %s", err.Error())
}
}

View File

@ -99,6 +99,22 @@ func (s *VCenterSimulator) ChooseSimulatorPreCreatedVM() (VirtualMachine, *simul
return vm, machine
}
//Simulator shortcut to choose any pre created Datastore.
func (s *VCenterSimulator) ChooseSimulatorPreCreatedDatastore() (Datastore, *simulator.Datastore) {
ds := simulator.Map.Any("Datastore").(*simulator.Datastore)
ref := ds.Reference()
datastore := s.driver.NewDatastore(&ref)
return datastore, ds
}
//Simulator shortcut to choose any pre created Host.
func (s *VCenterSimulator) ChooseSimulatorPreCreatedHost() (*Host, *simulator.HostSystem) {
h := simulator.Map.Any("HostSystem").(*simulator.HostSystem)
ref := h.Reference()
host := s.driver.NewHost(&ref)
return host, h
}
func (s *VCenterSimulator) NewSimulatorServer() (*simulator.Server, error) {
err := s.model.Create()
if err != nil {