bin: consolidate clients
[awesomized/libmemcached] / test / tests / bin / memrm.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/memrm") {
10 Shell sh{string{TESTING_ROOT "/../src/bin"}};
11
12 SECTION("no servers provided") {
13 string output;
14 REQUIRE_FALSE(sh.run("memrm", output));
15 REQUIRE(output == "No Servers provided\n");
16 }
17
18 SECTION("--help") {
19 string output;
20 REQUIRE(sh.run("memrm --help", output));
21 REQUIRE_THAT(output, Contains("memrm"));
22 REQUIRE_THAT(output, Contains("v1"));
23 REQUIRE_THAT(output, Contains("help"));
24 REQUIRE_THAT(output, Contains("version"));
25 REQUIRE_THAT(output, Contains("option"));
26 REQUIRE_THAT(output, Contains("--"));
27 REQUIRE_THAT(output, Contains("="));
28 }
29
30 SECTION("with server") {
31 Server server{MEMCACHED_BINARY, {"-p", random_port_string}};
32 MemcachedPtr memc;
33 LoneReturnMatcher test{*memc};
34
35 REQUIRE(server.ensureListening());
36 auto port = get<int>(server.getSocketOrPort());
37 auto comm = "memrm --servers=localhost:" + to_string(port) + " ";
38
39 REQUIRE_SUCCESS(memcached_server_add(*memc, "localhost", port));
40
41 SECTION("not found") {
42 string output;
43 REQUIRE(sh.run(comm + "-v key1", output));
44 REQUIRE_THAT(output, Contains("Could not find key \"key1\""));
45 }
46
47 SECTION("okay") {
48
49 REQUIRE_SUCCESS(memcached_set(*memc, S("key1"), S("val1"), 0, 0));
50 REQUIRE_SUCCESS(memcached_set(*memc, S("key2"), S("val2"), 0, 0));
51
52 this_thread::sleep_for(500ms);
53
54 string output;
55 REQUIRE(sh.run(comm + "key1", output));
56 REQUIRE(output.empty());
57
58 memcached_return_t rc;
59 REQUIRE(nullptr == memcached_get(*memc, S("key1"), nullptr, nullptr, &rc));
60 REQUIRE_RC(MEMCACHED_NOTFOUND, rc);
61 Malloced val(memcached_get(*memc, S("key2"), nullptr, nullptr, &rc));
62 REQUIRE(*val);
63 REQUIRE_SUCCESS(rc);
64 }
65
66 SECTION("connection failure") {
67 server.signal(SIGKILL);
68 server.wait();
69
70 string output;
71 REQUIRE_FALSE(sh.run(comm + " -v key2", output));
72 REQUIRE_THAT(output,
73 Contains("CONNECTION FAILURE")
74 || Contains("SERVER HAS FAILED")
75 || Contains("SYSTEM ERROR")
76 || Contains("TIMEOUT OCCURRED"));
77 }
78 }
79 }