From e0ea41e774b89ba8307116a7b79b724f7bf2231b Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Wed, 16 Aug 2017 11:27:30 -0700 Subject: [PATCH] Test createCheckReg --- command/agent/consul/unit_test.go | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/command/agent/consul/unit_test.go b/command/agent/consul/unit_test.go index 22973e740..3bda196ce 100644 --- a/command/agent/consul/unit_test.go +++ b/command/agent/consul/unit_test.go @@ -7,6 +7,7 @@ import ( "log" "os" "reflect" + "strings" "sync" "testing" "time" @@ -14,6 +15,7 @@ import ( "github.com/hashicorp/consul/api" cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/nomad/structs" + "github.com/kr/pretty" ) const ( @@ -1352,3 +1354,45 @@ func TestIsNomadService(t *testing.T) { }) } } + +// TestCreateCheckReg asserts Nomad ServiceCheck structs are properly converted +// to Consul API AgentCheckRegistrations. +func TestCreateCheckReg(t *testing.T) { + check := &structs.ServiceCheck{ + Name: "name", + Type: "http", + Path: "/path", + PortLabel: "label", + Method: "POST", + Header: map[string][]string{ + "Foo": {"bar"}, + }, + } + + serviceID := "testService" + checkID := check.Hash(serviceID) + host := "localhost" + port := 41111 + + expected := &api.AgentCheckRegistration{ + ID: checkID, + Name: "name", + ServiceID: serviceID, + } + expected.Timeout = "0s" + expected.Interval = "0s" + expected.HTTP = fmt.Sprintf("http://%s:%d/path", host, port) + expected.Header = map[string][]string{ + "Foo": {"bar"}, + } + expected.Method = "POST" + + actual, err := createCheckReg(serviceID, checkID, check, host, port) + if err != nil { + t.Fatalf("err: %v", err) + } + + if diff := pretty.Diff(actual, expected); len(diff) > 0 { + t.Fatalf("diff:\n%s\n", strings.Join(diff, "\n")) + } +}