intake/core/execute_test.go

167 lines
4.4 KiB
Go
Raw Normal View History

2025-01-17 21:49:23 +00:00
package core
import (
"testing"
"time"
)
func TestExecute(t *testing.T) {
assertLen := func(items []Item, length int) {
2025-02-05 17:25:33 +00:00
t.Helper()
2025-01-17 21:49:23 +00:00
if len(items) != length {
t.Fatalf("Expected %d items, got %d", length, len(items))
}
}
assertNil := func(err error) {
2025-02-05 17:25:33 +00:00
t.Helper()
2025-01-17 21:49:23 +00:00
if err != nil {
t.Fatal(err)
}
}
assertNotNil := func(err error) {
2025-02-05 17:25:33 +00:00
t.Helper()
2025-01-17 21:49:23 +00:00
if err == nil {
t.Fatal("expected err")
}
}
2025-01-28 05:27:20 +00:00
execute := func(argv []string) ([]Item, error) {
2025-02-05 21:21:31 +00:00
item, _, err := Execute("_", argv, nil, nil, "", time.Minute, nil)
return item, err
2025-01-28 05:27:20 +00:00
}
2025-01-17 21:49:23 +00:00
2025-01-28 05:27:20 +00:00
res, err := execute([]string{"true"})
2025-01-17 21:49:23 +00:00
assertNil(err)
assertLen(res, 0)
// Exit with error code
2025-01-28 05:27:20 +00:00
res, err = execute([]string{"false"})
2025-01-17 21:49:23 +00:00
assertNotNil(err)
assertLen(res, 0)
2025-01-28 05:27:20 +00:00
res, err = execute([]string{"sh", "-c", "exit 22"})
2025-01-17 21:49:23 +00:00
assertNotNil(err)
assertLen(res, 0)
// Timeout
2025-02-05 21:21:31 +00:00
res, _, err = Execute("_", []string{"sleep", "10"}, nil, nil, "", time.Millisecond, nil)
2025-01-17 21:49:23 +00:00
assertNotNil(err)
assertLen(res, 0)
// Returning items
2025-01-28 05:27:20 +00:00
res, err = execute([]string{"jq", "-cn", `{id: "foo"}`})
2025-01-17 21:49:23 +00:00
assertNil(err)
assertLen(res, 1)
if res[0].Id != "foo" {
t.Fatal("jq -cn test failed")
}
// Read from stdin
2025-02-05 21:21:31 +00:00
res, _, err = Execute("_", []string{"jq", "-cR", `{id: .}`}, nil, nil, "bar", time.Minute, nil)
2025-01-17 21:49:23 +00:00
assertNil(err)
assertLen(res, 1)
if res[0].Id != "bar" {
t.Fatal("jq -cR test failed")
}
// Set env
2025-02-05 21:21:31 +00:00
res, _, err = Execute("_", []string{"jq", "-cn", `{id: env.HELLO}`}, []string{"HELLO=baz"}, nil, "", time.Minute, nil)
2025-01-17 21:49:23 +00:00
assertNil(err)
assertLen(res, 1)
if res[0].Id != "baz" {
t.Fatal("jq -cn env test failed")
}
// With logging on stderr
2025-01-28 05:27:20 +00:00
res, err = execute([]string{"sh", "-c", `echo 1>&2 Hello; jq -cn '{id: "box"}'; echo 1>&2 World`})
2025-01-17 21:49:23 +00:00
assertNil(err)
assertLen(res, 1)
if res[0].Id != "box" {
t.Fatal("stderr test failed")
}
2025-01-28 05:27:20 +00:00
// 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)
2025-01-29 16:48:12 +00:00
// Action keys are detected even with empty values
res, err = execute([]string{"jq", "-cn", `{id: "test", action: {"hello": null}}`})
assertNil(err)
assertLen(res, 1)
if res[0].Action["hello"] == nil {
t.Fatal("missing hello action")
}
if res[0].Action["goodbye"] != nil {
t.Fatal("nonexistent action should key to nil in Action")
}
res, err = execute([]string{"jq", "-cn", `{id: "test", action: {"hello": ""}}`})
assertNil(err)
assertLen(res, 1)
if res[0].Action["hello"] == nil {
t.Fatal("missing hello action")
}
res, err = execute([]string{"jq", "-cn", `{id: "test", action: {"hello": []}}`})
assertNil(err)
assertLen(res, 1)
if res[0].Action["hello"] == nil {
t.Fatal("missing hello action")
}
res, err = execute([]string{"jq", "-cn", `{id: "test", action: {"hello": {}}}`})
assertNil(err)
assertLen(res, 1)
if res[0].Action["hello"] == nil {
t.Fatal("missing hello action")
}
// Read state
argv := []string{"sh", "-c", `cat $STATE_PATH | jq -cR '{id: "greeting", title: .} | .title = "Hello " + .title'`}
2025-02-05 21:21:31 +00:00
res, _, err = Execute("_", argv, nil, []byte("world"), "", time.Minute, nil)
assertNil(err)
assertLen(res, 1)
if res[0].Title != "Hello world" {
t.Fatalf("expected 'Hello world' from read state, got '%s'", res[0].Title)
}
// Write state
argv = []string{"sh", "-c", `printf "Hello world" > $STATE_PATH; jq -cn '{id: "test"}'`}
2025-02-05 21:21:31 +00:00
res, newState, err := Execute("_", argv, nil, nil, "", time.Minute, nil)
assertNil(err)
assertLen(res, 1)
if string(newState) != "Hello world" {
t.Fatalf("expected 'Hello world' from write state, got %s", string(newState))
}
2025-02-05 21:21:31 +00:00
// Postprocessing function
argv = []string{"jq", "-cn", `{id: "foo"}`}
res, _, err = Execute("_", argv, nil, nil, "", time.Minute, func(item Item) Item {
item.Ttl = 123456
return item
})
assertNil(err)
assertLen(res, 1)
if res[0].Ttl != 123456 {
t.Fatalf("expected ttl to be set to 123456, got %d", res[0].Ttl)
}
2025-01-17 21:49:23 +00:00
}