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

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