| Alexander Hansen | ade5c5a | 2025-07-29 13:38:24 +0200 | [diff] [blame^] | 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: |
| 24 | SoftwareAssocTest() : |
| 25 | exampleUpdater(ctx, "swVersion"), device(exampleUpdater.getDevice()), |
| 26 | objPathCurrentSoftware( |
| 27 | reinterpret_cast<ExampleSoftware*>(device->softwareCurrent.get()) |
| 28 | ->objectPath), |
| 29 | busName(exampleUpdater.getBusName()) |
| 30 | {} |
| 31 | ~SoftwareAssocTest() noexcept override {} |
| 32 | |
| 33 | sdbusplus::async::context ctx; |
| 34 | ExampleCodeUpdater exampleUpdater; |
| 35 | std::unique_ptr<ExampleDevice>& device; |
| 36 | |
| 37 | std::string objPathCurrentSoftware; |
| 38 | |
| 39 | std::string busName; |
| 40 | |
| 41 | public: |
| 42 | SoftwareAssocTest(const SoftwareAssocTest&) = delete; |
| 43 | SoftwareAssocTest(SoftwareAssocTest&&) = delete; |
| 44 | SoftwareAssocTest& operator=(const SoftwareAssocTest&) = delete; |
| 45 | SoftwareAssocTest& operator=(SoftwareAssocTest&&) = delete; |
| 46 | }; |
| 47 | |
| 48 | sdbusplus::async::task<> testSoftwareAssociationMissing( |
| 49 | sdbusplus::async::context& ctx, const std::string& objPathCurrentSoftware, |
| 50 | const std::string& busName) |
| 51 | { |
| 52 | auto client = |
| 53 | sdbusplus::client::xyz::openbmc_project::association::Definitions<>(ctx) |
| 54 | .service(busName) |
| 55 | .path(objPathCurrentSoftware); |
| 56 | |
| 57 | // by default there is no association on the software |
| 58 | try |
| 59 | { |
| 60 | co_await client.associations(); |
| 61 | |
| 62 | EXPECT_TRUE(false); |
| 63 | } |
| 64 | catch (std::exception& e) |
| 65 | { |
| 66 | error(e.what()); |
| 67 | } |
| 68 | |
| 69 | ctx.request_stop(); |
| 70 | co_return; |
| 71 | } |
| 72 | |
| 73 | TEST_F(SoftwareAssocTest, TestSoftwareAssociationMissing) |
| 74 | { |
| 75 | ctx.spawn( |
| 76 | testSoftwareAssociationMissing(ctx, objPathCurrentSoftware, busName)); |
| 77 | ctx.run(); |
| 78 | } |
| 79 | |
| 80 | sdbusplus::async::task<> testSoftwareAssociation( |
| 81 | sdbusplus::async::context& ctx, std::unique_ptr<ExampleDevice>& device, |
| 82 | const std::string& objPathCurrentSoftware, const std::string& busName, |
| 83 | bool createRunningAssoc, std::string expectAssociation) |
| 84 | { |
| 85 | auto client = |
| 86 | sdbusplus::client::xyz::openbmc_project::association::Definitions<>(ctx) |
| 87 | .service(busName) |
| 88 | .path(objPathCurrentSoftware); |
| 89 | |
| 90 | reinterpret_cast<ExampleSoftware*>(device->softwareCurrent.get()) |
| 91 | ->createInventoryAssociation(createRunningAssoc, exampleEndpoint); |
| 92 | |
| 93 | try |
| 94 | { |
| 95 | auto res = co_await client.associations(); |
| 96 | |
| 97 | EXPECT_EQ(res.size(), 1); |
| 98 | EXPECT_EQ(std::get<0>(res[0]), expectAssociation); |
| 99 | EXPECT_EQ(std::get<2>(res[0]), exampleEndpoint); |
| 100 | } |
| 101 | catch (std::exception& e) |
| 102 | { |
| 103 | error(e.what()); |
| 104 | EXPECT_TRUE(false); |
| 105 | } |
| 106 | |
| 107 | ctx.request_stop(); |
| 108 | co_return; |
| 109 | } |
| 110 | |
| 111 | TEST_F(SoftwareAssocTest, TestSoftwareAssociationRunning) |
| 112 | { |
| 113 | ctx.spawn(testSoftwareAssociation(ctx, device, objPathCurrentSoftware, |
| 114 | busName, true, "running")); |
| 115 | ctx.run(); |
| 116 | } |
| 117 | |
| 118 | TEST_F(SoftwareAssocTest, TestSoftwareAssociationActivating) |
| 119 | { |
| 120 | // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.Branch) |
| 121 | ctx.spawn(testSoftwareAssociation(ctx, device, objPathCurrentSoftware, |
| 122 | busName, false, "activating")); |
| 123 | ctx.run(); |
| 124 | } |