testing: fix osx
[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"));
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 server.start();
36 Retry{[&server] { return server.isListening(); }}();
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 Tempfile temp;
44 temp.put(S("123"));
45
46 string output;
47 REQUIRE(sh.run(comm + temp.getFn(), output));
48 REQUIRE(output == "");
49
50 size_t len;
51 memcached_return_t rc;
52 Malloced val(memcached_get(*memc, S(temp.getFn()), &len, nullptr, &rc));
53
54 REQUIRE(*val);
55 REQUIRE_SUCCESS(rc);
56 REQUIRE(string(*val, len) == "123");
57 }
58
59 SECTION("connection failure") {
60 server.signal(SIGKILL);
61 server.wait();
62
63 Tempfile temp;
64
65 string output;
66 REQUIRE_FALSE(sh.run(comm + temp.getFn(), output));
67 REQUIRE_THAT(output, Contains("CONNECTION FAILURE"));
68 }
69
70 SECTION("file not found") {
71 string output;
72 REQUIRE_FALSE(sh.run(comm + "nonexistent", output));
73 REQUIRE_THAT(output, Contains("No such file or directory"));
74 }
75 }
76 }