-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade ContainerPilot to Go 1.9.x and Consul 1.0.0 (#527)
* Update dependencies and peg Consul at v1.0.0 * tools: Switch builds and tooling to use Go 1.9.x and Consul 1.0.0 * testing: Don't cause NPEs by ignoring errors when configuring Consul * testing: Custom TestServer under `discovery` for exec'ing `consul` * discovery: Deprecate usage of Consul's internal `testutil` package This commit upgrades ContainerPilot to be built with Go 1.9 and tested against Consul 1.0.0. While upgrading Consul to version 1.0.0 I found that our tests caused a null pointer exception if there's an error configuring a Consul process to test against. This was due to the fact that we were ignoring errors when configuring Consul and continuing forward as if Consul was functional. This commit properly causes a fatal return from the test and includes the actual error returned by Consul's own internal testing framework (which we use for bootstrapping our own testing). Finally, 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. Fixes: #528
- Loading branch information
Justin Reagor
authored
Nov 14, 2017
1 parent
bda8eba
commit 4f9845a
Showing
6 changed files
with
210 additions
and
112 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
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,90 @@ | ||
package discovery | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
|
||
"github.com/hashicorp/consul/testutil/retry" | ||
cleanhttp "github.com/hashicorp/go-cleanhttp" | ||
) | ||
|
||
// 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 { | ||
HTTPAddr string | ||
cmd *exec.Cmd | ||
client *http.Client | ||
} | ||
|
||
// 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", "-http-port", strconv.Itoa(httpPort)} | ||
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) | ||
|
||
client := cleanhttp.DefaultClient() | ||
|
||
return &TestServer{httpAddr, cmd, client}, 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() | ||
} | ||
|
||
// failer implements the retry.Failer interface | ||
type failer struct { | ||
failed bool | ||
} | ||
|
||
func (f *failer) Log(args ...interface{}) { fmt.Println(args) } | ||
func (f *failer) FailNow() { f.failed = true } | ||
|
||
// WaitForAPI waits for only the agent HTTP endpoint to start responding. This | ||
// is an indication that the agent has started, but will likely return before a | ||
// leader is elected. | ||
func (s *TestServer) WaitForAPI() error { | ||
f := &failer{} | ||
retry.Run(f, func(r *retry.R) { | ||
resp, err := s.client.Get(s.HTTPAddr + "/v1/agent/self") | ||
if err != nil { | ||
r.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
r.Fatalf("bad status code %d", resp.StatusCode) | ||
} | ||
}) | ||
if f.failed { | ||
return errors.New("failed waiting for API") | ||
} | ||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.