From 0fbaa18ed35da84ce84199b0dbe00ed3a7656ad1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 30 Sep 2018 19:17:45 -0700 Subject: [PATCH] command/services/register: config mapping tests --- command/services/register/config.go | 11 ++----- command/services/register/config_test.go | 40 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/command/services/register/config.go b/command/services/register/config.go index fc01672710..0ab9df2486 100644 --- a/command/services/register/config.go +++ b/command/services/register/config.go @@ -1,7 +1,6 @@ package register import ( - "fmt" "github.com/hashicorp/consul/agent/config" "github.com/hashicorp/consul/api" "github.com/mitchellh/mapstructure" @@ -10,12 +9,8 @@ import ( // configToAgentService converts a ServiceDefinition struct to an // AgentServiceRegistration API struct. func configToAgentService(svc *config.ServiceDefinition) (*api.AgentServiceRegistration, error) { + // mapstructure can do this for us, but we encapsulate it in this + // helper function in case we need to change the logic in the future. var result api.AgentServiceRegistration - var m map[string]interface{} - err := mapstructure.Decode(svc, &m) - if err == nil { - println(fmt.Sprintf("%#v", m)) - err = mapstructure.Decode(m, &result) - } - return &result, err + return &result, mapstructure.Decode(svc, &result) } diff --git a/command/services/register/config_test.go b/command/services/register/config_test.go index 17349a2f80..dc4ed3a717 100644 --- a/command/services/register/config_test.go +++ b/command/services/register/config_test.go @@ -27,6 +27,46 @@ func TestConfigToAgentService(t *testing.T) { Port: 1234, }, }, + { + "Service with a check", + &config.ServiceDefinition{ + Name: strPtr("web"), + Check: &config.CheckDefinition{ + Name: strPtr("ping"), + }, + }, + &api.AgentServiceRegistration{ + Name: "web", + Check: &api.AgentServiceCheck{ + Name: "ping", + }, + }, + }, + { + "Service with checks", + &config.ServiceDefinition{ + Name: strPtr("web"), + Checks: []config.CheckDefinition{ + config.CheckDefinition{ + Name: strPtr("ping"), + }, + config.CheckDefinition{ + Name: strPtr("pong"), + }, + }, + }, + &api.AgentServiceRegistration{ + Name: "web", + Checks: api.AgentServiceChecks{ + &api.AgentServiceCheck{ + Name: "ping", + }, + &api.AgentServiceCheck{ + Name: "pong", + }, + }, + }, + }, } for _, tc := range cases {