intake/core/action_test.go

183 lines
4.4 KiB
Go
Raw Normal View History

2025-01-17 21:49:23 +00:00
package core
import (
"testing"
"time"
)
2025-01-21 03:53:22 +00:00
func TestActionCreate(t *testing.T) {
db := EphemeralDb(t)
if err := AddAction(db, "test", "hello", []string{"echo", "hello"}); err == nil {
t.Fatal("Action created for nonexistent source")
}
if err := AddSource(db, "test"); err != nil {
t.Fatal(err)
}
if err := AddAction(db, "test", "hello", []string{"echo", "hello"}); err != nil {
t.Fatal(err)
}
if err := AddAction(db, "test", "goodbye", []string{"exit", "1"}); err != nil {
t.Fatal(err)
}
if err := UpdateAction(db, "test", "goodbye", []string{"echo", "goodbye"}); err != nil {
t.Fatal(err)
}
actions, err := GetActionsForSource(db, "test")
if err != nil {
t.Fatal(err)
}
if len(actions) != 2 {
t.Fatal("expected 2 actions")
}
found := make(map[string]bool)
for _, action := range actions {
found[action] = true
}
if !found["hello"] || !found["goodbye"] {
t.Fatalf("missing hello and/or goodbye, got: %v", actions)
}
argv, err := GetArgvForAction(db, "test", "goodbye")
if err != nil {
t.Fatal(err)
}
if len(argv) != 2 || argv[0] != "echo" || argv[1] != "goodbye" {
t.Fatalf("expected [echo goodbye], got: %v", argv)
}
err = DeleteAction(db, "test", "hello")
if err != nil {
t.Fatal(err)
}
2025-01-21 03:53:22 +00:00
}
2025-01-17 21:49:23 +00:00
func TestExecute(t *testing.T) {
assertLen := func(items []Item, length int) {
if len(items) != length {
t.Fatalf("Expected %d items, got %d", length, len(items))
}
}
assertNil := func(err error) {
if err != nil {
t.Fatal(err)
}
}
assertNotNil := func(err error) {
if err == nil {
t.Fatal("expected err")
}
}
2025-01-28 05:27:20 +00:00
execute := func(argv []string) ([]Item, error) {
2025-01-29 15:39:00 +00:00
return Execute("_", argv, nil, "", time.Minute)
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-01-29 15:39:00 +00:00
res, err = Execute("_", []string{"sleep", "10"}, nil, "", time.Millisecond)
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-01-29 15:39:00 +00:00
res, err = Execute("_", []string{"jq", "-cR", `{id: .}`}, nil, "bar", time.Minute)
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-01-29 15:39:00 +00:00
res, err = Execute("_", []string{"jq", "-cn", `{id: env.HELLO}`}, []string{"HELLO=baz"}, "", time.Minute)
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")
}
2025-01-17 21:49:23 +00:00
}