github.com/vmware/govmomi@v0.43.0/simulator/user_directory_test.go (about)

     1  /*
     2  Copyright (c) 2017 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 simulator
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/vmware/govmomi"
    24  	"github.com/vmware/govmomi/simulator/esx"
    25  	"github.com/vmware/govmomi/vim25/methods"
    26  	"github.com/vmware/govmomi/vim25/types"
    27  )
    28  
    29  func TestUserDirectory(t *testing.T) {
    30  	s := New(NewServiceInstance(SpoofContext(), esx.ServiceContent, esx.RootFolder))
    31  
    32  	ts := s.NewServer()
    33  	defer ts.Close()
    34  
    35  	ctx := context.Background()
    36  
    37  	c, err := govmomi.NewClient(ctx, ts.URL, true)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	var tests = []struct {
    43  		findUsers  bool
    44  		findGroups bool
    45  	}{
    46  		{true, true},
    47  		{true, false},
    48  		{false, true},
    49  		{false, false},
    50  	}
    51  
    52  	ref := *c.ServiceContent.UserDirectory
    53  
    54  	for _, test := range tests {
    55  		req := types.RetrieveUserGroups{
    56  			This:       ref,
    57  			SearchStr:  "root",
    58  			ExactMatch: true,
    59  			FindUsers:  test.findUsers,
    60  			FindGroups: test.findGroups,
    61  		}
    62  
    63  		result, err := methods.RetrieveUserGroups(ctx, c.Client, &req)
    64  		if err != nil {
    65  			t.Fatal(err)
    66  		}
    67  
    68  		expectedSize := 0
    69  		if test.findGroups {
    70  			expectedSize++
    71  		}
    72  		if test.findUsers {
    73  			expectedSize++
    74  		}
    75  
    76  		if len(result.Returnval) != expectedSize {
    77  			t.Fatalf("expect search result for root is %d; got %d", expectedSize, len(result.Returnval))
    78  		}
    79  
    80  		for _, u := range result.Returnval {
    81  			if u.GetUserSearchResult().Principal != "root" {
    82  				t.Fatalf("expect principal to be root; got %s", u.GetUserSearchResult().Principal)
    83  			}
    84  			if !test.findGroups && u.GetUserSearchResult().Group {
    85  				t.Fatal("expect search result is non-group; got group")
    86  			}
    87  			if !test.findUsers && !u.GetUserSearchResult().Group {
    88  				t.Fatal("expect search result is non-user; got user")
    89  			}
    90  		}
    91  	}
    92  }
    93  
    94  func TestUserDirectoryExactlyMatch(t *testing.T) {
    95  	s := New(NewServiceInstance(SpoofContext(), esx.ServiceContent, esx.RootFolder))
    96  
    97  	ts := s.NewServer()
    98  	defer ts.Close()
    99  
   100  	ctx := context.Background()
   101  
   102  	c, err := govmomi.NewClient(ctx, ts.URL, true)
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	var tests = []struct {
   108  		exactly      bool
   109  		search       string
   110  		expectedSize int
   111  	}{
   112  		{true, "root", 2},
   113  		{false, "ROO", 2},
   114  	}
   115  
   116  	ref := *c.ServiceContent.UserDirectory
   117  
   118  	for _, test := range tests {
   119  		req := types.RetrieveUserGroups{
   120  			This:       ref,
   121  			SearchStr:  test.search,
   122  			ExactMatch: test.exactly,
   123  			FindUsers:  true,
   124  			FindGroups: true,
   125  		}
   126  
   127  		result, err := methods.RetrieveUserGroups(ctx, c.Client, &req)
   128  		if err != nil {
   129  			t.Fatal(err)
   130  		}
   131  
   132  		if err != nil {
   133  			t.Fatal(err)
   134  		}
   135  
   136  		if len(result.Returnval) != test.expectedSize {
   137  			t.Fatalf("expect result contains %d results; got %d", test.expectedSize, len(result.Returnval))
   138  		}
   139  	}
   140  }