github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/command/spawn_daemon_test.go (about)

     1  package command
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"os/exec"
     9  	"testing"
    10  )
    11  
    12  type nopCloser struct {
    13  	io.ReadWriter
    14  }
    15  
    16  func (n *nopCloser) Close() error {
    17  	return nil
    18  }
    19  
    20  func TestSpawnDaemon_WriteExitStatus(t *testing.T) {
    21  	// Check if there is python.
    22  	path, err := exec.LookPath("python")
    23  	if err != nil {
    24  		t.Skip("python not detected")
    25  	}
    26  
    27  	var b bytes.Buffer
    28  	daemon := &SpawnDaemonCommand{exitFile: &nopCloser{&b}}
    29  
    30  	code := 3
    31  	cmd := exec.Command(path, "./test-resources/exiter.py", fmt.Sprintf("%d", code))
    32  	err = cmd.Run()
    33  	actual := daemon.writeExitStatus(err)
    34  	if actual != code {
    35  		t.Fatalf("writeExitStatus(%v) returned %v; want %v", err, actual, code)
    36  	}
    37  
    38  	// De-serialize the passed command.
    39  	var exitStatus SpawnExitStatus
    40  	dec := json.NewDecoder(&b)
    41  	if err := dec.Decode(&exitStatus); err != nil {
    42  		t.Fatalf("failed to decode exit status: %v", err)
    43  	}
    44  
    45  	if exitStatus.ExitCode != code {
    46  		t.Fatalf("writeExitStatus(%v) wrote exit status %v; want %v", err, exitStatus.ExitCode, code)
    47  	}
    48  }