Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 1 | #include "item_updater.hpp" |
| 2 | #include "mocked_utils.hpp" |
| 3 | |
| 4 | #include <sdbusplus/test/sdbus_mock.hpp> |
| 5 | |
| 6 | #include <gmock/gmock.h> |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | using namespace phosphor::software::updater; |
| 10 | using ::testing::_; |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 11 | using ::testing::Pointee; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 12 | using ::testing::Return; |
| 13 | using ::testing::ReturnArg; |
| 14 | using ::testing::StrEq; |
| 15 | |
| 16 | using std::experimental::any; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 17 | |
| 18 | class TestItemUpdater : public ::testing::Test |
| 19 | { |
| 20 | public: |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 21 | using Properties = ItemUpdater::Properties; |
| 22 | using PropertyType = utils::UtilsInterface::PropertyType; |
| 23 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 24 | TestItemUpdater() : |
| 25 | mockedUtils( |
| 26 | reinterpret_cast<const utils::MockedUtils&>(utils::getUtils())) |
| 27 | { |
| 28 | ON_CALL(mockedUtils, getVersionId(_)).WillByDefault(ReturnArg<0>()); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 29 | ON_CALL(mockedUtils, getPropertyImpl(_, _, _, _, StrEq(PRESENT))) |
| 30 | .WillByDefault(Return(any(PropertyType(true)))); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | ~TestItemUpdater() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | const auto& GetActivations() |
| 38 | { |
| 39 | return itemUpdater->activations; |
| 40 | } |
| 41 | |
| 42 | std::string getObjPath(const std::string& versionId) |
| 43 | { |
| 44 | return std::string(dBusPath) + "/" + versionId; |
| 45 | } |
| 46 | |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 47 | void onPsuInventoryChanged(const std::string& psuPath, |
| 48 | const Properties& properties) |
| 49 | { |
| 50 | itemUpdater->onPsuInventoryChanged(psuPath, properties); |
| 51 | } |
| 52 | |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 53 | void scanDirectory(const fs::path& p) |
| 54 | { |
| 55 | itemUpdater->scanDirectory(p); |
| 56 | } |
| 57 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 58 | static constexpr auto dBusPath = SOFTWARE_OBJPATH; |
| 59 | sdbusplus::SdBusMock sdbusMock; |
| 60 | sdbusplus::bus::bus mockedBus = sdbusplus::get_mocked_new(&sdbusMock); |
| 61 | const utils::MockedUtils& mockedUtils; |
| 62 | std::unique_ptr<ItemUpdater> itemUpdater; |
| 63 | }; |
| 64 | |
| 65 | TEST_F(TestItemUpdater, ctordtor) |
| 66 | { |
| 67 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 68 | } |
| 69 | |
| 70 | TEST_F(TestItemUpdater, NotCreateObjectOnNotPresent) |
| 71 | { |
| 72 | constexpr auto psuPath = "/com/example/inventory/psu0"; |
| 73 | constexpr auto service = "com.example.Software.Psu"; |
| 74 | constexpr auto version = "version0"; |
| 75 | std::string objPath = getObjPath(version); |
| 76 | EXPECT_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 77 | .WillOnce(Return(std::vector<std::string>({psuPath}))); |
| 78 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psuPath), _)) |
| 79 | .WillOnce(Return(service)); |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 80 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psuPath))) |
| 81 | .WillOnce(Return(std::string(version))); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 82 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath), |
| 83 | _, StrEq(PRESENT))) |
| 84 | .WillOnce(Return(any(PropertyType(false)))); // not present |
| 85 | |
| 86 | // The item updater itself |
| 87 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath))) |
| 88 | .Times(1); |
| 89 | |
| 90 | // No activation/version objects are created |
| 91 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 92 | .Times(0); |
| 93 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 94 | } |
| 95 | |
| 96 | TEST_F(TestItemUpdater, CreateOnePSUOnPresent) |
| 97 | { |
| 98 | constexpr auto psuPath = "/com/example/inventory/psu0"; |
| 99 | constexpr auto service = "com.example.Software.Psu"; |
| 100 | constexpr auto version = "version0"; |
| 101 | std::string objPath = getObjPath(version); |
| 102 | EXPECT_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 103 | .WillOnce(Return(std::vector<std::string>({psuPath}))); |
| 104 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psuPath), _)) |
| 105 | .WillOnce(Return(service)); |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 106 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psuPath))) |
| 107 | .WillOnce(Return(std::string(version))); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 108 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath), |
| 109 | _, StrEq(PRESENT))) |
| 110 | .WillOnce(Return(any(PropertyType(true)))); // present |
| 111 | |
| 112 | // The item updater itself |
| 113 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath))) |
| 114 | .Times(1); |
| 115 | |
| 116 | // activation and version object will be added |
| 117 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 118 | .Times(2); |
| 119 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 120 | } |
| 121 | |
| 122 | TEST_F(TestItemUpdater, CreateTwoPSUsWithSameVersion) |
| 123 | { |
| 124 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 125 | constexpr auto psu1 = "/com/example/inventory/psu1"; |
| 126 | constexpr auto service = "com.example.Software.Psu"; |
| 127 | auto version0 = std::string("version0"); |
| 128 | auto version1 = std::string("version0"); |
| 129 | auto objPath0 = getObjPath(version0); |
| 130 | auto objPath1 = getObjPath(version1); |
| 131 | |
| 132 | EXPECT_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 133 | .WillOnce(Return(std::vector<std::string>({psu0, psu1}))); |
| 134 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psu0), _)) |
| 135 | .WillOnce(Return(service)); |
| 136 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psu1), _)) |
| 137 | .WillOnce(Return(service)); |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 138 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psu0))) |
| 139 | .WillOnce(Return(std::string(version0))); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 140 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu0), _, |
| 141 | StrEq(PRESENT))) |
| 142 | .WillOnce(Return(any(PropertyType(true)))); // present |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 143 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psu1))) |
| 144 | .WillOnce(Return(std::string(version1))); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 145 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu1), _, |
| 146 | StrEq(PRESENT))) |
| 147 | .WillOnce(Return(any(PropertyType(true)))); // present |
| 148 | |
| 149 | // The item updater itself |
| 150 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath))) |
| 151 | .Times(1); |
| 152 | |
| 153 | // activation and version object will be added |
| 154 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath0))) |
| 155 | .Times(2); |
| 156 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 157 | |
| 158 | // Verify there is only one activation and it has two associations |
| 159 | const auto& activations = GetActivations(); |
| 160 | EXPECT_EQ(1u, activations.size()); |
| 161 | const auto& activation = activations.find(version0)->second; |
| 162 | const auto& assocs = activation->associations(); |
| 163 | EXPECT_EQ(2u, assocs.size()); |
| 164 | EXPECT_EQ(psu0, std::get<2>(assocs[0])); |
| 165 | EXPECT_EQ(psu1, std::get<2>(assocs[1])); |
| 166 | } |
| 167 | |
| 168 | TEST_F(TestItemUpdater, CreateTwoPSUsWithDifferentVersion) |
| 169 | { |
| 170 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 171 | constexpr auto psu1 = "/com/example/inventory/psu1"; |
| 172 | constexpr auto service = "com.example.Software.Psu"; |
| 173 | auto version0 = std::string("version0"); |
| 174 | auto version1 = std::string("version1"); |
| 175 | auto objPath0 = getObjPath(version0); |
| 176 | auto objPath1 = getObjPath(version1); |
| 177 | |
| 178 | EXPECT_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 179 | .WillOnce(Return(std::vector<std::string>({psu0, psu1}))); |
| 180 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psu0), _)) |
| 181 | .WillOnce(Return(service)); |
| 182 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psu1), _)) |
| 183 | .WillOnce(Return(service)); |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 184 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psu0))) |
| 185 | .WillOnce(Return(std::string(version0))); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 186 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu0), _, |
| 187 | StrEq(PRESENT))) |
| 188 | .WillOnce(Return(any(PropertyType(true)))); // present |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 189 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psu1))) |
| 190 | .WillOnce(Return(std::string(version1))); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 191 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu1), _, |
| 192 | StrEq(PRESENT))) |
| 193 | .WillOnce(Return(any(PropertyType(true)))); // present |
| 194 | |
| 195 | // The item updater itself |
| 196 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath))) |
| 197 | .Times(1); |
| 198 | |
| 199 | // two new activation and version objects will be added |
| 200 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath0))) |
| 201 | .Times(2); |
| 202 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath1))) |
| 203 | .Times(2); |
| 204 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 205 | |
| 206 | // Verify there are two activations and each with one association |
| 207 | const auto& activations = GetActivations(); |
| 208 | EXPECT_EQ(2u, activations.size()); |
| 209 | const auto& activation0 = activations.find(version0)->second; |
| 210 | const auto& assocs0 = activation0->associations(); |
| 211 | EXPECT_EQ(1u, assocs0.size()); |
| 212 | EXPECT_EQ(psu0, std::get<2>(assocs0[0])); |
| 213 | |
| 214 | const auto& activation1 = activations.find(version1)->second; |
| 215 | const auto& assocs1 = activation1->associations(); |
| 216 | EXPECT_EQ(1u, assocs1.size()); |
| 217 | EXPECT_EQ(psu1, std::get<2>(assocs1[0])); |
| 218 | } |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 219 | |
| 220 | TEST_F(TestItemUpdater, OnOnePSURemoved) |
| 221 | { |
| 222 | constexpr auto psuPath = "/com/example/inventory/psu0"; |
| 223 | constexpr auto service = "com.example.Software.Psu"; |
| 224 | constexpr auto version = "version0"; |
| 225 | std::string objPath = getObjPath(version); |
| 226 | EXPECT_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 227 | .WillOnce(Return(std::vector<std::string>({psuPath}))); |
| 228 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psuPath), _)) |
| 229 | .WillOnce(Return(service)); |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 230 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psuPath))) |
| 231 | .WillOnce(Return(std::string(version))); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 232 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath), |
| 233 | _, StrEq(PRESENT))) |
| 234 | .WillOnce(Return(any(PropertyType(true)))); // present |
| 235 | |
| 236 | // The item updater itself |
| 237 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath))) |
| 238 | .Times(1); |
| 239 | |
| 240 | // activation and version object will be added |
| 241 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 242 | .Times(2); |
| 243 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 244 | |
| 245 | // the activation and version object will be removed |
| 246 | Properties p{{PRESENT, PropertyType(false)}}; |
| 247 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath))) |
| 248 | .Times(2); |
| 249 | onPsuInventoryChanged(psuPath, p); |
| 250 | |
| 251 | // on exit, item updater is removed |
| 252 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(dBusPath))) |
| 253 | .Times(1); |
| 254 | } |
| 255 | |
| 256 | TEST_F(TestItemUpdater, OnOnePSUAdded) |
| 257 | { |
| 258 | constexpr auto psuPath = "/com/example/inventory/psu0"; |
| 259 | constexpr auto service = "com.example.Software.Psu"; |
| 260 | constexpr auto version = "version0"; |
| 261 | std::string objPath = getObjPath(version); |
| 262 | EXPECT_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 263 | .WillOnce(Return(std::vector<std::string>({psuPath}))); |
| 264 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psuPath), _)) |
| 265 | .WillOnce(Return(service)); |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 266 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psuPath))) |
| 267 | .WillOnce(Return(std::string(version))); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 268 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath), |
| 269 | _, StrEq(PRESENT))) |
| 270 | .WillOnce(Return(any(PropertyType(false)))); // not present |
| 271 | |
| 272 | // The item updater itself |
| 273 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath))) |
| 274 | .Times(1); |
| 275 | |
| 276 | // No activation/version objects are created |
| 277 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 278 | .Times(0); |
| 279 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 280 | |
| 281 | // The PSU is present and version is added in a single call |
Lei YU | dcaf893 | 2019-09-09 16:09:35 +0800 | [diff] [blame] | 282 | Properties propAdded{{PRESENT, PropertyType(true)}}; |
| 283 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psuPath))) |
| 284 | .WillOnce(Return(std::string(version))); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 285 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 286 | .Times(2); |
| 287 | onPsuInventoryChanged(psuPath, propAdded); |
| 288 | } |
| 289 | |
| 290 | TEST_F(TestItemUpdater, OnOnePSURemovedAndAdded) |
| 291 | { |
| 292 | constexpr auto psuPath = "/com/example/inventory/psu0"; |
| 293 | constexpr auto service = "com.example.Software.Psu"; |
| 294 | constexpr auto version = "version0"; |
| 295 | std::string objPath = getObjPath(version); |
| 296 | EXPECT_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 297 | .WillOnce(Return(std::vector<std::string>({psuPath}))); |
| 298 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psuPath), _)) |
| 299 | .WillOnce(Return(service)); |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 300 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psuPath))) |
| 301 | .WillOnce(Return(std::string(version))); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 302 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath), |
| 303 | _, StrEq(PRESENT))) |
| 304 | .WillOnce(Return(any(PropertyType(true)))); // present |
| 305 | |
| 306 | // The item updater itself |
| 307 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath))) |
| 308 | .Times(1); |
| 309 | |
| 310 | // activation and version object will be added |
| 311 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 312 | .Times(2); |
| 313 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 314 | |
| 315 | // the activation and version object will be removed |
| 316 | Properties propRemoved{{PRESENT, PropertyType(false)}}; |
| 317 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath))) |
| 318 | .Times(2); |
| 319 | onPsuInventoryChanged(psuPath, propRemoved); |
| 320 | |
| 321 | Properties propAdded{{PRESENT, PropertyType(true)}}; |
Lei YU | dcaf893 | 2019-09-09 16:09:35 +0800 | [diff] [blame] | 322 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psuPath))) |
| 323 | .WillOnce(Return(std::string(version))); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 324 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 325 | .Times(2); |
| 326 | onPsuInventoryChanged(psuPath, propAdded); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 327 | |
| 328 | // on exit, objects are removed |
| 329 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath))) |
| 330 | .Times(2); |
| 331 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(dBusPath))) |
| 332 | .Times(1); |
| 333 | } |
| 334 | |
| 335 | TEST_F(TestItemUpdater, |
| 336 | TwoPSUsWithSameVersionRemovedAndAddedWithDifferntVersion) |
| 337 | { |
| 338 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 339 | constexpr auto psu1 = "/com/example/inventory/psu1"; |
| 340 | constexpr auto service = "com.example.Software.Psu"; |
| 341 | auto version0 = std::string("version0"); |
| 342 | auto version1 = std::string("version0"); |
| 343 | auto objPath0 = getObjPath(version0); |
| 344 | auto objPath1 = getObjPath(version1); |
| 345 | |
| 346 | EXPECT_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 347 | .WillOnce(Return(std::vector<std::string>({psu0, psu1}))); |
| 348 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psu0), _)) |
| 349 | .WillOnce(Return(service)); |
| 350 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psu1), _)) |
| 351 | .WillOnce(Return(service)); |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 352 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psu0))) |
| 353 | .WillOnce(Return(std::string(version0))); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 354 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu0), _, |
| 355 | StrEq(PRESENT))) |
| 356 | .WillOnce(Return(any(PropertyType(true)))); // present |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 357 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psu1))) |
| 358 | .WillOnce(Return(std::string(version1))); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 359 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu1), _, |
| 360 | StrEq(PRESENT))) |
| 361 | .WillOnce(Return(any(PropertyType(true)))); // present |
| 362 | |
| 363 | // The item updater itself |
| 364 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath))) |
| 365 | .Times(1); |
| 366 | |
| 367 | // activation and version object will be added |
| 368 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath0))) |
| 369 | .Times(2); |
| 370 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 371 | |
| 372 | // Verify there is only one activation and it has two associations |
| 373 | const auto& activations = GetActivations(); |
| 374 | EXPECT_EQ(1u, activations.size()); |
| 375 | const auto& activation = activations.find(version0)->second; |
| 376 | auto assocs = activation->associations(); |
| 377 | EXPECT_EQ(2u, assocs.size()); |
| 378 | EXPECT_EQ(psu0, std::get<2>(assocs[0])); |
| 379 | EXPECT_EQ(psu1, std::get<2>(assocs[1])); |
| 380 | |
| 381 | // PSU0 is removed, only associations shall be updated |
| 382 | Properties propRemoved{{PRESENT, PropertyType(false)}}; |
| 383 | onPsuInventoryChanged(psu0, propRemoved); |
| 384 | assocs = activation->associations(); |
| 385 | EXPECT_EQ(1u, assocs.size()); |
| 386 | EXPECT_EQ(psu1, std::get<2>(assocs[0])); |
| 387 | |
| 388 | // PSU1 is removed, the activation and version object shall be removed |
| 389 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath0))) |
| 390 | .Times(2); |
| 391 | onPsuInventoryChanged(psu1, propRemoved); |
| 392 | |
| 393 | // Add PSU0 and PSU1 back, but PSU1 with a different version |
| 394 | version1 = "version1"; |
| 395 | objPath1 = getObjPath(version1); |
Lei YU | dcaf893 | 2019-09-09 16:09:35 +0800 | [diff] [blame] | 396 | Properties propAdded0{{PRESENT, PropertyType(true)}}; |
| 397 | Properties propAdded1{{PRESENT, PropertyType(true)}}; |
| 398 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psu0))) |
| 399 | .WillOnce(Return(std::string(version0))); |
| 400 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psu1))) |
| 401 | .WillOnce(Return(std::string(version1))); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 402 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath0))) |
| 403 | .Times(2); |
| 404 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath1))) |
| 405 | .Times(2); |
| 406 | onPsuInventoryChanged(psu0, propAdded0); |
| 407 | onPsuInventoryChanged(psu1, propAdded1); |
| 408 | |
| 409 | // on exit, objects are removed |
| 410 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath0))) |
| 411 | .Times(2); |
| 412 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(objPath1))) |
| 413 | .Times(2); |
| 414 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_removed(_, StrEq(dBusPath))) |
| 415 | .Times(1); |
| 416 | } |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 417 | |
| 418 | TEST_F(TestItemUpdater, scanDirOnNoPSU) |
| 419 | { |
| 420 | constexpr auto psuPath = "/com/example/inventory/psu0"; |
| 421 | constexpr auto service = "com.example.Software.Psu"; |
| 422 | constexpr auto version = "version0"; |
| 423 | std::string objPath = getObjPath(version); |
| 424 | EXPECT_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 425 | .WillOnce(Return(std::vector<std::string>({psuPath}))); |
| 426 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psuPath), _)) |
| 427 | .WillOnce(Return(service)); |
| 428 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psuPath))) |
| 429 | .WillOnce(Return(std::string(version))); |
| 430 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath), |
| 431 | _, StrEq(PRESENT))) |
| 432 | .WillOnce(Return(any(PropertyType(false)))); // not present |
| 433 | |
| 434 | // The item updater itself |
| 435 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath))) |
| 436 | .Times(1); |
| 437 | |
| 438 | // No activation/version objects are created |
| 439 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 440 | .Times(0); |
| 441 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 442 | |
| 443 | // The valid image in test/psu-images-one-valid-one-invalid/model-1/ |
| 444 | auto objPathValid = getObjPath("psu-test.v0.4"); |
| 445 | auto objPathInvalid = getObjPath("psu-test.v0.5"); |
| 446 | // activation and version object will be added on scan dir |
| 447 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPathValid))) |
| 448 | .Times(2); |
| 449 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPathInvalid))) |
| 450 | .Times(0); |
| 451 | scanDirectory("./psu-images-one-valid-one-invalid"); |
| 452 | } |
| 453 | |
| 454 | TEST_F(TestItemUpdater, scanDirOnSamePSUVersion) |
| 455 | { |
| 456 | constexpr auto psuPath = "/com/example/inventory/psu0"; |
| 457 | constexpr auto service = "com.example.Software.Psu"; |
| 458 | constexpr auto version = "version0"; |
| 459 | std::string objPath = getObjPath(version); |
| 460 | EXPECT_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 461 | .WillOnce(Return(std::vector<std::string>({psuPath}))); |
| 462 | EXPECT_CALL(mockedUtils, getService(_, StrEq(psuPath), _)) |
| 463 | .WillOnce(Return(service)); |
| 464 | EXPECT_CALL(mockedUtils, getVersion(StrEq(psuPath))) |
| 465 | .WillOnce(Return(std::string(version))); |
| 466 | EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath), |
| 467 | _, StrEq(PRESENT))) |
| 468 | .WillOnce(Return(any(PropertyType(true)))); // present |
| 469 | |
| 470 | // The item updater itself |
| 471 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath))) |
| 472 | .Times(1); |
| 473 | |
| 474 | // activation and version object will be added |
| 475 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 476 | .Times(2); |
| 477 | itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath); |
| 478 | |
| 479 | // The valid image in test/psu-images-valid-version0/model-3/ has the same |
| 480 | // version as the running PSU, so no objects will be created, but only the |
| 481 | // path will be set to the version object |
| 482 | EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath))) |
| 483 | .Times(0); |
| 484 | EXPECT_CALL(sdbusMock, sd_bus_emit_properties_changed_strv( |
| 485 | _, StrEq(objPath), |
| 486 | StrEq("xyz.openbmc_project.Common.FilePath"), |
| 487 | Pointee(StrEq("Path")))) |
| 488 | .Times(1); |
| 489 | scanDirectory("./psu-images-valid-version0"); |
| 490 | } |