github.com/vmware/govmomi@v0.51.0/simulator/option_manager_test.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package simulator
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/vmware/govmomi/object"
    12  	"github.com/vmware/govmomi/simulator/esx"
    13  	"github.com/vmware/govmomi/vim25/types"
    14  )
    15  
    16  func TestOptionManagerESX(t *testing.T) {
    17  	ctx := context.Background()
    18  
    19  	model := ESX()
    20  	model.Datastore = 0
    21  	model.Machine = 0
    22  
    23  	err := model.Create()
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	c := model.Service.client()
    29  
    30  	m := object.NewOptionManager(c, *c.ServiceContent.Setting)
    31  	_, err = m.Query(ctx, "config.vpxd.")
    32  	if err == nil {
    33  		t.Error("expected error")
    34  	}
    35  
    36  	host := object.NewHostSystem(c, esx.HostSystem.Reference())
    37  	m, err = host.ConfigManager().OptionManager(ctx)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	res, err := m.Query(ctx, "Config.HostAgent.")
    43  	if err != nil {
    44  		t.Error(err)
    45  	}
    46  
    47  	if len(res) == 0 {
    48  		t.Error("no results")
    49  	}
    50  
    51  	err = m.Update(ctx, []types.BaseOptionValue{&types.OptionValue{
    52  		Key:   "Config.HostAgent.log.level",
    53  		Value: "verbose",
    54  	}})
    55  
    56  	if err != nil {
    57  		t.Error(err)
    58  	}
    59  }
    60  
    61  func TestOptionManagerVPX(t *testing.T) {
    62  	ctx := context.Background()
    63  
    64  	model := VPX()
    65  	model.Datastore = 0
    66  	model.Machine = 0
    67  
    68  	err := model.Create()
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	c := model.Service.client()
    74  
    75  	m := object.NewOptionManager(c, *c.ServiceContent.Setting)
    76  	_, err = m.Query(ctx, "enoent")
    77  	if err == nil {
    78  		t.Error("expected error")
    79  	}
    80  
    81  	res, err := m.Query(ctx, "event.")
    82  	if err != nil {
    83  		t.Error(err)
    84  	}
    85  
    86  	if len(res) == 0 {
    87  		t.Error("no results")
    88  	}
    89  
    90  	val := &types.OptionValue{
    91  		Key: "event.maxAge",
    92  	}
    93  
    94  	// Get the existing maxAge value
    95  	for _, r := range res {
    96  		opt := r.GetOptionValue()
    97  		if opt.Key == val.Key {
    98  			val.Value = opt.Value
    99  		}
   100  	}
   101  
   102  	// Increase maxAge * 2
   103  	val.Value = val.Value.(int32) * 2
   104  	err = m.Update(ctx, []types.BaseOptionValue{val})
   105  	if err != nil {
   106  		t.Error(err)
   107  	}
   108  
   109  	// Verify maxAge was updated
   110  	res, err = m.Query(ctx, val.Key)
   111  	if err != nil {
   112  		t.Error(err)
   113  	}
   114  	if res[0].GetOptionValue().Value != val.Value {
   115  		t.Errorf("%s was not updated", val.Key)
   116  	}
   117  
   118  	// Expected to throw InvalidName fault
   119  	err = m.Update(ctx, []types.BaseOptionValue{&types.OptionValue{
   120  		Key: "ENOENT.anything",
   121  	}})
   122  	if err == nil {
   123  		t.Error("expected error")
   124  	}
   125  
   126  	// Add a new option
   127  	err = m.Update(ctx, []types.BaseOptionValue{&types.OptionValue{
   128  		Key:   "config.anything",
   129  		Value: "OK",
   130  	}})
   131  	if err != nil {
   132  		t.Error(err)
   133  	}
   134  }