176 lines
5.0 KiB
C++
176 lines
5.0 KiB
C++
#include "networked_kv/application.hpp"
|
|
|
|
#include <Void/Net/Http.hpp>
|
|
|
|
#include <cassert>
|
|
#include <cstdio>
|
|
#include <string>
|
|
|
|
namespace
|
|
{
|
|
using namespace NetworkedKv;
|
|
|
|
void config_validation()
|
|
{
|
|
NetworkedConfig bad;
|
|
bad.listen_address.clear();
|
|
assert(!bad.validate().has_value());
|
|
|
|
NetworkedConfig no_connections;
|
|
no_connections.max_connections = 0;
|
|
assert(!no_connections.validate().has_value());
|
|
|
|
NetworkedConfig ok;
|
|
assert(ok.validate().has_value());
|
|
}
|
|
|
|
void managed_mode_owns_the_host()
|
|
{
|
|
ApplicationResources resources;
|
|
resources.ownership = ResourceOwnership::Managed;
|
|
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
|
assert(app.has_value());
|
|
|
|
// Construction opens no listener or worker.
|
|
const auto created = app->health();
|
|
assert(created.state == ApplicationState::Created);
|
|
assert(!created.ready);
|
|
|
|
assert(app->start().has_value());
|
|
const auto running = app->health();
|
|
assert(running.state == ApplicationState::Running);
|
|
assert(running.managed);
|
|
|
|
auto report = app->stop();
|
|
assert(report.has_value());
|
|
assert(app->health().state == ApplicationState::Stopped);
|
|
|
|
// Idempotent.
|
|
assert(app->stop().has_value());
|
|
}
|
|
|
|
void embedded_mode_borrows_the_host()
|
|
{
|
|
auto host = Void::Runtime::Host::create();
|
|
assert(host.has_value());
|
|
assert(host->start().has_value());
|
|
|
|
ApplicationResources resources;
|
|
resources.ownership = ResourceOwnership::Embedded;
|
|
resources.borrowed_host = &*host;
|
|
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
|
assert(app.has_value());
|
|
assert(app->start().has_value());
|
|
assert(app->health().state == ApplicationState::Running);
|
|
|
|
assert(app->stop().has_value());
|
|
// The borrowed host is not shut down by the composition.
|
|
assert(host->is_running());
|
|
(void)host->shutdown_blocking();
|
|
}
|
|
|
|
void embedded_requires_a_host()
|
|
{
|
|
ApplicationResources resources;
|
|
resources.ownership = ResourceOwnership::Embedded;
|
|
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
|
assert(!app.has_value());
|
|
}
|
|
|
|
void http_listener_serves_kv()
|
|
{
|
|
ApplicationResources resources;
|
|
resources.ownership = ResourceOwnership::Managed;
|
|
NetworkedConfig config;
|
|
config.listen_address = "127.0.0.1:0";
|
|
config.bearer_token = "secret";
|
|
auto app = NetworkedApplication::create(resources, config);
|
|
assert(app.has_value());
|
|
assert(app->start().has_value());
|
|
|
|
const auto port = app->health().bound_port;
|
|
assert(port != 0);
|
|
const std::string base =
|
|
"http://127.0.0.1:" + std::to_string(port);
|
|
Void::Net::HttpClient client;
|
|
|
|
auto unauthorized = client.get(base + "/v1/kv/a");
|
|
assert(unauthorized.has_value());
|
|
assert(unauthorized->status == 401);
|
|
|
|
Void::Net::Request put;
|
|
put.method = Void::Net::Method::Put;
|
|
put.url = base + "/v1/kv/a";
|
|
put.body = std::string{"one"};
|
|
put.headers.push_back(
|
|
Void::Net::Header{"authorization", "Bearer secret"});
|
|
put.headers.push_back(Void::Net::Header{"x-request-id", "r1"});
|
|
put.headers.push_back(Void::Net::Header{"if-none-match", "*"});
|
|
auto created = client.send(put);
|
|
assert(created.has_value());
|
|
assert(created->status == 200);
|
|
|
|
auto got = client.get(
|
|
base + "/v1/kv/a",
|
|
{Void::Net::Header{"authorization", "Bearer secret"}});
|
|
assert(got.has_value());
|
|
assert(got->status == 200);
|
|
assert(got->body == "one");
|
|
|
|
assert(app->stop().has_value());
|
|
assert(app->health().bound_port == 0);
|
|
}
|
|
|
|
void admin_listener_is_separate()
|
|
{
|
|
ApplicationResources resources;
|
|
resources.ownership = ResourceOwnership::Managed;
|
|
NetworkedConfig config;
|
|
config.listen_address = "127.0.0.1:0";
|
|
config.admin_address = "127.0.0.1:0";
|
|
config.bearer_token = "secret";
|
|
auto app = NetworkedApplication::create(resources, config);
|
|
assert(app.has_value());
|
|
assert(app->start().has_value());
|
|
|
|
const auto data_port = app->health().bound_port;
|
|
const auto admin_port = app->admin_bound_port();
|
|
assert(data_port != 0);
|
|
assert(admin_port != 0);
|
|
assert(admin_port != data_port);
|
|
|
|
Void::Net::HttpClient client;
|
|
const std::string admin_base =
|
|
"http://127.0.0.1:" + std::to_string(admin_port);
|
|
auto live = client.get(admin_base + "/live");
|
|
assert(live.has_value() && live->status == 200);
|
|
auto ready = client.get(admin_base + "/ready");
|
|
assert(ready.has_value() && ready->status == 200);
|
|
auto metrics = client.get(admin_base + "/metrics");
|
|
assert(metrics.has_value() && metrics->status == 200);
|
|
assert(metrics->body.find("networked_kv_requests_total") !=
|
|
std::string::npos);
|
|
|
|
// Administrative routes are not exposed on the data plane.
|
|
const std::string data_base =
|
|
"http://127.0.0.1:" + std::to_string(data_port);
|
|
auto not_admin = client.get(data_base + "/live");
|
|
assert(not_admin.has_value() && not_admin->status == 404);
|
|
|
|
assert(app->stop().has_value());
|
|
assert(app->admin_bound_port() == 0);
|
|
}
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
config_validation();
|
|
managed_mode_owns_the_host();
|
|
embedded_mode_borrows_the_host();
|
|
embedded_requires_a_host();
|
|
http_listener_serves_kv();
|
|
admin_listener_is_separate();
|
|
std::puts("networked-kv lifecycle ok");
|
|
return 0;
|
|
}
|