Patrick Venture | aceb4ba | 2018-09-27 14:50:37 -0700 | [diff] [blame] | 1 | #include <blobs-ipmid/manager.hpp> |
Patrick Venture | 7210b31 | 2018-10-03 14:01:35 -0700 | [diff] [blame] | 2 | #include <blobs-ipmid/test/blob_mock.hpp> |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | namespace blobs |
| 7 | { |
| 8 | |
| 9 | using ::testing::_; |
| 10 | using ::testing::Return; |
| 11 | |
| 12 | TEST(ManagerDeleteTest, FileIsOpenReturnsFailure) |
| 13 | { |
| 14 | // The blob manager maintains a naive list of open files and will |
| 15 | // return failure if you try to delete an open file. |
| 16 | |
| 17 | // Open the file. |
| 18 | BlobManager mgr; |
| 19 | std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>(); |
| 20 | auto m1ptr = m1.get(); |
| 21 | EXPECT_TRUE(mgr.registerHandler(std::move(m1))); |
| 22 | |
| 23 | uint16_t flags = OpenFlags::read, sess; |
| 24 | std::string path = "/asdf/asdf"; |
| 25 | |
| 26 | EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillRepeatedly(Return(true)); |
| 27 | EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true)); |
| 28 | EXPECT_TRUE(mgr.open(flags, path, &sess)); |
| 29 | |
| 30 | // Try to delete the file. |
| 31 | EXPECT_FALSE(mgr.deleteBlob(path)); |
| 32 | } |
| 33 | |
| 34 | TEST(ManagerDeleteTest, FileHasNoHandler) |
| 35 | { |
| 36 | // The blob manager cannot find any handler. |
| 37 | |
| 38 | BlobManager mgr; |
| 39 | std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>(); |
| 40 | auto m1ptr = m1.get(); |
| 41 | EXPECT_TRUE(mgr.registerHandler(std::move(m1))); |
| 42 | |
| 43 | std::string path = "/asdf/asdf"; |
| 44 | |
| 45 | EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(false)); |
| 46 | |
| 47 | // Try to delete the file. |
| 48 | EXPECT_FALSE(mgr.deleteBlob(path)); |
| 49 | } |
| 50 | |
| 51 | TEST(ManagerDeleteTest, FileIsNotOpenButHandlerDeleteFails) |
| 52 | { |
| 53 | // The Blob manager finds the handler but the handler returns failure |
| 54 | // on delete. |
| 55 | |
| 56 | BlobManager mgr; |
| 57 | std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>(); |
| 58 | auto m1ptr = m1.get(); |
| 59 | EXPECT_TRUE(mgr.registerHandler(std::move(m1))); |
| 60 | |
| 61 | std::string path = "/asdf/asdf"; |
| 62 | |
| 63 | EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true)); |
| 64 | EXPECT_CALL(*m1ptr, deleteBlob(path)).WillOnce(Return(false)); |
| 65 | |
| 66 | // Try to delete the file. |
| 67 | EXPECT_FALSE(mgr.deleteBlob(path)); |
| 68 | } |
| 69 | |
| 70 | TEST(ManagerDeleteTest, FileIsNotOpenAndHandlerSucceeds) |
| 71 | { |
| 72 | // The Blob manager finds the handler and the handler returns success. |
| 73 | |
| 74 | BlobManager mgr; |
| 75 | std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>(); |
| 76 | auto m1ptr = m1.get(); |
| 77 | EXPECT_TRUE(mgr.registerHandler(std::move(m1))); |
| 78 | |
| 79 | std::string path = "/asdf/asdf"; |
| 80 | |
| 81 | EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true)); |
| 82 | EXPECT_CALL(*m1ptr, deleteBlob(path)).WillOnce(Return(true)); |
| 83 | |
| 84 | // Try to delete the file. |
| 85 | EXPECT_TRUE(mgr.deleteBlob(path)); |
| 86 | } |
| 87 | } // namespace blobs |