bin: consolidate clients
[awesomized/libmemcached] / test / tests / bin / memcat.cpp
1 #include "test/lib/common.hpp"
2 #include "test/lib/Shell.hpp"
3 #include "test/lib/Server.hpp"
4 #include "test/lib/Retry.hpp"
5 #include "test/lib/ReturnMatcher.hpp"
6
7 using Catch::Matchers::Contains;
8
9 TEST_CASE("bin/memcat") {
10 Shell sh{string{TESTING_ROOT "/../src/bin"}};
11
12 SECTION("no servers provided") {
13 string output;
14 REQUIRE_FALSE(sh.run("memcat", output));
15 REQUIRE(output == "No servers provided\n");
16 }
17
18 SECTION("connection failure") {
19 string output;
20 CHECK_FALSE(sh.run("memcat --servers=localhost:" + random_port_string("-p") + " memcat", output));
21 REQUIRE_THAT(output, Contains("CONNECTION FAILURE"));
22 }
23
24 SECTION("--help") {
25 string output;
26 REQUIRE(sh.run("memcat --help", output));
27 REQUIRE_THAT(output, Contains("memcat"));
28 REQUIRE_THAT(output, Contains("v1"));
29 REQUIRE_THAT(output, Contains("help"));
30 REQUIRE_THAT(output, Contains("version"));
31 REQUIRE_THAT(output, Contains("option"));
32 REQUIRE_THAT(output, Contains("--"));
33 REQUIRE_THAT(output, Contains("="));
34 }
35
36 SECTION("with server") {
37 Server server{MEMCACHED_BINARY, {"-p", random_port_string}};
38 MemcachedPtr memc;
39 LoneReturnMatcher test{*memc};
40
41 REQUIRE(server.ensureListening());
42 auto port = get<int>(server.getSocketOrPort());
43 auto comm = "memcat --servers=localhost:" + to_string(port) + " ";
44
45 REQUIRE_SUCCESS(memcached_server_add(*memc, "localhost", port));
46
47 SECTION("not found") {
48 memcached_delete(*memc, S("memcat"), 0);
49
50 string output;
51 REQUIRE_FALSE(sh.run(comm + "memcat", output));
52 REQUIRE_THAT(output, !Contains("MEMCAT-SET"));
53 REQUIRE_THAT(output, Contains("NOT FOUND"));
54 }
55 SECTION("found") {
56 string output;
57 REQUIRE_SUCCESS(memcached_set(*memc, S("memcat"), S("MEMCAT-SET"), 0, 123));
58
59 SECTION("default") {
60 REQUIRE(sh.run(comm + "memcat", output));
61 REQUIRE(output == "MEMCAT-SET\n");
62 }
63 SECTION("flags") {
64 REQUIRE(sh.run(comm + "--flag memcat", output));
65 REQUIRE(output == "123\n");
66 output.clear();
67 REQUIRE(sh.run(comm + "--flag -v memcat", output));
68 REQUIRE(output == "key: memcat\nflags: 123\n");
69 }
70 SECTION("file") {
71 Tempfile temp;
72 REQUIRE(sh.run(comm + "--file " + temp.getFn() + " memcat", output));
73 REQUIRE(output.empty());
74 REQUIRE(temp.get() == "MEMCAT-SET");
75 }
76 }
77 }
78 }