github.com/vmware/govmomi@v0.37.2/test/functional/issue_242_test.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package functional
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/vmware/govmomi/object"
    26  	"github.com/vmware/govmomi/property"
    27  	"github.com/vmware/govmomi/vim25/mo"
    28  	"github.com/vmware/govmomi/vim25/types"
    29  )
    30  
    31  func TestIssue242(t *testing.T) {
    32  	ctx, cancel := context.WithCancel(context.Background())
    33  	defer cancel()
    34  
    35  	h := NewHelper(t)
    36  	defer h.Teardown()
    37  
    38  	h.RequireVirtualCenter()
    39  
    40  	df, err := h.Datacenter().Folders(ctx)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	cr := h.ComputeResource()
    46  
    47  	// Get local datastores for compute resource
    48  	dss, err := h.LocalDatastores(ctx, cr)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	if len(dss) == 0 {
    53  		t.Fatalf("No local datastores")
    54  	}
    55  
    56  	// Get root resource pool for compute resource
    57  	rp, err := cr.ResourcePool(ctx)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	spec := types.VirtualMachineConfigSpec{
    63  		Name:     fmt.Sprintf("govmomi-test-%s", time.Now().Format(time.RFC3339)),
    64  		Files:    &types.VirtualMachineFileInfo{VmPathName: fmt.Sprintf("[%s]", dss[0].Name())},
    65  		NumCPUs:  1,
    66  		MemoryMB: 32,
    67  	}
    68  
    69  	// Create new VM
    70  	task, err := df.VmFolder.CreateVM(context.Background(), spec, rp, nil)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	info, err := task.WaitForResult(context.Background(), nil)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	vm := object.NewVirtualMachine(h.c, info.Result.(types.ManagedObjectReference))
    81  	defer func() {
    82  		task, err := vm.Destroy(context.Background())
    83  		if err != nil {
    84  			panic(err)
    85  		}
    86  		err = task.Wait(context.Background())
    87  		if err != nil {
    88  			panic(err)
    89  		}
    90  	}()
    91  
    92  	// Mark VM as template
    93  	err = vm.MarkAsTemplate(context.Background())
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  
    98  	// Get "environmentBrowser" property for VM template
    99  	var mvm mo.VirtualMachine
   100  	err = property.DefaultCollector(h.c).RetrieveOne(ctx, vm.Reference(), []string{"environmentBrowser"}, &mvm)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  }