blob: 0910de9e92098507440f7d1ba026c45762e9c8e9 [file] [log] [blame]
Patrick Venture9efef5d2019-06-19 08:45:44 -07001#include "cleanup.hpp"
2#include "filesystem_mock.hpp"
3#include "util.hpp"
4
Patrick Venture48474bc2020-10-05 09:38:33 -07005#include <blobs-ipmid/blobs.hpp>
6
7#include <cstdint>
Patrick Venture2950c252020-07-16 15:11:03 -07008#include <memory>
Patrick Venture9efef5d2019-06-19 08:45:44 -07009#include <string>
10#include <vector>
11
12#include <gtest/gtest.h>
13
14namespace ipmi_flash
15{
16namespace
17{
18
19using ::testing::Return;
20using ::testing::UnorderedElementsAreArray;
21
22class CleanupHandlerTest : public ::testing::Test
23{
24 protected:
Patrick Venture2950c252020-07-16 15:11:03 -070025 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 Venture9efef5d2019-06-19 08:45:44 -070032 std::vector<std::string> blobs = {"abcd", "efgh"};
Patrick Venture2950c252020-07-16 15:11:03 -070033 std::unique_ptr<FileSystemMock> mock;
34 FileSystemMock* mock_ptr;
35 std::unique_ptr<FileCleanupHandler> handler;
Patrick Venture9efef5d2019-06-19 08:45:44 -070036};
37
38TEST_F(CleanupHandlerTest, GetBlobListReturnsExpectedList)
39{
Patrick Venture2950c252020-07-16 15:11:03 -070040 EXPECT_TRUE(handler->canHandleBlob(cleanupBlobId));
41 EXPECT_THAT(handler->getBlobIds(),
Patrick Venture9efef5d2019-06-19 08:45:44 -070042 UnorderedElementsAreArray({cleanupBlobId}));
43}
44
45TEST_F(CleanupHandlerTest, CommitShouldDeleteFiles)
46{
Patrick Venture2950c252020-07-16 15:11:03 -070047 EXPECT_CALL(*mock_ptr, remove("abcd")).WillOnce(Return());
48 EXPECT_CALL(*mock_ptr, remove("efgh")).WillOnce(Return());
Patrick Venture9efef5d2019-06-19 08:45:44 -070049
Patrick Venture2950c252020-07-16 15:11:03 -070050 EXPECT_TRUE(handler->commit(1, {}));
Patrick Venture9efef5d2019-06-19 08:45:44 -070051}
52
Patrick Venture48474bc2020-10-05 09:38:33 -070053TEST_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 Venture9efef5d2019-06-19 08:45:44 -070072} // namespace
73} // namespace ipmi_flash