blob: 97cba779d8b65777e2d488b42cef25e29794af52 [file] [log] [blame]
Lei YU12c9f4c2019-09-11 15:08:15 +08001#include "activation.hpp"
Lei YUffb36532019-10-15 13:55:24 +08002#include "mocked_activation_listener.hpp"
Lei YU7f2a2152019-09-16 16:50:18 +08003#include "mocked_association_interface.hpp"
Lei YUff83c2a2019-09-12 13:55:18 +08004#include "mocked_utils.hpp"
Lei YU12c9f4c2019-09-11 15:08:15 +08005
6#include <sdbusplus/test/sdbus_mock.hpp>
7
8#include <gmock/gmock.h>
9#include <gtest/gtest.h>
10
11using namespace phosphor::software::updater;
12
Lei YUff83c2a2019-09-12 13:55:18 +080013using ::testing::_;
Shawn McCarneyb8590f52025-01-13 13:09:22 -060014using ::testing::NiceMock;
Lei YUff83c2a2019-09-12 13:55:18 +080015using ::testing::Return;
Lei YU9edb7332019-09-19 14:46:19 +080016using ::testing::StrEq;
17
Patrick Williamsb5f9b822022-06-16 17:26:17 -050018using std::any;
Lei YUff83c2a2019-09-12 13:55:18 +080019
Lei YU12c9f4c2019-09-11 15:08:15 +080020class TestActivation : public ::testing::Test
21{
22 public:
Lei YU9edb7332019-09-19 14:46:19 +080023 using PropertyType = utils::UtilsInterface::PropertyType;
Lei YUff83c2a2019-09-12 13:55:18 +080024 using Status = Activation::Status;
25 using RequestedStatus = Activation::RequestedActivations;
George Liu66a54ad2024-08-23 13:53:39 +080026
27 TestActivation(const TestActivation&) = delete;
28 TestActivation& operator=(const TestActivation&) = delete;
29 TestActivation(TestActivation&&) = delete;
30 TestActivation& operator=(TestActivation&&) = delete;
31
Lei YUff83c2a2019-09-12 13:55:18 +080032 TestActivation() :
33 mockedUtils(
34 reinterpret_cast<const utils::MockedUtils&>(utils::getUtils()))
Lei YU12c9f4c2019-09-11 15:08:15 +080035 {
Shawn McCarney46ea3882024-12-10 11:25:38 -060036 // By default make PSU present
37 ON_CALL(mockedUtils, getPropertyImpl(_, _, _, _, StrEq(PRESENT)))
38 .WillByDefault(Return(any(PropertyType(true))));
39
40 // By default make PSU compatible with the test software
Lei YU9edb7332019-09-19 14:46:19 +080041 ON_CALL(mockedUtils, getPropertyImpl(_, _, _, _, StrEq(MANUFACTURER)))
42 .WillByDefault(Return(any(PropertyType(std::string("TestManu")))));
Shawn McCarney783406e2024-11-17 21:49:37 -060043 ON_CALL(mockedUtils, getModel(_))
44 .WillByDefault(Return(std::string("TestModel")));
Lei YU63f9e712019-10-12 15:16:55 +080045 ON_CALL(mockedUtils, isAssociated(_, _)).WillByDefault(Return(false));
Lei YU12c9f4c2019-09-11 15:08:15 +080046 }
George Liu047d9942024-08-23 13:44:31 +080047 ~TestActivation() override
Lei YU12c9f4c2019-09-11 15:08:15 +080048 {
Lei YUc09155b2019-10-11 17:30:48 +080049 utils::freeUtils();
Lei YU12c9f4c2019-09-11 15:08:15 +080050 }
Lei YUff83c2a2019-09-12 13:55:18 +080051
George Liu71ae5352024-08-23 15:21:05 +080052 void onUpdateDone() const
Lei YUff83c2a2019-09-12 13:55:18 +080053 {
54 activation->onUpdateDone();
55 }
George Liu71ae5352024-08-23 15:21:05 +080056 void onUpdateFailed() const
Lei YUff83c2a2019-09-12 13:55:18 +080057 {
58 activation->onUpdateFailed();
59 }
George Liu71ae5352024-08-23 15:21:05 +080060 int getProgress() const
Lei YUff83c2a2019-09-12 13:55:18 +080061 {
62 return activation->activationProgress->progress();
63 }
George Liu71ae5352024-08-23 15:21:05 +080064 const auto& getPsuQueue() const
Lei YU9edb7332019-09-19 14:46:19 +080065 {
66 return activation->psuQueue;
67 }
George Liu71ae5352024-08-23 15:21:05 +080068 std::string getUpdateService(const std::string& psuInventoryPath) const
Lei YUe8945ea2019-09-29 17:25:31 +080069 {
70 return activation->getUpdateService(psuInventoryPath);
71 }
Lei YU9edb7332019-09-19 14:46:19 +080072
Shawn McCarneyb8590f52025-01-13 13:09:22 -060073 NiceMock<sdbusplus::SdBusMock> sdbusMock;
Patrick Williams374fae52022-07-22 19:26:55 -050074 sdbusplus::bus_t mockedBus = sdbusplus::get_mocked_new(&sdbusMock);
Lei YUff83c2a2019-09-12 13:55:18 +080075 const utils::MockedUtils& mockedUtils;
Lei YU7f2a2152019-09-16 16:50:18 +080076 MockedAssociationInterface mockedAssociationInterface;
Lei YUffb36532019-10-15 13:55:24 +080077 MockedActivationListener mockedActivationListener;
Lei YU12c9f4c2019-09-11 15:08:15 +080078 std::unique_ptr<Activation> activation;
79 std::string versionId = "abcdefgh";
Lei YU9edb7332019-09-19 14:46:19 +080080 std::string extVersion = "manufacturer=TestManu,model=TestModel";
Lei YU63f9e712019-10-12 15:16:55 +080081 std::string filePath = "/tmp/images/abcdefgh";
Lei YU7f2a2152019-09-16 16:50:18 +080082 std::string dBusPath = std::string(SOFTWARE_OBJPATH) + "/" + versionId;
Lei YUff83c2a2019-09-12 13:55:18 +080083 Status status = Status::Ready;
Lei YU12c9f4c2019-09-11 15:08:15 +080084 AssociationList associations;
85};
86
87TEST_F(TestActivation, ctordtor)
88{
Lei YU99301372019-09-29 16:27:12 +080089 activation = std::make_unique<Activation>(
90 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +080091 filePath, &mockedAssociationInterface, &mockedActivationListener);
Lei YU12c9f4c2019-09-11 15:08:15 +080092}
93
Lei YU58c26e32019-09-27 17:52:06 +080094TEST_F(TestActivation, ctorWithInvalidExtVersion)
95{
96 extVersion = "invalid text";
97 activation = std::make_unique<Activation>(
98 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +080099 filePath, &mockedAssociationInterface, &mockedActivationListener);
Lei YU58c26e32019-09-27 17:52:06 +0800100}
101
Lei YU12c9f4c2019-09-11 15:08:15 +0800102TEST_F(TestActivation, getUpdateService)
103{
104 std::string psuInventoryPath = "/com/example/inventory/powersupply1";
Lei YU12c9f4c2019-09-11 15:08:15 +0800105 std::string toCompare = "psu-update@-com-example-inventory-"
106 "powersupply1\\x20-tmp-images-12345678.service";
Lei YUe8945ea2019-09-29 17:25:31 +0800107 versionId = "12345678";
108 filePath = "/tmp/images/12345678";
Lei YU12c9f4c2019-09-11 15:08:15 +0800109
Lei YUe8945ea2019-09-29 17:25:31 +0800110 activation = std::make_unique<Activation>(
111 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800112 filePath, &mockedAssociationInterface, &mockedActivationListener);
Lei YUe8945ea2019-09-29 17:25:31 +0800113
114 auto service = getUpdateService(psuInventoryPath);
Lei YU12c9f4c2019-09-11 15:08:15 +0800115 EXPECT_EQ(toCompare, service);
116}
Lei YUff83c2a2019-09-12 13:55:18 +0800117
118TEST_F(TestActivation, doUpdateWhenNoPSU)
119{
Lei YU99301372019-09-29 16:27:12 +0800120 activation = std::make_unique<Activation>(
121 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800122 filePath, &mockedAssociationInterface, &mockedActivationListener);
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600123 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YUff83c2a2019-09-12 13:55:18 +0800124 .WillByDefault(
125 Return(std::vector<std::string>({}))); // No PSU inventory
126 activation->requestedActivation(RequestedStatus::Active);
127
Lei YU7f2a2152019-09-16 16:50:18 +0800128 EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath))
129 .Times(0);
130 EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath))
131 .Times(0);
Lei YUa8b966f2020-03-18 10:32:24 +0800132 EXPECT_CALL(mockedAssociationInterface, addUpdateableAssociation(dBusPath))
133 .Times(0);
Lei YUffb36532019-10-15 13:55:24 +0800134 EXPECT_CALL(mockedActivationListener, onUpdateDone(_, _)).Times(0);
Lei YUff83c2a2019-09-12 13:55:18 +0800135 EXPECT_EQ(Status::Failed, activation->activation());
136}
137
138TEST_F(TestActivation, doUpdateOnePSUOK)
139{
140 constexpr auto psu0 = "/com/example/inventory/psu0";
Lei YU99301372019-09-29 16:27:12 +0800141 activation = std::make_unique<Activation>(
142 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800143 filePath, &mockedAssociationInterface, &mockedActivationListener);
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600144 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YUff83c2a2019-09-12 13:55:18 +0800145 .WillByDefault(
146 Return(std::vector<std::string>({psu0}))); // One PSU inventory
147 activation->requestedActivation(RequestedStatus::Active);
148
149 EXPECT_EQ(Status::Activating, activation->activation());
150
Lei YU7f2a2152019-09-16 16:50:18 +0800151 EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath))
152 .Times(1);
153 EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath))
154 .Times(1);
Lei YUa8b966f2020-03-18 10:32:24 +0800155 EXPECT_CALL(mockedAssociationInterface, addUpdateableAssociation(dBusPath))
156 .Times(1);
Lei YUffb36532019-10-15 13:55:24 +0800157 EXPECT_CALL(mockedActivationListener,
158 onUpdateDone(StrEq(versionId), StrEq(psu0)))
159 .Times(1);
Lei YUff83c2a2019-09-12 13:55:18 +0800160 onUpdateDone();
161 EXPECT_EQ(Status::Active, activation->activation());
162}
163
164TEST_F(TestActivation, doUpdateFourPSUsOK)
165{
166 constexpr auto psu0 = "/com/example/inventory/psu0";
167 constexpr auto psu1 = "/com/example/inventory/psu1";
168 constexpr auto psu2 = "/com/example/inventory/psu2";
169 constexpr auto psu3 = "/com/example/inventory/psu3";
Lei YU99301372019-09-29 16:27:12 +0800170 activation = std::make_unique<Activation>(
171 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800172 filePath, &mockedAssociationInterface, &mockedActivationListener);
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600173 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YUff83c2a2019-09-12 13:55:18 +0800174 .WillByDefault(Return(
175 std::vector<std::string>({psu0, psu1, psu2, psu3}))); // 4 PSUs
176 activation->requestedActivation(RequestedStatus::Active);
177
178 EXPECT_EQ(Status::Activating, activation->activation());
179 EXPECT_EQ(10, getProgress());
180
Lei YUffb36532019-10-15 13:55:24 +0800181 EXPECT_CALL(mockedActivationListener,
182 onUpdateDone(StrEq(versionId), StrEq(psu0)))
183 .Times(1);
Lei YUff83c2a2019-09-12 13:55:18 +0800184 onUpdateDone();
185 EXPECT_EQ(Status::Activating, activation->activation());
186 EXPECT_EQ(30, getProgress());
187
Lei YUffb36532019-10-15 13:55:24 +0800188 EXPECT_CALL(mockedActivationListener,
189 onUpdateDone(StrEq(versionId), StrEq(psu1)))
190 .Times(1);
Lei YUff83c2a2019-09-12 13:55:18 +0800191 onUpdateDone();
192 EXPECT_EQ(Status::Activating, activation->activation());
193 EXPECT_EQ(50, getProgress());
194
Lei YUffb36532019-10-15 13:55:24 +0800195 EXPECT_CALL(mockedActivationListener,
196 onUpdateDone(StrEq(versionId), StrEq(psu2)))
197 .Times(1);
Lei YUff83c2a2019-09-12 13:55:18 +0800198 onUpdateDone();
199 EXPECT_EQ(Status::Activating, activation->activation());
200 EXPECT_EQ(70, getProgress());
201
Lei YU7f2a2152019-09-16 16:50:18 +0800202 EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath))
203 .Times(1);
204 EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath))
205 .Times(1);
Lei YUa8b966f2020-03-18 10:32:24 +0800206 EXPECT_CALL(mockedAssociationInterface, addUpdateableAssociation(dBusPath))
207 .Times(1);
Lei YU7f2a2152019-09-16 16:50:18 +0800208
Lei YUffb36532019-10-15 13:55:24 +0800209 EXPECT_CALL(mockedActivationListener,
210 onUpdateDone(StrEq(versionId), StrEq(psu3)))
211 .Times(1);
Lei YUff83c2a2019-09-12 13:55:18 +0800212 onUpdateDone();
213 EXPECT_EQ(Status::Active, activation->activation());
214}
215
216TEST_F(TestActivation, doUpdateFourPSUsFailonSecond)
217{
218 constexpr auto psu0 = "/com/example/inventory/psu0";
219 constexpr auto psu1 = "/com/example/inventory/psu1";
220 constexpr auto psu2 = "/com/example/inventory/psu2";
221 constexpr auto psu3 = "/com/example/inventory/psu3";
Lei YU99301372019-09-29 16:27:12 +0800222 activation = std::make_unique<Activation>(
223 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800224 filePath, &mockedAssociationInterface, &mockedActivationListener);
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600225 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YUff83c2a2019-09-12 13:55:18 +0800226 .WillByDefault(Return(
227 std::vector<std::string>({psu0, psu1, psu2, psu3}))); // 4 PSUs
228 activation->requestedActivation(RequestedStatus::Active);
229
230 EXPECT_EQ(Status::Activating, activation->activation());
231 EXPECT_EQ(10, getProgress());
232
Lei YUffb36532019-10-15 13:55:24 +0800233 EXPECT_CALL(mockedActivationListener,
234 onUpdateDone(StrEq(versionId), StrEq(psu0)))
235 .Times(1);
Lei YUff83c2a2019-09-12 13:55:18 +0800236 onUpdateDone();
237 EXPECT_EQ(Status::Activating, activation->activation());
238 EXPECT_EQ(30, getProgress());
239
Lei YU7f2a2152019-09-16 16:50:18 +0800240 EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath))
241 .Times(0);
242 EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath))
243 .Times(0);
Lei YUa8b966f2020-03-18 10:32:24 +0800244 EXPECT_CALL(mockedAssociationInterface, addUpdateableAssociation(dBusPath))
245 .Times(0);
Lei YUffb36532019-10-15 13:55:24 +0800246 EXPECT_CALL(mockedActivationListener, onUpdateDone(_, _)).Times(0);
Lei YUff83c2a2019-09-12 13:55:18 +0800247 onUpdateFailed();
248 EXPECT_EQ(Status::Failed, activation->activation());
249}
250
251TEST_F(TestActivation, doUpdateOnExceptionFromDbus)
252{
253 constexpr auto psu0 = "/com/example/inventory/psu0";
Lei YU99301372019-09-29 16:27:12 +0800254 activation = std::make_unique<Activation>(
255 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800256 filePath, &mockedAssociationInterface, &mockedActivationListener);
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600257 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YUff83c2a2019-09-12 13:55:18 +0800258 .WillByDefault(
259 Return(std::vector<std::string>({psu0}))); // One PSU inventory
260 ON_CALL(sdbusMock, sd_bus_call(_, _, _, _, nullptr))
261 .WillByDefault(Return(-1)); // Make sdbus call failure
262 activation->requestedActivation(RequestedStatus::Active);
263
264 EXPECT_EQ(Status::Failed, activation->activation());
265}
Lei YU9edb7332019-09-19 14:46:19 +0800266
Shawn McCarney46ea3882024-12-10 11:25:38 -0600267TEST_F(TestActivation, doUpdateOnePSUNotPresent)
268{
269 constexpr auto psu0 = "/com/example/inventory/psu0";
270 activation = std::make_unique<Activation>(
271 mockedBus, dBusPath, versionId, extVersion, status, associations,
272 filePath, &mockedAssociationInterface, &mockedActivationListener);
273 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
274 .WillByDefault(Return(std::vector<std::string>({psu0})));
Shawn McCarneyb8590f52025-01-13 13:09:22 -0600275 ON_CALL(mockedUtils, getPropertyImpl(_, _, _, _, StrEq(PRESENT)))
276 .WillByDefault(Return(any(PropertyType(false)))); // not present
Shawn McCarney46ea3882024-12-10 11:25:38 -0600277 activation->requestedActivation(RequestedStatus::Active);
278
279 EXPECT_EQ(Status::Ready, activation->activation());
280}
281
Lei YU9edb7332019-09-19 14:46:19 +0800282TEST_F(TestActivation, doUpdateOnePSUModelNotCompatible)
283{
284 constexpr auto psu0 = "/com/example/inventory/psu0";
285 extVersion = "manufacturer=TestManu,model=DifferentModel";
Lei YU99301372019-09-29 16:27:12 +0800286 activation = std::make_unique<Activation>(
287 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800288 filePath, &mockedAssociationInterface, &mockedActivationListener);
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600289 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YU9edb7332019-09-19 14:46:19 +0800290 .WillByDefault(Return(std::vector<std::string>({psu0})));
291 activation->requestedActivation(RequestedStatus::Active);
292
Lei YU63f9e712019-10-12 15:16:55 +0800293 EXPECT_EQ(Status::Ready, activation->activation());
Lei YU9edb7332019-09-19 14:46:19 +0800294}
295
296TEST_F(TestActivation, doUpdateOnePSUManufactureNotCompatible)
297{
298 constexpr auto psu0 = "/com/example/inventory/psu0";
299 extVersion = "manufacturer=DifferentManu,model=TestModel";
Lei YU99301372019-09-29 16:27:12 +0800300 activation = std::make_unique<Activation>(
301 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800302 filePath, &mockedAssociationInterface, &mockedActivationListener);
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600303 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YU9edb7332019-09-19 14:46:19 +0800304 .WillByDefault(Return(std::vector<std::string>({psu0})));
305 activation->requestedActivation(RequestedStatus::Active);
306
Lei YU63f9e712019-10-12 15:16:55 +0800307 EXPECT_EQ(Status::Ready, activation->activation());
Lei YU9edb7332019-09-19 14:46:19 +0800308}
309
310TEST_F(TestActivation, doUpdateOnePSUSelfManufactureIsEmpty)
311{
312 ON_CALL(mockedUtils, getPropertyImpl(_, _, _, _, StrEq(MANUFACTURER)))
313 .WillByDefault(Return(any(PropertyType(std::string("")))));
314 extVersion = "manufacturer=AnyManu,model=TestModel";
315 // Below is the same as doUpdateOnePSUOK case
316 constexpr auto psu0 = "/com/example/inventory/psu0";
Lei YU99301372019-09-29 16:27:12 +0800317 activation = std::make_unique<Activation>(
318 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800319 filePath, &mockedAssociationInterface, &mockedActivationListener);
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600320 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YU9edb7332019-09-19 14:46:19 +0800321 .WillByDefault(
322 Return(std::vector<std::string>({psu0}))); // One PSU inventory
323 activation->requestedActivation(RequestedStatus::Active);
324
325 EXPECT_EQ(Status::Activating, activation->activation());
326
327 EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath))
328 .Times(1);
329 EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath))
330 .Times(1);
Lei YUa8b966f2020-03-18 10:32:24 +0800331 EXPECT_CALL(mockedAssociationInterface, addUpdateableAssociation(dBusPath))
332 .Times(1);
Lei YU9edb7332019-09-19 14:46:19 +0800333 onUpdateDone();
334 EXPECT_EQ(Status::Active, activation->activation());
335}
336
337TEST_F(TestActivation, doUpdateFourPSUsSecondPSUNotCompatible)
338{
339 constexpr auto psu0 = "/com/example/inventory/psu0";
340 constexpr auto psu1 = "/com/example/inventory/psu1";
341 constexpr auto psu2 = "/com/example/inventory/psu2";
342 constexpr auto psu3 = "/com/example/inventory/psu3";
Shawn McCarney783406e2024-11-17 21:49:37 -0600343 ON_CALL(mockedUtils, getModel(StrEq(psu1)))
344 .WillByDefault(Return(std::string("DifferentModel")));
Lei YU99301372019-09-29 16:27:12 +0800345 activation = std::make_unique<Activation>(
346 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800347 filePath, &mockedAssociationInterface, &mockedActivationListener);
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600348 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YU9edb7332019-09-19 14:46:19 +0800349 .WillByDefault(Return(
350 std::vector<std::string>({psu0, psu1, psu2, psu3}))); // 4 PSUs
351 activation->requestedActivation(RequestedStatus::Active);
352
353 const auto& psuQueue = getPsuQueue();
George Liu22c2fbd2024-08-23 15:23:21 +0800354 EXPECT_EQ(3U, psuQueue.size());
Lei YU9edb7332019-09-19 14:46:19 +0800355
356 // Only 3 PSUs shall be updated, and psu1 shall be skipped
357 EXPECT_EQ(Status::Activating, activation->activation());
358 EXPECT_EQ(10, getProgress());
359
360 onUpdateDone();
361 EXPECT_EQ(Status::Activating, activation->activation());
362 EXPECT_EQ(36, getProgress());
363
364 onUpdateDone();
365 EXPECT_EQ(Status::Activating, activation->activation());
366 EXPECT_EQ(62, getProgress());
367
368 EXPECT_CALL(mockedAssociationInterface, createActiveAssociation(dBusPath))
369 .Times(1);
370 EXPECT_CALL(mockedAssociationInterface, addFunctionalAssociation(dBusPath))
371 .Times(1);
Lei YUa8b966f2020-03-18 10:32:24 +0800372 EXPECT_CALL(mockedAssociationInterface, addUpdateableAssociation(dBusPath))
373 .Times(1);
Lei YU9edb7332019-09-19 14:46:19 +0800374
375 onUpdateDone();
376 EXPECT_EQ(Status::Active, activation->activation());
377}
Lei YU63f9e712019-10-12 15:16:55 +0800378
379TEST_F(TestActivation, doUpdateWhenNoFilePathInActiveState)
380{
381 filePath = "";
382 status = Status::Active; // Typically, a running PSU software is active
383 // without file path
384 constexpr auto psu0 = "/com/example/inventory/psu0";
385 activation = std::make_unique<Activation>(
386 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800387 filePath, &mockedAssociationInterface, &mockedActivationListener);
Lei YU63f9e712019-10-12 15:16:55 +0800388
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600389 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YU63f9e712019-10-12 15:16:55 +0800390 .WillByDefault(
391 Return(std::vector<std::string>({psu0}))); // One PSU inventory
392
393 // There shall be no DBus call to start update service
394 EXPECT_CALL(sdbusMock, sd_bus_message_new_method_call(_, _, _, _, _,
395 StrEq("StartUnit")))
396 .Times(0);
397
398 activation->requestedActivation(RequestedStatus::Active);
399 EXPECT_EQ(Status::Active, activation->activation());
400}
401
402TEST_F(TestActivation, doUpdateWhenNoFilePathInReadyState)
403{
404 filePath = "";
405 status = Status::Ready; // Usually a Ready activation should have file path,
406 // but we are testing this case as well
407 constexpr auto psu0 = "/com/example/inventory/psu0";
408 activation = std::make_unique<Activation>(
409 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800410 filePath, &mockedAssociationInterface, &mockedActivationListener);
Lei YU63f9e712019-10-12 15:16:55 +0800411
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600412 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YU63f9e712019-10-12 15:16:55 +0800413 .WillByDefault(
414 Return(std::vector<std::string>({psu0}))); // One PSU inventory
415
416 // There shall be no DBus call to start update service
417 EXPECT_CALL(sdbusMock, sd_bus_message_new_method_call(_, _, _, _, _,
418 StrEq("StartUnit")))
419 .Times(0);
420
421 activation->requestedActivation(RequestedStatus::Active);
422 EXPECT_EQ(Status::Ready, activation->activation());
423}
424
425TEST_F(TestActivation, doUpdateWhenPSUIsAssociated)
426{
427 constexpr auto psu0 = "/com/example/inventory/psu0";
428 status = Status::Active; // Typically, a running PSU software is associated
429 activation = std::make_unique<Activation>(
430 mockedBus, dBusPath, versionId, extVersion, status, associations,
Lei YUffb36532019-10-15 13:55:24 +0800431 filePath, &mockedAssociationInterface, &mockedActivationListener);
Lei YU63f9e712019-10-12 15:16:55 +0800432
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600433 ON_CALL(mockedUtils, getPSUInventoryPaths(_))
Lei YU63f9e712019-10-12 15:16:55 +0800434 .WillByDefault(
435 Return(std::vector<std::string>({psu0}))); // One PSU inventory
436
437 // When PSU is already associated, there shall be no DBus call to start
438 // update service
439 EXPECT_CALL(mockedUtils, isAssociated(StrEq(psu0), _))
440 .WillOnce(Return(true));
441 EXPECT_CALL(sdbusMock, sd_bus_message_new_method_call(_, _, _, _, _,
442 StrEq("StartUnit")))
443 .Times(0);
444
445 activation->requestedActivation(RequestedStatus::Active);
446 EXPECT_EQ(Status::Active, activation->activation());
447}