command/services/register: config mapping tests

This commit is contained in:
Mitchell Hashimoto 2018-09-30 19:17:45 -07:00
parent 3237047e72
commit 0fbaa18ed3
No known key found for this signature in database
GPG Key ID: A3A9A8F4F25C3E56
2 changed files with 43 additions and 8 deletions

View File

@ -1,7 +1,6 @@
package register package register
import ( import (
"fmt"
"github.com/hashicorp/consul/agent/config" "github.com/hashicorp/consul/agent/config"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -10,12 +9,8 @@ import (
// configToAgentService converts a ServiceDefinition struct to an // configToAgentService converts a ServiceDefinition struct to an
// AgentServiceRegistration API struct. // AgentServiceRegistration API struct.
func configToAgentService(svc *config.ServiceDefinition) (*api.AgentServiceRegistration, error) { 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 result api.AgentServiceRegistration
var m map[string]interface{} return &result, mapstructure.Decode(svc, &result)
err := mapstructure.Decode(svc, &m)
if err == nil {
println(fmt.Sprintf("%#v", m))
err = mapstructure.Decode(m, &result)
}
return &result, err
} }

View File

@ -27,6 +27,46 @@ func TestConfigToAgentService(t *testing.T) {
Port: 1234, 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 { for _, tc := range cases {