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 v1"));
28 REQUIRE_THAT(output, Contains("Usage:"));
29 REQUIRE_THAT(output, Contains("key [key ...]"));
30 REQUIRE_THAT(output, Contains("Options:"));
31 REQUIRE_THAT(output, Contains("-h|--help"));
32 REQUIRE_THAT(output, Contains("-V|--version"));
33 REQUIRE_THAT(output, Contains("Environment:"));
34 REQUIRE_THAT(output, Contains("MEMCACHED_SERVERS"));
35 }
36
37 SECTION("with server") {
38 Server server{MEMCACHED_BINARY, {"-p", random_port_string}};
39 MemcachedPtr memc;
40 LoneReturnMatcher test{*memc};
41
42 REQUIRE(server.ensureListening());
43 auto port = get<int>(server.getSocketOrPort());
44 auto comm = "memcat --servers=localhost:" + to_string(port) + " ";
45
46 REQUIRE_SUCCESS(memcached_server_add(*memc, "localhost", port));
47
48 SECTION("not found") {
49 memcached_delete(*memc, S("memcat"), 0);
50
51 string output;
52 REQUIRE_FALSE(sh.run(comm + "memcat", output));
53 REQUIRE(output.empty());
54 }
55 SECTION("not found --verbose") {
56 memcached_delete(*memc, S("memcat"), 0);
57
58 string output;
59 REQUIRE_FALSE(sh.run(comm + " -v memcat", output));
60 REQUIRE_THAT(output, !Contains("MEMCAT-SET"));
61 REQUIRE_THAT(output, Contains("NOT FOUND"));
62 }
63 SECTION("found") {
64 string output;
65 REQUIRE_SUCCESS(memcached_set(*memc, S("memcat"), S("MEMCAT-SET"), 0, 123));
66
67 SECTION("default") {
68 REQUIRE(sh.run(comm + "memcat", output));
69 REQUIRE(output == "MEMCAT-SET\n");
70 }
71 SECTION("flags") {
72 REQUIRE(sh.run(comm + "--flag memcat", output));
73 REQUIRE(output == "123\nMEMCAT-SET\n");
74 output.clear();
75 REQUIRE(sh.run(comm + "--flag -v memcat", output));
76 REQUIRE(output == "key: memcat\nflags: 123\nvalue: MEMCAT-SET\n");
77 }
78 SECTION("file") {
79 Tempfile temp;
80 REQUIRE(sh.run(comm + "--file " + temp.getFn() + " memcat", output));
81 REQUIRE(output.empty());
82 REQUIRE(temp.get() == "MEMCAT-SET");
83 }
84 }
85 }
86 }