File: posc_tests.cc

package info (click to toggle)
xrootd-s3-http 0.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 684 kB
  • sloc: cpp: 9,345; sh: 608; makefile: 13
file content (334 lines) | stat: -rw-r--r-- 11,114 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/***************************************************************
 *
 * Copyright (C) 2025, Pelican Project, Morgridge Institute for Research
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************/

#include "../src/Posc.hh"
#include "../src/shortfile.hh"

#include <XrdOss/XrdOssDefaultSS.hh>
#include <XrdOuc/XrdOucEnv.hh>
#include <XrdSys/XrdSysLogger.hh>
#include <XrdVersion.hh>

#include <gtest/gtest.h>

#include <chrono>
#include <fcntl.h>
#include <string>
#include <thread>

class TestPosc : public testing::Test {
  protected:
	virtual std::string GetConfig() {
		std::stringstream ss;
		ss << "oss.localroot " << m_temp_dir << "\n";
		ss << "posc.prefix "
		   << "/posc_test\n"
			  "posc.trace debug\n";
		return ss.str();
	}

	void SetUp() override {
		setenv("XRDINSTANCE", "xrootd", 1);
		char tmp_configfn[] = "/tmp/xrootd-gtest.cfg.XXXXXX";
		auto result = mkstemp(tmp_configfn);
		ASSERT_NE(result, -1) << "Failed to create temp file ("
							  << strerror(errno) << ", errno=" << errno << ")";
		m_configfn = std::string(tmp_configfn);

		auto temp_dir = std::filesystem::temp_directory_path() /
						"gtest_temp_xrootd_localroot"; // Generate a unique name
		std::error_code ec;
		if (!std::filesystem::create_directory(temp_dir, ec)) {
			ASSERT_FALSE(ec)
				<< "Failed to create temp directory: " << ec.message();
		}
		m_temp_dir = temp_dir.string();

		auto contents = GetConfig();
		ASSERT_FALSE(contents.empty());
		ASSERT_TRUE(writeShortFile(m_configfn, contents, 0))
			<< "Failed to write to temp file (" << strerror(errno)
			<< ", errno=" << errno << ")";
	}

	void TearDown() override {
		if (!m_configfn.empty()) {
			auto rv = unlink(m_configfn.c_str());
			ASSERT_EQ(rv, 0) << "Failed to delete temp file ("
							 << strerror(errno) << ", errno=" << errno << ")";
		}
		if (!m_temp_dir.empty()) {
			std::error_code ec;
			auto rv = std::filesystem::remove_all(m_temp_dir, ec);
			ASSERT_NE(rv, -1)
				<< "Failed to delete temp directory (" << ec.message()
				<< ", errno=" << ec.value() << ")";
		}
	}

	std::string GetConfigFile() const { return m_configfn; }

  private:
	std::string m_temp_dir;
	std::string m_configfn;
};

TEST_F(TestPosc, BasicFileVisibility) {
	XrdSysLogger logger(2, 0);
	XrdOucEnv env;

	XrdVERSIONINFODEF(XrdOssDefault, Oss, XrdVNUMBER, XrdVERSION);

	XrdOss *default_oss =
		XrdOssDefaultSS(&logger, GetConfigFile().c_str(), XrdOssDefault);

	ASSERT_NE(default_oss, nullptr) << "Failed to get Posc OSS instance";

	std::unique_ptr<XrdSysError> log(new XrdSysError(&logger, "posc_"));
	PoscFileSystem *posc_fs_raw;
	try {
		posc_fs_raw = new PoscFileSystem(default_oss, std::move(log),
										 GetConfigFile().c_str(), &env);
	} catch (const std::exception &e) {
		FAIL() << "Failed to create PoscFileSystem: " << e.what();
	}
	std::unique_ptr<PoscFileSystem> posc_fs(posc_fs_raw);

	std::unique_ptr<XrdOssDF> fp(posc_fs->newFile());
	ASSERT_NE(fp, nullptr) << "Failed to create new file object";

	fp->Open("/testfile.txt", O_CREAT | O_RDWR, 0644, env);

	auto rv = posc_fs->Stat("/testfile.txt", nullptr, 0, &env);
	ASSERT_NE(rv, 0) << "Temporary file is visible";
	ASSERT_EQ(rv, -ENOENT)
		<< "Stat on not-visible file should have resulted in ENOENT: "
		<< strerror(-rv);

	ASSERT_EQ(fp->Close(), 0) << "Failed to close file";

	struct stat sb;
	rv = posc_fs->Stat("/testfile.txt", &sb, 0, &env);

	ASSERT_EQ(rv, 0) << "Stat on created file failed: " << strerror(-rv);
	ASSERT_TRUE(S_ISREG(sb.st_mode))
		<< "Stat on created file did not return regular file";
	ASSERT_EQ(sb.st_size, 0) << "Stat on created file did not return size 0";

	fp->Open("/testfile2.txt", O_CREAT | O_RDWR, 0644, env);
	auto write_size = strlen("Hello, POSC!");
	ASSERT_EQ(fp->Write("Hello, POSC!", 0, write_size), write_size);

	rv = posc_fs->Stat("/testfile2.txt", nullptr, 0, &env);
	ASSERT_NE(rv, 0) << "Temporary file is visible";
	ASSERT_EQ(rv, -ENOENT)
		<< "Stat on not-visible file should have resulted in ENOENT: "
		<< strerror(-rv);

	ASSERT_EQ(fp->Close(), 0) << "Failed to close file";

	memset(&sb, '\0', sizeof(sb));
	rv = posc_fs->Stat("/testfile2.txt", &sb, 0, &env);

	ASSERT_EQ(rv, 0) << "Stat on created file failed: " << strerror(-rv);
	ASSERT_TRUE(S_ISREG(sb.st_mode))
		<< "Stat on created file did not return regular file";
	ASSERT_EQ(sb.st_size, write_size)
		<< "Stat on created file did not return size " << write_size;
}

TEST_F(TestPosc, BasicFilesystemVisibility) {
	XrdSysLogger logger(2, 0);
	XrdOucEnv env;

	XrdVERSIONINFODEF(XrdOssDefault, Oss, XrdVNUMBER, XrdVERSION);

	XrdOss *default_oss =
		XrdOssDefaultSS(&logger, GetConfigFile().c_str(), XrdOssDefault);

	ASSERT_NE(default_oss, nullptr) << "Failed to get Posc OSS instance";

	std::unique_ptr<XrdSysError> log(new XrdSysError(&logger, "posc_"));
	PoscFileSystem *posc_fs_raw;
	try {
		posc_fs_raw = new PoscFileSystem(default_oss, std::move(log),
										 GetConfigFile().c_str(), &env);
	} catch (const std::exception &e) {
		FAIL() << "Failed to create PoscFileSystem: " << e.what();
	}
	ASSERT_NE(posc_fs_raw, nullptr) << "Failed to allocate filesystem object";
	std::unique_ptr<PoscFileSystem> posc_fs(posc_fs_raw);

	// Should not be able to stat the POSC directory
	struct stat buff;
	auto rv = posc_fs->Stat("/posc_test", &buff);
	ASSERT_NE(rv, 0) << "Expected stat of hidden directory to fail";
	ASSERT_EQ(rv, -ENOENT)
		<< "Expected stat of hidden directory to fail with ENOENT; got "
		<< strerror(errno);

	// Should not be able to create the POSC directory
	rv = posc_fs->Mkdir("/posc_test/foo", 0755, 1, &env);
	ASSERT_NE(rv, 0) << "Expected mkdir inside POSC dir to fail";
	ASSERT_EQ(rv, -EIO)
		<< "Expected mkdir inside POSC dir to result in an EIO.  Got "
		<< strerror(errno);

	// Listing of the parent directory should not contain the POSC directory
	auto dp_raw = posc_fs->newDir();
	ASSERT_NE(dp_raw, nullptr);
	std::unique_ptr<XrdOssDF> dp(dp_raw);
	rv = dp->Opendir("/", env);
	ASSERT_EQ(rv, 0) << "Failed to open root directory";
	char fname[NAME_MAX];
	while (((rv = dp->Readdir(fname, NAME_MAX)) == 0) && (fname[0] != '\0')) {
		fprintf(stderr, "File component: %s\n", fname);
	}
	ASSERT_EQ(rv, 0) << "Failed to list directory: " << strerror(-rv);
	ASSERT_EQ(dp->Close(), 0) << "Failed to close root directory";
}

TEST_F(TestPosc, TempfileUpdate) {
	XrdSysLogger logger(2, 0);
	XrdOucEnv env;

	XrdVERSIONINFODEF(XrdOssDefault, Oss, XrdVNUMBER, XrdVERSION);

	XrdOss *default_oss =
		XrdOssDefaultSS(&logger, GetConfigFile().c_str(), XrdOssDefault);

	ASSERT_NE(default_oss, nullptr) << "Failed to get Posc OSS instance";

	std::unique_ptr<XrdSysError> log(new XrdSysError(&logger, "posc_"));
	PoscFileSystem *posc_fs_raw;
	try {
		posc_fs_raw = new PoscFileSystem(default_oss, std::move(log),
										 GetConfigFile().c_str(), &env);
	} catch (const std::exception &e) {
		FAIL() << "Failed to create PoscFileSystem: " << e.what();
	}
	std::unique_ptr<PoscFileSystem> posc_fs(posc_fs_raw);

	std::unique_ptr<XrdOssDF> fp(posc_fs->newFile());
	ASSERT_NE(fp, nullptr) << "Failed to create new file object";

	fp->Open("/testfile.txt", O_CREAT | O_RDWR, 0644, env);

	auto pfp = dynamic_cast<PoscFile *>(fp.get());
	ASSERT_NE(fp, nullptr)
		<< "Returned file pointer should be of type PoscFile";

	auto posc_filename = pfp->GetPoscFilename();
	ASSERT_FALSE(posc_filename.empty())
		<< "POSC file is not opened in POSC mode";

	struct stat buff;
	auto rv = default_oss->Stat(posc_filename.c_str(), &buff, 0, &env);
	ASSERT_EQ(rv, 0) << "Failed to stat underlying POSC file";

#ifdef __APPLE__
	struct timespec now = buff.st_mtimespec;
#else
	struct timespec now = buff.st_mtim;
#endif

	auto now_chrono = std::chrono::time_point<std::chrono::system_clock,
											  std::chrono::nanoseconds>(
		std::chrono::seconds(now.tv_sec) +
		std::chrono::nanoseconds(now.tv_nsec));

	PoscFile::SetFileUpdateDuration(std::chrono::nanoseconds(100));

	std::this_thread::sleep_for(std::chrono::milliseconds(1500));

	PoscFile::UpdateOpenFiles();

	memset(&buff, '\0', sizeof(buff));
	rv = default_oss->Stat(posc_filename.c_str(), &buff, 0, &env);
	ASSERT_EQ(rv, 0) << "Failed to stat underlying POSC file";

#ifdef __APPLE__
	now = buff.st_mtimespec;
#else
	now = buff.st_mtim;
#endif

	auto update_chrono = std::chrono::time_point<std::chrono::system_clock,
												 std::chrono::nanoseconds>(
		std::chrono::seconds(now.tv_sec) +
		std::chrono::nanoseconds(now.tv_nsec));

	ASSERT_TRUE(update_chrono > now_chrono)
		<< "POSC file mtime was not updated after UpdateOpenFiles call";

	// Try expiring the user files - shouldn't do anything as the default
	// timeout is quite large.
	posc_fs->ExpireFiles();

	rv = default_oss->Stat(posc_filename.c_str(), &buff, 0, &env);
	ASSERT_EQ(rv, 0) << "Failed to stat underlying POSC file";

	// Decrease the file expiration time, sleep, and then expire again.
	PoscFileSystem::SetFileTimeout(std::chrono::nanoseconds(100));
	std::this_thread::sleep_for(std::chrono::milliseconds(1));

	posc_fs->ExpireFiles();

	rv = default_oss->Stat(posc_filename.c_str(), &buff, 0, &env);
	ASSERT_NE(rv, 0) << "Expire failed to delete underlying user file";
	ASSERT_EQ(rv, -ENOENT) << "Unexpected error when stating POSC file: "
						   << strerror(errno);
}

TEST_F(TestPosc, CreateENOENT) {
	XrdSysLogger logger(2, 0);
	XrdOucEnv env;

	XrdVERSIONINFODEF(XrdOssDefault, Oss, XrdVNUMBER, XrdVERSION);

	XrdOss *default_oss =
		XrdOssDefaultSS(&logger, GetConfigFile().c_str(), XrdOssDefault);

	ASSERT_NE(default_oss, nullptr) << "Failed to get Posc OSS instance";

	std::unique_ptr<XrdSysError> log(new XrdSysError(&logger, "posc_"));
	PoscFileSystem *posc_fs_raw;
	try {
		posc_fs_raw = new PoscFileSystem(default_oss, std::move(log),
										 GetConfigFile().c_str(), &env);
	} catch (const std::exception &e) {
		FAIL() << "Failed to create PoscFileSystem: " << e.what();
	}
	std::unique_ptr<PoscFileSystem> posc_fs(posc_fs_raw);

	std::unique_ptr<XrdOssDF> fp(posc_fs->newFile());
	ASSERT_NE(fp, nullptr) << "Failed to create new file object";

	auto rv = fp->Open("/subdir/testfile.txt", O_CREAT | O_RDWR, 0644, env);
	ASSERT_NE(rv, 0) << "Open should have failed";
	ASSERT_EQ(rv, -ENOENT)
		<< "Open on non-existent path should have resulted in ENOENT: "
		<< strerror(-rv);

	posc_fs->Mkdir("/subdir", 0755, 1, &env);

	rv = fp->Open("/subdir/testfile.txt", O_CREAT | O_RDWR, 0644, env);
	ASSERT_EQ(rv, 0) << "Open on newly created directory failed: "
					 << strerror(-rv);
	ASSERT_EQ(fp->Close(), 0) << "Failed to close file";
}