Add more Execute test cases
This commit is contained in:
parent
18dd930579
commit
9dacdb987a
@ -92,7 +92,7 @@ func readStdout(stdout io.ReadCloser, source string, items chan Item, cparse cha
|
||||
for scanout.Scan() {
|
||||
data := scanout.Bytes()
|
||||
err := json.Unmarshal(data, &item)
|
||||
if err != nil {
|
||||
if err != nil || item.Id == "" {
|
||||
log.Printf("[%s: stdout] %s\n", source, strings.TrimSpace(string(data)))
|
||||
parseError = true
|
||||
} else {
|
||||
|
@ -71,57 +71,30 @@ func TestExecute(t *testing.T) {
|
||||
t.Fatal("expected err")
|
||||
}
|
||||
}
|
||||
execute := func(argv []string) ([]Item, error) {
|
||||
return Execute("", argv, nil, "", time.Minute)
|
||||
}
|
||||
|
||||
res, err := Execute(
|
||||
"",
|
||||
[]string{"true"},
|
||||
nil,
|
||||
"",
|
||||
time.Minute,
|
||||
)
|
||||
res, err := execute([]string{"true"})
|
||||
assertNil(err)
|
||||
assertLen(res, 0)
|
||||
|
||||
// Exit with error code
|
||||
res, err = Execute(
|
||||
"",
|
||||
[]string{"false"},
|
||||
nil,
|
||||
"",
|
||||
time.Minute,
|
||||
)
|
||||
res, err = execute([]string{"false"})
|
||||
assertNotNil(err)
|
||||
assertLen(res, 0)
|
||||
|
||||
res, err = Execute(
|
||||
"",
|
||||
[]string{"sh", "-c", "exit 22"},
|
||||
nil,
|
||||
"",
|
||||
time.Minute,
|
||||
)
|
||||
res, err = execute([]string{"sh", "-c", "exit 22"})
|
||||
assertNotNil(err)
|
||||
assertLen(res, 0)
|
||||
|
||||
// Timeout
|
||||
res, err = Execute(
|
||||
"",
|
||||
[]string{"sleep", "10"},
|
||||
nil,
|
||||
"",
|
||||
time.Millisecond,
|
||||
)
|
||||
res, err = Execute("", []string{"sleep", "10"}, nil, "", time.Millisecond)
|
||||
assertNotNil(err)
|
||||
assertLen(res, 0)
|
||||
|
||||
// Returning items
|
||||
res, err = Execute(
|
||||
"",
|
||||
[]string{"jq", "-cn", `{id: "foo"}`},
|
||||
nil,
|
||||
"",
|
||||
time.Minute,
|
||||
)
|
||||
res, err = execute([]string{"jq", "-cn", `{id: "foo"}`})
|
||||
assertNil(err)
|
||||
assertLen(res, 1)
|
||||
if res[0].Id != "foo" {
|
||||
@ -129,13 +102,7 @@ func TestExecute(t *testing.T) {
|
||||
}
|
||||
|
||||
// Read from stdin
|
||||
res, err = Execute(
|
||||
"",
|
||||
[]string{"jq", "-cR", `{id: .}`},
|
||||
nil,
|
||||
"bar",
|
||||
time.Minute,
|
||||
)
|
||||
res, err = Execute("", []string{"jq", "-cR", `{id: .}`}, nil, "bar", time.Minute)
|
||||
assertNil(err)
|
||||
assertLen(res, 1)
|
||||
if res[0].Id != "bar" {
|
||||
@ -143,13 +110,7 @@ func TestExecute(t *testing.T) {
|
||||
}
|
||||
|
||||
// Set env
|
||||
res, err = Execute(
|
||||
"",
|
||||
[]string{"jq", "-cn", `{id: env.HELLO}`},
|
||||
[]string{"HELLO=baz"},
|
||||
"",
|
||||
time.Minute,
|
||||
)
|
||||
res, err = Execute("", []string{"jq", "-cn", `{id: env.HELLO}`}, []string{"HELLO=baz"}, "", time.Minute)
|
||||
assertNil(err)
|
||||
assertLen(res, 1)
|
||||
if res[0].Id != "baz" {
|
||||
@ -157,16 +118,33 @@ func TestExecute(t *testing.T) {
|
||||
}
|
||||
|
||||
// With logging on stderr
|
||||
res, err = Execute(
|
||||
"",
|
||||
[]string{"sh", "-c", `echo 1>&2 Hello; jq -cn '{id: "box"}'; echo 1>&2 World`},
|
||||
nil,
|
||||
"",
|
||||
time.Minute,
|
||||
)
|
||||
res, err = execute([]string{"sh", "-c", `echo 1>&2 Hello; jq -cn '{id: "box"}'; echo 1>&2 World`})
|
||||
assertNil(err)
|
||||
assertLen(res, 1)
|
||||
if res[0].Id != "box" {
|
||||
t.Fatal("stderr test failed")
|
||||
}
|
||||
|
||||
// Unsupported item field is silently discarded
|
||||
res, err = execute([]string{"jq", "-cn", `{id: "test", unknownField: "what is this"}`})
|
||||
assertNil(err)
|
||||
assertLen(res, 1)
|
||||
|
||||
// Field with incorrect type fails
|
||||
res, err = execute([]string{"jq", "-cn", `{id: ["list"]}`})
|
||||
assertNotNil(err)
|
||||
assertLen(res, 0)
|
||||
|
||||
res, err = execute([]string{"jq", "-cn", `{id: "test", time: "0"}`})
|
||||
assertNotNil(err)
|
||||
assertLen(res, 0)
|
||||
|
||||
res, err = execute([]string{"jq", "-cn", `{id: null}`})
|
||||
assertNotNil(err)
|
||||
assertLen(res, 0)
|
||||
|
||||
// Items with duplicate ids is not a fetch error, but it will fail to update
|
||||
res, err = execute([]string{"jq", "-cn", `["a", "a"] | .[] | {id: .}`})
|
||||
assertNil(err)
|
||||
assertLen(res, 2)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user