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