431d1ce7552911113e5ca7ff0ad8f522f46c7638
[m6w6/libmemcached] / test / tests / bin / memstat.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/memstat") {
10 Shell sh{string{TESTING_ROOT "/../src/bin"}};
11
12 SECTION("no servers provided") {
13 string output;
14 REQUIRE_FALSE(sh.run("memstat", output));
15 REQUIRE(output == "No Servers provided\n");
16 }
17
18 SECTION("--help") {
19 string output;
20 REQUIRE(sh.run("memstat --help", output));
21 REQUIRE_THAT(output, Contains("memstat"));
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 = "memstat --servers=localhost:" + to_string(port)
38 + ",localhost:" + to_string(port) + " ";
39
40 REQUIRE_SUCCESS(memcached_server_add(*memc, "localhost", port));
41
42 SECTION("okay") {
43 SECTION("version") {
44 REQUIRE_SUCCESS(memcached_version(*memc));
45 auto inst = memcached_server_instance_by_position(*memc, 0);
46
47 string output;
48 REQUIRE(sh.run(comm + "--server-version", output));
49 REQUIRE_THAT(output, Contains("localhost:" + to_string(port) + " "
50 + to_string(memcached_server_major_version(inst)) + "."
51 + to_string(memcached_server_minor_version(inst)) + "."
52 + to_string(memcached_server_micro_version(inst))));
53 }
54 SECTION("analyze") {
55 string output;
56 REQUIRE(sh.run(comm + "--analyze", output));
57 REQUIRE_THAT(output, Contains("Number of Servers Analyzed : 2"));
58 }
59 }
60
61 SECTION("connection failure") {
62 server.signal(SIGKILL);
63 server.wait();
64
65 string output;
66 REQUIRE_FALSE(sh.run(comm + "--analyze", output));
67 REQUIRE_THAT(output,
68 Contains("CONNECTION FAILURE")
69 || Contains("SERVER HAS FAILED")
70 || Contains("SYSTEM ERROR")
71 || Contains("TIMEOUT OCCURRED"));
72 }
73 }
74 }