]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/rgw/test_http_manager.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / test / rgw / test_http_manager.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2015 Red Hat
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14 #include "rgw/rgw_rados.h"
15 #include "rgw/rgw_http_client.h"
16 #include "global/global_init.h"
17 #include "common/ceph_argparse.h"
18 #include <curl/curl.h>
19 #include <boost/asio/ip/tcp.hpp>
20 #include <boost/asio/write.hpp>
21 #include <thread>
22 #include <gtest/gtest.h>
23
24 using namespace std;
25
26 TEST(HTTPManager, ReadTruncated)
27 {
28 using tcp = boost::asio::ip::tcp;
29 tcp::endpoint endpoint(tcp::v4(), 0);
30 boost::asio::io_context ioctx;
31 tcp::acceptor acceptor(ioctx);
32 acceptor.open(endpoint.protocol());
33 acceptor.bind(endpoint);
34 acceptor.listen();
35
36 std::thread server{[&] {
37 tcp::socket socket{ioctx};
38 acceptor.accept(socket);
39 std::string_view response =
40 "HTTP/1.1 200 OK\r\n"
41 "Content-Length: 1024\r\n"
42 "\r\n"
43 "short body";
44 boost::asio::write(socket, boost::asio::buffer(response));
45 }};
46 const auto url = std::string{"http://127.0.0.1:"} + std::to_string(acceptor.local_endpoint().port());
47
48 RGWHTTPClient client{g_ceph_context, "GET", url};
49 EXPECT_EQ(-EAGAIN, RGWHTTP::process(&client, null_yield));
50
51 server.join();
52 }
53
54 TEST(HTTPManager, Head)
55 {
56 using tcp = boost::asio::ip::tcp;
57 tcp::endpoint endpoint(tcp::v4(), 0);
58 boost::asio::io_context ioctx;
59 tcp::acceptor acceptor(ioctx);
60 acceptor.open(endpoint.protocol());
61 acceptor.bind(endpoint);
62 acceptor.listen();
63
64 std::thread server{[&] {
65 tcp::socket socket{ioctx};
66 acceptor.accept(socket);
67 std::string_view response =
68 "HTTP/1.1 200 OK\r\n"
69 "Content-Length: 1024\r\n"
70 "\r\n";
71 boost::asio::write(socket, boost::asio::buffer(response));
72 }};
73 const auto url = std::string{"http://127.0.0.1:"} + std::to_string(acceptor.local_endpoint().port());
74
75 RGWHTTPClient client{g_ceph_context, "HEAD", url};
76 EXPECT_EQ(0, RGWHTTP::process(&client, null_yield));
77
78 server.join();
79 }
80
81 TEST(HTTPManager, SignalThread)
82 {
83 auto cct = g_ceph_context;
84 RGWHTTPManager http(cct);
85
86 ASSERT_EQ(0, http.start());
87
88 // default pipe buffer size according to man pipe
89 constexpr size_t max_pipe_buffer_size = 65536;
90 // each signal writes 4 bytes to the pipe
91 constexpr size_t max_pipe_signals = max_pipe_buffer_size / sizeof(uint32_t);
92 // add_request and unregister_request
93 constexpr size_t pipe_signals_per_request = 2;
94 // number of http requests to fill the pipe buffer
95 constexpr size_t max_requests = max_pipe_signals / pipe_signals_per_request;
96
97 // send one extra request to test that we don't deadlock
98 constexpr size_t num_requests = max_requests + 1;
99
100 for (size_t i = 0; i < num_requests; i++) {
101 RGWHTTPClient client{cct, "PUT", "http://127.0.0.1:80"};
102 http.add_request(&client);
103 }
104 }
105
106 int main(int argc, char** argv)
107 {
108 auto args = argv_to_vec(argc, argv);
109 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
110 CODE_ENVIRONMENT_UTILITY,
111 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
112 common_init_finish(g_ceph_context);
113
114 rgw_http_client_init(cct->get());
115 rgw_setup_saved_curl_handles();
116 ::testing::InitGoogleTest(&argc, argv);
117 int r = RUN_ALL_TESTS();
118 rgw_release_all_curl_handles();
119 rgw_http_client_cleanup();
120 return r;
121 }