Added unit tests for structs and fixed PartialClone()

This commit is contained in:
Pierre Souchay 2018-02-09 01:37:45 +01:00
parent 80dde5465b
commit 66fdf445e8
2 changed files with 38 additions and 10 deletions

View File

@ -380,6 +380,10 @@ type ServiceNode struct {
func (s *ServiceNode) PartialClone() *ServiceNode { func (s *ServiceNode) PartialClone() *ServiceNode {
tags := make([]string, len(s.ServiceTags)) tags := make([]string, len(s.ServiceTags))
copy(tags, s.ServiceTags) copy(tags, s.ServiceTags)
nsmeta := make(map[string]string)
for k, v := range s.ServiceMeta {
nsmeta[k] = v
}
return &ServiceNode{ return &ServiceNode{
// Skip ID, see above. // Skip ID, see above.
@ -391,7 +395,7 @@ func (s *ServiceNode) PartialClone() *ServiceNode {
ServiceTags: tags, ServiceTags: tags,
ServiceAddress: s.ServiceAddress, ServiceAddress: s.ServiceAddress,
ServicePort: s.ServicePort, ServicePort: s.ServicePort,
ServiceMeta: s.ServiceMeta, ServiceMeta: nsmeta,
ServiceEnableTagOverride: s.ServiceEnableTagOverride, ServiceEnableTagOverride: s.ServiceEnableTagOverride,
RaftIndex: RaftIndex{ RaftIndex: RaftIndex{
CreateIndex: s.CreateIndex, CreateIndex: s.CreateIndex,

View File

@ -133,11 +133,14 @@ func testServiceNode() *ServiceNode {
NodeMeta: map[string]string{ NodeMeta: map[string]string{
"tag": "value", "tag": "value",
}, },
ServiceID: "service1", ServiceID: "service1",
ServiceName: "dogs", ServiceName: "dogs",
ServiceTags: []string{"prod", "v1"}, ServiceTags: []string{"prod", "v1"},
ServiceAddress: "127.0.0.2", ServiceAddress: "127.0.0.2",
ServicePort: 8080, ServicePort: 8080,
ServiceMeta: map[string]string{
"service": "metadata",
},
ServiceEnableTagOverride: true, ServiceEnableTagOverride: true,
RaftIndex: RaftIndex{ RaftIndex: RaftIndex{
CreateIndex: 1, CreateIndex: 1,
@ -175,6 +178,17 @@ func TestStructs_ServiceNode_PartialClone(t *testing.T) {
if reflect.DeepEqual(sn, clone) { if reflect.DeepEqual(sn, clone) {
t.Fatalf("clone wasn't independent of the original") 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) { 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) { func TestStructs_NodeService_IsSame(t *testing.T) {
ns := &NodeService{ ns := &NodeService{
ID: "node1", ID: "node1",
Service: "theservice", Service: "theservice",
Tags: []string{"foo", "bar"}, Tags: []string{"foo", "bar"},
Address: "127.0.0.1", Address: "127.0.0.1",
ServiceMeta: map[string]string{
"meta1": "value1",
"meta2": "value2",
},
Port: 1234, Port: 1234,
EnableTagOverride: true, EnableTagOverride: true,
} }
@ -214,6 +232,11 @@ func TestStructs_NodeService_IsSame(t *testing.T) {
Address: "127.0.0.1", Address: "127.0.0.1",
Port: 1234, Port: 1234,
EnableTagOverride: true, EnableTagOverride: true,
ServiceMeta: map[string]string{
// We don't care about order
"meta2": "value2",
"meta1": "value1",
},
RaftIndex: RaftIndex{ RaftIndex: RaftIndex{
CreateIndex: 1, CreateIndex: 1,
ModifyIndex: 2, 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.Tags = []string{"foo"} }, func() { other.Tags = []string{"foo", "bar"} })
check(func() { other.Address = "XXX" }, func() { other.Address = "127.0.0.1" }) check(func() { other.Address = "XXX" }, func() { other.Address = "127.0.0.1" })
check(func() { other.Port = 9999 }, func() { other.Port = 1234 }) 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 }) check(func() { other.EnableTagOverride = false }, func() { other.EnableTagOverride = true })
} }