blob: 2b9af43301de3813b41c29e9dcb17ada6d9d41dc [file] [log] [blame]
Lei YUf77189f2019-08-07 14:26:30 +08001#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
9using namespace phosphor::software::updater;
10using ::testing::_;
11using ::testing::Return;
12using ::testing::ReturnArg;
13using ::testing::StrEq;
14
15using std::experimental::any;
16using PropertyType = utils::UtilsInterface::PropertyType;
17
18class TestItemUpdater : public ::testing::Test
19{
20 public:
21 TestItemUpdater() :
22 mockedUtils(
23 reinterpret_cast<const utils::MockedUtils&>(utils::getUtils()))
24 {
25 ON_CALL(mockedUtils, getVersionId(_)).WillByDefault(ReturnArg<0>());
26 }
27
28 ~TestItemUpdater()
29 {
30 }
31
32 const auto& GetActivations()
33 {
34 return itemUpdater->activations;
35 }
36
37 std::string getObjPath(const std::string& versionId)
38 {
39 return std::string(dBusPath) + "/" + versionId;
40 }
41
42 static constexpr auto dBusPath = SOFTWARE_OBJPATH;
43 sdbusplus::SdBusMock sdbusMock;
44 sdbusplus::bus::bus mockedBus = sdbusplus::get_mocked_new(&sdbusMock);
45 const utils::MockedUtils& mockedUtils;
46 std::unique_ptr<ItemUpdater> itemUpdater;
47};
48
49TEST_F(TestItemUpdater, ctordtor)
50{
51 itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath);
52}
53
54TEST_F(TestItemUpdater, NotCreateObjectOnNotPresent)
55{
56 constexpr auto psuPath = "/com/example/inventory/psu0";
57 constexpr auto service = "com.example.Software.Psu";
58 constexpr auto version = "version0";
59 std::string objPath = getObjPath(version);
60 EXPECT_CALL(mockedUtils, getPSUInventoryPath(_))
61 .WillOnce(Return(std::vector<std::string>({psuPath})));
62 EXPECT_CALL(mockedUtils, getService(_, StrEq(psuPath), _))
63 .WillOnce(Return(service));
64 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath),
65 _, StrEq(VERSION)))
66 .WillOnce(Return(any(PropertyType(std::string(version)))));
67 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath),
68 _, StrEq(PRESENT)))
69 .WillOnce(Return(any(PropertyType(false)))); // not present
70
71 // The item updater itself
72 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath)))
73 .Times(1);
74
75 // No activation/version objects are created
76 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath)))
77 .Times(0);
78 itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath);
79}
80
81TEST_F(TestItemUpdater, CreateOnePSUOnPresent)
82{
83 constexpr auto psuPath = "/com/example/inventory/psu0";
84 constexpr auto service = "com.example.Software.Psu";
85 constexpr auto version = "version0";
86 std::string objPath = getObjPath(version);
87 EXPECT_CALL(mockedUtils, getPSUInventoryPath(_))
88 .WillOnce(Return(std::vector<std::string>({psuPath})));
89 EXPECT_CALL(mockedUtils, getService(_, StrEq(psuPath), _))
90 .WillOnce(Return(service));
91 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath),
92 _, StrEq(VERSION)))
93 .WillOnce(Return(any(PropertyType(std::string(version)))));
94 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psuPath),
95 _, StrEq(PRESENT)))
96 .WillOnce(Return(any(PropertyType(true)))); // present
97
98 // The item updater itself
99 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath)))
100 .Times(1);
101
102 // activation and version object will be added
103 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath)))
104 .Times(2);
105 itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath);
106}
107
108TEST_F(TestItemUpdater, CreateTwoPSUsWithSameVersion)
109{
110 constexpr auto psu0 = "/com/example/inventory/psu0";
111 constexpr auto psu1 = "/com/example/inventory/psu1";
112 constexpr auto service = "com.example.Software.Psu";
113 auto version0 = std::string("version0");
114 auto version1 = std::string("version0");
115 auto objPath0 = getObjPath(version0);
116 auto objPath1 = getObjPath(version1);
117
118 EXPECT_CALL(mockedUtils, getPSUInventoryPath(_))
119 .WillOnce(Return(std::vector<std::string>({psu0, psu1})));
120 EXPECT_CALL(mockedUtils, getService(_, StrEq(psu0), _))
121 .WillOnce(Return(service));
122 EXPECT_CALL(mockedUtils, getService(_, StrEq(psu1), _))
123 .WillOnce(Return(service));
124 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu0), _,
125 StrEq(VERSION)))
126 .WillOnce(Return(any(PropertyType(version0))));
127 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu0), _,
128 StrEq(PRESENT)))
129 .WillOnce(Return(any(PropertyType(true)))); // present
130 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu1), _,
131 StrEq(VERSION)))
132 .WillOnce(Return(any(PropertyType(version1))));
133 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu1), _,
134 StrEq(PRESENT)))
135 .WillOnce(Return(any(PropertyType(true)))); // present
136
137 // The item updater itself
138 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath)))
139 .Times(1);
140
141 // activation and version object will be added
142 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath0)))
143 .Times(2);
144 itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath);
145
146 // Verify there is only one activation and it has two associations
147 const auto& activations = GetActivations();
148 EXPECT_EQ(1u, activations.size());
149 const auto& activation = activations.find(version0)->second;
150 const auto& assocs = activation->associations();
151 EXPECT_EQ(2u, assocs.size());
152 EXPECT_EQ(psu0, std::get<2>(assocs[0]));
153 EXPECT_EQ(psu1, std::get<2>(assocs[1]));
154}
155
156TEST_F(TestItemUpdater, CreateTwoPSUsWithDifferentVersion)
157{
158 constexpr auto psu0 = "/com/example/inventory/psu0";
159 constexpr auto psu1 = "/com/example/inventory/psu1";
160 constexpr auto service = "com.example.Software.Psu";
161 auto version0 = std::string("version0");
162 auto version1 = std::string("version1");
163 auto objPath0 = getObjPath(version0);
164 auto objPath1 = getObjPath(version1);
165
166 EXPECT_CALL(mockedUtils, getPSUInventoryPath(_))
167 .WillOnce(Return(std::vector<std::string>({psu0, psu1})));
168 EXPECT_CALL(mockedUtils, getService(_, StrEq(psu0), _))
169 .WillOnce(Return(service));
170 EXPECT_CALL(mockedUtils, getService(_, StrEq(psu1), _))
171 .WillOnce(Return(service));
172 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu0), _,
173 StrEq(VERSION)))
174 .WillOnce(Return(any(PropertyType(version0))));
175 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu0), _,
176 StrEq(PRESENT)))
177 .WillOnce(Return(any(PropertyType(true)))); // present
178 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu1), _,
179 StrEq(VERSION)))
180 .WillOnce(Return(any(PropertyType(version1))));
181 EXPECT_CALL(mockedUtils, getPropertyImpl(_, StrEq(service), StrEq(psu1), _,
182 StrEq(PRESENT)))
183 .WillOnce(Return(any(PropertyType(true)))); // present
184
185 // The item updater itself
186 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(dBusPath)))
187 .Times(1);
188
189 // two new activation and version objects will be added
190 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath0)))
191 .Times(2);
192 EXPECT_CALL(sdbusMock, sd_bus_emit_object_added(_, StrEq(objPath1)))
193 .Times(2);
194 itemUpdater = std::make_unique<ItemUpdater>(mockedBus, dBusPath);
195
196 // Verify there are two activations and each with one association
197 const auto& activations = GetActivations();
198 EXPECT_EQ(2u, activations.size());
199 const auto& activation0 = activations.find(version0)->second;
200 const auto& assocs0 = activation0->associations();
201 EXPECT_EQ(1u, assocs0.size());
202 EXPECT_EQ(psu0, std::get<2>(assocs0[0]));
203
204 const auto& activation1 = activations.find(version1)->second;
205 const auto& assocs1 = activation1->associations();
206 EXPECT_EQ(1u, assocs1.size());
207 EXPECT_EQ(psu1, std::get<2>(assocs1[0]));
208}