|
|
@@ -0,0 +1,134 @@
|
|
|
+// v2.15.0 client/server boundary test: capability negotiation.
|
|
|
+//
|
|
|
+// A client must be able to ask what the server is and which features it has,
|
|
|
+// so a client-only release can refuse a call an older server would misread
|
|
|
+// instead of sending it. Phases, run by test_server_info.sh (and, against
|
|
|
+// released servers, by packaging/compat.sh):
|
|
|
+// current <version> - against this tree's server: version, commit, the
|
|
|
+// feature list (every name the client knows is
|
|
|
+// reported), the version header arrives, caching.
|
|
|
+// old <addr> - against a pre-2.15.0 server: serverInfo() is nullopt,
|
|
|
+// serverSupports() is false, ordinary calls still work,
|
|
|
+// a filtered similaritySearch is refused.
|
|
|
+// down - server stopped: serverInfo() THROWS (an outage is not
|
|
|
+// "an old server").
|
|
|
+
|
|
|
+#include <smartbotic/database/client.hpp>
|
|
|
+
|
|
|
+#include <iostream>
|
|
|
+#include <string>
|
|
|
+#include <vector>
|
|
|
+
|
|
|
+using namespace smartbotic::database;
|
|
|
+using Cap = Client::Capability;
|
|
|
+static int pass = 0, fail = 0;
|
|
|
+static void ck(bool c, const std::string& m) {
|
|
|
+ if (c) { ++pass; } else { ++fail; std::cerr << "FAIL: " << m << "\n"; }
|
|
|
+}
|
|
|
+
|
|
|
+static std::string gAddr = "127.0.0.1:9027";
|
|
|
+
|
|
|
+static Client::Config cfg() {
|
|
|
+ Client::Config c;
|
|
|
+ c.address = gAddr;
|
|
|
+ c.project = "si";
|
|
|
+ c.timeoutMs = 3000;
|
|
|
+ return c;
|
|
|
+}
|
|
|
+
|
|
|
+static const std::vector<const char*> kAllCaps = {
|
|
|
+ Cap::kProjects, Cap::kAccessPolicy, Cap::kQueryProjection, Cap::kFileTtl,
|
|
|
+ Cap::kDocumentTtlUpdate, Cap::kSecondaryIndexes, Cap::kIndexValues, Cap::kRelations,
|
|
|
+ Cap::kUniqueIndexes, Cap::kErrorDetail, Cap::kPatchIfVersion, Cap::kIncrement,
|
|
|
+ Cap::kCompositeUnique, Cap::kBatchDeleteIds, Cap::kEventActor, Cap::kVectorReadback,
|
|
|
+ Cap::kExportCollection, Cap::kWriteSizeGuard, Cap::kFilteredSimilarity, Cap::kServerInfo,
|
|
|
+ Cap::kClientVersionHeader,
|
|
|
+};
|
|
|
+
|
|
|
+// Ordinary calls a client of any version makes; they must work across versions.
|
|
|
+static void basics(Client& c, const std::string& tag) {
|
|
|
+ c.createCollection("v", 0, false, 0, 4);
|
|
|
+ const auto id = c.insert("v", {{"k", "a"}, {"_vector", std::vector<float>{1, 0, 0, 0}}});
|
|
|
+ ck(!id.empty(), tag + ": insert");
|
|
|
+ const auto got = c.get("v", id);
|
|
|
+ ck(got && got->value("k", "") == "a", tag + ": get");
|
|
|
+ Client::QueryOptions o;
|
|
|
+ o.filters = {Client::Filter::eq("k", "a")};
|
|
|
+ ck(!c.find("v", o).empty(), tag + ": find");
|
|
|
+ ck(!c.similaritySearch("v", {1, 0, 0, 0}, 3).empty(), tag + ": plain similaritySearch");
|
|
|
+ ck(c.remove("v", id), tag + ": remove");
|
|
|
+}
|
|
|
+
|
|
|
+static int current(const std::string& version) {
|
|
|
+ Client c(cfg());
|
|
|
+ ck(c.connect(), "connect");
|
|
|
+ const auto si = c.serverInfo();
|
|
|
+ ck(si.has_value(), "serverInfo answers");
|
|
|
+ if (!si) return 0;
|
|
|
+ ck(si->version == version, "version is " + version + " (got '" + si->version + "')");
|
|
|
+ ck(!si->commit.empty(), "commit is set ('" + si->commit + "')");
|
|
|
+ ck(si->clientVersionSeen == clientLibraryVersion(),
|
|
|
+ "the server received this library's version (" + si->clientVersionSeen + ")");
|
|
|
+ for (const char* cap : kAllCaps) {
|
|
|
+ ck(c.serverSupports(cap), std::string("server reports '") + cap + "'");
|
|
|
+ }
|
|
|
+ ck(si->capabilities.size() == kAllCaps.size(),
|
|
|
+ "client and server know the same feature names (" +
|
|
|
+ std::to_string(si->capabilities.size()) + " vs " + std::to_string(kAllCaps.size()) + ")");
|
|
|
+ ck(!c.serverSupports("no_such_feature"), "an unknown name is not supported");
|
|
|
+ ck(c.serverInfo(true).has_value(), "refresh answers too");
|
|
|
+ basics(c, "current");
|
|
|
+ const auto f = c.similaritySearch("v", {1, 0, 0, 0}, 3, -1.f, {Client::Filter::eq("k", "zz")});
|
|
|
+ ck(f.empty(), "filtered similaritySearch works against a server that has it");
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int old() {
|
|
|
+ Client c(cfg());
|
|
|
+ ck(c.connect(), "old: connect");
|
|
|
+ bool threw = false;
|
|
|
+ std::optional<Client::ServerInfo> si;
|
|
|
+ try { si = c.serverInfo(); } catch (...) { threw = true; }
|
|
|
+ ck(!threw && !si.has_value(), "old: serverInfo is nullopt, not an error");
|
|
|
+ ck(!c.serverSupports(Cap::kServerInfo), "old: serverSupports is false");
|
|
|
+ basics(c, "old");
|
|
|
+ // A pre-2.14.0 server would answer a filtered search UNFILTERED; a 2.14.0
|
|
|
+ // one applies it. Either way the answer must never be unfiltered rows.
|
|
|
+ c.createCollection("w", 0, false, 0, 4);
|
|
|
+ c.insert("w", {{"k", "a"}, {"_vector", std::vector<float>{1, 0, 0, 0}}});
|
|
|
+ bool refused = false;
|
|
|
+ std::vector<Client::SimilarityResult> r;
|
|
|
+ try {
|
|
|
+ r = c.similaritySearch("w", {1, 0, 0, 0}, 3, -1.f, {Client::Filter::eq("k", "zz")});
|
|
|
+ } catch (const Client::FailedPrecondition& e) {
|
|
|
+ const auto* d = Client::errorDetail(e);
|
|
|
+ refused = d && d->reason == "filters_unsupported";
|
|
|
+ }
|
|
|
+ ck(refused || r.empty(), "old: a filtered search is refused or really filtered, never unfiltered");
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int down() {
|
|
|
+ Client c(cfg());
|
|
|
+ c.connect();
|
|
|
+ bool threwUnavailable = false;
|
|
|
+ try {
|
|
|
+ (void)c.serverInfo(true);
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ threwUnavailable = e.code() == Client::ErrorCode::Unavailable ||
|
|
|
+ e.code() == Client::ErrorCode::DeadlineExceeded;
|
|
|
+ }
|
|
|
+ ck(threwUnavailable, "down: serverInfo throws, it does not claim an old server");
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int main(int argc, char** argv) {
|
|
|
+ const std::string phase = argc > 1 ? argv[1] : "";
|
|
|
+ if (argc > 3) gAddr = argv[3];
|
|
|
+ if (phase == "current" && argc > 2) current(argv[2]);
|
|
|
+ else if (phase == "old") { if (argc > 2) gAddr = argv[2]; old(); }
|
|
|
+ else if (phase == "down") down();
|
|
|
+ else { std::cerr << "usage: current <version> [addr] | old [addr] | down\n"; return 2; }
|
|
|
+ std::cout << phase << ": " << pass << " passed, " << fail << " failed\n";
|
|
|
+ return fail == 0 ? 0 : 1;
|
|
|
+}
|