首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unittest失败的原因是我的proto存在差异,但diff看起来是一样的

Unittest失败的原因是我的proto存在差异,但diff看起来是一样的
EN

Stack Overflow用户
提问于 2021-09-24 20:21:39
回答 1查看 31关注 0票数 0

我正在做一个单元测试,用来测试一个返回proto消息的函数。在其中一个Proto字段中测试失败,但我在这些字段上看不到任何差异。

下面是我的函数:

代码语言:javascript
复制
 func (c *Client) contructInstanceRequest() *compute.Instance {
      prefix := "https://www.googleapis.com/compute/v1/projects/" + c.ProjectID
      sshKey := c.readSSHKey()

      instance := &compute.Instance{
          Name:           c.InstanceName,
          Description:    "compute sample instance",
          MinCpuPlatform: "Intel Cascade Lake",
          MachineType:    prefix + "/zones/" + c.Zone + "/machineTypes/c2-standard-4",
          CanIpForward:   true,
          Tags: &compute.Tags{
              Items: []string{
                  "http-server",
                  "https-server",
                  "eve-ng",
              },
          },
          Disks: []*compute.AttachedDisk{
              {
                  AutoDelete: true,
                  Boot:       true,
                  Type:       "PERSISTENT",
                  InitializeParams: &compute.AttachedDiskInitializeParams{
                      DiskName:    "my-root-" + c.InstanceName,
                      SourceImage: "projects/" + c.ProjectID + "/global/images/" + c.CustomEveNGImageName,
                      DiskType:    "projects/" + c.ProjectID + "/zones/us-central1-a/diskTypes/pd-ssd",
                  },
              },
          },
          NetworkInterfaces: []*compute.NetworkInterface{
              {
                  AccessConfigs: []*compute.AccessConfig{
                      {
                          Type: "ONE_TO_ONE_NAT",
                          Name: "External NAT",
                      },
                  },
                  Network: prefix + "/global/networks/default",
              },
          },
          ServiceAccounts: []*compute.ServiceAccount{
              {
                  Email: "default",
                  Scopes: []string{
                      compute.DevstorageFullControlScope,
                      compute.ComputeScope,
                  },
              },
          },
          Metadata: &compute.Metadata{
              Kind: "compute#metadata",
              Items: []*compute.MetadataItems{
                  {
                      Key:   "ssh-keys",
                      Value: proto.String(c.SSHKeyUsername + ":" + string(sshKey)),
                  },
              },
          },
      }
      return instance

  }

单元测试

代码语言:javascript
复制
func TestConstructInstanceRequest(t *testing.T) {
      client, err := setup(t)
      if err != nil {
          t.Errorf("could not create a new goeve client, error: %v", err)
      }

      tests := []struct {
          name string
          zone string
          want *compute.Instance
      }{
          {
              name: "instance1",
              want: &compute.Instance{
                  Name:                   "instance1",
                  Description:            "compute sample instance",
                  MinCpuPlatform:         "Intel Cascade Lake",
                  LastSuspendedTimestamp: "",
                  MachineType:            "https://www.googleapis.com/compute/v1/projects/testProject/zones/us-central1-a/machineTypes/c2-standard-4",
                  CanIpForward:           true,
                  Tags: &compute.Tags{
                      Items: []string{
                          "http-server",
                          "https-server",
                          "eve-ng",
                      },
                  },
                  Disks: []*compute.AttachedDisk{
                      {
                          AutoDelete: true,
                          Boot:       true,
                          Type:       "PERSISTENT",
                          InitializeParams: &compute.AttachedDiskInitializeParams{
                              DiskName:    "my-root-instance1",
                              SourceImage: "projects/testProject/global/images/test-eve-ng",
                              DiskType:    "projects/testProject/zones/us-central1-a/diskTypes/pd-ssd",
                          },
                      },
                  },
                  NetworkInterfaces: []*compute.NetworkInterface{
                      {
                          AccessConfigs: []*compute.AccessConfig{
                              {
                                  Type: "ONE_TO_ONE_NAT",
                                  Name: "External NAT",
                              },
                          },
                          Network: "https://www.googleapis.com/compute/v1/projects/testProject/global/networks/default",
                      },
                  },
                  ServiceAccounts: []*compute.ServiceAccount{
                      {
                          Email: "default",
                          Scopes: []string{
                              compute.DevstorageFullControlScope,
                              compute.ComputeScope,
                          },
                      },
                  },
                  Metadata: &compute.Metadata{
                      Kind:            "compute#metadata",
                      ForceSendFields: nil,
                      NullFields:      nil,
                      Items: []*compute.MetadataItems{
                          {
                              Key:   "ssh-keys",
                              Value: proto.String("eve:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDIn5Zc9uF4qO8c3e0bxL2jOfPckeuzS56aATA/5aj/Cjx/xiZF+z7t8k5dIg4qX2KJR162iI  NDnef0XnTPsPs6q6rlVY1ZztZ6OcjqR7bhjfCNVd3s1+zY31uIj3WuorcRzy29yYZUSS7ZTUDXj2ZY5aGDsB47+Cybx/xVsedV83hATB05kQOKFpvRUKdnrnRxjyliwE9C2PbFWViK7sJk"),
                          },
                      },
                  },
              },
          },
      }

      for _, tc := range tests {
          got := client.contructInstanceRequest()
          if diff := cmp.Diff(tc.want.Metadata.Items, got.Metadata.Items); diff != "" {
              t.Errorf("contructInstanceRequest() returned unexpected diff (-want +got):\n%s", diff)
          }

      }
  }

我在ssh-keys value下遇到了这个问题。

以下是diff的输出:

代码语言:javascript
复制
go test
--- FAIL: TestConstructInstanceRequest (0.00s)
    goeve_test.go:150: contructInstanceRequest() returned unexpected diff (-want +got):
          []*compute.MetadataItems{
                &{
                        Key:             "ssh-keys",
        -               Value:           &"eve:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDIn5Zc9uF4qO8c3e0bxL2jOfPckeuzS56aATA/5aj/Cjx/xiZF+z7t8k5dIg4qX2KJR162iINDnef0XnTPsPs6q6rlVY1ZztZ6OcjqR7bhjfCNVd3s1+zY31uIj3WuorcRzy29yYZUSS7ZTUDXj2ZY5aGDsB47+Cybx/xVsedV83hATB05kQOKFpvRUKdnrnRxjyliwE9C2PbFWViK7sJk",
        +               Value:           &"eve:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDIn5Zc9uF4qO8c3e0bxL2jOfPckeuzS56aATA/5aj/Cjx/xiZF+z7t8k5dIg4qX2KJR162iINDnef0XnTPsPs6q6rlVY1ZztZ6OcjqR7bhjfCNVd3s1+zY31uIj3WuorcRzy29yYZUSS7ZTUDXj2ZY5aGDsB47+Cybx/xVsedV83hATB05kQOKFpvRUKdnrnRxjyliwE9C2PbFWViK7sJk"...,
                        ForceSendFields: nil,
                        NullFields:      nil,
                },
          }
FAIL
exit status 1
FAIL    go-eve/goeve    0.007s

正如您所看到的,ssh-keys值没有区别。我很确定我做错了什么,如果有人能看看这个,我将不胜感激。谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-09-25 01:24:44

我打印这两个值。tc.want.Metadata.Items[0].Valuegot.Metadata.Items[0].Value,看到了,是的,有不同之处。我几乎漏掉了一半的弦。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69320745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档