diff --git a/command/services/config.go b/command/services/config.go index 2241c90ec..ee2477d3c 100644 --- a/command/services/config.go +++ b/command/services/config.go @@ -65,13 +65,10 @@ func serviceToAgentService(svc *structs.ServiceDefinition) (*api.AgentServiceReg // The structs version has non-pointer checks and the destination // has pointers, so we need to set the destination to nil if there - // is no check ID set. - if result.Check != nil && result.Check.Name == "" { + // is a zero-value Check field. + if result.Check != nil && reflect.DeepEqual(*result.Check, api.AgentServiceCheck{}) { result.Check = nil } - if len(result.Checks) == 1 && result.Checks[0].Name == "" { - result.Checks = nil - } return &result, nil } diff --git a/command/services/config_test.go b/command/services/config_test.go index d98e52a19..d6e93b3db 100644 --- a/command/services/config_test.go +++ b/command/services/config_test.go @@ -2,6 +2,7 @@ package services import ( "testing" + "time" "github.com/hashicorp/consul/agent/config" "github.com/hashicorp/consul/agent/structs" @@ -63,6 +64,32 @@ func TestStructsToAgentService(t *testing.T) { }, }, }, + { + "Service with an unnamed check", + &structs.ServiceDefinition{ + Name: "web", + Check: structs.CheckType{ + TTL: 5 * time.Second, + }, + }, + &api.AgentServiceRegistration{ + Name: "web", + Check: &api.AgentServiceCheck{ + TTL: "5s", + }, + }, + }, + { + "Service with a zero-value check", + &structs.ServiceDefinition{ + Name: "web", + Check: structs.CheckType{}, + }, + &api.AgentServiceRegistration{ + Name: "web", + Check: nil, + }, + }, { "Service with checks", &structs.ServiceDefinition{ diff --git a/command/services/register/register_test.go b/command/services/register/register_test.go index fa2d95daa..e00e73626 100644 --- a/command/services/register/register_test.go +++ b/command/services/register/register_test.go @@ -153,6 +153,43 @@ func TestCommand_Flags_TaggedAddresses(t *testing.T) { require.Equal(svc.TaggedAddresses["v6"].Port, 1234) } +func TestCommand_FileWithUnnamedCheck(t *testing.T) { + t.Parallel() + + require := require.New(t) + a := agent.NewTestAgent(t, t.Name(), ``) + defer a.Shutdown() + client := a.Client() + + ui := cli.NewMockUi() + c := New(ui) + + contents := `{ "Service": { "Name": "web", "Check": { "TTL": "10s" } } }` + f := testFile(t, "json") + defer os.Remove(f.Name()) + if _, err := f.WriteString(contents); err != nil { + t.Fatalf("err: %#v", err) + } + + args := []string{ + "-http-addr=" + a.HTTPAddr(), + f.Name(), + } + + require.Equal(0, c.Run(args), ui.ErrorWriter.String()) + + svcs, err := client.Agent().Services() + require.NoError(err) + require.Len(svcs, 1) + + svc := svcs["web"] + require.NotNil(svc) + + checks, err := client.Agent().Checks() + require.NoError(err) + require.Len(checks, 1) +} + func testFile(t *testing.T, suffix string) *os.File { f := testutil.TempFile(t, "register-test-file") if err := f.Close(); err != nil {