|
| 1 | +#include <filesystem> |
| 2 | +#include <fstream> |
1 | 3 | #include <gtest/gtest.h> |
2 | 4 | #include <memory> |
3 | 5 |
|
@@ -114,31 +116,105 @@ TEST_F(NfsBackendTest, TestDeleteReturnsOkAndSameSize) { |
114 | 116 | NfsBackend backend(metrics_registry_); |
115 | 117 | std::shared_ptr<NfsStorageSpec> spec(new NfsStorageSpec); |
116 | 118 | spec->set_key_count_per_file(1); |
117 | | - spec->set_root_path("/data/"); |
| 119 | + spec->set_root_path(GetPrivateTestRuntimeDataPath()); |
118 | 120 | StorageConfig storage_config(DataStorageType::DATA_STORAGE_TYPE_NFS, "test", spec); |
119 | 121 | ASSERT_EQ(EC_OK, backend.Open(storage_config, "fake_trace_id_1")); |
120 | | - std::vector<DataStorageUri> uris(3); |
121 | | - auto res = backend.Delete(uris, "fake_trace_id_2", []() {}); |
| 122 | + |
| 123 | + const auto file1 = GetPrivateTestRuntimeDataPath() + "delete-1"; |
| 124 | + const auto file2 = GetPrivateTestRuntimeDataPath() + "delete-2"; |
| 125 | + { |
| 126 | + std::ofstream(file1) << "1"; |
| 127 | + std::ofstream(file2) << "2"; |
| 128 | + } |
| 129 | + ASSERT_TRUE(std::filesystem::exists(file1)); |
| 130 | + ASSERT_TRUE(std::filesystem::exists(file2)); |
| 131 | + std::vector<DataStorageUri> uris; |
| 132 | + uris.emplace_back("file://nfs_test" + file1); |
| 133 | + uris.emplace_back("file://nfs_test" + file2); |
| 134 | + uris.emplace_back("file://nfs_test" + GetPrivateTestRuntimeDataPath() + "missing"); |
| 135 | + |
| 136 | + bool callback_called = false; |
| 137 | + auto res = backend.Delete(uris, "fake_trace_id_2", [&callback_called]() { callback_called = true; }); |
| 138 | + ASSERT_TRUE(callback_called); |
122 | 139 | ASSERT_EQ(res.size(), uris.size()); |
123 | 140 | for (auto code : res) { |
124 | 141 | ASSERT_EQ(code, EC_OK); |
125 | 142 | } |
| 143 | + ASSERT_FALSE(std::filesystem::exists(file1)); |
| 144 | + ASSERT_FALSE(std::filesystem::exists(file2)); |
| 145 | +} |
| 146 | + |
| 147 | +TEST_F(NfsBackendTest, TestDeleteOnlyRemovesMultiBlockFileForBlockZero) { |
| 148 | + NfsBackend backend(metrics_registry_); |
| 149 | + std::shared_ptr<NfsStorageSpec> spec(new NfsStorageSpec); |
| 150 | + spec->set_key_count_per_file(2); |
| 151 | + spec->set_root_path(GetPrivateTestRuntimeDataPath()); |
| 152 | + StorageConfig storage_config(DataStorageType::DATA_STORAGE_TYPE_NFS, "test", spec); |
| 153 | + ASSERT_EQ(EC_OK, backend.Open(storage_config, "fake_trace_id_1")); |
| 154 | + |
| 155 | + const auto file = GetPrivateTestRuntimeDataPath() + "multi-block-delete"; |
| 156 | + std::ofstream(file) << "shared"; |
| 157 | + ASSERT_TRUE(std::filesystem::exists(file)); |
| 158 | + |
| 159 | + DataStorageUri block_zero("file://nfs_test" + file + "?blkid=0&size=6"); |
| 160 | + DataStorageUri block_one("file://nfs_test" + file + "?blkid=1&size=6"); |
| 161 | + DataStorageUri invalid_block("file://nfs_test" + file + "?blkid=bad&size=6"); |
| 162 | + |
| 163 | + auto skip_res = backend.Delete({block_one}, "fake_trace_id_2", []() {}); |
| 164 | + ASSERT_EQ(skip_res.size(), 1u); |
| 165 | + ASSERT_EQ(skip_res[0], EC_OK); |
| 166 | + ASSERT_TRUE(std::filesystem::exists(file)); |
| 167 | + |
| 168 | + auto invalid_res = backend.Delete({invalid_block}, "fake_trace_id_3", []() {}); |
| 169 | + ASSERT_EQ(invalid_res.size(), 1u); |
| 170 | + ASSERT_EQ(invalid_res[0], EC_ERROR); |
| 171 | + ASSERT_TRUE(std::filesystem::exists(file)); |
| 172 | + |
| 173 | + DataStorageUri missing_blkid("file://nfs_test" + file + "?size=6"); |
| 174 | + auto missing_blkid_res = backend.Delete({missing_blkid}, "fake_trace_id_4", []() {}); |
| 175 | + ASSERT_EQ(missing_blkid_res.size(), 1u); |
| 176 | + ASSERT_EQ(missing_blkid_res[0], EC_OK); |
| 177 | + ASSERT_FALSE(std::filesystem::exists(file)); |
| 178 | + |
| 179 | + std::ofstream(file) << "shared"; |
| 180 | + ASSERT_TRUE(std::filesystem::exists(file)); |
| 181 | + DataStorageUri empty_blkid("file://nfs_test" + file + "?blkid=&size=6"); |
| 182 | + auto empty_blkid_res = backend.Delete({empty_blkid}, "fake_trace_id_5", []() {}); |
| 183 | + ASSERT_EQ(empty_blkid_res.size(), 1u); |
| 184 | + ASSERT_EQ(empty_blkid_res[0], EC_OK); |
| 185 | + ASSERT_FALSE(std::filesystem::exists(file)); |
| 186 | + |
| 187 | + std::ofstream(file) << "shared"; |
| 188 | + ASSERT_TRUE(std::filesystem::exists(file)); |
| 189 | + auto delete_res = backend.Delete({block_zero}, "fake_trace_id_6", []() {}); |
| 190 | + ASSERT_EQ(delete_res.size(), 1u); |
| 191 | + ASSERT_EQ(delete_res[0], EC_OK); |
| 192 | + ASSERT_FALSE(std::filesystem::exists(file)); |
126 | 193 | } |
127 | 194 |
|
128 | | -TEST_F(NfsBackendTest, TestExistReturnsTrues) { |
| 195 | +TEST_F(NfsBackendTest, TestExistReturnsActualFileState) { |
129 | 196 | NfsBackend backend(metrics_registry_); |
130 | 197 | std::shared_ptr<NfsStorageSpec> spec(new NfsStorageSpec); |
131 | 198 | spec->set_key_count_per_file(1); |
132 | | - spec->set_root_path("/data/"); |
| 199 | + spec->set_root_path(GetPrivateTestRuntimeDataPath()); |
133 | 200 | StorageConfig storage_config(DataStorageType::DATA_STORAGE_TYPE_NFS, "test", spec); |
134 | 201 | ASSERT_EQ(EC_OK, backend.Open(storage_config, "fake_trace_id_1")); |
135 | | - // TODO(qisa.cb) 没实现 |
136 | | - std::vector<DataStorageUri> uris(5); |
| 202 | + |
| 203 | + const auto file1 = GetPrivateTestRuntimeDataPath() + "exists-1"; |
| 204 | + std::ofstream(file1) << "1"; |
| 205 | + std::vector<DataStorageUri> uris; |
| 206 | + uris.emplace_back("file://nfs_test" + file1); |
| 207 | + uris.emplace_back("file://nfs_test" + GetPrivateTestRuntimeDataPath() + "missing"); |
| 208 | + |
137 | 209 | auto res = backend.Exist(uris); |
138 | 210 | ASSERT_EQ(res.size(), uris.size()); |
139 | | - for (bool flag : res) { |
140 | | - ASSERT_TRUE(flag); |
141 | | - } |
| 211 | + ASSERT_TRUE(res[0]); |
| 212 | + ASSERT_FALSE(res[1]); |
| 213 | + |
| 214 | + auto might_res = backend.MightExist(uris); |
| 215 | + ASSERT_EQ(might_res.size(), uris.size()); |
| 216 | + ASSERT_TRUE(might_res[0]); |
| 217 | + ASSERT_TRUE(might_res[1]); |
142 | 218 | } |
143 | 219 |
|
144 | 220 | TEST_F(NfsBackendTest, TestLockAndUnLockReturnOk) { |
|
0 commit comments