github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/vapp_vm_test.go (about)

     1  //go:build vapp || vm || functional || ALL
     2  
     3  /*
     4   * Copyright 2019 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    11  	. "gopkg.in/check.v1"
    12  )
    13  
    14  // guestPropertyGetSetter interface is used for covering tests in both VM and vApp guest property
    15  type productSectionListGetSetter interface {
    16  	GetProductSectionList() (*types.ProductSectionList, error)
    17  	SetProductSectionList(productSection *types.ProductSectionList) (*types.ProductSectionList, error)
    18  }
    19  
    20  // propertyTester is a guest property setter accepting guestPropertyGetSetter interface for trying
    21  // out settings on all objects implementing such interface
    22  func propertyTester(vcd *TestVCD, check *C, object productSectionListGetSetter) {
    23  	productSection := &types.ProductSectionList{
    24  		ProductSection: &types.ProductSection{
    25  			Info: "Custom properties",
    26  			Property: []*types.Property{
    27  				&types.Property{
    28  					UserConfigurable: false,
    29  					Key:              "sys_owner",
    30  					Label:            "sys_owner_label",
    31  					Type:             "string",
    32  					DefaultValue:     "sys_owner_default",
    33  					Value:            &types.Value{Value: "test"},
    34  				},
    35  				&types.Property{
    36  					UserConfigurable: true,
    37  					Key:              "asset_tag",
    38  					Label:            "asset_tag_label",
    39  					Type:             "string",
    40  					DefaultValue:     "asset_tag_default",
    41  					Value:            &types.Value{Value: "xxxyyy"},
    42  				},
    43  				&types.Property{
    44  					UserConfigurable: true,
    45  					Key:              "guestinfo.config.bootstrap.ip",
    46  					Label:            "guestinfo.config.bootstrap.ip_label",
    47  					Type:             "string",
    48  					DefaultValue:     "default_ip",
    49  					Value:            &types.Value{Value: "192.168.12.180"},
    50  				},
    51  			},
    52  		},
    53  	}
    54  
    55  	productSection.SortByPropertyKeyName()
    56  
    57  	gotproductSection, err := object.SetProductSectionList(productSection)
    58  	check.Assert(err, IsNil)
    59  	gotproductSection.SortByPropertyKeyName()
    60  
    61  	getproductSection, err := object.GetProductSectionList()
    62  	check.Assert(err, IsNil)
    63  	getproductSection.SortByPropertyKeyName()
    64  
    65  	// Check that values were set in API
    66  	check.Assert(getproductSection, NotNil)
    67  	check.Assert(getproductSection.ProductSection, NotNil)
    68  	check.Assert(len(getproductSection.ProductSection.Property), Equals, 3)
    69  
    70  	check.Assert(getproductSection.ProductSection.Property[0].Key, Equals, "asset_tag")
    71  	check.Assert(getproductSection.ProductSection.Property[0].Label, Equals, "asset_tag_label")
    72  	check.Assert(getproductSection.ProductSection.Property[0].Type, Equals, "string")
    73  	check.Assert(getproductSection.ProductSection.Property[0].Value.Value, Equals, "xxxyyy")
    74  	check.Assert(getproductSection.ProductSection.Property[0].DefaultValue, Equals, "asset_tag_default")
    75  	check.Assert(getproductSection.ProductSection.Property[0].UserConfigurable, Equals, true)
    76  
    77  	check.Assert(getproductSection.ProductSection.Property[1].Key, Equals, "guestinfo.config.bootstrap.ip")
    78  	check.Assert(getproductSection.ProductSection.Property[1].Label, Equals, "guestinfo.config.bootstrap.ip_label")
    79  	check.Assert(getproductSection.ProductSection.Property[1].Type, Equals, "string")
    80  	check.Assert(getproductSection.ProductSection.Property[1].Value.Value, Equals, "192.168.12.180")
    81  	check.Assert(getproductSection.ProductSection.Property[1].DefaultValue, Equals, "default_ip")
    82  	check.Assert(getproductSection.ProductSection.Property[1].UserConfigurable, Equals, true)
    83  
    84  	check.Assert(getproductSection.ProductSection.Property[2].Key, Equals, "sys_owner")
    85  	check.Assert(getproductSection.ProductSection.Property[2].Label, Equals, "sys_owner_label")
    86  	check.Assert(getproductSection.ProductSection.Property[2].Type, Equals, "string")
    87  	check.Assert(getproductSection.ProductSection.Property[2].Value.Value, Equals, "test")
    88  	check.Assert(getproductSection.ProductSection.Property[2].DefaultValue, Equals, "sys_owner_default")
    89  	check.Assert(getproductSection.ProductSection.Property[2].UserConfigurable, Equals, false)
    90  
    91  	// Ensure the object are deeply equal
    92  	check.Assert(gotproductSection.ProductSection.Property, DeepEquals, productSection.ProductSection.Property)
    93  	check.Assert(getproductSection, DeepEquals, gotproductSection)
    94  }
    95  
    96  // guestPropertyGetSetter interface is used for covering tests
    97  type getGuestCustomizationSectionGetSetter interface {
    98  	GetGuestCustomizationSection() (*types.GuestCustomizationSection, error)
    99  	SetGuestCustomizationSection(guestCustomizationSection *types.GuestCustomizationSection) (*types.GuestCustomizationSection, error)
   100  }
   101  
   102  // guestCustomizationPropertyTester is a guest customization property get and setter accepting guestPropertyGetSetter interface for trying
   103  // out settings on all objects implementing such interface
   104  func guestCustomizationPropertyTester(vcd *TestVCD, check *C, object getGuestCustomizationSectionGetSetter) {
   105  	setupedGuestCustomizationSection := &types.GuestCustomizationSection{
   106  		Enabled: addrOf(true), JoinDomainEnabled: addrOf(false), UseOrgSettings: addrOf(false),
   107  		DomainUserName: "", DomainName: "", DomainUserPassword: "",
   108  		AdminPasswordEnabled: addrOf(true), AdminPassword: "adminPass", AdminPasswordAuto: addrOf(false),
   109  		AdminAutoLogonEnabled: addrOf(true), AdminAutoLogonCount: 15, ResetPasswordRequired: addrOf(true),
   110  		CustomizationScript: "ls", ComputerName: "Cname18"}
   111  
   112  	guestCustomizationSection, err := object.SetGuestCustomizationSection(setupedGuestCustomizationSection)
   113  	check.Assert(err, IsNil)
   114  
   115  	// Check that values were set from API
   116  	check.Assert(guestCustomizationSection, NotNil)
   117  
   118  	check.Assert(*guestCustomizationSection.Enabled, Equals, true)
   119  	check.Assert(*guestCustomizationSection.JoinDomainEnabled, Equals, false)
   120  	check.Assert(*guestCustomizationSection.UseOrgSettings, Equals, false)
   121  	check.Assert(guestCustomizationSection.DomainUserName, Equals, "")
   122  	check.Assert(guestCustomizationSection.DomainName, Equals, "")
   123  	check.Assert(guestCustomizationSection.DomainUserPassword, Equals, "")
   124  	check.Assert(*guestCustomizationSection.AdminPasswordEnabled, Equals, true)
   125  	check.Assert(*guestCustomizationSection.AdminPasswordAuto, Equals, false)
   126  	check.Assert(guestCustomizationSection.AdminPassword, Equals, "adminPass")
   127  	check.Assert(guestCustomizationSection.AdminAutoLogonCount, Equals, 15)
   128  	check.Assert(*guestCustomizationSection.AdminAutoLogonEnabled, Equals, true)
   129  	check.Assert(*guestCustomizationSection.ResetPasswordRequired, Equals, true)
   130  	check.Assert(guestCustomizationSection.CustomizationScript, Equals, "ls")
   131  	check.Assert(guestCustomizationSection.ComputerName, Equals, "Cname18")
   132  
   133  	// Double check if GuestCustomizationSection retrieved from separate API is the same as the one
   134  	// embedded directly in the VM
   135  	vm := object.(*VM)
   136  
   137  	// Refresh VM to have the latest structure
   138  	err = vm.Refresh()
   139  	check.Assert(err, IsNil)
   140  
   141  	// Deep compare values retrieved from separate API to the ones embedded into VM
   142  	guestCustomizationSection.Xmlns = "" // embedded VM structure does not have this field
   143  	// Links amount may differ between structures
   144  	guestCustomizationSection.Link = types.LinkList{}
   145  	vm.VM.GuestCustomizationSection.Link = types.LinkList{}
   146  	check.Assert(guestCustomizationSection, DeepEquals, vm.VM.GuestCustomizationSection)
   147  }