1 #include "../exampledevice/example_device.hpp"
2
3 #include <phosphor-logging/lg2.hpp>
4 #include <sdbusplus/asio/connection.hpp>
5 #include <sdbusplus/asio/object_server.hpp>
6 #include <sdbusplus/async.hpp>
7 #include <sdbusplus/server.hpp>
8 #include <xyz/openbmc_project/Association/Definitions/client.hpp>
9
10 #include <memory>
11
12 #include <gtest/gtest.h>
13
14 PHOSPHOR_LOG2_USING;
15
16 using namespace phosphor::software;
17 using namespace phosphor::software::example_device;
18
19 constexpr const char* exampleEndpoint = "/xyz/example_endpoint";
20
21 class SoftwareAssocTest : public testing::Test
22 {
23 protected:
SoftwareAssocTest()24 SoftwareAssocTest() :
25 exampleUpdater(ctx, true, "swVersion"),
26 device(exampleUpdater.getDevice()),
27 objPathCurrentSoftware(
28 reinterpret_cast<ExampleSoftware*>(device->softwareCurrent.get())
29 ->objectPath),
30 busName(exampleUpdater.getBusName())
31 {}
~SoftwareAssocTest()32 ~SoftwareAssocTest() noexcept override {}
33
34 sdbusplus::async::context ctx;
35 ExampleCodeUpdater exampleUpdater;
36 std::unique_ptr<ExampleDevice>& device;
37
38 std::string objPathCurrentSoftware;
39
40 std::string busName;
41
42 public:
43 SoftwareAssocTest(const SoftwareAssocTest&) = delete;
44 SoftwareAssocTest(SoftwareAssocTest&&) = delete;
45 SoftwareAssocTest& operator=(const SoftwareAssocTest&) = delete;
46 SoftwareAssocTest& operator=(SoftwareAssocTest&&) = delete;
47 };
48
testSoftwareAssociationMissing(sdbusplus::async::context & ctx,const std::string & objPathCurrentSoftware,const std::string & busName)49 sdbusplus::async::task<> testSoftwareAssociationMissing(
50 sdbusplus::async::context& ctx, const std::string& objPathCurrentSoftware,
51 const std::string& busName)
52 {
53 auto client =
54 sdbusplus::client::xyz::openbmc_project::association::Definitions<>(ctx)
55 .service(busName)
56 .path(objPathCurrentSoftware);
57
58 // by default there is no association on the software
59 try
60 {
61 co_await client.associations();
62
63 EXPECT_TRUE(false);
64 }
65 catch (std::exception& e)
66 {
67 error(e.what());
68 }
69
70 ctx.request_stop();
71 co_return;
72 }
73
TEST_F(SoftwareAssocTest,TestSoftwareAssociationMissing)74 TEST_F(SoftwareAssocTest, TestSoftwareAssociationMissing)
75 {
76 ctx.spawn(
77 testSoftwareAssociationMissing(ctx, objPathCurrentSoftware, busName));
78 ctx.run();
79 }
80
testSoftwareAssociation(sdbusplus::async::context & ctx,std::unique_ptr<ExampleDevice> & device,const std::string & objPathCurrentSoftware,const std::string & busName,bool createRunningAssoc,std::string expectAssociation)81 sdbusplus::async::task<> testSoftwareAssociation(
82 sdbusplus::async::context& ctx, std::unique_ptr<ExampleDevice>& device,
83 const std::string& objPathCurrentSoftware, const std::string& busName,
84 bool createRunningAssoc, std::string expectAssociation)
85 {
86 auto client =
87 sdbusplus::client::xyz::openbmc_project::association::Definitions<>(ctx)
88 .service(busName)
89 .path(objPathCurrentSoftware);
90
91 reinterpret_cast<ExampleSoftware*>(device->softwareCurrent.get())
92 ->createInventoryAssociation(createRunningAssoc, exampleEndpoint);
93
94 try
95 {
96 auto res = co_await client.associations();
97
98 EXPECT_EQ(res.size(), 1);
99 EXPECT_EQ(std::get<0>(res[0]), expectAssociation);
100 EXPECT_EQ(std::get<2>(res[0]), exampleEndpoint);
101 }
102 catch (std::exception& e)
103 {
104 error(e.what());
105 EXPECT_TRUE(false);
106 }
107
108 ctx.request_stop();
109 co_return;
110 }
111
TEST_F(SoftwareAssocTest,TestSoftwareAssociationRunning)112 TEST_F(SoftwareAssocTest, TestSoftwareAssociationRunning)
113 {
114 ctx.spawn(testSoftwareAssociation(ctx, device, objPathCurrentSoftware,
115 busName, true, "running"));
116 ctx.run();
117 }
118
TEST_F(SoftwareAssocTest,TestSoftwareAssociationActivating)119 TEST_F(SoftwareAssocTest, TestSoftwareAssociationActivating)
120 {
121 ctx.spawn(testSoftwareAssociation(ctx, device, objPathCurrentSoftware,
122 busName, false, "activating"));
123 ctx.run();
124 }
125