Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 1 | #include "cleanup.hpp" |
| 2 | #include "filesystem_mock.hpp" |
| 3 | #include "util.hpp" |
| 4 | |
Patrick Venture | 48474bc | 2020-10-05 09:38:33 -0700 | [diff] [blame] | 5 | #include <blobs-ipmid/blobs.hpp> |
| 6 | |
| 7 | #include <cstdint> |
Patrick Venture | 2950c25 | 2020-07-16 15:11:03 -0700 | [diff] [blame] | 8 | #include <memory> |
Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <gtest/gtest.h> |
| 13 | |
| 14 | namespace ipmi_flash |
| 15 | { |
| 16 | namespace |
| 17 | { |
| 18 | |
| 19 | using ::testing::Return; |
| 20 | using ::testing::UnorderedElementsAreArray; |
| 21 | |
| 22 | class CleanupHandlerTest : public ::testing::Test |
| 23 | { |
| 24 | protected: |
Patrick Venture | 2950c25 | 2020-07-16 15:11:03 -0700 | [diff] [blame] | 25 | CleanupHandlerTest() : mock(std::make_unique<FileSystemMock>()) |
| 26 | { |
| 27 | mock_ptr = mock.get(); |
| 28 | handler = std::make_unique<FileCleanupHandler>(cleanupBlobId, blobs, |
| 29 | std::move(mock)); |
| 30 | } |
| 31 | |
Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 32 | std::vector<std::string> blobs = {"abcd", "efgh"}; |
Patrick Venture | 2950c25 | 2020-07-16 15:11:03 -0700 | [diff] [blame] | 33 | std::unique_ptr<FileSystemMock> mock; |
| 34 | FileSystemMock* mock_ptr; |
| 35 | std::unique_ptr<FileCleanupHandler> handler; |
Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | TEST_F(CleanupHandlerTest, GetBlobListReturnsExpectedList) |
| 39 | { |
Patrick Venture | 2950c25 | 2020-07-16 15:11:03 -0700 | [diff] [blame] | 40 | EXPECT_TRUE(handler->canHandleBlob(cleanupBlobId)); |
| 41 | EXPECT_THAT(handler->getBlobIds(), |
Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 42 | UnorderedElementsAreArray({cleanupBlobId})); |
| 43 | } |
| 44 | |
| 45 | TEST_F(CleanupHandlerTest, CommitShouldDeleteFiles) |
| 46 | { |
Patrick Venture | 2950c25 | 2020-07-16 15:11:03 -0700 | [diff] [blame] | 47 | EXPECT_CALL(*mock_ptr, remove("abcd")).WillOnce(Return()); |
| 48 | EXPECT_CALL(*mock_ptr, remove("efgh")).WillOnce(Return()); |
Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 49 | |
Patrick Venture | 2950c25 | 2020-07-16 15:11:03 -0700 | [diff] [blame] | 50 | EXPECT_TRUE(handler->commit(1, {})); |
Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Patrick Venture | 48474bc | 2020-10-05 09:38:33 -0700 | [diff] [blame] | 53 | TEST_F(CleanupHandlerTest, VerifyDefaultBlobMethods) |
| 54 | { |
| 55 | // Test each of the blob handler commands. |
| 56 | EXPECT_TRUE(handler->open(/*session*/ 0, /*flags*/ 0, "abcd")); |
| 57 | EXPECT_TRUE(handler->close(/*session*/ 0)); |
| 58 | EXPECT_TRUE(handler->expire(/*session*/ 0)); |
| 59 | EXPECT_FALSE(handler->deleteBlob("abcd")); |
| 60 | |
| 61 | blobs::BlobMeta meta; |
| 62 | EXPECT_FALSE(handler->stat("abcd", &meta)); |
| 63 | EXPECT_FALSE(handler->stat(/*session*/ 0, &meta)); |
| 64 | |
| 65 | EXPECT_THAT(handler->read(/*session*/ 0, /*offset*/ 0, 1), |
| 66 | ::testing::IsEmpty()); |
| 67 | |
| 68 | std::vector<uint8_t> data = {0x01}; |
| 69 | EXPECT_FALSE(handler->write(/*session*/ 0, /*offset*/ 0, data)); |
| 70 | } |
| 71 | |
Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 72 | } // namespace |
| 73 | } // namespace ipmi_flash |