github.com/vmware/govmomi@v0.51.0/object/datastore_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 object_test
     6  
     7  import (
     8  	"context"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/vmware/govmomi/find"
    13  	"github.com/vmware/govmomi/object"
    14  	"github.com/vmware/govmomi/property"
    15  	"github.com/vmware/govmomi/simulator"
    16  	"github.com/vmware/govmomi/vim25"
    17  	"github.com/vmware/govmomi/vim25/mo"
    18  	"github.com/vmware/govmomi/vim25/soap"
    19  )
    20  
    21  // Datastore should implement the Reference interface.
    22  var _ object.Reference = object.Datastore{}
    23  
    24  func TestDatastoreFindInventoryPath(t *testing.T) {
    25  	model := simulator.VPX()
    26  	model.Datacenter = 2
    27  	model.Datastore = 2
    28  
    29  	simulator.Test(func(ctx context.Context, c *vim25.Client) {
    30  		refs := simulator.Map(ctx).All("Datastore")
    31  
    32  		for _, obj := range refs {
    33  			ds := object.NewDatastore(c, obj.Reference())
    34  
    35  			upload := func() error {
    36  				p := soap.DefaultUpload
    37  				r := strings.NewReader(ds.Name())
    38  				p.ContentLength = r.Size()
    39  				return ds.Upload(ctx, r, "name.txt", &p)
    40  
    41  			}
    42  
    43  			// with InventoryPath not set
    44  			if err := upload(); err != nil {
    45  				t.Error(err)
    46  			}
    47  
    48  			if ds.InventoryPath != "" {
    49  				t.Error("InventoryPath should still be empty")
    50  			}
    51  
    52  			// Set InventoryPath and DatacenterPath
    53  			err := ds.FindInventoryPath(ctx)
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  
    58  			if !strings.HasPrefix(ds.InventoryPath, ds.DatacenterPath) {
    59  				t.Errorf("InventoryPath=%s, DatacenterPath=%s", ds.InventoryPath, ds.DatacenterPath)
    60  			}
    61  
    62  			// with InventoryPath set
    63  			if err := upload(); err != nil {
    64  				t.Error(err)
    65  			}
    66  
    67  			tests := []struct {
    68  				path, kind string
    69  			}{
    70  				{ds.InventoryPath, "Datastore"},
    71  				{ds.DatacenterPath, "Datacenter"},
    72  			}
    73  
    74  			for _, test := range tests {
    75  				ref, err := object.NewSearchIndex(c).FindByInventoryPath(ctx, test.path)
    76  				if err != nil {
    77  					t.Fatal(err)
    78  				}
    79  
    80  				if ref == nil {
    81  					t.Fatalf("failed to find %s", test.path)
    82  				}
    83  				if ref.Reference().Type != test.kind {
    84  					t.Errorf("%s is not a %s", test.path, test.kind)
    85  				}
    86  			}
    87  		}
    88  	}, model)
    89  }
    90  
    91  func TestDatastoreInfo(t *testing.T) {
    92  	simulator.Test(func(ctx context.Context, c *vim25.Client) {
    93  		obj, err := find.NewFinder(c).DefaultDatastore(ctx)
    94  		if err != nil {
    95  			t.Fatal(err)
    96  		}
    97  		pc := property.DefaultCollector(c)
    98  
    99  		props := []string{
   100  			"info.url",
   101  			"info.name",
   102  		}
   103  
   104  		var ds mo.Datastore
   105  		err = pc.RetrieveOne(ctx, obj.Reference(), props, &ds)
   106  		if err != nil {
   107  			t.Fatal(err)
   108  		}
   109  
   110  		info := ds.Info.GetDatastoreInfo()
   111  		if info.Url == "" {
   112  			t.Error("no info.url")
   113  		}
   114  		if info.Name == "" {
   115  			t.Error("no info.name")
   116  		}
   117  	})
   118  }