github.com/vmware/govmomi@v0.43.0/object/datastore_test.go (about)

     1  /*
     2  Copyright (c) 2015 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 object_test
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/vmware/govmomi/object"
    25  	"github.com/vmware/govmomi/simulator"
    26  	"github.com/vmware/govmomi/vim25"
    27  	"github.com/vmware/govmomi/vim25/soap"
    28  )
    29  
    30  // Datastore should implement the Reference interface.
    31  var _ object.Reference = object.Datastore{}
    32  
    33  func TestDatastoreFindInventoryPath(t *testing.T) {
    34  	model := simulator.VPX()
    35  	model.Datacenter = 2
    36  	model.Datastore = 2
    37  
    38  	simulator.Test(func(ctx context.Context, c *vim25.Client) {
    39  		refs := simulator.Map.All("Datastore")
    40  
    41  		for _, obj := range refs {
    42  			ds := object.NewDatastore(c, obj.Reference())
    43  
    44  			upload := func() error {
    45  				p := soap.DefaultUpload
    46  				r := strings.NewReader(ds.Name())
    47  				p.ContentLength = r.Size()
    48  				return ds.Upload(ctx, r, "name.txt", &p)
    49  
    50  			}
    51  
    52  			// with InventoryPath not set
    53  			if err := upload(); err != nil {
    54  				t.Error(err)
    55  			}
    56  
    57  			if ds.InventoryPath != "" {
    58  				t.Error("InventoryPath should still be empty")
    59  			}
    60  
    61  			// Set InventoryPath and DatacenterPath
    62  			err := ds.FindInventoryPath(ctx)
    63  			if err != nil {
    64  				t.Fatal(err)
    65  			}
    66  
    67  			if !strings.HasPrefix(ds.InventoryPath, ds.DatacenterPath) {
    68  				t.Errorf("InventoryPath=%s, DatacenterPath=%s", ds.InventoryPath, ds.DatacenterPath)
    69  			}
    70  
    71  			// with InventoryPath set
    72  			if err := upload(); err != nil {
    73  				t.Error(err)
    74  			}
    75  
    76  			tests := []struct {
    77  				path, kind string
    78  			}{
    79  				{ds.InventoryPath, "Datastore"},
    80  				{ds.DatacenterPath, "Datacenter"},
    81  			}
    82  
    83  			for _, test := range tests {
    84  				ref, err := object.NewSearchIndex(c).FindByInventoryPath(ctx, test.path)
    85  				if err != nil {
    86  					t.Fatal(err)
    87  				}
    88  
    89  				if ref == nil {
    90  					t.Fatalf("failed to find %s", test.path)
    91  				}
    92  				if ref.Reference().Type != test.kind {
    93  					t.Errorf("%s is not a %s", test.path, test.kind)
    94  				}
    95  			}
    96  		}
    97  	}, model)
    98  }