Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame^] | 1 | #include <stdint.h> |
| 2 | |
| 3 | #include <atomic> |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 4 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <csignal> |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame^] | 6 | #include <functional> |
| 7 | #include <ostream> |
| 8 | #include <string> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 9 | #include <thread> |
| 10 | |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame^] | 11 | #include "absl/strings/str_format.h" |
| 12 | #include "flatbuffers/string.h" |
| 13 | #include "gflags/gflags.h" |
| 14 | #include "glog/logging.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
| 16 | |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame^] | 17 | #include "aos/configuration.h" |
| 18 | #include "aos/events/event_loop.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 19 | #include "aos/events/ping_generated.h" |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame^] | 20 | #include "aos/events/shm_event_loop.h" |
| 21 | #include "aos/flatbuffers.h" |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 22 | #include "aos/ipc_lib/event.h" |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame^] | 23 | #include "aos/ipc_lib/shm_base.h" |
| 24 | #include "aos/json_to_flatbuffer.h" |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 25 | #include "aos/network/team_number.h" |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame^] | 26 | #include "aos/starter/starter_generated.h" |
| 27 | #include "aos/starter/starter_rpc_generated.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 28 | #include "aos/starter/starter_rpc_lib.h" |
| 29 | #include "aos/starter/starterd_lib.h" |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 30 | #include "aos/testing/path.h" |
Austin Schuh | fc0bca4 | 2021-12-31 21:49:41 -0800 | [diff] [blame] | 31 | #include "aos/util/file.h" |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame^] | 32 | #include "aos/util/process_info_generated.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 33 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 34 | using aos::testing::ArtifactPath; |
| 35 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 36 | namespace aos::starter { |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 37 | |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 38 | class ThreadedStarterRunner { |
| 39 | public: |
| 40 | ThreadedStarterRunner(Starter *starter) |
| 41 | : my_thread_([this, starter]() { |
| 42 | starter->event_loop()->OnRun([this]() { event_.Set(); }); |
| 43 | starter->Run(); |
| 44 | }) { |
| 45 | event_.Wait(); |
| 46 | } |
| 47 | |
| 48 | ~ThreadedStarterRunner() { my_thread_.join(); } |
| 49 | |
| 50 | private: |
| 51 | aos::Event event_; |
| 52 | std::thread my_thread_; |
| 53 | }; |
| 54 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 55 | class StarterdTest : public ::testing::Test { |
| 56 | public: |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 57 | StarterdTest() { |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 58 | // Nuke the shm dir: |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 59 | aos::util::UnlinkRecursive(FLAGS_shm_base); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | protected: |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 63 | void SetupStarterCleanup(aos::starter::Starter *starter) { |
| 64 | starter->event_loop() |
| 65 | ->AddTimer([this, starter]() { |
| 66 | if (test_done_) { |
| 67 | starter->Cleanup(); |
| 68 | } |
| 69 | }) |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 70 | ->Schedule(starter->event_loop()->monotonic_now(), |
| 71 | std::chrono::milliseconds(100)); |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 72 | } |
| 73 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 74 | gflags::FlagSaver flag_saver_; |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 75 | // Used to track when the test completes so that we can clean up the starter |
| 76 | // in its thread. |
| 77 | std::atomic<bool> test_done_{false}; |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | struct TestParams { |
| 81 | std::string config; |
| 82 | std::string hostname; |
| 83 | }; |
| 84 | |
| 85 | class StarterdConfigParamTest |
| 86 | : public StarterdTest, |
| 87 | public ::testing::WithParamInterface<TestParams> {}; |
| 88 | |
| 89 | TEST_P(StarterdConfigParamTest, MultiNodeStartStopTest) { |
| 90 | gflags::FlagSaver flag_saver; |
| 91 | FLAGS_override_hostname = GetParam().hostname; |
| 92 | const std::string config_file = ArtifactPath(GetParam().config); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 93 | |
| 94 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 95 | aos::configuration::ReadConfig(config_file); |
| 96 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 97 | auto new_config = aos::configuration::MergeWithConfig( |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 98 | &config.message(), |
| 99 | absl::StrFormat( |
| 100 | R"({"applications": [ |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 101 | { |
| 102 | "name": "ping", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 103 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 104 | "nodes": ["pi1"], |
| 105 | "args": ["--shm_base", "%s", "--config", "%s", "--override_hostname", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 106 | }, |
| 107 | { |
| 108 | "name": "pong", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 109 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 110 | "nodes": ["pi1"], |
| 111 | "args": ["--shm_base", "%s", "--config", "%s", "--override_hostname", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 112 | } |
| 113 | ]})", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 114 | ArtifactPath("aos/events/ping"), FLAGS_shm_base, config_file, |
| 115 | GetParam().hostname, ArtifactPath("aos/events/pong"), FLAGS_shm_base, |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 116 | config_file, GetParam().hostname)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 117 | |
| 118 | const aos::Configuration *config_msg = &new_config.message(); |
| 119 | |
| 120 | // Set up starter with config file |
| 121 | aos::starter::Starter starter(config_msg); |
| 122 | |
| 123 | // Create an event loop to watch for ping messages, verifying it actually |
| 124 | // started. |
| 125 | aos::ShmEventLoop watcher_loop(config_msg); |
| 126 | watcher_loop.SkipAosLog(); |
| 127 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 128 | aos::ShmEventLoop client_loop(config_msg); |
| 129 | client_loop.SkipAosLog(); |
| 130 | StarterClient client(&client_loop); |
| 131 | client.SetTimeoutHandler( |
| 132 | []() { FAIL() << ": Command should not have timed out."; }); |
| 133 | bool success = false; |
| 134 | client.SetSuccessHandler([&success, &client_loop]() { |
| 135 | client_loop.Exit(); |
| 136 | success = true; |
| 137 | }); |
| 138 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 139 | watcher_loop |
| 140 | .AddTimer([&watcher_loop] { |
| 141 | watcher_loop.Exit(); |
| 142 | FAIL(); |
| 143 | }) |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 144 | ->Schedule(watcher_loop.monotonic_now() + std::chrono::seconds(7)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 145 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 146 | std::atomic<int> test_stage = 0; |
| 147 | // Watch on the client loop since we need to interact with the StarterClient. |
| 148 | client_loop.MakeWatcher("/test", [&test_stage, &client, |
| 149 | &client_loop](const aos::examples::Ping &) { |
| 150 | switch (test_stage) { |
| 151 | case 1: { |
| 152 | test_stage = 2; |
| 153 | break; |
| 154 | } |
| 155 | case 2: { |
| 156 | { |
| 157 | client.SendCommands({{Command::STOP, "ping", {client_loop.node()}}}, |
| 158 | std::chrono::seconds(3)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 159 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 160 | test_stage = 3; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 165 | |
| 166 | watcher_loop.MakeWatcher( |
| 167 | "/aos", [&test_stage, &watcher_loop](const aos::starter::Status &status) { |
| 168 | const aos::starter::ApplicationStatus *app_status = |
| 169 | FindApplicationStatus(status, "ping"); |
| 170 | if (app_status == nullptr) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | switch (test_stage) { |
| 175 | case 0: { |
| 176 | if (app_status->has_state() && |
| 177 | app_status->state() == aos::starter::State::RUNNING) { |
| 178 | test_stage = 1; |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | case 3: { |
| 184 | if (app_status->has_state() && |
| 185 | app_status->state() == aos::starter::State::STOPPED) { |
| 186 | watcher_loop.Exit(); |
| 187 | SUCCEED(); |
| 188 | } |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | }); |
| 193 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 194 | SetupStarterCleanup(&starter); |
| 195 | |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 196 | ThreadedStarterRunner starterd_thread(&starter); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 197 | |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 198 | aos::Event event; |
| 199 | client_loop.OnRun([&event]() { event.Set(); }); |
| 200 | std::thread client_thread([&client_loop] { client_loop.Run(); }); |
| 201 | event.Wait(); |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 202 | |
| 203 | watcher_loop.Run(); |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 204 | test_done_ = true; |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 205 | client_thread.join(); |
Austin Schuh | cad2af9 | 2023-05-28 13:56:55 -0700 | [diff] [blame] | 206 | ASSERT_TRUE(success); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 207 | } |
| 208 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 209 | INSTANTIATE_TEST_SUITE_P( |
| 210 | StarterdConfigParamTest, StarterdConfigParamTest, |
| 211 | ::testing::Values(TestParams{"aos/events/pingpong_config.json", ""}, |
| 212 | TestParams{"aos/starter/multinode_pingpong_config.json", |
| 213 | "pi1"})); |
| 214 | |
| 215 | TEST_F(StarterdTest, DeathTest) { |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 216 | const std::string config_file = |
| 217 | ArtifactPath("aos/events/pingpong_config.json"); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 218 | |
| 219 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 220 | aos::configuration::ReadConfig(config_file); |
| 221 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 222 | auto new_config = aos::configuration::MergeWithConfig( |
| 223 | &config.message(), absl::StrFormat( |
| 224 | R"({"applications": [ |
| 225 | { |
| 226 | "name": "ping", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 227 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 228 | "args": ["--shm_base", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 229 | }, |
| 230 | { |
| 231 | "name": "pong", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 232 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 233 | "args": ["--shm_base", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 234 | } |
| 235 | ]})", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 236 | ArtifactPath("aos/events/ping"), FLAGS_shm_base, |
| 237 | ArtifactPath("aos/events/pong"), FLAGS_shm_base)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 238 | |
| 239 | const aos::Configuration *config_msg = &new_config.message(); |
| 240 | |
| 241 | // Set up starter with config file |
| 242 | aos::starter::Starter starter(config_msg); |
| 243 | |
| 244 | // Create an event loop to watch for ping messages, verifying it actually |
| 245 | // started. |
| 246 | aos::ShmEventLoop watcher_loop(config_msg); |
| 247 | watcher_loop.SkipAosLog(); |
| 248 | |
| 249 | watcher_loop |
| 250 | .AddTimer([&watcher_loop] { |
| 251 | watcher_loop.Exit(); |
| 252 | FAIL(); |
| 253 | }) |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 254 | ->Schedule(watcher_loop.monotonic_now() + std::chrono::seconds(11)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 255 | |
| 256 | int test_stage = 0; |
| 257 | uint64_t id; |
| 258 | |
| 259 | watcher_loop.MakeWatcher("/aos", [&test_stage, &watcher_loop, |
| 260 | &id](const aos::starter::Status &status) { |
| 261 | const aos::starter::ApplicationStatus *app_status = |
| 262 | FindApplicationStatus(status, "ping"); |
| 263 | if (app_status == nullptr) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | switch (test_stage) { |
| 268 | case 0: { |
| 269 | if (app_status->has_state() && |
| 270 | app_status->state() == aos::starter::State::RUNNING) { |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 271 | LOG(INFO) << "Ping is running"; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 272 | test_stage = 1; |
| 273 | ASSERT_TRUE(app_status->has_pid()); |
| 274 | ASSERT_TRUE(kill(app_status->pid(), SIGINT) != -1); |
| 275 | ASSERT_TRUE(app_status->has_id()); |
| 276 | id = app_status->id(); |
| 277 | } |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | case 1: { |
| 282 | if (app_status->has_state() && |
| 283 | app_status->state() == aos::starter::State::RUNNING && |
| 284 | app_status->has_id() && app_status->id() != id) { |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 285 | LOG(INFO) << "Ping restarted"; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 286 | watcher_loop.Exit(); |
| 287 | SUCCEED(); |
| 288 | } |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | }); |
| 293 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 294 | SetupStarterCleanup(&starter); |
| 295 | |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 296 | ThreadedStarterRunner starterd_thread(&starter); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 297 | watcher_loop.Run(); |
| 298 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 299 | test_done_ = true; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 300 | } |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 301 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 302 | TEST_F(StarterdTest, Autostart) { |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 303 | const std::string config_file = |
| 304 | ArtifactPath("aos/events/pingpong_config.json"); |
| 305 | |
| 306 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 307 | aos::configuration::ReadConfig(config_file); |
| 308 | |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 309 | auto new_config = aos::configuration::MergeWithConfig( |
| 310 | &config.message(), absl::StrFormat( |
| 311 | R"({"applications": [ |
| 312 | { |
| 313 | "name": "ping", |
| 314 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 315 | "args": ["--shm_base", "%s"], |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 316 | "autostart": false |
| 317 | }, |
| 318 | { |
| 319 | "name": "pong", |
| 320 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 321 | "args": ["--shm_base", "%s"] |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 322 | } |
| 323 | ]})", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 324 | ArtifactPath("aos/events/ping"), FLAGS_shm_base, |
| 325 | ArtifactPath("aos/events/pong"), FLAGS_shm_base)); |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 326 | |
| 327 | const aos::Configuration *config_msg = &new_config.message(); |
| 328 | |
| 329 | // Set up starter with config file |
| 330 | aos::starter::Starter starter(config_msg); |
| 331 | |
| 332 | // Create an event loop to watch for the application starting up. |
| 333 | aos::ShmEventLoop watcher_loop(config_msg); |
| 334 | watcher_loop.SkipAosLog(); |
| 335 | |
| 336 | watcher_loop |
| 337 | .AddTimer([&watcher_loop] { |
| 338 | watcher_loop.Exit(); |
| 339 | FAIL(); |
| 340 | }) |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 341 | ->Schedule(watcher_loop.monotonic_now() + std::chrono::seconds(7)); |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 342 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 343 | int pong_running_count = 0; |
| 344 | watcher_loop.MakeWatcher("/aos", [&watcher_loop, &pong_running_count]( |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 345 | const aos::starter::Status &status) { |
| 346 | const aos::starter::ApplicationStatus *ping_app_status = |
| 347 | FindApplicationStatus(status, "ping"); |
| 348 | const aos::starter::ApplicationStatus *pong_app_status = |
| 349 | FindApplicationStatus(status, "pong"); |
| 350 | if (ping_app_status == nullptr || pong_app_status == nullptr) { |
| 351 | return; |
| 352 | } |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 353 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 354 | if (ping_app_status->has_state() && |
| 355 | ping_app_status->state() != aos::starter::State::STOPPED) { |
| 356 | watcher_loop.Exit(); |
| 357 | FAIL(); |
| 358 | } |
| 359 | if (pong_app_status->has_state() && |
| 360 | pong_app_status->state() == aos::starter::State::RUNNING) { |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 361 | ++pong_running_count; |
| 362 | // Sometimes if the timing for everything is *just* off, then the |
| 363 | // process_info will say that the process name is "starter_test" because |
| 364 | // it grabbed the name after the fork() but before the execvp(). To |
| 365 | // protect against that, wait an extra cycle. If things aren't fixed by |
| 366 | // the second cycle, then that is a problem. |
James Kuszmaul | 37a56af | 2023-07-29 15:15:16 -0700 | [diff] [blame] | 367 | if (pong_running_count < 3) { |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 368 | return; |
| 369 | } |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 370 | ASSERT_TRUE(pong_app_status->has_process_info()); |
Austin Schuh | 6665e92 | 2022-04-01 10:06:25 -0700 | [diff] [blame] | 371 | ASSERT_EQ("pong", pong_app_status->process_info()->name()->string_view()) |
| 372 | << aos::FlatbufferToJson(&status); |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 373 | ASSERT_EQ(pong_app_status->pid(), pong_app_status->process_info()->pid()); |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 374 | ASSERT_TRUE(pong_app_status->process_info()->has_cpu_usage()); |
| 375 | ASSERT_LE(0.0, pong_app_status->process_info()->cpu_usage()); |
James Kuszmaul | 8544c49 | 2023-07-31 15:00:38 -0700 | [diff] [blame] | 376 | ASSERT_TRUE(pong_app_status->has_has_active_timing_report()); |
| 377 | ASSERT_TRUE(pong_app_status->has_active_timing_report()); |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 378 | watcher_loop.Exit(); |
| 379 | SUCCEED(); |
| 380 | } |
| 381 | }); |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 382 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 383 | SetupStarterCleanup(&starter); |
| 384 | |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 385 | ThreadedStarterRunner starterd_thread(&starter); |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 386 | watcher_loop.Run(); |
| 387 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 388 | test_done_ = true; |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 389 | } |
| 390 | |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 391 | // Tests that starterd respects autorestart. |
| 392 | TEST_F(StarterdTest, DeathNoRestartTest) { |
| 393 | const std::string config_file = |
| 394 | ArtifactPath("aos/events/pingpong_config.json"); |
| 395 | |
| 396 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 397 | aos::configuration::ReadConfig(config_file); |
| 398 | |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 399 | auto new_config = aos::configuration::MergeWithConfig( |
| 400 | &config.message(), absl::StrFormat( |
| 401 | R"({"applications": [ |
| 402 | { |
| 403 | "name": "ping", |
| 404 | "executable_name": "%s", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 405 | "args": ["--shm_base", "%s"], |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 406 | "autorestart": false |
| 407 | }, |
| 408 | { |
| 409 | "name": "pong", |
| 410 | "executable_name": "%s", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 411 | "args": ["--shm_base", "%s"] |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 412 | } |
| 413 | ]})", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 414 | ArtifactPath("aos/events/ping"), FLAGS_shm_base, |
| 415 | ArtifactPath("aos/events/pong"), FLAGS_shm_base)); |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 416 | |
| 417 | const aos::Configuration *config_msg = &new_config.message(); |
| 418 | |
| 419 | // Set up starter with config file |
| 420 | aos::starter::Starter starter(config_msg); |
| 421 | |
| 422 | // Create an event loop to watch for the Status message to watch the state |
| 423 | // transitions. |
| 424 | aos::ShmEventLoop watcher_loop(config_msg); |
| 425 | watcher_loop.SkipAosLog(); |
| 426 | |
| 427 | watcher_loop |
| 428 | .AddTimer([&watcher_loop] { |
| 429 | watcher_loop.Exit(); |
| 430 | SUCCEED(); |
| 431 | }) |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 432 | ->Schedule(watcher_loop.monotonic_now() + std::chrono::seconds(11)); |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 433 | |
| 434 | int test_stage = 0; |
| 435 | uint64_t id; |
| 436 | |
| 437 | watcher_loop.MakeWatcher("/aos", [&test_stage, &watcher_loop, |
| 438 | &id](const aos::starter::Status &status) { |
| 439 | const aos::starter::ApplicationStatus *app_status = |
| 440 | FindApplicationStatus(status, "ping"); |
| 441 | if (app_status == nullptr) { |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | switch (test_stage) { |
| 446 | case 0: { |
| 447 | if (app_status->has_state() && |
| 448 | app_status->state() == aos::starter::State::RUNNING) { |
| 449 | LOG(INFO) << "Ping is running"; |
| 450 | test_stage = 1; |
| 451 | ASSERT_TRUE(app_status->has_pid()); |
| 452 | ASSERT_TRUE(kill(app_status->pid(), SIGINT) != -1); |
| 453 | ASSERT_TRUE(app_status->has_id()); |
| 454 | id = app_status->id(); |
| 455 | } |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | case 1: { |
| 460 | if (app_status->has_state() && |
| 461 | app_status->state() == aos::starter::State::RUNNING && |
| 462 | app_status->has_id() && app_status->id() != id) { |
| 463 | LOG(INFO) << "Ping restarted, it shouldn't..."; |
| 464 | watcher_loop.Exit(); |
| 465 | FAIL(); |
| 466 | } |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | }); |
| 471 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 472 | SetupStarterCleanup(&starter); |
| 473 | |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 474 | ThreadedStarterRunner starterd_thread(&starter); |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 475 | watcher_loop.Run(); |
| 476 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 477 | test_done_ = true; |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 480 | TEST_F(StarterdTest, StarterChainTest) { |
| 481 | // This test was written in response to a bug that was found |
| 482 | // in StarterClient::Succeed. The bug caused the timeout handler |
| 483 | // to be reset after the success handler was called. |
| 484 | // the bug has been fixed, and this test will ensure it does |
| 485 | // not regress. |
| 486 | const std::string config_file = |
| 487 | ArtifactPath("aos/events/pingpong_config.json"); |
| 488 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 489 | aos::configuration::ReadConfig(config_file); |
| 490 | auto new_config = aos::configuration::MergeWithConfig( |
| 491 | &config.message(), absl::StrFormat( |
| 492 | R"({"applications": [ |
| 493 | { |
| 494 | "name": "ping", |
| 495 | "executable_name": "%s", |
| 496 | "args": ["--shm_base", "%s"], |
| 497 | "autorestart": false |
| 498 | }, |
| 499 | { |
| 500 | "name": "pong", |
| 501 | "executable_name": "%s", |
| 502 | "args": ["--shm_base", "%s"] |
| 503 | } |
| 504 | ]})", |
| 505 | ArtifactPath("aos/events/ping"), FLAGS_shm_base, |
| 506 | ArtifactPath("aos/events/pong"), FLAGS_shm_base)); |
| 507 | |
| 508 | const aos::Configuration *config_msg = &new_config.message(); |
| 509 | // Set up starter with config file |
| 510 | aos::starter::Starter starter(config_msg); |
| 511 | aos::ShmEventLoop client_loop(config_msg); |
| 512 | client_loop.SkipAosLog(); |
| 513 | StarterClient client(&client_loop); |
| 514 | bool success = false; |
| 515 | auto client_node = client_loop.node(); |
| 516 | |
| 517 | // limit the amount of time we will wait for the test to finish. |
| 518 | client_loop |
| 519 | .AddTimer([&client_loop] { |
| 520 | client_loop.Exit(); |
| 521 | FAIL() << "ERROR: The test has failed, the watcher has timed out. " |
| 522 | "The chain of stages defined below did not complete " |
| 523 | "within the time limit."; |
| 524 | }) |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 525 | ->Schedule(client_loop.monotonic_now() + std::chrono::seconds(20)); |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 526 | |
| 527 | // variables have been defined, here we define the body of the test. |
| 528 | // We want stage1 to succeed, triggering stage2. |
| 529 | // We want stage2 to timeout, triggering stage3. |
| 530 | |
| 531 | auto stage3 = [&client_loop, &success]() { |
| 532 | LOG(INFO) << "Begin stage3."; |
| 533 | SUCCEED(); |
| 534 | success = true; |
| 535 | client_loop.Exit(); |
| 536 | LOG(INFO) << "End stage3."; |
| 537 | }; |
| 538 | auto stage2 = [this, &starter, &client, &client_node, &stage3] { |
| 539 | LOG(INFO) << "Begin stage2"; |
| 540 | test_done_ = true; // trigger `starter` to exit. |
| 541 | |
| 542 | // wait for the starter event loop to close, so we can |
| 543 | // intentionally trigger a timeout. |
| 544 | int attempts = 0; |
| 545 | while (starter.event_loop()->is_running()) { |
| 546 | ++attempts; |
| 547 | if (attempts > 5) { |
| 548 | LOG(INFO) << "Timeout while waiting for starter to exit"; |
| 549 | return; |
| 550 | } |
| 551 | LOG(INFO) << "Waiting for starter to close."; |
| 552 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 553 | } |
Austin Schuh | 0dcdf1d | 2024-03-27 12:55:41 -0700 | [diff] [blame] | 554 | client.SetTimeoutHandler(std::ref(stage3)); |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 555 | client.SetSuccessHandler([]() { |
| 556 | LOG(INFO) << "stage3 success handler called."; |
| 557 | FAIL() << ": Command should not have succeeded here."; |
| 558 | }); |
| 559 | // we want this command to timeout |
| 560 | client.SendCommands({{Command::START, "ping", {client_node}}}, |
| 561 | std::chrono::seconds(5)); |
| 562 | LOG(INFO) << "End stage2"; |
| 563 | }; |
| 564 | auto stage1 = [&client, &client_node, &stage2] { |
| 565 | LOG(INFO) << "Begin stage1"; |
| 566 | client.SetTimeoutHandler( |
| 567 | []() { FAIL() << ": Command should not have timed out."; }); |
Austin Schuh | 0dcdf1d | 2024-03-27 12:55:41 -0700 | [diff] [blame] | 568 | client.SetSuccessHandler(std::ref(stage2)); |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 569 | client.SendCommands({{Command::STOP, "ping", {client_node}}}, |
| 570 | std::chrono::seconds(5)); |
| 571 | LOG(INFO) << "End stage1"; |
| 572 | }; |
| 573 | // start the test body |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 574 | client_loop.AddTimer(stage1)->Schedule(client_loop.monotonic_now() + |
| 575 | std::chrono::milliseconds(1)); |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 576 | |
| 577 | // prepare the cleanup for starter. This will finish when we call |
| 578 | // `test_done_ = true;`. |
| 579 | SetupStarterCleanup(&starter); |
| 580 | |
| 581 | // run `starter.Run()` in a thread to simulate it running on |
| 582 | // another process. |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 583 | ThreadedStarterRunner starterd_thread(&starter); |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 584 | |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 585 | client_loop.Run(); |
| 586 | EXPECT_TRUE(success); |
| 587 | ASSERT_FALSE(starter.event_loop()->is_running()); |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 590 | } // namespace aos::starter |