Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 1 | #include "activation.hpp" |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 2 | #include "mocked_association_interface.hpp" |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 3 | #include "mocked_utils.hpp" |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 4 | |
| 5 | #include <sdbusplus/test/sdbus_mock.hpp> |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | using namespace phosphor::software::updater; |
| 11 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 12 | using ::testing::_; |
| 13 | using ::testing::Return; |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 14 | using ::testing::StrEq; |
| 15 | |
| 16 | using std::experimental::any; |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 17 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 18 | class TestActivation : public ::testing::Test |
| 19 | { |
| 20 | public: |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 21 | using PropertyType = utils::UtilsInterface::PropertyType; |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 22 | using Status = Activation::Status; |
| 23 | using RequestedStatus = Activation::RequestedActivations; |
| 24 | TestActivation() : |
| 25 | mockedUtils( |
| 26 | reinterpret_cast<const utils::MockedUtils&>(utils::getUtils())) |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 27 | { |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 28 | // By default make it compatible with the test software |
| 29 | ON_CALL(mockedUtils, getPropertyImpl(_, _, _, _, StrEq(MANUFACTURER))) |
| 30 | .WillByDefault(Return(any(PropertyType(std::string("TestManu"))))); |
| 31 | ON_CALL(mockedUtils, getPropertyImpl(_, _, _, _, StrEq(MODEL))) |
| 32 | .WillByDefault(Return(any(PropertyType(std::string("TestModel"))))); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 33 | } |
| 34 | ~TestActivation() |
| 35 | { |
| 36 | } |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 37 | |
| 38 | void onUpdateDone() |
| 39 | { |
| 40 | activation->onUpdateDone(); |
| 41 | } |
| 42 | void onUpdateFailed() |
| 43 | { |
| 44 | activation->onUpdateFailed(); |
| 45 | } |
| 46 | int getProgress() |
| 47 | { |
| 48 | return activation->activationProgress->progress(); |
| 49 | } |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 50 | const auto& getPsuQueue() |
| 51 | { |
| 52 | return activation->psuQueue; |
| 53 | } |
| 54 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 55 | sdbusplus::SdBusMock sdbusMock; |
| 56 | sdbusplus::bus::bus mockedBus = sdbusplus::get_mocked_new(&sdbusMock); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 57 | const utils::MockedUtils& mockedUtils; |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 58 | MockedAssociationInterface mockedAssociationInterface; |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 59 | std::unique_ptr<Activation> activation; |
| 60 | std::string versionId = "abcdefgh"; |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 61 | std::string extVersion = "manufacturer=TestManu,model=TestModel"; |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 62 | std::string dBusPath = std::string(SOFTWARE_OBJPATH) + "/" + versionId; |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 63 | Status status = Status::Ready; |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 64 | AssociationList associations; |
| 65 | }; |
| 66 | |
| 67 | TEST_F(TestActivation, ctordtor) |
| 68 | { |
| 69 | activation = std::make_unique<Activation>(mockedBus, dBusPath, versionId, |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 70 | extVersion, status, associations, |
| 71 | &mockedAssociationInterface); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | namespace phosphor::software::updater::internal |
| 75 | { |
| 76 | extern std::string getUpdateService(const std::string& psuInventoryPath, |
| 77 | const std::string& versionId); |
| 78 | } |
| 79 | |
| 80 | TEST_F(TestActivation, getUpdateService) |
| 81 | { |
| 82 | std::string psuInventoryPath = "/com/example/inventory/powersupply1"; |
| 83 | std::string versionId = "12345678"; |
| 84 | std::string toCompare = "psu-update@-com-example-inventory-" |
| 85 | "powersupply1\\x20-tmp-images-12345678.service"; |
| 86 | |
| 87 | auto service = phosphor::software::updater::internal::getUpdateService( |
| 88 | psuInventoryPath, versionId); |
| 89 | EXPECT_EQ(toCompare, service); |
| 90 | } |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 91 | |
| 92 | TEST_F(TestActivation, doUpdateWhenNoPSU) |
| 93 | { |
| 94 | activation = std::make_unique<Activation>(mockedBus, dBusPath, versionId, |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 95 | extVersion, status, associations, |
| 96 | &mockedAssociationInterface); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 97 | ON_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 98 | .WillByDefault( |
| 99 | Return(std::vector<std::string>({}))); // No PSU inventory |
| 100 | activation->requestedActivation(RequestedStatus::Active); |
| 101 | |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 102 | EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath)) |
| 103 | .Times(0); |
| 104 | EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath)) |
| 105 | .Times(0); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 106 | EXPECT_EQ(Status::Failed, activation->activation()); |
| 107 | } |
| 108 | |
| 109 | TEST_F(TestActivation, doUpdateOnePSUOK) |
| 110 | { |
| 111 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 112 | activation = std::make_unique<Activation>(mockedBus, dBusPath, versionId, |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 113 | extVersion, status, associations, |
| 114 | &mockedAssociationInterface); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 115 | ON_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 116 | .WillByDefault( |
| 117 | Return(std::vector<std::string>({psu0}))); // One PSU inventory |
| 118 | activation->requestedActivation(RequestedStatus::Active); |
| 119 | |
| 120 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 121 | |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 122 | EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath)) |
| 123 | .Times(1); |
| 124 | EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath)) |
| 125 | .Times(1); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 126 | onUpdateDone(); |
| 127 | EXPECT_EQ(Status::Active, activation->activation()); |
| 128 | } |
| 129 | |
| 130 | TEST_F(TestActivation, doUpdateFourPSUsOK) |
| 131 | { |
| 132 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 133 | constexpr auto psu1 = "/com/example/inventory/psu1"; |
| 134 | constexpr auto psu2 = "/com/example/inventory/psu2"; |
| 135 | constexpr auto psu3 = "/com/example/inventory/psu3"; |
| 136 | activation = std::make_unique<Activation>(mockedBus, dBusPath, versionId, |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 137 | extVersion, status, associations, |
| 138 | &mockedAssociationInterface); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 139 | ON_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 140 | .WillByDefault(Return( |
| 141 | std::vector<std::string>({psu0, psu1, psu2, psu3}))); // 4 PSUs |
| 142 | activation->requestedActivation(RequestedStatus::Active); |
| 143 | |
| 144 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 145 | EXPECT_EQ(10, getProgress()); |
| 146 | |
| 147 | onUpdateDone(); |
| 148 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 149 | EXPECT_EQ(30, getProgress()); |
| 150 | |
| 151 | onUpdateDone(); |
| 152 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 153 | EXPECT_EQ(50, getProgress()); |
| 154 | |
| 155 | onUpdateDone(); |
| 156 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 157 | EXPECT_EQ(70, getProgress()); |
| 158 | |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 159 | EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath)) |
| 160 | .Times(1); |
| 161 | EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath)) |
| 162 | .Times(1); |
| 163 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 164 | onUpdateDone(); |
| 165 | EXPECT_EQ(Status::Active, activation->activation()); |
| 166 | } |
| 167 | |
| 168 | TEST_F(TestActivation, doUpdateFourPSUsFailonSecond) |
| 169 | { |
| 170 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 171 | constexpr auto psu1 = "/com/example/inventory/psu1"; |
| 172 | constexpr auto psu2 = "/com/example/inventory/psu2"; |
| 173 | constexpr auto psu3 = "/com/example/inventory/psu3"; |
| 174 | activation = std::make_unique<Activation>(mockedBus, dBusPath, versionId, |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 175 | extVersion, status, associations, |
| 176 | &mockedAssociationInterface); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 177 | ON_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 178 | .WillByDefault(Return( |
| 179 | std::vector<std::string>({psu0, psu1, psu2, psu3}))); // 4 PSUs |
| 180 | activation->requestedActivation(RequestedStatus::Active); |
| 181 | |
| 182 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 183 | EXPECT_EQ(10, getProgress()); |
| 184 | |
| 185 | onUpdateDone(); |
| 186 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 187 | EXPECT_EQ(30, getProgress()); |
| 188 | |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 189 | EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath)) |
| 190 | .Times(0); |
| 191 | EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath)) |
| 192 | .Times(0); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 193 | onUpdateFailed(); |
| 194 | EXPECT_EQ(Status::Failed, activation->activation()); |
| 195 | } |
| 196 | |
| 197 | TEST_F(TestActivation, doUpdateOnExceptionFromDbus) |
| 198 | { |
| 199 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 200 | activation = std::make_unique<Activation>(mockedBus, dBusPath, versionId, |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 201 | extVersion, status, associations, |
| 202 | &mockedAssociationInterface); |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame] | 203 | ON_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 204 | .WillByDefault( |
| 205 | Return(std::vector<std::string>({psu0}))); // One PSU inventory |
| 206 | ON_CALL(sdbusMock, sd_bus_call(_, _, _, _, nullptr)) |
| 207 | .WillByDefault(Return(-1)); // Make sdbus call failure |
| 208 | activation->requestedActivation(RequestedStatus::Active); |
| 209 | |
| 210 | EXPECT_EQ(Status::Failed, activation->activation()); |
| 211 | } |
Lei YU | 9edb733 | 2019-09-19 14:46:19 +0800 | [diff] [blame] | 212 | |
| 213 | TEST_F(TestActivation, doUpdateOnePSUModelNotCompatible) |
| 214 | { |
| 215 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 216 | extVersion = "manufacturer=TestManu,model=DifferentModel"; |
| 217 | activation = std::make_unique<Activation>(mockedBus, dBusPath, versionId, |
| 218 | extVersion, status, associations, |
| 219 | &mockedAssociationInterface); |
| 220 | ON_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 221 | .WillByDefault(Return(std::vector<std::string>({psu0}))); |
| 222 | activation->requestedActivation(RequestedStatus::Active); |
| 223 | |
| 224 | EXPECT_EQ(Status::Failed, activation->activation()); |
| 225 | } |
| 226 | |
| 227 | TEST_F(TestActivation, doUpdateOnePSUManufactureNotCompatible) |
| 228 | { |
| 229 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 230 | extVersion = "manufacturer=DifferentManu,model=TestModel"; |
| 231 | activation = std::make_unique<Activation>(mockedBus, dBusPath, versionId, |
| 232 | extVersion, status, associations, |
| 233 | &mockedAssociationInterface); |
| 234 | ON_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 235 | .WillByDefault(Return(std::vector<std::string>({psu0}))); |
| 236 | activation->requestedActivation(RequestedStatus::Active); |
| 237 | |
| 238 | EXPECT_EQ(Status::Failed, activation->activation()); |
| 239 | } |
| 240 | |
| 241 | TEST_F(TestActivation, doUpdateOnePSUSelfManufactureIsEmpty) |
| 242 | { |
| 243 | ON_CALL(mockedUtils, getPropertyImpl(_, _, _, _, StrEq(MANUFACTURER))) |
| 244 | .WillByDefault(Return(any(PropertyType(std::string(""))))); |
| 245 | extVersion = "manufacturer=AnyManu,model=TestModel"; |
| 246 | // Below is the same as doUpdateOnePSUOK case |
| 247 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 248 | activation = std::make_unique<Activation>(mockedBus, dBusPath, versionId, |
| 249 | extVersion, status, associations, |
| 250 | &mockedAssociationInterface); |
| 251 | ON_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 252 | .WillByDefault( |
| 253 | Return(std::vector<std::string>({psu0}))); // One PSU inventory |
| 254 | activation->requestedActivation(RequestedStatus::Active); |
| 255 | |
| 256 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 257 | |
| 258 | EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath)) |
| 259 | .Times(1); |
| 260 | EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath)) |
| 261 | .Times(1); |
| 262 | onUpdateDone(); |
| 263 | EXPECT_EQ(Status::Active, activation->activation()); |
| 264 | } |
| 265 | |
| 266 | TEST_F(TestActivation, doUpdateFourPSUsSecondPSUNotCompatible) |
| 267 | { |
| 268 | constexpr auto psu0 = "/com/example/inventory/psu0"; |
| 269 | constexpr auto psu1 = "/com/example/inventory/psu1"; |
| 270 | constexpr auto psu2 = "/com/example/inventory/psu2"; |
| 271 | constexpr auto psu3 = "/com/example/inventory/psu3"; |
| 272 | ON_CALL(mockedUtils, getPropertyImpl(_, _, StrEq(psu1), _, StrEq(MODEL))) |
| 273 | .WillByDefault( |
| 274 | Return(any(PropertyType(std::string("DifferentModel"))))); |
| 275 | activation = std::make_unique<Activation>(mockedBus, dBusPath, versionId, |
| 276 | extVersion, status, associations, |
| 277 | &mockedAssociationInterface); |
| 278 | ON_CALL(mockedUtils, getPSUInventoryPath(_)) |
| 279 | .WillByDefault(Return( |
| 280 | std::vector<std::string>({psu0, psu1, psu2, psu3}))); // 4 PSUs |
| 281 | activation->requestedActivation(RequestedStatus::Active); |
| 282 | |
| 283 | const auto& psuQueue = getPsuQueue(); |
| 284 | EXPECT_EQ(3u, psuQueue.size()); |
| 285 | |
| 286 | // Only 3 PSUs shall be updated, and psu1 shall be skipped |
| 287 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 288 | EXPECT_EQ(10, getProgress()); |
| 289 | |
| 290 | onUpdateDone(); |
| 291 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 292 | EXPECT_EQ(36, getProgress()); |
| 293 | |
| 294 | onUpdateDone(); |
| 295 | EXPECT_EQ(Status::Activating, activation->activation()); |
| 296 | EXPECT_EQ(62, getProgress()); |
| 297 | |
| 298 | EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath)) |
| 299 | .Times(1); |
| 300 | EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath)) |
| 301 | .Times(1); |
| 302 | |
| 303 | onUpdateDone(); |
| 304 | EXPECT_EQ(Status::Active, activation->activation()); |
| 305 | } |