1

I'm working with vSphere govmomi client to interact with vCenter.

I'm having problem in unit testing the govmomi vapi/rest client. The sample code for creating rest client is:

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
}

To write their unit tests, I'm using govmomi simulator and vapi/simulator.

import(
 "context"
 "testing"
 "net/url"
 "github.com/vmware/govmomi/simulator"
 _ "github.com/vmware/govmomi/vapi/simulator"
)

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
 }
}

The testcase should pass, but it returns an error

err= POST "http://127.0.0.1:34691/rest/com/vmware/cis/session: 404 Not Found

I'm not able to understand what I'm missing or doing wrong in unit testing. I've done similar test for creating a govmomi client using simulator and it never came across this error.

NOTE: This rest client is created successfully as I'm able to interact with vCenter using this client.

Thanks in advance!

temptech
  • 11
  • 2

0 Answers0