blob: 3b4af79a5b8216eea5c997002d70c14395159faf [file] [log] [blame]
Alexander Hansenade5c5a2025-07-29 13:38:24 +02001#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
14PHOSPHOR_LOG2_USING;
15
16using namespace phosphor::software;
17using namespace phosphor::software::example_device;
18
19constexpr const char* exampleEndpoint = "/xyz/example_endpoint";
20
21class SoftwareAssocTest : public testing::Test
22{
23 protected:
24 SoftwareAssocTest() :
Alexander Hansend880e4d2025-08-08 11:24:28 +020025 exampleUpdater(ctx, true, "swVersion"),
26 device(exampleUpdater.getDevice()),
Alexander Hansenade5c5a2025-07-29 13:38:24 +020027 objPathCurrentSoftware(
28 reinterpret_cast<ExampleSoftware*>(device->softwareCurrent.get())
29 ->objectPath),
30 busName(exampleUpdater.getBusName())
31 {}
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
49sdbusplus::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
74TEST_F(SoftwareAssocTest, TestSoftwareAssociationMissing)
75{
76 ctx.spawn(
77 testSoftwareAssociationMissing(ctx, objPathCurrentSoftware, busName));
78 ctx.run();
79}
80
81sdbusplus::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
112TEST_F(SoftwareAssocTest, TestSoftwareAssociationRunning)
113{
114 ctx.spawn(testSoftwareAssociation(ctx, device, objPathCurrentSoftware,
115 busName, true, "running"));
116 ctx.run();
117}
118
119TEST_F(SoftwareAssocTest, TestSoftwareAssociationActivating)
120{
Alexander Hansenade5c5a2025-07-29 13:38:24 +0200121 ctx.spawn(testSoftwareAssociation(ctx, device, objPathCurrentSoftware,
122 busName, false, "activating"));
123 ctx.run();
124}