From 1ef12ae24b0b1f76459f50ef0cb93b2b4c2c35ef Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Thu, 22 Jan 2015 10:24:50 +0100 Subject: [PATCH 1/2] Add agent service Address field to the api The Address field was introduced in #570. This patch extends this to the api. --- api/agent.go | 14 ++++++++------ api/agent_test.go | 7 ++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/api/agent.go b/api/agent.go index 08b144133..53ea191a7 100644 --- a/api/agent.go +++ b/api/agent.go @@ -22,6 +22,7 @@ type AgentService struct { Service string Tags []string Port int + Address string } // AgentMember represents a cluster member known to the agent @@ -41,12 +42,13 @@ type AgentMember struct { // AgentServiceRegistration is used to register a new service type AgentServiceRegistration struct { - ID string `json:",omitempty"` - Name string `json:",omitempty"` - Tags []string `json:",omitempty"` - Port int `json:",omitempty"` - Check *AgentServiceCheck - Checks AgentServiceChecks + ID string `json:",omitempty"` + Name string `json:",omitempty"` + Tags []string `json:",omitempty"` + Port int `json:",omitempty"` + Address string `json:",omitempty"` + Check *AgentServiceCheck + Checks AgentServiceChecks } // AgentCheckRegistration is used to register a new check diff --git a/api/agent_test.go b/api/agent_test.go index e27887d6f..1af8c090d 100644 --- a/api/agent_test.go +++ b/api/agent_test.go @@ -45,9 +45,10 @@ func TestAgent_Services(t *testing.T) { agent := c.Agent() reg := &AgentServiceRegistration{ - Name: "foo", - Tags: []string{"bar", "baz"}, - Port: 8000, + Name: "foo", + Tags: []string{"bar", "baz"}, + Port: 8000, + Address: "192.168.0.42", Check: &AgentServiceCheck{ TTL: "15s", }, From b46d20727b139942ffed250ec766be3b89ca2ee0 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Thu, 22 Jan 2015 11:50:20 +0100 Subject: [PATCH 2/2] Add test case for service Address field --- api/agent_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/api/agent_test.go b/api/agent_test.go index 1af8c090d..84ae5d8d0 100644 --- a/api/agent_test.go +++ b/api/agent_test.go @@ -48,7 +48,6 @@ func TestAgent_Services(t *testing.T) { Name: "foo", Tags: []string{"bar", "baz"}, Port: 8000, - Address: "192.168.0.42", Check: &AgentServiceCheck{ TTL: "15s", }, @@ -78,6 +77,52 @@ func TestAgent_Services(t *testing.T) { } } +func TestAgent_ServiceAddress(t *testing.T) { + c, s := makeClient(t) + defer s.stop() + + agent := c.Agent() + + reg1 := &AgentServiceRegistration{ + Name: "foo1", + Port: 8000, + Address: "192.168.0.42", + } + reg2 := &AgentServiceRegistration{ + Name: "foo2", + Port: 8000, + } + if err := agent.ServiceRegister(reg1); err != nil { + t.Fatalf("err: %v", err) + } + if err := agent.ServiceRegister(reg2); err != nil { + t.Fatalf("err: %v", err) + } + + services, err := agent.Services() + if err != nil { + t.Fatalf("err: %v", err) + } + + if _, ok := services["foo1"]; !ok { + t.Fatalf("missing service: %v", services) + } + if _, ok := services["foo2"]; !ok { + t.Fatalf("missing service: %v", services) + } + + if services["foo1"].Address != "192.168.0.42" { + t.Fatalf("missing Address field in service foo1: %v", services) + } + if services["foo2"].Address != "" { + t.Fatalf("missing Address field in service foo2: %v", services) + } + + if err := agent.ServiceDeregister("foo"); err != nil { + t.Fatalf("err: %v", err) + } +} + func TestAgent_Services_MultipleChecks(t *testing.T) { c, s := makeClient(t) defer s.stop()