diff --git a/command/agent/consul/client.go b/command/agent/consul/client.go index 1f8dba93d615..47a04da7d54d 100644 --- a/command/agent/consul/client.go +++ b/command/agent/consul/client.go @@ -1064,6 +1064,8 @@ func createCheckReg(serviceID, checkID string, check *structs.ServiceCheck, host chkReg.TCP = net.JoinHostPort(host, strconv.Itoa(port)) case structs.ServiceCheckScript: chkReg.TTL = (check.Interval + ttlCheckBuffer).String() + // As of Consul 1.0.0 setting TTL and Interval is a 400 + chkReg.Interval = "" default: return nil, fmt.Errorf("check type %+q not valid", check.Type) } diff --git a/command/agent/consul/int_test.go b/command/agent/consul/int_test.go index 0d4087707f51..9295a696c9ad 100644 --- a/command/agent/consul/int_test.go +++ b/command/agent/consul/int_test.go @@ -19,6 +19,7 @@ import ( "github.com/hashicorp/nomad/command/agent/consul" "github.com/hashicorp/nomad/nomad/mock" "github.com/hashicorp/nomad/nomad/structs" + "github.com/stretchr/testify/assert" ) func testLogger() *log.Logger { @@ -37,6 +38,8 @@ func TestConsul_Integration(t *testing.T) { if testing.Short() { t.Skip("-short set; skipping") } + assert := assert.New(t) + // Create an embedded Consul server testconsul, err := testutil.NewTestServerConfig(func(c *testutil.TestServerConfig) { // If -v wasn't specified squelch consul logging @@ -128,9 +131,8 @@ func TestConsul_Integration(t *testing.T) { taskDir := allocDir.NewTaskDir(task.Name) vclient := vaultclient.NewMockVaultClient() consulClient, err := consulapi.NewClient(consulConfig) - if err != nil { - t.Fatalf("error creating consul client: %v", err) - } + assert.Nil(err) + serviceClient := consul.NewServiceClient(consulClient.Agent(), true, logger) defer serviceClient.Shutdown() // just-in-case cleanup consulRan := make(chan struct{}) @@ -154,9 +156,7 @@ func TestConsul_Integration(t *testing.T) { // Block waiting for the service to appear catalog := consulClient.Catalog() res, meta, err := catalog.Service("httpd2", "test", nil) - if err != nil { - t.Fatalf("bad: %v", err) - } + assert.Nil(err) for i := 0; len(res) == 0 && i < 10; i++ { //Expected initial request to fail, do a blocking query @@ -165,32 +165,25 @@ func TestConsul_Integration(t *testing.T) { t.Fatalf("error querying for service: %v", err) } } - if len(res) != 1 { - t.Fatalf("expected 1 service but found %d:\n%#v", len(res), res) - } + assert.Len(res, 1) + + // Truncate results res = res[:] // Assert the service with the checks exists for i := 0; len(res) == 0 && i < 10; i++ { res, meta, err = catalog.Service("httpd", "http", &consulapi.QueryOptions{WaitIndex: meta.LastIndex + 1, WaitTime: 3 * time.Second}) - if err != nil { - t.Fatalf("error querying for service: %v", err) - } - } - if len(res) != 1 { - t.Fatalf("exepcted 1 service but found %d:\n%#v", len(res), res) + assert.Nil(err) } + assert.Len(res, 1) // Assert the script check passes (mock_driver script checks always // pass) after having time to run once time.Sleep(2 * time.Second) checks, _, err := consulClient.Health().Checks("httpd", nil) - if err != nil { - t.Fatalf("error querying checks: %v", err) - } - if expected := 2; len(checks) != expected { - t.Fatalf("expected %d checks but found %d:\n%#v", expected, len(checks), checks) - } + assert.Nil(err) + assert.Len(checks, 2) + for _, check := range checks { if expected := "httpd"; check.ServiceName != expected { t.Fatalf("expected checks to be for %q but found service name = %q", expected, check.ServiceName) @@ -244,13 +237,7 @@ func TestConsul_Integration(t *testing.T) { // Ensure Consul is clean services, _, err := catalog.Services(nil) - if err != nil { - t.Fatalf("error query services: %v", err) - } - if len(services) != 1 { - t.Fatalf("expected only 1 service in Consul but found %d:\n%#v", len(services), services) - } - if _, ok := services["consul"]; !ok { - t.Fatalf(`expected only the "consul" key in Consul but found: %#v`, services) - } + assert.Nil(err) + assert.Len(services, 1) + assert.Contains(services, "consul") }