Search code examples
govmwarevspheregovmomi

Govmomi Rest/Vapi API does not support ESX() simulators to create Rest clients?


I was doing some unit testing with vSphere Govmomi/vapi REST client used to interact with V Center.

var insecure bool = true
type RestClient struct {
 url *url.URL
 restClient *rest.Client
}

func (c *RestClient) GetRestClient(ctx context.Context) (*rest.Client, error){
 soapClient := soap.NewClient(c.url, insecure)
 vim25Client, err := vim25.NewClient(ctx, soapClient)
 if err != nil {
  return nil, err
 }
 c.restClient = rest.NewClient(vim25Client)
 err = c.restClient.Login(ctx, c.url.User)
 if err != nil {
  return nil, err
 }
 return c.restClient, nil
}

I used simulator.ESX() to simulate the model.

func TestGetRestClient(t *testing.T){
 ctx := context.Background()
 model := simulator.ESX()
 defer model.Remove()
 err := model.Create()
 if err != nil {
  t.Fatal(err)
 }
 server := model.Service.NewServer()
 defer server.Close()
 url := server.URL
 var client RestClient = RestClient{url: url,}
 _, err := client.GetRestClient(ctx)
 if err != nil {
  t.Errorf("err = %v", err)
  return
 }
}

But after creating the rest client and trying to login(), I get

error= POST http://127.0.0.1:36655/rest/com/vmware/cis/session: 404 Not Found

Am I doing something wrong? Am I supposed to use a different simulator for vapi/rest clients in vmware's govmomi. Any help would be highly appreciated


Solution

  • Was facing this issue myself. Upon going through the source code of vapi/rest simulators, found out that they are internally supporting only VPX simulators. Use

    model := simulator.VPX()
    

    instead of

    model := simulator.ESX()
    

    In unit testing,do not try to call API directly, there are authentication issues.The vapi/rest API provides with a functionality for testing purposes.

    simulator.Test(func(ctx context.Context, vc *vim25.Client)
    

    This function automatically generates a *vim25.Client object and also it can be provided with a VPX simulator instance.For more info,check the documentation.

    Check it here in the official source code.