Repository: vmware/govmomi
Pull request #3120 (2023-05-08 09:52:01)

vcsim: Fix FindByInventoryPath to handle root folder


## Description

This PR fixes FindByInventoryPath so that it can respond the root folder `/`.

Closes: #3119

## Type of change

Please mark options that are relevant:

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to
  not work as expected)
- [ ] This change requires a documentation update
- [ ] Build related change

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. If applicable, please also list any relevant
details for your test configuration.

- [x] add unit test

## Checklist:

- [x] My code follows the `CONTRIBUTION`
  [guidelines](https://github.com/vmware/govmomi/blob/main/CONTRIBUTING.md) of
  this project
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged


Patch:
diff --git a/simulator/search_index.go b/simulator/search_index.go
index 96e29627..79193862 100644
--- a/simulator/search_index.go
+++ b/simulator/search_index.go
@@ -53,6 +53,14 @@ func (s *SearchIndex) FindByDatastorePath(r *types.FindByDatastorePath) soap.Has
 func (s *SearchIndex) FindByInventoryPath(req *types.FindByInventoryPath) soap.HasFault {
 	body := &methods.FindByInventoryPathBody{Res: new(types.FindByInventoryPathResponse)}
 
+	root := Map.content().RootFolder
+	o := &root
+
+	if req.InventoryPath == "/" {
+		body.Res.Returnval = o
+		return body
+	}
+
 	split := func(c rune) bool {
 		return c == '/'
 	}
@@ -61,9 +69,6 @@ func (s *SearchIndex) FindByInventoryPath(req *types.FindByInventoryPath) soap.H
 		return body
 	}
 
-	root := Map.content().RootFolder
-	o := &root
-
 	for _, name := range path {
 		f := s.FindChild(&types.FindChild{Entity: *o, Name: name})
 


Test patch:
diff --git a/simulator/search_index_test.go b/simulator/search_index_test.go
index 0cac8dde..7e39d5d4 100644
--- a/simulator/search_index_test.go
+++ b/simulator/search_index_test.go
@@ -127,6 +127,20 @@ func TestSearchIndex(t *testing.T) {
 		if ref.Reference() != host.Reference() {
 			t.Errorf("moref mismatch %s != %s", ref, host.Reference())
 		}
+
+		rootFolder, err := finder.Folder(ctx, "/")
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		ref, err = si.FindByInventoryPath(ctx, "/")
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		if ref.Reference() != rootFolder.Reference() {
+			t.Errorf("moref mismatch %s != %s", ref, rootFolder.Reference())
+		}
 	}
 }
 
@@ -215,7 +229,7 @@ func TestSearchIndexFindChild(t *testing.T) {
 		t.Error("expected ManagedObjectNotFound fault")
 	}
 
-	for _, path := range []string{"", "/", "/enoent"} {
+	for _, path := range []string{"", "/enoent"} {
 		ref, err := si.FindByInventoryPath(ctx, path)
 		if err != nil {
 			t.Fatal(err)