]> git.proxmox.com Git - ceph.git/blame - ceph/src/seastar/tests/unit/file_utils_test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / seastar / tests / unit / file_utils_test.cc
CommitLineData
f67539c2
TL
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19/*
20 * Copyright (C) 2020 ScyllaDB
21 */
22
23#include <stdlib.h>
24
25#include <seastar/testing/test_case.hh>
26#include <seastar/testing/thread_test_case.hh>
27#include <seastar/testing/test_runner.hh>
28
29#include <seastar/core/file.hh>
30#include <seastar/core/seastar.hh>
31#include <seastar/core/print.hh>
32#include <seastar/core/loop.hh>
33#include <seastar/util/tmp_file.hh>
34#include <seastar/util/file.hh>
35
36using namespace seastar;
37namespace fs = std::filesystem;
38
39class expected_exception : std::runtime_error {
40public:
41 expected_exception() : runtime_error("expected") {}
42};
43
44SEASTAR_TEST_CASE(test_make_tmp_file) {
45 return make_tmp_file().then([] (tmp_file tf) {
46 return async([tf = std::move(tf)] () mutable {
47 const sstring tmp_path = tf.get_path().native();
48 BOOST_REQUIRE(file_exists(tmp_path).get0());
49 tf.close().get();
50 tf.remove().get();
51 BOOST_REQUIRE(!file_exists(tmp_path).get0());
52 });
53 });
54}
55
56static temporary_buffer<char> get_init_buffer(file& f) {
57 auto buf = temporary_buffer<char>::aligned(f.memory_dma_alignment(), f.memory_dma_alignment());
58 memset(buf.get_write(), 0, buf.size());
59 return buf;
60}
61
62SEASTAR_THREAD_TEST_CASE(test_tmp_file) {
63 size_t expected = ~0;
64 size_t actual = 0;
65
66 tmp_file::do_with([&] (tmp_file& tf) mutable {
67 auto& f = tf.get_file();
68 auto buf = get_init_buffer(f);
69 return do_with(std::move(buf), [&] (auto& buf) mutable {
70 expected = buf.size();
71 return f.dma_write(0, buf.get(), buf.size()).then([&] (size_t written) {
72 actual = written;
73 return make_ready_future<>();
74 });
75 });
76 }).get();
77 BOOST_REQUIRE_EQUAL(expected , actual);
78}
79
80SEASTAR_THREAD_TEST_CASE(test_non_existing_TMPDIR) {
f67539c2
TL
81 BOOST_REQUIRE_EXCEPTION(tmp_file::do_with("/tmp/non-existing-TMPDIR", [] (tmp_file& tf) {}).get(),
82 std::system_error, testing::exception_predicate::message_contains("No such file or directory"));
f67539c2
TL
83}
84
85static future<> touch_file(const sstring& filename, open_flags oflags = open_flags::rw | open_flags::create) noexcept {
86 return open_file_dma(filename, oflags).then([] (file f) {
87 return f.close().finally([f] {});
88 });
89}
90
91SEASTAR_THREAD_TEST_CASE(test_recursive_remove_directory) {
92 struct test_dir {
93 test_dir *parent;
94 sstring name;
95 std::list<sstring> sub_files = {};
96 std::list<test_dir> sub_dirs = {};
97
98 test_dir(test_dir* parent, sstring name)
99 : parent(parent)
100 , name(std::move(name))
101 { }
102
103 fs::path path() const {
104 if (!parent) {
105 return fs::path(name.c_str());
106 }
107 return parent->path() / name.c_str();
108 }
109
110 void fill_random_file(std::uniform_int_distribution<unsigned>& dist, std::default_random_engine& eng) {
111 sub_files.emplace_back(format("file-{}", dist(eng)));
112 }
113
114 test_dir& fill_random_dir(std::uniform_int_distribution<unsigned>& dist, std::default_random_engine& eng) {
115 sub_dirs.emplace_back(this, format("dir-{}", dist(eng)));
116 return sub_dirs.back();
117 }
118
119 void random_fill(int level, int levels, std::uniform_int_distribution<unsigned>& dist, std::default_random_engine& eng) {
120 int num_files = dist(eng) % 10;
121 int num_dirs = (level < levels - 1) ? (1 + dist(eng) % 3) : 0;
122
123 for (int i = 0; i < num_files; i++) {
124 fill_random_file(dist, eng);
125 }
126
127 if (num_dirs) {
128 level++;
129 for (int i = 0; i < num_dirs; i++) {
130 fill_random_dir(dist, eng).random_fill(level, levels, dist, eng);
131 }
132 }
133 }
134
135 future<> populate() {
136 return touch_directory(path().native()).then([this] {
137 return parallel_for_each(sub_files, [this] (auto& name) {
138 return touch_file((path() / name.c_str()).native());
139 }).then([this] {
140 return parallel_for_each(sub_dirs, [] (auto& sub_dir) {
141 return sub_dir.populate();
142 });
143 });
144 });
145 }
146 };
147
148 auto& eng = testing::local_random_engine;
149 auto dist = std::uniform_int_distribution<unsigned>();
150 int levels = 1 + dist(eng) % 3;
151 test_dir root = { nullptr, default_tmpdir().native() };
152 test_dir base = { &root, format("base-{}", dist(eng)) };
153 base.random_fill(0, levels, dist, eng);
154 base.populate().get();
155 recursive_remove_directory(base.path()).get();
156 BOOST_REQUIRE(!file_exists(base.path().native()).get0());
157}
158
159SEASTAR_TEST_CASE(test_make_tmp_dir) {
160 return make_tmp_dir().then([] (tmp_dir td) {
161 return async([td = std::move(td)] () mutable {
162 const sstring tmp_path = td.get_path().native();
163 BOOST_REQUIRE(file_exists(tmp_path).get0());
164 td.remove().get();
165 BOOST_REQUIRE(!file_exists(tmp_path).get0());
166 });
167 });
168}
169
170SEASTAR_THREAD_TEST_CASE(test_tmp_dir) {
171 size_t expected;
172 size_t actual;
173 tmp_dir::do_with([&] (tmp_dir& td) {
174 return tmp_file::do_with(td.get_path(), [&] (tmp_file& tf) {
175 auto& f = tf.get_file();
176 auto buf = get_init_buffer(f);
177 return do_with(std::move(buf), [&] (auto& buf) mutable {
178 expected = buf.size();
179 return f.dma_write(0, buf.get(), buf.size()).then([&] (size_t written) {
180 actual = written;
181 return make_ready_future<>();
182 });
183 });
184 });
185 }).get();
186 BOOST_REQUIRE_EQUAL(expected , actual);
187}
188
189SEASTAR_THREAD_TEST_CASE(test_tmp_dir_with_path) {
190 size_t expected;
191 size_t actual;
192 tmp_dir::do_with(".", [&] (tmp_dir& td) {
193 return tmp_file::do_with(td.get_path(), [&] (tmp_file& tf) {
194 auto& f = tf.get_file();
195 auto buf = get_init_buffer(f);
196 return do_with(std::move(buf), [&] (auto& buf) mutable {
197 expected = buf.size();
198 return tf.get_file().dma_write(0, buf.get(), buf.size()).then([&] (size_t written) {
199 actual = written;
200 return make_ready_future<>();
201 });
202 });
203 });
204 }).get();
205 BOOST_REQUIRE_EQUAL(expected , actual);
206}
207
208SEASTAR_THREAD_TEST_CASE(test_tmp_dir_with_non_existing_path) {
209 BOOST_REQUIRE_EXCEPTION(tmp_dir::do_with("/tmp/this_name_should_not_exist", [] (tmp_dir&) {}).get(),
210 std::system_error, testing::exception_predicate::message_contains("No such file or directory"));
211}
212
213SEASTAR_TEST_CASE(tmp_dir_with_thread_test) {
214 return tmp_dir::do_with_thread([] (tmp_dir& td) {
215 tmp_file tf = make_tmp_file(td.get_path()).get0();
216 auto& f = tf.get_file();
217 auto buf = get_init_buffer(f);
218 auto expected = buf.size();
219 auto actual = f.dma_write(0, buf.get(), buf.size()).get0();
220 BOOST_REQUIRE_EQUAL(expected, actual);
221 tf.close().get();
222 tf.remove().get();
223 });
224}
225
226SEASTAR_TEST_CASE(tmp_dir_with_leftovers_test) {
227 return tmp_dir::do_with_thread([] (tmp_dir& td) {
228 fs::path path = td.get_path() / "testfile.tmp";
229 touch_file(path.native()).get();
230 BOOST_REQUIRE(file_exists(path.native()).get0());
231 });
232}
233
234SEASTAR_TEST_CASE(tmp_dir_do_with_fail_func_test) {
235 return tmp_dir::do_with_thread([] (tmp_dir& outer) {
236 BOOST_REQUIRE_THROW(tmp_dir::do_with([] (tmp_dir& inner) mutable {
237 return make_exception_future<>(expected_exception());
238 }).get(), expected_exception);
239 });
240}
241
242SEASTAR_TEST_CASE(tmp_dir_do_with_fail_remove_test) {
243 return tmp_dir::do_with_thread([] (tmp_dir& outer) {
244 auto saved_default_tmpdir = default_tmpdir();
245 sstring outer_path = outer.get_path().native();
246 sstring inner_path;
20effc67 247 sstring inner_path_renamed;
f67539c2 248 set_default_tmpdir(outer_path.c_str());
20effc67 249 BOOST_REQUIRE_THROW(tmp_dir::do_with([&] (tmp_dir& inner) mutable {
f67539c2 250 inner_path = inner.get_path().native();
20effc67
TL
251 inner_path_renamed = inner_path + ".renamed";
252 return rename_file(inner_path, inner_path_renamed);
f67539c2 253 }).get(), std::system_error);
20effc67
TL
254 BOOST_REQUIRE(!file_exists(inner_path).get0());
255 BOOST_REQUIRE(file_exists(inner_path_renamed).get0());
f67539c2
TL
256 set_default_tmpdir(saved_default_tmpdir.c_str());
257 });
258}
259
260SEASTAR_TEST_CASE(tmp_dir_do_with_thread_fail_func_test) {
261 return tmp_dir::do_with_thread([] (tmp_dir& outer) {
262 BOOST_REQUIRE_THROW(tmp_dir::do_with_thread([] (tmp_dir& inner) mutable {
263 throw expected_exception();
264 }).get(), expected_exception);
265 });
266}
267
268SEASTAR_TEST_CASE(tmp_dir_do_with_thread_fail_remove_test) {
269 return tmp_dir::do_with_thread([] (tmp_dir& outer) {
270 auto saved_default_tmpdir = default_tmpdir();
271 sstring outer_path = outer.get_path().native();
272 sstring inner_path;
20effc67 273 sstring inner_path_renamed;
f67539c2 274 set_default_tmpdir(outer_path.c_str());
20effc67 275 BOOST_REQUIRE_THROW(tmp_dir::do_with_thread([&] (tmp_dir& inner) mutable {
f67539c2 276 inner_path = inner.get_path().native();
20effc67
TL
277 inner_path_renamed = inner_path + ".renamed";
278 return rename_file(inner_path, inner_path_renamed);
f67539c2 279 }).get(), std::system_error);
20effc67
TL
280 BOOST_REQUIRE(!file_exists(inner_path).get0());
281 BOOST_REQUIRE(file_exists(inner_path_renamed).get0());
f67539c2
TL
282 set_default_tmpdir(saved_default_tmpdir.c_str());
283 });
284}
20effc67
TL
285
286SEASTAR_TEST_CASE(test_read_entire_file_contiguous) {
287 return tmp_file::do_with([] (tmp_file& tf) {
288 return async([&tf] {
289 file& f = tf.get_file();
290 auto& eng = testing::local_random_engine;
291 auto dist = std::uniform_int_distribution<unsigned>();
292 size_t size = f.memory_dma_alignment() * (1 + dist(eng) % 1000);
293 auto wbuf = temporary_buffer<char>::aligned(f.memory_dma_alignment(), size);
294 for (size_t i = 0; i < size; i++) {
295 static char chars[] = "abcdefghijklmnopqrstuvwxyz0123456789";
296 wbuf.get_write()[i] = chars[dist(eng) % sizeof(chars)];
297 }
298
299 BOOST_REQUIRE_EQUAL(f.dma_write(0, wbuf.begin(), wbuf.size()).get0(), wbuf.size());
300 f.flush().get();
301
302 sstring res = util::read_entire_file_contiguous(tf.get_path()).get0();
303 BOOST_REQUIRE_EQUAL(res, std::string_view(wbuf.begin(), wbuf.size()));
304 });
305 });
306}