Update from comments, initial test function

This commit is contained in:
Jake Champlin 2016-06-21 16:46:42 -04:00
parent 597892e1bc
commit 84ad9925d7
No known key found for this signature in database
GPG Key ID: DC31F41958EF4AC2
3 changed files with 59 additions and 3 deletions

View File

@ -118,18 +118,22 @@ func (c *RunCommand) Run(args []string) int {
var f io.Reader = os.Stdin
if path != "-" {
file, err := os.Open(path)
defer file.Close()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error opening file: %s", err))
return 1
}
defer file.Close()
f = file
}
// Parse the JobFile
job, err := jobspec.Parse(f)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing job file %s: %s", f, err))
if path == "-" {
c.Ui.Error(fmt.Sprintf("Error parsing job from STDIN: %s", err))
} else {
c.Ui.Error(fmt.Sprintf("Error parsing job file %s: %s", f, err))
}
return 1
}

View File

@ -1,6 +1,7 @@
package command
import (
"fmt"
"io/ioutil"
"os"
"strings"
@ -147,3 +148,40 @@ job "job1" {
ui.ErrorWriter.Reset()
}
func TestRunCommand_From_STDIN(t *testing.T) {
fromStdin, toFile, err := os.Pipe()
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.Remove(toFile.Name())
_, err = toFile.WriteString(`
job "job1" {
type = "service"
datacenters = [ "dc1" ]
group "group1" {
count = 1
task "task1" {
driver = "exec"
resources = {
cpu = 1000
disk = 150
memory = 512
}
}
}
}`)
if err != nil {
t.Fatalf("err: %s", err)
}
ui := new(cli.MockUi)
ui.InputReader = fromStdin
cmd := &RunCommand{Meta: Meta{Ui: ui}}
if code := cmd.Run([]string{"-"}); code != 0 {
os.Stdout.Write([]byte(fmt.Sprintf("####OUTPUT\n%s\n\n", ui.OutputWriter.String())))
t.Fatalf("expected exit code 0, got: %d", code)
}
}

View File

@ -15,13 +15,16 @@ format.
## Usage
```
nomad run [options] <file>
nomad run [options] <path>
```
The run command requires a single argument, specifying the path to a file
containing a valid [job specification](/docs/jobspec/index.html). This file
will be read and the job will be submitted to Nomad for scheduling.
If the supplied path is "-", the jobfile is read from STDIN. Otherwise it is
read from the file at the supplied path.
By default, on successful job submission the run command will enter an
interactive monitor and display log information detailing the scheduling
decisions and placement information for the provided job. The monitor will
@ -108,3 +111,14 @@ $ nomad run failing.nomad
* Constraint "${attr.kernel.name} = linux" filtered 1 nodes
Evaluation "67493a64" waiting for additional capacity to place remainder
```
Schedule a job from STDIN:
```
$ cat job1.nomad | nomad run -
==> Monitoring evaluation "52dee78a"
Allocation "5e0b39f0" created: node "3e84d3d2", group "group1"
Allocation "5e0b39f0" status changed: "pending" -> "running"
Evaluation status changed: "pending" -> "complete"
==> Evaluation "52dee78a" finished with status "complete"
```