github.com/vmware/govmomi@v0.43.0/vapi/library/finder/path_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 finder_test
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/vmware/govmomi/find"
    26  	"github.com/vmware/govmomi/object"
    27  	"github.com/vmware/govmomi/simulator"
    28  	"github.com/vmware/govmomi/vapi/library"
    29  	"github.com/vmware/govmomi/vapi/library/finder"
    30  	"github.com/vmware/govmomi/vapi/rest"
    31  	_ "github.com/vmware/govmomi/vapi/simulator"
    32  	"github.com/vmware/govmomi/vim25"
    33  	"github.com/vmware/govmomi/vim25/mo"
    34  	"github.com/vmware/govmomi/vim25/types"
    35  )
    36  
    37  func TestResolveLibraryItemStorage(t *testing.T) {
    38  	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
    39  		rc := rest.NewClient(vc)
    40  
    41  		ds, err := find.NewFinder(vc).Datastore(ctx, "*")
    42  		if err != nil {
    43  			t.Fatal(err)
    44  		}
    45  
    46  		var props mo.Datastore
    47  		err = ds.Properties(ctx, ds.Reference(), []string{"name", "summary"}, &props)
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  
    52  		fsTypes := []string{
    53  			string(types.HostFileSystemVolumeFileSystemTypeOTHER),
    54  			string(types.HostFileSystemVolumeFileSystemTypeVsan),
    55  		}
    56  
    57  		for _, fs := range fsTypes {
    58  			// client uses DatastoreNamespaceManager only when datastore fs is vsan/vvol
    59  			simulator.Map.Get(ds.Reference()).(*simulator.Datastore).Summary.Type = fs
    60  
    61  			u := props.Summary.Url
    62  
    63  			storage := []library.Storage{
    64  				{
    65  					StorageBacking: library.StorageBacking{DatastoreID: ds.Reference().Value, Type: "DATASTORE"},
    66  					StorageURIs: []string{
    67  						fmt.Sprintf("%s/contentlib-${lib_id}/${item_id}/${file_name}_${file_id}.iso", u),
    68  						fmt.Sprintf("%s/contentlib-${lib_id}/${item_id}/${file_name}_${file_id}.iso?serverId=${server_id}", u),
    69  					},
    70  				},
    71  			}
    72  
    73  			f := finder.NewPathFinder(library.NewManager(rc), vc)
    74  
    75  			err = f.ResolveLibraryItemStorage(ctx, storage)
    76  			if err != nil {
    77  				t.Fatal(err)
    78  			}
    79  
    80  			var path object.DatastorePath
    81  			for _, s := range storage {
    82  				for _, u := range s.StorageURIs {
    83  					path.FromString(u)
    84  					if path.Datastore != props.Name {
    85  						t.Errorf("failed to parse %s", u)
    86  					}
    87  					if strings.Contains(u, "?") {
    88  						t.Errorf("includes query: %s", u)
    89  					}
    90  				}
    91  			}
    92  		}
    93  	})
    94  }