github.com/vmware/govmomi@v0.37.1/lookup/simulator/simulator_test.go (about)

     1  /*
     2  Copyright (c) 2018 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  	"log"
    22  	"testing"
    23  
    24  	"github.com/vmware/govmomi"
    25  	"github.com/vmware/govmomi/lookup"
    26  	"github.com/vmware/govmomi/lookup/types"
    27  	"github.com/vmware/govmomi/simulator"
    28  )
    29  
    30  func TestClient(t *testing.T) {
    31  	ctx := context.Background()
    32  
    33  	model := simulator.VPX()
    34  
    35  	defer model.Remove()
    36  	err := model.Create()
    37  	if err != nil {
    38  		log.Fatal(err)
    39  	}
    40  
    41  	s := model.Service.NewServer()
    42  	defer s.Close()
    43  
    44  	model.Service.RegisterSDK(New())
    45  
    46  	vc, err := govmomi.NewClient(ctx, s.URL, true)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	c, err := lookup.NewClient(ctx, vc.Client)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	id, err := c.SiteID(ctx)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if id != siteID {
    61  		t.Errorf("SiteID=%s", id)
    62  	}
    63  
    64  	vc.Logout(ctx) // List does not require authentication
    65  
    66  	_, err = c.List(ctx, nil)
    67  	if err == nil {
    68  		t.Error("expected error")
    69  	}
    70  
    71  	// test filters that should return 1 service
    72  	filters := []*types.LookupServiceRegistrationFilter{
    73  		{
    74  			ServiceType: &types.LookupServiceRegistrationServiceType{
    75  				Product: "com.vmware.cis",
    76  				Type:    "vcenterserver",
    77  			},
    78  			EndpointType: &types.LookupServiceRegistrationEndpointType{
    79  				Protocol: "vmomi",
    80  				Type:     "com.vmware.vim",
    81  			},
    82  		},
    83  		{
    84  			ServiceType: &types.LookupServiceRegistrationServiceType{
    85  				Type: "cs.identity",
    86  			},
    87  			EndpointType: &types.LookupServiceRegistrationEndpointType{
    88  				Protocol: "wsTrust",
    89  			},
    90  		},
    91  		{
    92  			ServiceType: &types.LookupServiceRegistrationServiceType{},
    93  			EndpointType: &types.LookupServiceRegistrationEndpointType{
    94  				Protocol: "vmomi",
    95  				Type:     "com.vmware.vim",
    96  			},
    97  		},
    98  	}
    99  
   100  	for _, filter := range filters {
   101  		info, err := c.List(ctx, filter)
   102  		if err != nil {
   103  			t.Fatal(err)
   104  		}
   105  
   106  		if len(info) != 1 {
   107  			t.Errorf("len=%d", len(info))
   108  		}
   109  
   110  		filter.ServiceType.Type = "enoent"
   111  
   112  		info, err = c.List(ctx, filter)
   113  		if err != nil {
   114  			t.Fatal(err)
   115  		}
   116  
   117  		if len(info) != 0 {
   118  			t.Errorf("len=%d", len(info))
   119  		}
   120  	}
   121  
   122  	// "empty" filters should return all services
   123  	filters = []*types.LookupServiceRegistrationFilter{
   124  		{},
   125  		{
   126  			ServiceType:  new(types.LookupServiceRegistrationServiceType),
   127  			EndpointType: new(types.LookupServiceRegistrationEndpointType),
   128  		},
   129  		{
   130  			EndpointType: new(types.LookupServiceRegistrationEndpointType),
   131  		},
   132  		{
   133  			ServiceType: new(types.LookupServiceRegistrationServiceType),
   134  		},
   135  	}
   136  
   137  	for _, filter := range filters {
   138  		info, err := c.List(ctx, filter)
   139  		if err != nil {
   140  			t.Fatal(err)
   141  		}
   142  
   143  		if len(info) != 4 {
   144  			t.Errorf("len=%d", len(info))
   145  		}
   146  	}
   147  
   148  	vc.Client.ServiceContent.Setting = nil // ensure we don't NPE without this set
   149  	_, err = lookup.NewClient(ctx, vc.Client)
   150  	if err != nil {
   151  		t.Fatal(err)
   152  	}
   153  }