From 66fdf445e8d9eb06056ae2e9a9eed9c5df8c4239 Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Fri, 9 Feb 2018 01:37:45 +0100
Subject: [PATCH] Added unit tests for structs and fixed PartialClone()
---
agent/structs/structs.go | 6 ++++-
agent/structs/structs_test.go | 42 +++++++++++++++++++++++++++--------
2 files changed, 38 insertions(+), 10 deletions(-)
diff --git a/agent/structs/structs.go b/agent/structs/structs.go
index c67a7255da..c937d34fcb 100644
--- a/agent/structs/structs.go
+++ b/agent/structs/structs.go
@@ -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,
diff --git a/agent/structs/structs_test.go b/agent/structs/structs_test.go
index e98df4abe4..af7cc2b049 100644
--- a/agent/structs/structs_test.go
+++ b/agent/structs/structs_test.go
@@ -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 })
}