Files
splinter/tests/test_delegate.c

555 lines
19 KiB
C

/*
* splinter -- delegate selection.
*
* The choice is code, so it is tested like code: given a task and a set of
* delegates, the same one must win every time, and the reason must name what
* decided it.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include "splinter.h"
#include "providers.h"
#include "usage.h"
#include "splinter_plugin.h"
#include "config.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
add(struct sp_config *c, const char *id, const char *about, const char *tags)
{
struct sp_delegate *d = &c->delegate[c->ndelegates++];
memset(d, 0, sizeof(*d));
snprintf(d->id, sizeof(d->id), "%s", id);
snprintf(d->about, sizeof(d->about), "%s", about);
snprintf(d->tags, sizeof(d->tags), "%s", tags);
snprintf(d->endpoint, sizeof(d->endpoint), "http://example/v1");
snprintf(d->model, sizeof(d->model), "m");
}
/*
* The child of a cli delegate has to find plugins/cli/cli.so also when the
* binary was installed away from the tree. Before this, the path was
* dirname(self)/plugins/... and nothing else: an installed splinter found no
* plugin, registered no 'cli' transport, and its exit 2 was reported as the
* delegate's.
*/
static void
test_cli_plugin_path(void)
{
char root[512], dir[1024], tree[700], inst[700], got[700];
char expect[1024];
const char *tmp = getenv("TMPDIR");
FILE *f;
snprintf(root, sizeof(root), "%s/splinter-delegate-XXXXXX",
tmp != NULL && *tmp != '\0' ? tmp : "/var/tmp");
CHECK(mkdtemp(root) != NULL);
snprintf(dir, sizeof(dir), "mkdir -p '%s/bin'", root);
CHECK(system(dir) == 0);
snprintf(dir, sizeof(dir), "mkdir -p '%s/lib/splinter/plugins/cli'", root);
CHECK(system(dir) == 0);
snprintf(dir, sizeof(dir), "mkdir -p '%s/bin/plugins/cli'", root);
CHECK(system(dir) == 0);
snprintf(tree, sizeof(tree), "%s/bin/plugins/cli/cli.so", root);
snprintf(inst, sizeof(inst), "%s/lib/splinter/plugins/cli/cli.so", root);
snprintf(dir, sizeof(dir), "%s/bin/splinter", root);
(void)unsetenv("SPLINTER_CLI_PLUGIN");
/* installed layout: the sibling of the binary is not there */
f = fopen(inst, "w");
CHECK(f != NULL);
if (f != NULL)
fclose(f);
sp_delegate_cli_plugin(dir, got, sizeof(got));
snprintf(expect, sizeof(expect), "%s/bin/../lib/splinter/plugins/cli/cli.so",
root);
CHECK(strcmp(got, expect) == 0);
/* running from the tree: the sibling wins over the installed layout */
f = fopen(tree, "w");
CHECK(f != NULL);
if (f != NULL)
fclose(f);
sp_delegate_cli_plugin(dir, got, sizeof(got));
CHECK(strcmp(got, tree) == 0);
/* an explicit path is used as given, existing or not */
(void)setenv("SPLINTER_CLI_PLUGIN", "/explicit/cli.so", 1);
sp_delegate_cli_plugin(dir, got, sizeof(got));
CHECK(strcmp(got, "/explicit/cli.so") == 0);
(void)unsetenv("SPLINTER_CLI_PLUGIN");
/* nothing found: the answer still names a cli plugin, so the child's
* own error points at a place one can put it in (or at the build tree
* this binary was compiled in) */
(void)unlink(tree);
(void)unlink(inst);
sp_delegate_cli_plugin(dir, got, sizeof(got));
CHECK(got[0] != '\0');
CHECK(strstr(got, "/plugins/cli/cli.so") != NULL);
snprintf(dir, sizeof(dir), "rm -rf '%s'", root);
(void)system(dir);
}
int
main(void)
{
struct sp_config c;
char id[64], why[192];
int score;
/* scoring reads the usage and cooldown files: not the real ones */
{
static char ud[] = "/var/tmp/splinter-dlg-usage-XXXXXX";
if (mkdtemp(ud) != NULL)
setenv("SPLINTER_USAGE_DIR", ud, 1);
}
setenv("SPLINTER_COOLDOWN_DIR", "/var/tmp/splinter-nonexistent-cd", 1);
test_cli_plugin_path();
memset(&c, 0, sizeof(c));
add(&c, "coder", "C99, POSIX, kennt den Quellbaum", "code,c99,review");
add(&c, "quick", "kurze Antworten, Chat", "chat,cheap,summary");
/* a tag in the task wins by tag */
CHECK(sp_delegate_choose(&c, "pruefe den code auf fehler", NULL, id, sizeof(id),
why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "coder") == 0);
CHECK(score >= 3);
CHECK(strstr(why, "code") != NULL);
CHECK(sp_delegate_choose(&c, "mach eine kurze summary", NULL, id, sizeof(id),
why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "quick") == 0);
/* a requested tag outweighs the wording */
CHECK(sp_delegate_choose(&c, "irgendwas mit code", "chat", id, sizeof(id),
why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "quick") == 0);
CHECK(strstr(why, "requested") != NULL);
/* a description word is weaker than a tag but still counts */
CHECK(sp_delegate_choose(&c, "kennt jemand den quellbaum", NULL, id,
sizeof(id), why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "coder") == 0);
/* nothing matches: no pick rather than a random one */
CHECK(sp_delegate_choose(&c, "xyzzy", NULL, id, sizeof(id), why,
sizeof(why), &score) != 0);
/* prio decides a tie: same score, higher prio wins */
memset(&c, 0, sizeof(c));
add(&c, "low", "gleich gut", "x");
add(&c, "high", "gleich gut", "x");
c.delegate[1].prio = 9;
CHECK(sp_delegate_choose(&c, "x", NULL, id, sizeof(id), why, sizeof(why),
&score) == 0);
CHECK(strcmp(id, "high") == 0);
CHECK(sp_delegate_prio(&c, "high") == 9);
/*
* ---- tag weighting ----
* Words, not substrings: "client" is not "cli". Synonyms: "gross" is
* "big". A private task goes to a private delegate whatever the score.
*/
memset(&c, 0, sizeof(c));
add(&c, "cli", "terminal programs", "cli");
add(&c, "heavy", "grosse Aufgaben", "big,code");
add(&c, "home", "lokal und privat", "local,private,code");
add(&c, "cloud", "schnell", "cloud,fast,code");
CHECK(sp_delegate_choose(&c, "der client stuerzt ab", NULL, id,
sizeof(id), why, sizeof(why), &score) != 0 ||
strcmp(id, "cli") != 0);
CHECK(sp_delegate_choose(&c, "eine grosse migration", NULL, id,
sizeof(id), why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "heavy") == 0);
CHECK(strstr(why, "big") != NULL);
/* a codebase is code (prefix), a synonym counts less than the tag itself */
CHECK(sp_delegate_choose(&c, "schau in die codebase", NULL, id,
sizeof(id), why, sizeof(why), &score) == 0);
CHECK(score >= 2);
/* private: only the private delegate may take it, even if others score more */
CHECK(sp_delegate_choose(&c, "privater code, grosse migration, schnell",
NULL, id, sizeof(id), why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "home") == 0);
CHECK(sp_delegate_choose(&c, "x", "private", id, sizeof(id), why,
sizeof(why), &score) == 0);
CHECK(strcmp(id, "home") == 0);
/* nobody private is available: an error, not a leak to the cloud */
c.ndelegates = 1;
c.delegate[0].tags[0] = '\0';
snprintf(c.delegate[0].tags, sizeof(c.delegate[0].tags), "code");
CHECK(sp_delegate_choose(&c, "geheimer code", NULL, id, sizeof(id), why,
sizeof(why), &score) != 0);
CHECK(strstr(why, "private") != NULL);
/* nothing fits: the default takes the task, unless it is private */
memset(&c, 0, sizeof(c));
add(&c, "gen", "allgemein", "general");
add(&c, "spec", "speziell", "special");
snprintf(c.default_id, sizeof(c.default_id), "gen");
CHECK(sp_delegate_choose(&c, "xyzzy plugh", NULL, id, sizeof(id), why,
sizeof(why), &score) == 0);
CHECK(strcmp(id, "gen") == 0 && strstr(why, "default") != NULL);
CHECK(sp_delegate_choose(&c, "xyzzy geheim", NULL, id, sizeof(id), why,
sizeof(why), &score) != 0);
/*
* ---- roles ----
* Claude analyses and decides, the flash models implement; two flash
* providers without a declared limit take turns by what they were given.
*/
memset(&c, 0, sizeof(c));
add(&c, "claude", "analyse und entscheidung", "analyze,decide,plan,review");
add(&c, "flash1", "umsetzung", "code,fast");
add(&c, "flash2", "umsetzung", "code,fast");
CHECK(sp_delegate_choose(&c, "analysiere die ursache und empfehle eine loesung", NULL,
id, sizeof(id), why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "claude") == 0);
CHECK(sp_delegate_choose(&c, "entscheide ob wir A oder B nehmen", NULL, id,
sizeof(id), why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "claude") == 0);
CHECK(sp_delegate_choose(&c, "implementiere die funktion", NULL, id,
sizeof(id), why, sizeof(why), &score) == 0);
CHECK(strncmp(id, "flash", 5) == 0);
{
const struct sp_delegate *pick;
c.npools = 1;
snprintf(c.pool[0].id, sizeof(c.pool[0].id), "work");
snprintf(c.pool[0].strategy, sizeof(c.pool[0].strategy), "balance");
snprintf(c.pool[0].member[0], 32, "flash1");
snprintf(c.pool[0].member[1], 32, "flash2");
c.pool[0].nmembers = 2;
pick = sp_pool_pick(&c, &c.pool[0], NULL, NULL, NULL);
CHECK(pick != NULL && strcmp(pick->id, "flash1") == 0);
sp_usage_record("flash1", 0, 0);
sp_usage_record("flash1", 0, 0);
pick = sp_pool_pick(&c, &c.pool[0], NULL, NULL, NULL);
CHECK(pick != NULL && strcmp(pick->id, "flash2") == 0);
}
/* max_parallel defaults to one and never to zero */
memset(&c, 0, sizeof(c));
add(&c, "d", "x", "x");
CHECK(c.delegate[0].max_parallel == 0); /* add() does not set it */
/* an empty set is not a match either */
c.ndelegates = 0;
CHECK(sp_delegate_choose(&c, "code", NULL, id, sizeof(id), why,
sizeof(why), &score) != 0);
/*
* ---- a remote delegate: the task reaches the far side as one word ----
* ssh joins its arguments into a command line for the remote shell. A
* fake ssh in PATH plays that shell: it re-splits the line with eval,
* which runs anything an unquoted task would smuggle in, and records the
* words it got.
*/
{
char tdir[] = "/var/tmp/splinter-del-XXXXXX";
char path[600], mark[600], words[600], fake[1600];
char task[700], args[1200], line[800];
const char *oldpath = getenv("PATH");
struct sp_registry r;
struct sp_buf out;
struct stat st;
FILE *f;
int n = 0, task_ok = 0, env_ok = 0;
CHECK(mkdtemp(tdir) != NULL);
snprintf(mark, sizeof(mark), "%s/pwned", tdir);
snprintf(words, sizeof(words), "%s/words", tdir);
snprintf(path, sizeof(path), "%s/ssh", tdir);
snprintf(fake, sizeof(fake), "#!/bin/sh\n"
"[ \"$3\" = -- ] && [ \"$4\" = farhost ] || exit 3\n"
"eval \"set -- $5\"\n"
"printf '%%s\\n' \"$@\" > '%s'\n"
"echo fertig\n", words);
f = fopen(path, "w");
CHECK(f != NULL);
if (f != NULL) {
fputs(fake, f);
fclose(f);
}
(void)chmod(path, 0755);
snprintf(line, sizeof(line), "%s:%s", tdir,
oldpath != NULL ? oldpath : "/usr/bin:/bin");
(void)setenv("PATH", line, 1);
memset(&c, 0, sizeof(c));
add(&c, "far", "remote", "far");
snprintf(c.delegate[0].host, sizeof(c.delegate[0].host), "farhost");
c.delegate[0].max_parallel = 1;
/* env= travels in front as `env 'NAME=VALUE'`, quoted like the rest */
snprintf(c.delegate[0].env[0], sizeof(c.delegate[0].env[0]),
"TOK=a b'c");
c.delegate[0].nenv = 1;
memset(&r, 0, sizeof(r));
CHECK(sp_delegate_register(&r, &c, NULL) == 0);
snprintf(task, sizeof(task), "it's x'; touch %s; echo '", mark);
snprintf(args, sizeof(args), "{\"task\":\"%s\",\"tags\":\"far\"}",
task);
CHECK(sp_buf_init(&out) == 0);
{
const struct sp_tool *t = sp_registry_find(&r, "delegate");
CHECK(t != NULL);
if (t != NULL)
(void)t->run(args, &out, t->ctx);
}
CHECK(stat(mark, &st) != 0);
if (getenv("TEST_VERBOSE") != NULL)
fprintf(stderr, "delegate said: %s\n", out.p);
f = fopen(words, "r");
CHECK(f != NULL);
if (f != NULL) {
while (fgets(line, sizeof(line), f) != NULL) {
line[strcspn(line, "\n")] = '\0';
if (n == 0 && strcmp(line, "env") == 0)
env_ok = 1;
if (n == 1 && strcmp(line, "TOK=a b'c") == 0)
env_ok++;
if (n == 5 && strcmp(line, task) == 0)
task_ok = 1;
n++;
}
fclose(f);
}
CHECK(task_ok);
CHECK(env_ok == 2);
sp_buf_free(&out);
sp_registry_free(&r);
(void)setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1);
snprintf(line, sizeof(line), "rm -rf '%s'", tdir);
(void)system(line);
}
/*
* ---- a delegate is a task on the panel, and the panel says so ----
* The call opens a task before it runs and closes it after; this is the
* cycle that used to be claimed without a test. The widget keeps the
* finished entry, so the assertion is possible after the turn.
*/
{
struct sp_config c;
struct sp_registry r;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf out, err;
char tdir[] = "/tmp/splinter-panel-XXXXXX";
char path[400], line[400], snap[512];
const char *oldpath = getenv("PATH");
const struct sp_tool *t;
FILE *f;
size_t n;
CHECK(mkdtemp(tdir) != NULL);
snprintf(path, sizeof(path), "%s/ssh", tdir);
f = fopen(path, "w");
CHECK(f != NULL);
if (f != NULL) {
fputs("#!/bin/sh\necho 'antwort vom panel'\n", f);
fclose(f);
}
(void)chmod(path, 0755);
snprintf(line, sizeof(line), "%s:%s", tdir,
oldpath != NULL ? oldpath : "/usr/bin:/bin");
(void)setenv("PATH", line, 1);
memset(&c, 0, sizeof(c));
add(&c, "panel", "zeigt sich im panel", "panel");
snprintf(c.delegate[0].host, sizeof(c.delegate[0].host), "panelsrv");
c.delegate[0].max_parallel = 1;
memset(&r, 0, sizeof(r));
memset(&ag, 0, sizeof(ag));
CHECK(sp_buf_init(&out) == 0);
CHECK(sp_buf_init(&err) == 0);
host = sp_host_new(&r, &ag, NULL);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/tasks/tasks.so", &err) == 0);
CHECK(sp_delegate_register(&r, &c, host) == 0);
t = sp_registry_find(&r, "delegate");
CHECK(t != NULL);
if (t != NULL)
(void)t->run("{\"task\":\"zeig dich\",\"tags\":\"panel\"}",
&out, t->ctx);
CHECK(out.p != NULL && strstr(out.p, "antwort vom panel") != NULL);
n = sp_host_widget_snapshot(host, snap, sizeof(snap));
if (getenv("TEST_VERBOSE") != NULL)
fprintf(stderr, "panel: %s\n", snap);
CHECK(n > 0);
CHECK(strstr(snap, "delegate:panel") != NULL);
CHECK(strstr(snap, "exit 0") != NULL);
CHECK(sp_host_meter(host)->tasks_running == 0);
sp_host_free(host);
sp_registry_free(&r);
sp_buf_free(&out);
sp_buf_free(&err);
(void)setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1);
snprintf(line, sizeof(line), "rm -rf '%s'", tdir);
(void)system(line);
}
/*
* ---- a delegate at its limit is benched and the task goes on ----
* Fake ssh: host "spent" answers like claude with an empty account, host
* "fresh" answers. Two delegates fit the same task; the better one is the
* spent one.
*/
{
struct sp_config c;
struct sp_registry r;
struct sp_buf out;
char tdir[64] = "/tmp/splinter-cool-XXXXXX";
char path[400], line[400], cool[100];
const char *oldpath = getenv("PATH");
const struct sp_tool *t;
FILE *f;
long until = 0;
CHECK(mkdtemp(tdir) != NULL);
snprintf(path, sizeof(path), "%s/ssh", tdir);
f = fopen(path, "w");
CHECK(f != NULL);
if (f != NULL) {
fprintf(f, "#!/bin/sh\n"
"echo \"$4\" >> '%s/hosts'\n"
"if [ \"$4\" = spent ]; then\n"
" echo \"You've hit your weekly limit · resets 3am"
" (Europe/Lisbon)\"; exit 1\n"
"fi\n"
"echo 'antwort von fresh'\n", tdir);
fclose(f);
}
(void)chmod(path, 0755);
snprintf(line, sizeof(line), "%s:%s", tdir,
oldpath != NULL ? oldpath : "/usr/bin:/bin");
(void)setenv("PATH", line, 1);
snprintf(cool, sizeof(cool), "%s/cool", tdir);
(void)setenv("SPLINTER_COOLDOWN_DIR", cool, 1);
memset(&c, 0, sizeof(c));
add(&c, "claude0", "first account", "big");
snprintf(c.delegate[0].host, sizeof(c.delegate[0].host), "spent");
c.delegate[0].prio = 9;
c.delegate[0].max_parallel = 1;
add(&c, "claude1", "second account", "big");
snprintf(c.delegate[1].host, sizeof(c.delegate[1].host), "fresh");
c.delegate[1].prio = 5;
c.delegate[1].max_parallel = 1;
memset(&r, 0, sizeof(r));
CHECK(sp_delegate_register(&r, &c, NULL) == 0);
t = sp_registry_find(&r, "delegate");
CHECK(t != NULL);
CHECK(sp_buf_init(&out) == 0);
/* the spent one is tried, benched, and the task still gets done */
if (t != NULL)
(void)t->run("{\"task\":\"x\",\"tags\":\"big\"}", &out,
t->ctx);
if (getenv("TEST_VERBOSE") != NULL)
fprintf(stderr, "fallback said: %s\n", out.p);
CHECK(out.p != NULL && strstr(out.p, "antwort von fresh") != NULL);
CHECK(out.p != NULL && strstr(out.p, "falling back to claude1") != NULL);
snprintf(path, sizeof(path), "%s/claude0", cool);
f = fopen(path, "r");
CHECK(f != NULL);
if (f != NULL) {
CHECK(fscanf(f, "%ld", &until) == 1);
fclose(f);
}
/* until the next 3am in Lisbon: in the future, within a day */
CHECK(until > (long)time(NULL));
CHECK(until <= (long)time(NULL) + 86400 + 3600);
/* benched: the next task does not knock on the spent door */
snprintf(path, sizeof(path), "%s/hosts", tdir);
(void)unlink(path);
sp_buf_clear(&out);
if (t != NULL)
(void)t->run("{\"task\":\"y\",\"tags\":\"big\"}", &out,
t->ctx);
CHECK(out.p != NULL && strstr(out.p, "antwort von fresh") != NULL);
/* not even when it is asked for by name */
sp_buf_clear(&out);
if (t != NULL)
(void)t->run("{\"task\":\"z\",\"delegate\":\"claude0\"}",
&out, t->ctx);
CHECK(out.p != NULL && strstr(out.p, "antwort von fresh") != NULL);
f = fopen(path, "r");
CHECK(f != NULL);
if (f != NULL) {
while (fgets(line, sizeof(line), f) != NULL)
CHECK(strncmp(line, "spent", 5) != 0);
fclose(f);
}
/*
* parallel: the spent one fails mid-run, the task moves to the
* next one in the same slot, the other task is not disturbed
*/
snprintf(line, sizeof(line), "rm -rf '%s'", cool);
(void)system(line);
sp_buf_free(&out);
sp_registry_free(&r);
add(&c, "other", "unrelated", "small");
snprintf(c.delegate[2].host, sizeof(c.delegate[2].host), "fresh");
c.delegate[2].max_parallel = 1;
memset(&r, 0, sizeof(r));
CHECK(sp_delegate_register(&r, &c, NULL) == 0);
t = sp_registry_find(&r, "delegate_parallel");
CHECK(t != NULL);
CHECK(sp_buf_init(&out) == 0);
if (t != NULL)
(void)t->run("{\"tasks\":[{\"task\":\"p1\",\"tags\":\"big\"},"
"{\"task\":\"p2\",\"tags\":\"small\"}]}", &out, t->ctx);
if (getenv("TEST_VERBOSE") != NULL)
fprintf(stderr, "parallel said: %s\n", out.p);
CHECK(out.p != NULL && strstr(out.p, "[claude0 at its limit") != NULL);
CHECK(out.p != NULL && strstr(out.p, "[falling back to claude1]") != NULL);
CHECK(out.p != NULL && strstr(out.p, "model claude1") != NULL);
CHECK(out.p != NULL && strstr(out.p, "model other") != NULL);
CHECK(out.p != NULL && strstr(out.p, "exit 1") == NULL);
snprintf(path, sizeof(path), "%s/claude0", cool);
CHECK(access(path, F_OK) == 0);
sp_buf_free(&out);
sp_registry_free(&r);
(void)setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1);
(void)unsetenv("SPLINTER_COOLDOWN_DIR");
snprintf(line, sizeof(line), "rm -rf '%s'", tdir);
(void)system(line);
}
{
const char *ud = getenv("SPLINTER_USAGE_DIR");
char cmd[300];
if (ud != NULL && strstr(ud, "splinter-dlg-usage-") != NULL) {
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", ud);
(void)system(cmd);
}
}
if (failures == 0)
printf("test_delegate: ok\n");
return failures == 0 ? 0 : 1;
}