bin: consolidate clients
[m6w6/libmemcached] / test / tests / bin / memcp.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/memcp") {
10 Shell sh{string{TESTING_ROOT "/../src/bin"}};
11
12 SECTION("no servers provided") {
13 string output;
14 REQUIRE_FALSE(sh.run("memcp nonexistent", output));
15 REQUIRE(output == "No servers provided.\n");
16 }
17
18 SECTION("--help") {
19 string output;
20 REQUIRE(sh.run("memcp --help", output));
21 REQUIRE_THAT(output, Contains("memcp v1"));
22 REQUIRE_THAT(output, Contains("Usage:"));
23 REQUIRE_THAT(output, Contains("file [file ...]"));
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, {"-U", 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 = "memcp --servers=localhost:" + to_string(port) + " ";
39
40 REQUIRE_SUCCESS(memcached_server_add(*memc, "localhost", port));
41
42 SECTION("okay") {
43 auto udp_buffer = GENERATE(0,1,2);
44 auto binary = GENERATE(0,1);
45 auto set_add_replace = GENERATE(0,1,2);
46 auto expire = GENERATE(0, random_num(10,12345));
47 string set_add_replace_s[3] = {
48 "set", "add", "replace"
49 };
50
51 DYNAMIC_SECTION("udp=" << (udp_buffer==1) <<" buffer=" << (udp_buffer==2) << " binary=" << binary << " mode=" << set_add_replace_s[set_add_replace] << " expire=" << expire) {
52 Tempfile temp;
53 temp.put(S("123"));
54
55 if (udp_buffer == 1) {
56 comm += " --udp ";
57 } else if (udp_buffer == 2) {
58 comm += " --buffer ";
59 }
60 if(binary) {
61 comm += " --binary ";
62 }
63 if (expire) {
64 comm += " --expire " + to_string(expire) + " ";
65 }
66 switch (set_add_replace) {
67 case 2:
68 comm += " --replace ";
69 REQUIRE_SUCCESS(memcached_set(*memc, S(temp.getFn()), S("foo"), 0, 0));
70 break;
71 case 1:
72 comm += " --add ";
73 }
74
75 INFO(comm);
76 string output;
77 auto ok = sh.run(comm + temp.getFn(), output);
78 REQUIRE(output == "");
79 REQUIRE(ok);
80
81 Retry settled{[&memc, &temp]{
82 size_t len;
83 memcached_return_t rc;
84 Malloced val(memcached_get(*memc, S(temp.getFn()), &len, nullptr, &rc));
85
86 return MEMCACHED_SUCCESS == rc && *val && string(*val, len) == "123";
87 }};
88 REQUIRE(settled());
89 }
90 }
91
92 SECTION("connection failure") {
93 server.signal(SIGKILL);
94 server.wait();
95
96 Tempfile temp;
97
98 string output;
99 REQUIRE_FALSE(sh.run(comm + temp.getFn(), output));
100 REQUIRE_THAT(output,
101 Contains("CONNECTION FAILURE")
102 || Contains("SERVER HAS FAILED")
103 || Contains("SYSTEM ERROR")
104 || Contains("TIMEOUT OCCURRED"));
105 }
106
107 SECTION("file not found") {
108 string output;
109 REQUIRE_FALSE(sh.run(comm + "nonexistent", output));
110 REQUIRE_THAT(output, Contains("No such file or directory"));
111 }
112 }
113 }