forked from TritonDataCenter/containerpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
discovery: Deprecate usage of Consul's internal testutil package
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: TritonDataCenter#528
- Loading branch information
Justin Reagor
committed
Nov 13, 2017
1 parent
56ce7a4
commit 936a82d
Showing
2 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |