Govmomi Rest/Vapi API不支持使用ESX()模拟器创建Rest客户端吗?

huangapple go评论89阅读模式
英文:

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

问题

我正在使用与vSphere Govmomi/vapi REST客户端交互的代码进行单元测试,用于与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
}

我使用了simulator.ESX()来模拟这个模型。

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

但是在创建REST客户端并尝试登录之后,我得到了以下错误:

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

我做错了什么吗?我是否应该在vmware的govmomi中使用不同的模拟器来进行vapi/rest客户端的测试?任何帮助将不胜感激。

英文:

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

答案1

得分: 0

我自己也遇到了这个问题。在查看vapi/rest模拟器的源代码时,发现它们内部只支持VPX模拟器。使用以下代码代替:

model := simulator.VPX()

在单元测试中,不要直接调用API,因为存在身份验证问题。vapi/rest API提供了用于测试目的的功能。

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

这个函数会自动生成一个*vim25.Client对象,并且还可以提供一个VPX模拟器实例。更多信息,请查阅文档。

在官方源代码的这里查看。

英文:

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.

huangapple
  • 本文由 发表于 2022年6月30日 19:51:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/72815309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定