mirror of https://github.com/status-im/consul.git
Added unit tests for structs and fixed PartialClone()
This commit is contained in:
parent
80dde5465b
commit
66fdf445e8
|
@ -380,6 +380,10 @@ type ServiceNode struct {
|
|||
func (s *ServiceNode) PartialClone() *ServiceNode {
|
||||
tags := make([]string, len(s.ServiceTags))
|
||||
copy(tags, s.ServiceTags)
|
||||
nsmeta := make(map[string]string)
|
||||
for k, v := range s.ServiceMeta {
|
||||
nsmeta[k] = v
|
||||
}
|
||||
|
||||
return &ServiceNode{
|
||||
// Skip ID, see above.
|
||||
|
@ -391,7 +395,7 @@ func (s *ServiceNode) PartialClone() *ServiceNode {
|
|||
ServiceTags: tags,
|
||||
ServiceAddress: s.ServiceAddress,
|
||||
ServicePort: s.ServicePort,
|
||||
ServiceMeta: s.ServiceMeta,
|
||||
ServiceMeta: nsmeta,
|
||||
ServiceEnableTagOverride: s.ServiceEnableTagOverride,
|
||||
RaftIndex: RaftIndex{
|
||||
CreateIndex: s.CreateIndex,
|
||||
|
|
|
@ -133,11 +133,14 @@ func testServiceNode() *ServiceNode {
|
|||
NodeMeta: map[string]string{
|
||||
"tag": "value",
|
||||
},
|
||||
ServiceID: "service1",
|
||||
ServiceName: "dogs",
|
||||
ServiceTags: []string{"prod", "v1"},
|
||||
ServiceAddress: "127.0.0.2",
|
||||
ServicePort: 8080,
|
||||
ServiceID: "service1",
|
||||
ServiceName: "dogs",
|
||||
ServiceTags: []string{"prod", "v1"},
|
||||
ServiceAddress: "127.0.0.2",
|
||||
ServicePort: 8080,
|
||||
ServiceMeta: map[string]string{
|
||||
"service": "metadata",
|
||||
},
|
||||
ServiceEnableTagOverride: true,
|
||||
RaftIndex: RaftIndex{
|
||||
CreateIndex: 1,
|
||||
|
@ -175,6 +178,17 @@ func TestStructs_ServiceNode_PartialClone(t *testing.T) {
|
|||
if reflect.DeepEqual(sn, clone) {
|
||||
t.Fatalf("clone wasn't independent of the original")
|
||||
}
|
||||
|
||||
revert := make([]string, len(sn.ServiceTags)-1)
|
||||
copy(revert, sn.ServiceTags[0:len(sn.ServiceTags)-1])
|
||||
sn.ServiceTags = revert
|
||||
if !reflect.DeepEqual(sn, clone) {
|
||||
t.Fatalf("bad: %v VS %v", clone, sn)
|
||||
}
|
||||
sn.ServiceMeta["new_meta"] = "new_value"
|
||||
if reflect.DeepEqual(sn, clone) {
|
||||
t.Fatalf("clone wasn't independent of the original for ServiceMeta")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStructs_ServiceNode_Conversions(t *testing.T) {
|
||||
|
@ -196,10 +210,14 @@ func TestStructs_ServiceNode_Conversions(t *testing.T) {
|
|||
|
||||
func TestStructs_NodeService_IsSame(t *testing.T) {
|
||||
ns := &NodeService{
|
||||
ID: "node1",
|
||||
Service: "theservice",
|
||||
Tags: []string{"foo", "bar"},
|
||||
Address: "127.0.0.1",
|
||||
ID: "node1",
|
||||
Service: "theservice",
|
||||
Tags: []string{"foo", "bar"},
|
||||
Address: "127.0.0.1",
|
||||
ServiceMeta: map[string]string{
|
||||
"meta1": "value1",
|
||||
"meta2": "value2",
|
||||
},
|
||||
Port: 1234,
|
||||
EnableTagOverride: true,
|
||||
}
|
||||
|
@ -214,6 +232,11 @@ func TestStructs_NodeService_IsSame(t *testing.T) {
|
|||
Address: "127.0.0.1",
|
||||
Port: 1234,
|
||||
EnableTagOverride: true,
|
||||
ServiceMeta: map[string]string{
|
||||
// We don't care about order
|
||||
"meta2": "value2",
|
||||
"meta1": "value1",
|
||||
},
|
||||
RaftIndex: RaftIndex{
|
||||
CreateIndex: 1,
|
||||
ModifyIndex: 2,
|
||||
|
@ -245,6 +268,7 @@ func TestStructs_NodeService_IsSame(t *testing.T) {
|
|||
check(func() { other.Tags = []string{"foo"} }, func() { other.Tags = []string{"foo", "bar"} })
|
||||
check(func() { other.Address = "XXX" }, func() { other.Address = "127.0.0.1" })
|
||||
check(func() { other.Port = 9999 }, func() { other.Port = 1234 })
|
||||
check(func() { other.ServiceMeta["meta2"] = "wrongValue" }, func() { other.ServiceMeta["meta2"] = "value2" })
|
||||
check(func() { other.EnableTagOverride = false }, func() { other.EnableTagOverride = true })
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue