-
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.
add local executor and way to test logging
- Loading branch information
Showing
5 changed files
with
171 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,92 @@ | ||
package crony | ||
|
||
import ( | ||
"os/exec" | ||
"syscall" | ||
|
||
"github.com/mattn/go-shellwords" | ||
) | ||
|
||
type Process struct { | ||
command *exec.Cmd | ||
done chan int | ||
} | ||
|
||
func (process *Process) Done() chan int { | ||
return process.done | ||
} | ||
|
||
type Executor interface { | ||
Execute(command string) error | ||
Execute(command string) (*Process, error) | ||
} | ||
|
||
type FakeExecutor struct { | ||
Commands []string | ||
} | ||
|
||
func (executor *FakeExecutor) Execute(command string) error { | ||
func (executor *FakeExecutor) Execute(command string) (*Process, error) { | ||
executor.Commands = append(executor.Commands, command) | ||
return nil | ||
return &Process{}, nil | ||
} | ||
|
||
func CreateFakeExecutor() *FakeExecutor { | ||
return &FakeExecutor{make([]string, 0)} | ||
} | ||
|
||
type LocalExecutor struct { | ||
stdout ReaderConsumer | ||
stderr ReaderConsumer | ||
} | ||
|
||
func waitForExitCode(cmd *exec.Cmd, doneChan chan int) { | ||
if err := cmd.Wait(); err != nil { | ||
if exiterr, ok := err.(*exec.ExitError); ok { | ||
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { | ||
doneChan <- status.ExitStatus() | ||
} else { | ||
panic("Unable to convert to syscall.WaitStatus; this shouldn't happen") | ||
} | ||
} else { | ||
// we failed while attempting to launch process | ||
doneChan <- -1 | ||
} | ||
} else { | ||
doneChan <- 0 | ||
} | ||
} | ||
|
||
func (executor *LocalExecutor) Execute(command string) (*Process, error) { | ||
commandParts, err := shellwords.Parse(command) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cmd := exec.Command(commandParts[0], commandParts[1:]...) | ||
|
||
stderr, err := cmd.StderrPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
doneChan := make(chan int) | ||
|
||
if err := cmd.Start(); err != nil { | ||
return nil, err | ||
} | ||
|
||
go executor.stderr.ConsumeReader(stderr) | ||
go executor.stdout.ConsumeReader(stdout) | ||
go waitForExitCode(cmd, doneChan) | ||
|
||
return &Process{ | ||
command: cmd, | ||
done: doneChan, | ||
}, nil | ||
} | ||
|
||
func NewLocalExecutor(stdout ReaderConsumer, stderr ReaderConsumer) *LocalExecutor { | ||
return &LocalExecutor{stdout, stderr} | ||
} |
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,47 @@ | ||
package crony | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFakeExecutorRecordsCommands(t *testing.T) { | ||
executor := CreateFakeExecutor() | ||
|
||
_, err := executor.Execute("echo the command") | ||
|
||
if err != nil { | ||
t.Error("should not return an error") | ||
} | ||
|
||
if len(executor.Commands) != 1 { | ||
t.Error("should have recorded one command") | ||
} | ||
|
||
if executor.Commands[0] != "echo the command" { | ||
t.Error("should have recorded the command string") | ||
} | ||
} | ||
|
||
func TestLocalExecutorRunsTheCommandOnTheShell(t *testing.T) { | ||
stdoutSink := NewSink() | ||
stderrSink := NewSink() | ||
executor := NewLocalExecutor(stdoutSink, stderrSink) | ||
|
||
process, err := executor.Execute("/bin/echo hi") | ||
|
||
if err != nil { | ||
t.Error("should not return an error") | ||
} | ||
|
||
if exit := <-process.Done(); exit != 0 { | ||
t.Errorf("Should have exited with 0, got %q", exit) | ||
} | ||
|
||
if stderrSink.Content != "" { | ||
t.Error("should have gotten no stderr") | ||
} | ||
|
||
if stdoutSink.Content != "hi\n" { | ||
t.Errorf("should have gotten hi, got %q", stdoutSink.Content) | ||
} | ||
} |
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,28 @@ | ||
package crony | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
) | ||
|
||
type ReaderConsumer interface { | ||
ConsumeReader(reader io.Reader) error | ||
} | ||
|
||
type Sink struct { | ||
Content string | ||
} | ||
|
||
func NewSink() *Sink { | ||
return &Sink{} | ||
} | ||
|
||
func (sink *Sink) ConsumeReader(reader io.Reader) error { | ||
bytes, err := ioutil.ReadAll(reader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sink.Content = string(bytes) | ||
return nil | ||
} |
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,16 @@ | ||
package crony | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestSink(t *testing.T) { | ||
reader := strings.NewReader("this is a string") | ||
sink := NewSink() | ||
sink.ConsumeReader(reader) | ||
|
||
if sink.Content != "this is a string" { | ||
t.Error("sink did not properly consume reader") | ||
} | ||
} |
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