Network selection test

This commit is contained in:
Michael Kuzmin 2019-06-13 16:56:14 +03:00 committed by Michael Kuzmin
parent 395ce39611
commit f7151b4058
3 changed files with 92 additions and 2 deletions

View File

@ -322,6 +322,51 @@ func checkLinkedClone(t *testing.T) builderT.TestCheckFunc {
}
}
func TestCloneBuilderAcc_network(t *testing.T) {
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: networkConfig(),
Check: checkNetwork(t, "VM Network 2"),
})
}
func networkConfig() string {
config := defaultConfig()
config["template"] = "alpine-host4"
config["host"] = "esxi-4.vsphere65.test"
config["datastore"] = "datastore4"
config["network"] = "VM Network 2"
return commonT.RenderConfig(config)
}
func checkNetwork(t *testing.T, name string) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error {
d := commonT.TestConn(t)
vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("network")
if err != nil {
t.Fatalf("Cannot read VM properties: %v", err)
}
n := len(vmInfo.Network)
if n != 1 {
t.Fatalf("VM should have 1 network, got %v", n)
}
ds := d.NewNetwork(&vmInfo.Network[0])
info, err := ds.Info("name")
if err != nil {
t.Fatalf("Cannot read network properties: %v", err)
}
if info.Name != name {
t.Errorf("Wrong network. expected: %v, got: %v", name, info.Name)
}
return nil
}
}
func TestCloneBuilderAcc_hardware(t *testing.T) {
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},

45
driver/network.go Normal file
View File

@ -0,0 +1,45 @@
package driver
import (
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
type Network struct {
driver *Driver
network *object.Network
}
func (d *Driver) NewNetwork(ref *types.ManagedObjectReference) *Network {
return &Network{
network: object.NewNetwork(d.client.Client, *ref),
driver: d,
}
}
func (d *Driver) FindNetwork(name string) (*Network, error) {
n, err := d.finder.Network(d.ctx, name)
if err != nil {
return nil, err
}
return &Network{
network: n.(*object.Network),
driver: d,
}, nil
}
func (n *Network) Info(params ...string) (*mo.Network, error) {
var p []string
if len(params) == 0 {
p = []string{"*"}
} else {
p = params
}
var info mo.Network
err := n.network.Properties(n.driver.ctx, n.network.Reference(), p, &info)
if err != nil {
return nil, err
}
return &info, nil
}

View File

@ -231,11 +231,11 @@ func (template *VirtualMachine) Clone(ctx context.Context, config *CloneConfig)
}
if config.Network != "" {
net, err := template.driver.finder.Network(ctx, config.Network)
net, err := template.driver.FindNetwork(config.Network)
if err != nil {
return nil, err
}
backing, err := net.EthernetCardBackingInfo(ctx)
backing, err := net.network.EthernetCardBackingInfo(ctx)
if err != nil {
return nil, err
}