c6499777fe2171be1e3ef8709a7061df92c9df90
[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 v1"));
22 REQUIRE_THAT(output, Contains("Usage:"));
23 REQUIRE_THAT(output, Contains("[stat ...]"));
24 REQUIRE_THAT(output, Contains("Options:"));
25 REQUIRE_THAT(output, Contains("-h|--help"));
26 REQUIRE_THAT(output, Contains("-V|--version"));
27 REQUIRE_THAT(output, Contains("Environment:"));
28 REQUIRE_THAT(output, Contains("MEMCACHED_SERVERS"));
29 }
30
31 SECTION("with server") {
32 Server server{MEMCACHED_BINARY, {"-p", random_port_string}};
33 MemcachedPtr memc;
34 LoneReturnMatcher test{*memc};
35
36 REQUIRE(server.ensureListening());
37 auto port = get<int>(server.getSocketOrPort());
38 auto comm = "memstat --servers=localhost:" + to_string(port)
39 + ",localhost:" + to_string(port) + " ";
40
41 REQUIRE_SUCCESS(memcached_server_add(*memc, "localhost", port));
42
43 SECTION("okay") {
44 SECTION("version") {
45 REQUIRE_SUCCESS(memcached_version(*memc));
46 auto inst = memcached_server_instance_by_position(*memc, 0);
47
48 string output;
49 REQUIRE(sh.run(comm + "--server-version", output));
50 REQUIRE_THAT(output, Contains("localhost:" + to_string(port) + " "
51 + to_string(memcached_server_major_version(inst)) + "."
52 + to_string(memcached_server_minor_version(inst)) + "."
53 + to_string(memcached_server_micro_version(inst))));
54 }
55 SECTION("analyze") {
56 string output;
57 REQUIRE(sh.run(comm + "--analyze", output));
58 REQUIRE_THAT(output, Contains("Number of Servers Analyzed : 2"));
59 }
60 SECTION("analyze=latency") {
61 string output;
62 REQUIRE(sh.run(comm + "--analyze=latency", output));
63 REQUIRE_THAT(output, Contains("Network Latency Test:"));
64 }
65 }
66
67 SECTION("connection failure") {
68 server.signal(SIGKILL);
69 server.wait();
70
71 string output;
72 REQUIRE_FALSE(sh.run(comm + "--analyze", output));
73 REQUIRE_THAT(output,
74 Contains("CONNECTION FAILURE")
75 || Contains("SERVER HAS FAILED")
76 || Contains("SYSTEM ERROR")
77 || Contains("TIMEOUT OCCURRED"));
78 }
79 }
80 }