/* * splinter -- junction plugin tests. * * The plugin is loaded for real and talks HTTP to tests/fake_junction.py. What * is proven: the X-Junction-Mcp header is what makes the endpoint "junction", * the counters come from /status (not from the plugin counting), the sidebar * text carries no colon in a continuation line (the sidebar would read it as a * new widget), and neither the tool nor /junction prints the key. */ #include #include #include #include #include "splinter.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 nap_ms(long ms) { struct timespec ts; ts.tv_sec = ms / 1000; ts.tv_nsec = (ms % 1000) * 1000000L; nanosleep(&ts, NULL); } /* the sidebar reads "name: text"; line 2 and later must not look like a name */ static int continuation_has_colon(const char *text) { const char *p = strchr(text, '\n'); while (p != NULL && *p != '\0') { const char *start = p + 1; const char *nl = strchr(start, '\n'); p = nl; if (strchr(start, ':') != NULL && (nl == NULL || strchr(start, ':') < nl)) return 1; } return 0; } int main(int argc, char **argv) { struct sp_registry reg; struct sp_agent ag; struct sp_host *host; struct sp_buf err, out; char snap[4096], url[256]; if (argc < 2) { fprintf(stderr, "usage: %s PORT\n", argv[0]); return 1; } memset(®, 0, sizeof(reg)); memset(&ag, 0, sizeof(ag)); if (sp_buf_init(&err) != 0 || sp_buf_init(&out) != 0) return 1; snprintf(url, sizeof(url), "http://127.0.0.1:%s/mcp", argv[1]); setenv("SPLINTER_JUNCTION_URL", url, 1); unsetenv("SPLINTER_JUNCTION_TOKEN"); unsetenv("JUNCTION_TOKEN"); host = sp_host_new(®, &ag, NULL); CHECK(host != NULL); /* * The route comes from the run's model: /status' rules are matched * against it, so the plugin must read it from the config, exactly as * the sidebar names it. */ { static struct sp_config conf; sp_config_init(&conf); snprintf(conf.model, sizeof(conf.model), "m"); sp_host_set_config(host, &conf); } CHECK(sp_plugin_load(host, "plugins/junction/junction.so", &err) == 0); /* the loader reports a failed init; a CHECK here would have caught it */ /* the first tick runs immediately, the HTTP round trips take a moment */ nap_ms(2500); snap[0] = '\0'; CHECK(sp_host_widget_snapshot(host, snap, sizeof(snap)) > 0); CHECK(strstr(snap, "junction: ") != NULL); CHECK(strstr(snap, "up") != NULL); /* * The widget is only the model and its route: a is up, b is not (hence * "b!"), and the counters are not repeated here - they are /junction's * business. */ CHECK(strstr(snap, "m\nserving (no request yet)\nroute a\nb!\n1/2 up") != NULL); CHECK(strstr(snap, "mcp 4/5") == NULL); CHECK(strstr(snap, "models 3") == NULL); CHECK(strstr(snap, "endpoint=") == NULL); CHECK(!continuation_has_colon(snap)); /* the status segment the status line and /status show */ { char buf[160]; buf[0] = '\0'; (void)sp_host_status_text(host, buf, sizeof(buf)); CHECK(strstr(buf, "junction") != NULL); CHECK(strstr(buf, "up") != NULL); } /* /junction: every field, no key */ sp_buf_clear(&out); CHECK(sp_host_command(host, "junction", "", &out) == 0); CHECK(strstr(out.p, "junction: up") != NULL); CHECK(strstr(out.p, "mcp: 4/5 answering") != NULL); CHECK(strstr(out.p, "header: no") != NULL); /* no token was set */ CHECK(strstr(out.p, "Authorization") == NULL); /* every header of every probe, and the status alias that answered */ CHECK(strstr(out.p, "/v1/status 200") != NULL); CHECK(strstr(out.p, "headers /mcp") != NULL); CHECK(strstr(out.p, "headers /v1/models") != NULL); CHECK(strstr(out.p, "X-Junction-Endpoint: fake") != NULL); CHECK(strstr(out.p, "statistics: ") != NULL); CHECK(strstr(out.p, "statistics: endpoint=") == NULL); CHECK(strstr(out.p, "probe-ms=12") != NULL); CHECK(strstr(out.p, "model: m") != NULL); CHECK(strstr(out.p, "serving: (no request yet)") != NULL); CHECK(strstr(out.p, "route: a b") != NULL); CHECK(strstr(out.p, "route health: 1/2 up, down: b") != NULL); CHECK(strstr(out.p, "not the session's") != NULL); CHECK(strstr(out.p, "models: 3") != NULL); /* the tool is registered and returns the same snapshot */ CHECK(sp_registry_find(®, "junction_status") != NULL); sp_host_free(host); sp_buf_free(&err); sp_buf_free(&out); sp_registry_free(®); if (failures == 0) printf("test_junction: ok\n"); return failures == 0 ? 0 : 1; }