mirror of
https://github.com/kemko/nomad.git
synced 2026-01-08 03:15:42 +03:00
Refactor spawn-daemon so it can be used by all OSes and make it write exit code to a file
This commit is contained in:
48
command/spawn_daemon_test.go
Normal file
48
command/spawn_daemon_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type nopCloser struct {
|
||||
io.ReadWriter
|
||||
}
|
||||
|
||||
func (n *nopCloser) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestSpawnDaemon_WriteExitStatus(t *testing.T) {
|
||||
// Check if there is python.
|
||||
path, err := exec.LookPath("python")
|
||||
if err != nil {
|
||||
t.Skip("python not detected")
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
daemon := &SpawnDaemonCommand{exitFile: &nopCloser{&b}}
|
||||
|
||||
code := 3
|
||||
cmd := exec.Command(path, "./test-resources/exiter.py", fmt.Sprintf("%d", code))
|
||||
err = cmd.Run()
|
||||
actual := daemon.writeExitStatus(err)
|
||||
if actual != code {
|
||||
t.Fatalf("writeExitStatus(%v) returned %v; want %v", err, actual, code)
|
||||
}
|
||||
|
||||
// De-serialize the passed command.
|
||||
var exitStatus SpawnExitStatus
|
||||
dec := json.NewDecoder(&b)
|
||||
if err := dec.Decode(&exitStatus); err != nil {
|
||||
t.Fatalf("failed to decode exit status: %v", err)
|
||||
}
|
||||
|
||||
if exitStatus.ExitCode != code {
|
||||
t.Fatalf("writeExitStatus(%v) wrote exit status %v; want %v", err, exitStatus.ExitCode, code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user