William A. Kennington III | eba0c34 | 2021-01-12 14:23:01 -0800 | [diff] [blame] | 1 | #include "version_handler.hpp" |
| 2 | #include "version_mock.hpp" |
| 3 | |
| 4 | #include <memory> |
| 5 | #include <string> |
| 6 | #include <unordered_map> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | using ::testing::_; |
| 12 | using ::testing::Return; |
| 13 | |
| 14 | namespace ipmi_flash |
| 15 | { |
| 16 | |
| 17 | class VersionStatBlobTest : public ::testing::Test |
| 18 | { |
| 19 | protected: |
| 20 | void SetUp() override |
| 21 | { |
| 22 | h = std::make_unique<VersionBlobHandler>( |
| 23 | createMockVersionConfigs(blobNames, &im, &tm)); |
| 24 | |
| 25 | EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true)); |
| 26 | EXPECT_TRUE(h->open(0, blobs::read, "blob0")); |
| 27 | |
| 28 | blobs::BlobMeta meta; |
| 29 | EXPECT_TRUE(h->stat(0, &meta)); |
| 30 | EXPECT_EQ(blobs::StateFlags::committing, meta.blobState); |
| 31 | } |
| 32 | |
| 33 | std::unique_ptr<blobs::GenericBlobInterface> h; |
| 34 | std::vector<std::string> blobNames{"blob0"}; |
| 35 | std::unordered_map<std::string, TriggerMock*> tm; |
| 36 | std::unordered_map<std::string, ImageHandlerMock*> im; |
| 37 | }; |
| 38 | |
| 39 | TEST_F(VersionStatBlobTest, CreateError) |
| 40 | { |
| 41 | EXPECT_CALL(*tm.at("blob0"), status()) |
| 42 | .WillOnce(Return(ActionStatus::failed)); |
| 43 | tm.at("blob0")->cb(*tm.at("blob0")); |
| 44 | |
| 45 | blobs::BlobMeta meta; |
| 46 | EXPECT_TRUE(h->stat(0, &meta)); |
| 47 | EXPECT_EQ(blobs::StateFlags::commit_error, meta.blobState); |
| 48 | } |
| 49 | |
| 50 | class VersionStatSizeBlobTest : |
| 51 | public VersionStatBlobTest, |
| 52 | public ::testing::WithParamInterface<std::vector<uint8_t>> |
| 53 | {}; |
| 54 | |
| 55 | TEST_P(VersionStatSizeBlobTest, StatWithSize) |
| 56 | { |
| 57 | const std::vector<uint8_t> data = GetParam(); |
| 58 | EXPECT_CALL(*tm.at("blob0"), status()) |
| 59 | .WillOnce(Return(ActionStatus::success)); |
| 60 | EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true)); |
| 61 | EXPECT_CALL(*im.at("blob0"), read(0, ::testing::Ge(data.size()))) |
| 62 | .WillOnce(Return(data)); |
| 63 | EXPECT_CALL(*im.at("blob0"), close()).Times(1); |
| 64 | tm.at("blob0")->cb(*tm.at("blob0")); |
| 65 | |
| 66 | blobs::BlobMeta meta; |
| 67 | EXPECT_TRUE(h->stat(0, &meta)); |
| 68 | EXPECT_EQ(blobs::StateFlags::committed | blobs::StateFlags::open_read, |
| 69 | meta.blobState); |
| 70 | EXPECT_EQ(data.size(), meta.size); |
| 71 | } |
| 72 | |
| 73 | const std::vector<std::vector<uint8_t>> datas = { |
| 74 | {}, |
| 75 | {0, 1, 2, 3, 4, 5, 6}, |
| 76 | }; |
| 77 | |
| 78 | INSTANTIATE_TEST_SUITE_P(DifferentData, VersionStatSizeBlobTest, |
| 79 | testing::ValuesIn(datas)); |
| 80 | |
| 81 | } // namespace ipmi_flash |