Skip to content

Commit

Permalink
discovery: Deprecate usage of Consul's internal testutil package
Browse files Browse the repository at this point in the history
This commit removes the use case within the discovery package's tests that
depended on an internal Consul test package, `testutil`. We replace this with
our own TestServer object which is responsible for executing a locally installed
`consul` binary. Consul is installed by our Makefile target `tools` for local
development use.

Ref: #528
  • Loading branch information
Justin Reagor committed Nov 13, 2017
1 parent 56ce7a4 commit 936a82d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
12 changes: 6 additions & 6 deletions discovery/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package discovery
import (
"fmt"
"testing"
"time"

consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/testutil"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -77,18 +77,18 @@ a Consul server for testing. The 'consul' binary must be in the $PATH
ref https://github.com/hashicorp/consul/tree/master/testutil
*/

var testServer *testutil.TestServer
var testServer *TestServer

func TestWithConsul(t *testing.T) {
var err error
testServer, err = testutil.NewTestServerConfigT(t, func(c *testutil.TestServerConfig) {
c.LogLevel = "err"
})
testServer, err = NewTestServer(8500)
if err != nil {
t.Fatal(err)
}

defer testServer.Stop()

time.Sleep(300 * time.Millisecond)

t.Run("TestConsulTTLPass", testConsulTTLPass)
t.Run("TestConsulReregister", testConsulReregister)
t.Run("TestConsulCheckForChanges", testConsulCheckForChanges)
Expand Down
55 changes: 55 additions & 0 deletions discovery/test_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package discovery

import (
"errors"
"fmt"
"io"
"os"
"os/exec"
)

// TestServer represents a Consul server we can run our tests against. Depends
// on a local `consul` binary installed into our environ's PATH.
type TestServer struct {
cmd *exec.Cmd
HTTPAddr string
}

// NewTestServer constructs a new TestServer by including the httpPort as well.
func NewTestServer(httpPort int) (*TestServer, error) {
path, err := exec.LookPath("consul")
if err != nil || path == "" {
return nil, fmt.Errorf("consul not found on $PATH - download and install " +
"consul or skip this test")
}

args := []string{"agent", "-dev"}
cmd := exec.Command("consul", args...)
cmd.Stdout = io.Writer(os.Stdout)
cmd.Stderr = io.Writer(os.Stderr)
if err := cmd.Start(); err != nil {
return nil, errors.New("failed starting command")
}

httpAddr := fmt.Sprintf("127.0.0.1:%d", httpPort)

return &TestServer{
cmd: cmd,
HTTPAddr: httpAddr,
}, nil
}

// Stop stops a TestServer
func (s *TestServer) Stop() error {
if s.cmd == nil {
return nil
}

if s.cmd.Process != nil {
if err := s.cmd.Process.Signal(os.Interrupt); err != nil {
return errors.New("failed to kill consul server")
}
}

return s.cmd.Wait()
}

0 comments on commit 936a82d

Please sign in to comment.