2011-09-12 09:21:10 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
2019-04-18 17:51:19 +00:00
|
|
|
//
|
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
2011-09-12 09:21:10 +00:00
|
|
|
|
2020-06-22 20:25:41 +00:00
|
|
|
#include <algorithm>
|
2013-01-20 10:07:13 +00:00
|
|
|
#include <memory>
|
2011-09-12 09:21:10 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "env/mock_env.h"
|
2021-01-09 17:42:21 +00:00
|
|
|
#include "file/file_util.h"
|
2020-09-15 22:12:58 +00:00
|
|
|
#include "rocksdb/convenience.h"
|
2016-06-03 22:13:03 +00:00
|
|
|
#include "rocksdb/env.h"
|
2020-06-22 20:25:41 +00:00
|
|
|
#include "rocksdb/env_encryption.h"
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/testharness.h"
|
2016-06-03 22:13:03 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2021-07-16 14:57:47 +00:00
|
|
|
namespace {
|
2021-09-07 18:31:12 +00:00
|
|
|
using CreateEnvFunc = Env*();
|
|
|
|
|
2021-07-16 14:57:47 +00:00
|
|
|
// These functions are used to create the various environments under which this
|
|
|
|
// test can execute. These functions are used to allow the test cases to be
|
|
|
|
// created without the Env being initialized, thereby eliminating a potential
|
|
|
|
// static initialization fiasco/race condition when attempting to get a
|
|
|
|
// custom/configured env prior to main being invoked.
|
|
|
|
|
|
|
|
static Env* GetDefaultEnv() { return Env::Default(); }
|
|
|
|
|
|
|
|
static Env* GetMockEnv() {
|
|
|
|
static std::unique_ptr<Env> mock_env(new MockEnv(Env::Default()));
|
|
|
|
return mock_env.get();
|
|
|
|
}
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
static Env* NewTestEncryptedEnv(Env* base, const std::string& provider_id) {
|
|
|
|
ConfigOptions config_opts;
|
|
|
|
config_opts.invoke_prepare_options = false;
|
|
|
|
|
|
|
|
std::shared_ptr<EncryptionProvider> provider;
|
|
|
|
EXPECT_OK(EncryptionProvider::CreateFromString(config_opts, provider_id,
|
|
|
|
&provider));
|
|
|
|
return NewEncryptedEnv(base, provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Env* GetCtrEncryptedEnv() {
|
|
|
|
static std::unique_ptr<Env> ctr_encrypt_env(
|
|
|
|
NewTestEncryptedEnv(Env::Default(), "CTR://test"));
|
|
|
|
return ctr_encrypt_env.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Env* GetMemoryEnv() {
|
|
|
|
static std::unique_ptr<Env> mem_env(NewMemEnv(Env::Default()));
|
|
|
|
return mem_env.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Env* GetTestEnv() {
|
|
|
|
static std::shared_ptr<Env> env_guard;
|
|
|
|
static Env* custom_env = nullptr;
|
|
|
|
if (custom_env == nullptr) {
|
|
|
|
const char* uri = getenv("TEST_ENV_URI");
|
|
|
|
if (uri != nullptr) {
|
|
|
|
EXPECT_OK(Env::CreateFromUri(ConfigOptions(), uri, "", &custom_env,
|
|
|
|
&env_guard));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPECT_NE(custom_env, nullptr);
|
|
|
|
return custom_env;
|
|
|
|
}
|
2011-09-12 09:21:10 +00:00
|
|
|
|
2021-07-16 14:57:47 +00:00
|
|
|
static Env* GetTestFS() {
|
|
|
|
static std::shared_ptr<Env> fs_env_guard;
|
|
|
|
static Env* fs_env = nullptr;
|
|
|
|
if (fs_env == nullptr) {
|
|
|
|
const char* uri = getenv("TEST_FS_URI");
|
|
|
|
if (uri != nullptr) {
|
|
|
|
EXPECT_OK(
|
|
|
|
Env::CreateFromUri(ConfigOptions(), uri, "", &fs_env, &fs_env_guard));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPECT_NE(fs_env, nullptr);
|
|
|
|
return fs_env;
|
|
|
|
}
|
|
|
|
#endif // ROCKSDB_LITE
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
class EnvBasicTestWithParam
|
|
|
|
: public testing::Test,
|
|
|
|
public ::testing::WithParamInterface<CreateEnvFunc*> {
|
2011-09-12 09:21:10 +00:00
|
|
|
public:
|
|
|
|
Env* env_;
|
2013-06-07 22:35:17 +00:00
|
|
|
const EnvOptions soptions_;
|
2016-06-04 01:44:22 +00:00
|
|
|
std::string test_dir_;
|
2011-09-12 09:21:10 +00:00
|
|
|
|
2021-07-16 14:57:47 +00:00
|
|
|
EnvBasicTestWithParam() : env_(GetParam()()) {
|
2018-07-14 00:18:39 +00:00
|
|
|
test_dir_ = test::PerThreadDBPath(env_, "env_basic_test");
|
2016-06-04 01:44:22 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 17:42:21 +00:00
|
|
|
void SetUp() override { ASSERT_OK(env_->CreateDirIfMissing(test_dir_)); }
|
2016-07-01 01:33:49 +00:00
|
|
|
|
2021-01-09 17:42:21 +00:00
|
|
|
void TearDown() override { ASSERT_OK(DestroyDir(env_, test_dir_)); }
|
2011-09-12 09:21:10 +00:00
|
|
|
};
|
|
|
|
|
2016-06-22 23:16:21 +00:00
|
|
|
class EnvMoreTestWithParam : public EnvBasicTestWithParam {};
|
|
|
|
|
2020-06-03 22:53:09 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(EnvDefault, EnvBasicTestWithParam,
|
2021-07-16 14:57:47 +00:00
|
|
|
::testing::Values(&GetDefaultEnv));
|
2020-06-03 22:53:09 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(EnvDefault, EnvMoreTestWithParam,
|
2021-07-16 14:57:47 +00:00
|
|
|
::testing::Values(&GetDefaultEnv));
|
2016-07-01 01:34:29 +00:00
|
|
|
|
2020-06-03 22:53:09 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(MockEnv, EnvBasicTestWithParam,
|
2021-07-16 14:57:47 +00:00
|
|
|
::testing::Values(&GetMockEnv));
|
2020-06-22 20:25:41 +00:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
2020-09-15 22:12:58 +00:00
|
|
|
// next statements run env test against default encryption code.
|
2020-06-22 20:25:41 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(EncryptedEnv, EnvBasicTestWithParam,
|
2021-07-16 14:57:47 +00:00
|
|
|
::testing::Values(&GetCtrEncryptedEnv));
|
2020-06-22 20:25:41 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(EncryptedEnv, EnvMoreTestWithParam,
|
2021-07-16 14:57:47 +00:00
|
|
|
::testing::Values(&GetCtrEncryptedEnv));
|
2020-06-22 20:25:41 +00:00
|
|
|
|
2020-06-03 22:53:09 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(MemEnv, EnvBasicTestWithParam,
|
2021-07-16 14:57:47 +00:00
|
|
|
::testing::Values(&GetMemoryEnv));
|
2016-06-04 01:44:22 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Returns a vector of 0 or 1 Env*, depending whether an Env is registered for
|
|
|
|
// TEST_ENV_URI.
|
|
|
|
//
|
|
|
|
// The purpose of returning an empty vector (instead of nullptr) is that gtest
|
|
|
|
// ValuesIn() will skip running tests when given an empty collection.
|
2021-07-16 14:57:47 +00:00
|
|
|
std::vector<CreateEnvFunc*> GetCustomEnvs() {
|
|
|
|
std::vector<CreateEnvFunc*> res;
|
|
|
|
const char* uri = getenv("TEST_ENV_URI");
|
|
|
|
if (uri != nullptr) {
|
|
|
|
res.push_back(&GetTestEnv);
|
|
|
|
}
|
|
|
|
uri = getenv("TEST_FS_URI");
|
|
|
|
if (uri != nullptr) {
|
|
|
|
res.push_back(&GetTestFS);
|
2016-06-04 01:44:22 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2020-06-03 22:53:09 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(CustomEnv, EnvBasicTestWithParam,
|
|
|
|
::testing::ValuesIn(GetCustomEnvs()));
|
2016-06-22 23:16:21 +00:00
|
|
|
|
2020-06-03 22:53:09 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(CustomEnv, EnvMoreTestWithParam,
|
|
|
|
::testing::ValuesIn(GetCustomEnvs()));
|
2016-06-03 22:13:03 +00:00
|
|
|
#endif // ROCKSDB_LITE
|
|
|
|
|
|
|
|
TEST_P(EnvBasicTestWithParam, Basics) {
|
2011-09-26 16:37:09 +00:00
|
|
|
uint64_t file_size;
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> writable_file;
|
2011-09-12 09:21:10 +00:00
|
|
|
std::vector<std::string> children;
|
|
|
|
|
|
|
|
// Check that the directory is empty.
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_EQ(Status::NotFound(), env_->FileExists(test_dir_ + "/non_existent"));
|
|
|
|
ASSERT_TRUE(!env_->GetFileSize(test_dir_ + "/non_existent", &file_size).ok());
|
|
|
|
ASSERT_OK(env_->GetChildren(test_dir_, &children));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0U, children.size());
|
2011-09-12 09:21:10 +00:00
|
|
|
|
|
|
|
// Create a file.
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(test_dir_ + "/f", &writable_file, soptions_));
|
2016-06-29 22:33:27 +00:00
|
|
|
ASSERT_OK(writable_file->Close());
|
2013-01-20 10:07:13 +00:00
|
|
|
writable_file.reset();
|
2011-09-12 09:21:10 +00:00
|
|
|
|
|
|
|
// Check that the file exists.
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->FileExists(test_dir_ + "/f"));
|
|
|
|
ASSERT_OK(env_->GetFileSize(test_dir_ + "/f", &file_size));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0U, file_size);
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->GetChildren(test_dir_, &children));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(1U, children.size());
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_EQ("f", children[0]);
|
2016-07-08 00:51:36 +00:00
|
|
|
ASSERT_OK(env_->DeleteFile(test_dir_ + "/f"));
|
2011-09-12 09:21:10 +00:00
|
|
|
|
|
|
|
// Write to the file.
|
2016-07-08 00:51:36 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
env_->NewWritableFile(test_dir_ + "/f1", &writable_file, soptions_));
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_OK(writable_file->Append("abc"));
|
2016-06-29 22:33:27 +00:00
|
|
|
ASSERT_OK(writable_file->Close());
|
2013-01-20 10:07:13 +00:00
|
|
|
writable_file.reset();
|
2016-07-08 00:51:36 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
env_->NewWritableFile(test_dir_ + "/f2", &writable_file, soptions_));
|
|
|
|
ASSERT_OK(writable_file->Close());
|
|
|
|
writable_file.reset();
|
2011-09-12 09:21:10 +00:00
|
|
|
|
|
|
|
// Check for expected size.
|
2016-07-08 00:51:36 +00:00
|
|
|
ASSERT_OK(env_->GetFileSize(test_dir_ + "/f1", &file_size));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(3U, file_size);
|
2011-09-12 09:21:10 +00:00
|
|
|
|
|
|
|
// Check that renaming works.
|
2016-07-08 00:51:36 +00:00
|
|
|
ASSERT_TRUE(
|
|
|
|
!env_->RenameFile(test_dir_ + "/non_existent", test_dir_ + "/g").ok());
|
|
|
|
ASSERT_OK(env_->RenameFile(test_dir_ + "/f1", test_dir_ + "/g"));
|
|
|
|
ASSERT_EQ(Status::NotFound(), env_->FileExists(test_dir_ + "/f1"));
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->FileExists(test_dir_ + "/g"));
|
|
|
|
ASSERT_OK(env_->GetFileSize(test_dir_ + "/g", &file_size));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(3U, file_size);
|
2011-09-12 09:21:10 +00:00
|
|
|
|
2016-07-08 00:51:36 +00:00
|
|
|
// Check that renaming overwriting works
|
|
|
|
ASSERT_OK(env_->RenameFile(test_dir_ + "/f2", test_dir_ + "/g"));
|
|
|
|
ASSERT_OK(env_->GetFileSize(test_dir_ + "/g", &file_size));
|
|
|
|
ASSERT_EQ(0U, file_size);
|
|
|
|
|
2011-09-12 09:21:10 +00:00
|
|
|
// Check that opening non-existent file fails.
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<SequentialFile> seq_file;
|
|
|
|
std::unique_ptr<RandomAccessFile> rand_file;
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_TRUE(!env_->NewSequentialFile(test_dir_ + "/non_existent", &seq_file,
|
|
|
|
soptions_)
|
|
|
|
.ok());
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_TRUE(!seq_file);
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_NOK(env_->NewRandomAccessFile(test_dir_ + "/non_existent", &rand_file,
|
|
|
|
soptions_));
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_TRUE(!rand_file);
|
|
|
|
|
|
|
|
// Check that deleting works.
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_NOK(env_->DeleteFile(test_dir_ + "/non_existent"));
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->DeleteFile(test_dir_ + "/g"));
|
|
|
|
ASSERT_EQ(Status::NotFound(), env_->FileExists(test_dir_ + "/g"));
|
|
|
|
ASSERT_OK(env_->GetChildren(test_dir_, &children));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0U, children.size());
|
Fix many tests to run with MEM_ENV and ENCRYPTED_ENV; Introduce a MemoryFileSystem class (#7566)
Summary:
This PR does a few things:
1. The MockFileSystem class was split out from the MockEnv. This change would theoretically allow a MockFileSystem to be used by other Environments as well (if we created a means of constructing one). The MockFileSystem implements a FileSystem in its entirety and does not rely on any Wrapper implementation.
2. Make the RocksDB test suite work when MOCK_ENV=1 and ENCRYPTED_ENV=1 are set. To accomplish this, a few things were needed:
- The tests that tried to use the "wrong" environment (Env::Default() instead of env_) were updated
- The MockFileSystem was changed to support the features it was missing or mishandled (such as recursively deleting files in a directory or supporting renaming of a directory).
3. Updated the test framework to have a ROCKSDB_GTEST_SKIP macro. This can be used to flag tests that are skipped. Currently, this defaults to doing nothing (marks the test as SUCCESS) but will mark the tests as SKIPPED when RocksDB is upgraded to a version of gtest that supports this (gtest-1.10).
I have run a full "make check" with MEM_ENV, ENCRYPTED_ENV, both, and neither under both MacOS and RedHat. A few tests were disabled/skipped for the MEM/ENCRYPTED cases. The error_handler_fs_test fails/hangs for MEM_ENV (presumably a timing problem) and I will introduce another PR/issue to track that problem. (I will also push a change to disable those tests soon). There is one more test in DBTest2 that also fails which I need to investigate or skip before this PR is merged.
Theoretically, this PR should also allow the test suite to run against an Env loaded from the registry, though I do not have one to try it with currently.
Finally, once this is accepted, it would be nice if there was a CircleCI job to run these tests on a checkin so this effort does not become stale. I do not know how to do that, so if someone could write that job, it would be appreciated :)
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7566
Reviewed By: zhichao-cao
Differential Revision: D24408980
Pulled By: jay-zhuang
fbshipit-source-id: 911b1554a4d0da06fd51feca0c090a4abdcb4a5f
2020-10-27 17:31:34 +00:00
|
|
|
Status s = env_->GetChildren(test_dir_ + "/non_existent", &children);
|
|
|
|
ASSERT_TRUE(s.IsNotFound());
|
2011-09-12 09:21:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 22:13:03 +00:00
|
|
|
TEST_P(EnvBasicTestWithParam, ReadWrite) {
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> writable_file;
|
|
|
|
std::unique_ptr<SequentialFile> seq_file;
|
|
|
|
std::unique_ptr<RandomAccessFile> rand_file;
|
2011-09-12 09:21:10 +00:00
|
|
|
Slice result;
|
|
|
|
char scratch[100];
|
|
|
|
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(test_dir_ + "/f", &writable_file, soptions_));
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_OK(writable_file->Append("hello "));
|
|
|
|
ASSERT_OK(writable_file->Append("world"));
|
2016-06-29 22:33:27 +00:00
|
|
|
ASSERT_OK(writable_file->Close());
|
2013-01-20 10:07:13 +00:00
|
|
|
writable_file.reset();
|
2011-09-12 09:21:10 +00:00
|
|
|
|
|
|
|
// Read sequentially.
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->NewSequentialFile(test_dir_ + "/f", &seq_file, soptions_));
|
2016-06-03 22:13:03 +00:00
|
|
|
ASSERT_OK(seq_file->Read(5, &result, scratch)); // Read "hello".
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_EQ(0, result.compare("hello"));
|
|
|
|
ASSERT_OK(seq_file->Skip(1));
|
2016-06-03 22:13:03 +00:00
|
|
|
ASSERT_OK(seq_file->Read(1000, &result, scratch)); // Read "world".
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_EQ(0, result.compare("world"));
|
2016-06-03 22:13:03 +00:00
|
|
|
ASSERT_OK(seq_file->Read(1000, &result, scratch)); // Try reading past EOF.
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0U, result.size());
|
2016-06-03 22:13:03 +00:00
|
|
|
ASSERT_OK(seq_file->Skip(100)); // Try to skip past end of file.
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_OK(seq_file->Read(1000, &result, scratch));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0U, result.size());
|
2011-09-12 09:21:10 +00:00
|
|
|
|
|
|
|
// Random reads.
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(test_dir_ + "/f", &rand_file, soptions_));
|
2016-06-03 22:13:03 +00:00
|
|
|
ASSERT_OK(rand_file->Read(6, 5, &result, scratch)); // Read "world".
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_EQ(0, result.compare("world"));
|
2016-06-03 22:13:03 +00:00
|
|
|
ASSERT_OK(rand_file->Read(0, 5, &result, scratch)); // Read "hello".
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_EQ(0, result.compare("hello"));
|
2016-06-03 22:13:03 +00:00
|
|
|
ASSERT_OK(rand_file->Read(10, 100, &result, scratch)); // Read "d".
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_EQ(0, result.compare("d"));
|
|
|
|
|
|
|
|
// Too high offset.
|
2016-05-27 19:10:26 +00:00
|
|
|
ASSERT_TRUE(rand_file->Read(1000, 5, &result, scratch).ok());
|
2011-09-12 09:21:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 22:13:03 +00:00
|
|
|
TEST_P(EnvBasicTestWithParam, Misc) {
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> writable_file;
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(test_dir_ + "/b", &writable_file, soptions_));
|
2011-09-12 09:21:10 +00:00
|
|
|
|
|
|
|
// These are no-ops, but we test they return success.
|
|
|
|
ASSERT_OK(writable_file->Sync());
|
|
|
|
ASSERT_OK(writable_file->Flush());
|
|
|
|
ASSERT_OK(writable_file->Close());
|
2013-01-20 10:07:13 +00:00
|
|
|
writable_file.reset();
|
2011-09-12 09:21:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 22:13:03 +00:00
|
|
|
TEST_P(EnvBasicTestWithParam, LargeWrite) {
|
2011-09-12 09:21:10 +00:00
|
|
|
const size_t kWriteSize = 300 * 1024;
|
|
|
|
char* scratch = new char[kWriteSize * 2];
|
|
|
|
|
|
|
|
std::string write_data;
|
|
|
|
for (size_t i = 0; i < kWriteSize; ++i) {
|
|
|
|
write_data.append(1, static_cast<char>(i));
|
|
|
|
}
|
|
|
|
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> writable_file;
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(test_dir_ + "/f", &writable_file, soptions_));
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_OK(writable_file->Append("foo"));
|
|
|
|
ASSERT_OK(writable_file->Append(write_data));
|
2016-06-29 22:33:27 +00:00
|
|
|
ASSERT_OK(writable_file->Close());
|
2013-01-20 10:07:13 +00:00
|
|
|
writable_file.reset();
|
2011-09-12 09:21:10 +00:00
|
|
|
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<SequentialFile> seq_file;
|
2011-09-12 09:21:10 +00:00
|
|
|
Slice result;
|
2016-06-04 01:44:22 +00:00
|
|
|
ASSERT_OK(env_->NewSequentialFile(test_dir_ + "/f", &seq_file, soptions_));
|
2016-06-03 22:13:03 +00:00
|
|
|
ASSERT_OK(seq_file->Read(3, &result, scratch)); // Read "foo".
|
2011-09-12 09:21:10 +00:00
|
|
|
ASSERT_EQ(0, result.compare("foo"));
|
|
|
|
|
|
|
|
size_t read = 0;
|
|
|
|
std::string read_data;
|
|
|
|
while (read < kWriteSize) {
|
|
|
|
ASSERT_OK(seq_file->Read(kWriteSize - read, &result, scratch));
|
|
|
|
read_data.append(result.data(), result.size());
|
|
|
|
read += result.size();
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(write_data == read_data);
|
|
|
|
delete [] scratch;
|
|
|
|
}
|
|
|
|
|
2016-06-22 23:16:21 +00:00
|
|
|
TEST_P(EnvMoreTestWithParam, GetModTime) {
|
|
|
|
ASSERT_OK(env_->CreateDirIfMissing(test_dir_ + "/dir1"));
|
|
|
|
uint64_t mtime1 = 0x0;
|
|
|
|
ASSERT_OK(env_->GetFileModificationTime(test_dir_ + "/dir1", &mtime1));
|
|
|
|
}
|
2011-09-12 09:21:10 +00:00
|
|
|
|
2016-06-22 23:16:21 +00:00
|
|
|
TEST_P(EnvMoreTestWithParam, MakeDir) {
|
2016-06-24 19:31:58 +00:00
|
|
|
ASSERT_OK(env_->CreateDir(test_dir_ + "/j"));
|
2016-06-22 23:16:21 +00:00
|
|
|
ASSERT_OK(env_->FileExists(test_dir_ + "/j"));
|
|
|
|
std::vector<std::string> children;
|
2020-12-24 00:54:05 +00:00
|
|
|
ASSERT_OK(env_->GetChildren(test_dir_, &children));
|
2016-06-22 23:16:21 +00:00
|
|
|
ASSERT_EQ(1U, children.size());
|
|
|
|
// fail because file already exists
|
|
|
|
ASSERT_TRUE(!env_->CreateDir(test_dir_ + "/j").ok());
|
|
|
|
ASSERT_OK(env_->CreateDirIfMissing(test_dir_ + "/j"));
|
|
|
|
ASSERT_OK(env_->DeleteDir(test_dir_ + "/j"));
|
2016-06-24 19:31:58 +00:00
|
|
|
ASSERT_EQ(Status::NotFound(), env_->FileExists(test_dir_ + "/j"));
|
2016-06-22 23:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(EnvMoreTestWithParam, GetChildren) {
|
|
|
|
// empty folder returns empty vector
|
|
|
|
std::vector<std::string> children;
|
|
|
|
std::vector<Env::FileAttributes> childAttr;
|
2016-06-24 19:31:58 +00:00
|
|
|
ASSERT_OK(env_->CreateDirIfMissing(test_dir_));
|
|
|
|
ASSERT_OK(env_->GetChildren(test_dir_, &children));
|
|
|
|
ASSERT_OK(env_->FileExists(test_dir_));
|
|
|
|
ASSERT_OK(env_->GetChildrenFileAttributes(test_dir_, &childAttr));
|
2016-06-22 23:16:21 +00:00
|
|
|
ASSERT_EQ(0U, children.size());
|
|
|
|
ASSERT_EQ(0U, childAttr.size());
|
|
|
|
|
2016-06-24 19:31:58 +00:00
|
|
|
// folder with contents returns relative path to test dir
|
2016-08-19 22:10:31 +00:00
|
|
|
ASSERT_OK(env_->CreateDirIfMissing(test_dir_ + "/niu"));
|
|
|
|
ASSERT_OK(env_->CreateDirIfMissing(test_dir_ + "/you"));
|
|
|
|
ASSERT_OK(env_->CreateDirIfMissing(test_dir_ + "/guo"));
|
2016-06-24 19:31:58 +00:00
|
|
|
ASSERT_OK(env_->GetChildren(test_dir_, &children));
|
|
|
|
ASSERT_OK(env_->GetChildrenFileAttributes(test_dir_, &childAttr));
|
2016-06-22 23:16:21 +00:00
|
|
|
ASSERT_EQ(3U, children.size());
|
|
|
|
ASSERT_EQ(3U, childAttr.size());
|
2016-06-24 19:31:58 +00:00
|
|
|
for (auto each : children) {
|
2020-07-29 05:58:28 +00:00
|
|
|
env_->DeleteDir(test_dir_ + "/" + each).PermitUncheckedError();
|
2016-07-01 01:33:49 +00:00
|
|
|
} // necessary for default POSIX env
|
2016-06-22 23:16:21 +00:00
|
|
|
|
|
|
|
// non-exist directory returns IOError
|
2016-06-24 19:31:58 +00:00
|
|
|
ASSERT_OK(env_->DeleteDir(test_dir_));
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_NOK(env_->FileExists(test_dir_));
|
|
|
|
ASSERT_NOK(env_->GetChildren(test_dir_, &children));
|
|
|
|
ASSERT_NOK(env_->GetChildrenFileAttributes(test_dir_, &childAttr));
|
2016-06-22 23:16:21 +00:00
|
|
|
|
|
|
|
// if dir is a file, returns IOError
|
2016-06-24 19:31:58 +00:00
|
|
|
ASSERT_OK(env_->CreateDir(test_dir_));
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> writable_file;
|
2016-06-22 23:16:21 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
env_->NewWritableFile(test_dir_ + "/file", &writable_file, soptions_));
|
2016-06-29 22:33:27 +00:00
|
|
|
ASSERT_OK(writable_file->Close());
|
2016-06-22 23:16:21 +00:00
|
|
|
writable_file.reset();
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_NOK(env_->GetChildren(test_dir_ + "/file", &children));
|
2016-06-22 23:16:21 +00:00
|
|
|
ASSERT_EQ(0U, children.size());
|
|
|
|
}
|
|
|
|
|
2021-01-09 17:42:21 +00:00
|
|
|
TEST_P(EnvMoreTestWithParam, GetChildrenIgnoresDotAndDotDot) {
|
|
|
|
auto* env = Env::Default();
|
|
|
|
ASSERT_OK(env->CreateDirIfMissing(test_dir_));
|
|
|
|
|
|
|
|
// Create a single file
|
|
|
|
std::string path = test_dir_;
|
|
|
|
const EnvOptions soptions;
|
|
|
|
#ifdef OS_WIN
|
|
|
|
path.append("\\test_file");
|
|
|
|
#else
|
|
|
|
path.append("/test_file");
|
|
|
|
#endif
|
|
|
|
std::string data("test data");
|
|
|
|
std::unique_ptr<WritableFile> file;
|
|
|
|
ASSERT_OK(env->NewWritableFile(path, &file, soptions));
|
|
|
|
ASSERT_OK(file->Append("test data"));
|
|
|
|
|
|
|
|
// get the children
|
|
|
|
std::vector<std::string> result;
|
|
|
|
ASSERT_OK(env->GetChildren(test_dir_, &result));
|
|
|
|
|
|
|
|
// expect only one file named `test_data`, i.e. no `.` or `..` names
|
|
|
|
ASSERT_EQ(result.size(), 1);
|
|
|
|
ASSERT_EQ(result.at(0), "test_file");
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2011-09-12 09:21:10 +00:00
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2011-09-12 09:21:10 +00:00
|
|
|
}
|