Unittest由于我的原型上的diff而失败,但diff看起来是一样的



我正在编写一个单元测试,用于测试返回原型消息的函数。在一个Proto字段中测试失败,但我没有看到字段有任何差异。

下面是我的函数:

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
}

Unittest

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值下遇到问题。

以下是diff的输出:

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值没有差异。我很确定我做错了什么,如果有人能看看这个,我会很感激的。由于

我打印这两个值。tc.want.Metadata.Items[0].Valuegot.Metadata.Items[0].Value,我发现确实有差异,我丢失了几乎一半的字符串。

相关内容

最新更新