1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package simulator
import (
"context"
"testing"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/simulator/esx"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
)
func TestUserDirectory(t *testing.T) {
s := New(NewServiceInstance(esx.ServiceContent, esx.RootFolder))
ts := s.NewServer()
defer ts.Close()
ctx := context.Background()
c, err := govmomi.NewClient(ctx, ts.URL, true)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
findUsers bool
findGroups bool
}{
{true, true},
{true, false},
{false, true},
{false, false},
}
ref := *c.ServiceContent.UserDirectory
for _, test := range tests {
req := types.RetrieveUserGroups{
This: ref,
SearchStr: "root",
ExactMatch: true,
FindUsers: test.findUsers,
FindGroups: test.findGroups,
}
result, err := methods.RetrieveUserGroups(ctx, c.Client, &req)
if err != nil {
t.Fatal(err)
}
expectedSize := 0
if test.findGroups {
expectedSize++
}
if test.findUsers {
expectedSize++
}
if len(result.Returnval) != expectedSize {
t.Fatalf("expect search result for root is %d; got %d", expectedSize, len(result.Returnval))
}
for _, u := range result.Returnval {
if u.GetUserSearchResult().Principal != "root" {
t.Fatalf("expect principal to be root; got %s", u.GetUserSearchResult().Principal)
}
if !test.findGroups && u.GetUserSearchResult().Group {
t.Fatal("expect search result is non-group; got group")
}
if !test.findUsers && !u.GetUserSearchResult().Group {
t.Fatal("expect search result is non-user; got user")
}
}
}
}
func TestUserDirectoryExactlyMatch(t *testing.T) {
s := New(NewServiceInstance(esx.ServiceContent, esx.RootFolder))
ts := s.NewServer()
defer ts.Close()
ctx := context.Background()
c, err := govmomi.NewClient(ctx, ts.URL, true)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
exactly bool
search string
expectedSize int
}{
{true, "root", 2},
{false, "ROO", 2},
}
ref := *c.ServiceContent.UserDirectory
for _, test := range tests {
req := types.RetrieveUserGroups{
This: ref,
SearchStr: test.search,
ExactMatch: test.exactly,
FindUsers: true,
FindGroups: true,
}
result, err := methods.RetrieveUserGroups(ctx, c.Client, &req)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
if len(result.Returnval) != test.expectedSize {
t.Fatalf("expect result contains %d results; got %d", test.expectedSize, len(result.Returnval))
}
}
}
|