From f7151b405809a037802d84718e932b76b0f0faaf Mon Sep 17 00:00:00 2001 From: Michael Kuzmin Date: Thu, 13 Jun 2019 16:56:14 +0300 Subject: [PATCH] Network selection test --- clone/builder_acc_test.go | 45 +++++++++++++++++++++++++++++++++++++++ driver/network.go | 45 +++++++++++++++++++++++++++++++++++++++ driver/vm.go | 4 ++-- 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 driver/network.go diff --git a/clone/builder_acc_test.go b/clone/builder_acc_test.go index 984b4e3df..cb7b12f52 100644 --- a/clone/builder_acc_test.go +++ b/clone/builder_acc_test.go @@ -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{}, diff --git a/driver/network.go b/driver/network.go new file mode 100644 index 000000000..de923a01d --- /dev/null +++ b/driver/network.go @@ -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 +} diff --git a/driver/vm.go b/driver/vm.go index 93c52432e..8ef1fd580 100644 --- a/driver/vm.go +++ b/driver/vm.go @@ -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 }