Merge pull request #347 from sorenh/remote_exec_exit_code

agent: Pass exitCode by reference
This commit is contained in:
Armon Dadgar 2014-09-18 15:52:37 -07:00
commit 48b759e0ff
2 changed files with 20 additions and 9 deletions

View File

@ -137,7 +137,7 @@ func (a *Agent) handleRemoteExec(msg *UserEvent) {
// Ensure we write out an exit code // Ensure we write out an exit code
exitCode := 0 exitCode := 0
defer a.remoteExecWriteExitCode(&event, exitCode) defer a.remoteExecWriteExitCode(&event, &exitCode)
// Check if this is a script, we may need to spill to disk // Check if this is a script, we may need to spill to disk
var script string var script string
@ -190,7 +190,7 @@ func (a *Agent) handleRemoteExec(msg *UserEvent) {
err := cmd.Wait() err := cmd.Wait()
writer.Flush() writer.Flush()
close(writer.BufCh) close(writer.BufCh)
if err != nil { if err == nil {
exitCh <- 0 exitCh <- 0
return return
} }
@ -287,8 +287,8 @@ func (a *Agent) remoteExecWriteOutput(event *remoteExecEvent, num int, output []
} }
// remoteExecWriteExitCode is used to write an exit code // remoteExecWriteExitCode is used to write an exit code
func (a *Agent) remoteExecWriteExitCode(event *remoteExecEvent, exitCode int) bool { func (a *Agent) remoteExecWriteExitCode(event *remoteExecEvent, exitCode *int) bool {
val := []byte(strconv.FormatInt(int64(exitCode), 10)) val := []byte(strconv.FormatInt(int64(*exitCode), 10))
if err := a.remoteExecWriteKey(event, remoteExecExitSuffix, val); err != nil { if err := a.remoteExecWriteKey(event, remoteExecExitSuffix, val); err != nil {
a.logger.Printf("[ERR] agent: failed to write exit code for remote exec job: %v", err) a.logger.Printf("[ERR] agent: failed to write exit code for remote exec job: %v", err)
return false return false

View File

@ -140,7 +140,8 @@ func TestRemoteExecWrites(t *testing.T) {
t.Fatalf("bad") t.Fatalf("bad")
} }
if !agent.remoteExecWriteExitCode(event, 1) { exitCode := 1
if !agent.remoteExecWriteExitCode(event, &exitCode) {
t.Fatalf("bad") t.Fatalf("bad")
} }
@ -169,7 +170,9 @@ func TestRemoteExecWrites(t *testing.T) {
} }
} }
func TestHandleRemoteExec(t *testing.T) {
func testHandleRemoteExec(t *testing.T, command string, expectedSubstring string, expectedReturnCode string) {
dir, agent := makeAgent(t, nextConfig()) dir, agent := makeAgent(t, nextConfig())
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
defer agent.Shutdown() defer agent.Shutdown()
@ -182,7 +185,7 @@ func TestHandleRemoteExec(t *testing.T) {
defer destroySession(t, agent, event.Session) defer destroySession(t, agent, event.Session)
spec := &remoteExecSpec{ spec := &remoteExecSpec{
Command: "uptime", Command: command,
Wait: time.Second, Wait: time.Second,
} }
buf, err := json.Marshal(spec) buf, err := json.Marshal(spec)
@ -215,18 +218,26 @@ func TestHandleRemoteExec(t *testing.T) {
key = "_rexec/" + event.Session + "/" + agent.config.NodeName + "/out/00000" key = "_rexec/" + event.Session + "/" + agent.config.NodeName + "/out/00000"
d = getKV(t, agent, key) d = getKV(t, agent, key)
if d == nil || d.Session != event.Session || if d == nil || d.Session != event.Session ||
!bytes.Contains(d.Value, []byte("load")) { !bytes.Contains(d.Value, []byte(expectedSubstring)) {
t.Fatalf("bad output: %#v", d) t.Fatalf("bad output: %#v", d)
} }
// Verify we have an exit code // Verify we have an exit code
key = "_rexec/" + event.Session + "/" + agent.config.NodeName + "/exit" key = "_rexec/" + event.Session + "/" + agent.config.NodeName + "/exit"
d = getKV(t, agent, key) d = getKV(t, agent, key)
if d == nil || d.Session != event.Session || string(d.Value) != "0" { if d == nil || d.Session != event.Session || string(d.Value) != expectedReturnCode {
t.Fatalf("bad output: %#v", d) t.Fatalf("bad output: %#v", d)
} }
} }
func TestHandleRemoteExec(t *testing.T) {
testHandleRemoteExec(t, "uptime", "load", "0")
}
func TestHandleRemoteExecFailed(t *testing.T) {
testHandleRemoteExec(t, "echo failing;exit 2", "failing", "2")
}
func makeRexecSession(t *testing.T, agent *Agent) string { func makeRexecSession(t *testing.T, agent *Agent) string {
args := structs.SessionRequest{ args := structs.SessionRequest{
Datacenter: agent.config.Datacenter, Datacenter: agent.config.Datacenter,