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

     1  /*
     2  Copyright (c) 2024-2024 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_test
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/vmware/govmomi/find"
    24  	"github.com/vmware/govmomi/object"
    25  	"github.com/vmware/govmomi/simulator"
    26  	"github.com/vmware/govmomi/vim25"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  func TestVmCompatibilityChecker(t *testing.T) {
    31  	simulator.Test(func(ctx context.Context, c *vim25.Client) {
    32  		finder := find.NewFinder(c, true)
    33  		datacenter, err := finder.DefaultDatacenter(ctx)
    34  		if err != nil {
    35  			t.Fatalf("default datacenter not found: %s", err)
    36  		}
    37  		finder.SetDatacenter(datacenter)
    38  		vmList, err := finder.VirtualMachineList(ctx, "*")
    39  		if len(vmList) == 0 {
    40  			t.Fatal("vmList == 0")
    41  		}
    42  		vm := vmList[0]
    43  		vmRef := vm.Reference()
    44  
    45  		vmcc := object.NewVmCompatibilityChecker(c)
    46  
    47  		t.Run("CheckCompatibility", func(t *testing.T) {
    48  			results, err := vmcc.CheckCompatibility(
    49  				ctx,
    50  				vm.Reference(),
    51  				nil,
    52  				nil)
    53  
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  
    58  			for _, result := range results {
    59  				if len(result.Error) > 0 {
    60  					t.Fatal("result has errors")
    61  				}
    62  				if len(result.Warning) > 0 {
    63  					t.Fatal("result has warnings")
    64  				}
    65  			}
    66  		})
    67  
    68  		t.Run("CheckVmConfig", func(t *testing.T) {
    69  			t.Run("for existing VM", func(t *testing.T) {
    70  				results, err := vmcc.CheckVmConfig(
    71  					ctx,
    72  					types.VirtualMachineConfigSpec{
    73  						NumCPUs: 2,
    74  					},
    75  					&vmRef,
    76  					nil,
    77  					nil)
    78  
    79  				if err != nil {
    80  					t.Fatal(err)
    81  				}
    82  
    83  				for _, result := range results {
    84  					if len(result.Error) > 0 {
    85  						t.Fatal("result has errors")
    86  					}
    87  					if len(result.Warning) > 0 {
    88  						t.Fatal("result has warnings")
    89  					}
    90  				}
    91  			})
    92  			t.Run("for new VM", func(t *testing.T) {
    93  				results, err := vmcc.CheckVmConfig(
    94  					ctx,
    95  					types.VirtualMachineConfigSpec{
    96  						Name:    "my-vm",
    97  						NumCPUs: 2,
    98  					},
    99  					&vmRef,
   100  					nil,
   101  					nil)
   102  
   103  				if err != nil {
   104  					t.Fatal(err)
   105  				}
   106  
   107  				for _, result := range results {
   108  					if len(result.Error) > 0 {
   109  						t.Fatal("result has errors")
   110  					}
   111  					if len(result.Warning) > 0 {
   112  						t.Fatal("result has warnings")
   113  					}
   114  				}
   115  			})
   116  		})
   117  	})
   118  }