Alexander Hansen | 8d9e6da | 2025-01-14 14:17:19 +0100 | [diff] [blame^] | 1 | #include "../nopdevice/nopdevice.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 | // const std::string objPathInventory = "/xyz/openbmc_project/inventory/item9"; |
| 15 | |
| 16 | // NOLINTBEGIN |
| 17 | sdbusplus::async::task<> |
| 18 | testSoftwareAssociationMissing(sdbusplus::async::context& ctx) |
| 19 | // NOLINTEND |
| 20 | { |
| 21 | NopCodeUpdater nopcu(ctx); |
| 22 | NopCodeUpdater* cu = &nopcu; |
| 23 | |
| 24 | std::string service = nopcu.setupBusName(); |
| 25 | |
| 26 | std::unique_ptr<NopDevice> device = std::make_unique<NopDevice>(ctx, cu); |
| 27 | |
| 28 | device->softwareCurrent = |
| 29 | std::make_unique<Software>(ctx, "myswid", *device); |
| 30 | |
| 31 | std::string objPathCurrentSoftware = |
| 32 | device->softwareCurrent->getObjectPath(); |
| 33 | |
| 34 | auto client = |
| 35 | sdbusplus::client::xyz::openbmc_project::association::Definitions<>(ctx) |
| 36 | .service(service) |
| 37 | .path(objPathCurrentSoftware); |
| 38 | |
| 39 | // by default there is no association on the software |
| 40 | try |
| 41 | { |
| 42 | co_await client.associations(); |
| 43 | |
| 44 | assert(false); |
| 45 | } |
| 46 | catch (std::exception& e) |
| 47 | { |
| 48 | lg2::debug(e.what()); |
| 49 | } |
| 50 | |
| 51 | co_await device->softwareCurrent |
| 52 | ->setAssociationDefinitionsRunningActivating(false, false); |
| 53 | |
| 54 | assert((co_await client.associations()).empty()); |
| 55 | |
| 56 | ctx.request_stop(); |
| 57 | |
| 58 | co_return; |
| 59 | } |
| 60 | |
| 61 | TEST(SoftwareTest, TestSoftwareAssociationMissing) |
| 62 | { |
| 63 | sdbusplus::async::context ctx; |
| 64 | |
| 65 | ctx.spawn(testSoftwareAssociationMissing(ctx)); |
| 66 | |
| 67 | ctx.run(); |
| 68 | } |
| 69 | |
| 70 | // NOLINTBEGIN |
| 71 | sdbusplus::async::task<> |
| 72 | testSoftwareAssociationRunning(sdbusplus::async::context& ctx) |
| 73 | // NOLINTEND |
| 74 | { |
| 75 | NopCodeUpdater nopcu(ctx); |
| 76 | NopCodeUpdater* cu = &nopcu; |
| 77 | |
| 78 | std::string service = nopcu.setupBusName(); |
| 79 | |
| 80 | std::unique_ptr<NopDevice> device = std::make_unique<NopDevice>(ctx, cu); |
| 81 | |
| 82 | device->softwareCurrent = |
| 83 | std::make_unique<Software>(ctx, "myotherswid", *device); |
| 84 | |
| 85 | std::string objPathCurrentSoftware = |
| 86 | device->softwareCurrent->getObjectPath(); |
| 87 | |
| 88 | auto client = |
| 89 | sdbusplus::client::xyz::openbmc_project::association::Definitions<>(ctx) |
| 90 | .service(service) |
| 91 | .path(objPathCurrentSoftware); |
| 92 | |
| 93 | co_await device->softwareCurrent |
| 94 | ->setAssociationDefinitionsRunningActivating(true, false); |
| 95 | |
| 96 | auto res = co_await client.associations(); |
| 97 | |
| 98 | assert(res.size() == 1); |
| 99 | |
| 100 | auto assoc = res[0]; |
| 101 | |
| 102 | std::string forward = std::get<0>(assoc); |
| 103 | std::string reverse = std::get<1>(assoc); |
| 104 | std::string endpoint = std::get<2>(assoc); |
| 105 | |
| 106 | assert(forward == "running"); |
| 107 | assert(reverse == "ran_on"); |
| 108 | assert(endpoint == (co_await device->getInventoryItemObjectPath())); |
| 109 | |
| 110 | ctx.request_stop(); |
| 111 | |
| 112 | co_return; |
| 113 | } |
| 114 | |
| 115 | TEST(SoftwareTest, TestSoftwareAssociationRunning) |
| 116 | { |
| 117 | sdbusplus::async::context ctx; |
| 118 | |
| 119 | ctx.spawn(testSoftwareAssociationRunning(ctx)); |
| 120 | |
| 121 | ctx.run(); |
| 122 | } |
| 123 | |
| 124 | // NOLINTBEGIN |
| 125 | sdbusplus::async::task<> |
| 126 | testSoftwareAssociationActivating(sdbusplus::async::context& ctx) |
| 127 | // NOLINTEND |
| 128 | { |
| 129 | NopCodeUpdater nopcu(ctx); |
| 130 | NopCodeUpdater* cu = &nopcu; |
| 131 | |
| 132 | std::string service = nopcu.setupBusName(); |
| 133 | |
| 134 | std::unique_ptr<NopDevice> device = std::make_unique<NopDevice>(ctx, cu); |
| 135 | |
| 136 | device->softwareCurrent = |
| 137 | std::make_unique<Software>(ctx, "newswid", *device); |
| 138 | |
| 139 | std::string objPathCurrentSoftware = |
| 140 | device->softwareCurrent->getObjectPath(); |
| 141 | |
| 142 | auto client = |
| 143 | sdbusplus::client::xyz::openbmc_project::association::Definitions<>(ctx) |
| 144 | .service(service) |
| 145 | .path(objPathCurrentSoftware); |
| 146 | |
| 147 | co_await device->softwareCurrent |
| 148 | ->setAssociationDefinitionsRunningActivating(false, true); |
| 149 | |
| 150 | auto res = co_await client.associations(); |
| 151 | |
| 152 | assert(res.size() == 1); |
| 153 | |
| 154 | auto assoc = res[0]; |
| 155 | |
| 156 | std::string forward = std::get<0>(assoc); |
| 157 | std::string reverse = std::get<1>(assoc); |
| 158 | std::string endpoint = std::get<2>(assoc); |
| 159 | |
| 160 | assert(forward == "activating"); |
| 161 | assert(reverse == "activated_on"); |
| 162 | assert(endpoint == (co_await device->getInventoryItemObjectPath())); |
| 163 | |
| 164 | ctx.request_stop(); |
| 165 | |
| 166 | co_return; |
| 167 | } |
| 168 | |
| 169 | TEST(SoftwareTest, TestSoftwareAssociationActivating) |
| 170 | { |
| 171 | sdbusplus::async::context ctx; |
| 172 | |
| 173 | ctx.spawn(testSoftwareAssociationActivating(ctx)); |
| 174 | |
| 175 | ctx.run(); |
| 176 | } |