Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 1 | #include <csignal> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 2 | #include <future> |
| 3 | #include <thread> |
| 4 | |
| 5 | #include "aos/events/ping_generated.h" |
| 6 | #include "aos/events/pong_generated.h" |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 7 | #include "aos/network/team_number.h" |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 8 | #include "aos/testing/path.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 9 | #include "aos/testing/tmpdir.h" |
Austin Schuh | fc0bca4 | 2021-12-31 21:49:41 -0800 | [diff] [blame] | 10 | #include "aos/util/file.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 11 | #include "gtest/gtest.h" |
| 12 | #include "starter_rpc_lib.h" |
| 13 | #include "starterd_lib.h" |
| 14 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 15 | using aos::testing::ArtifactPath; |
| 16 | |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 17 | namespace aos { |
| 18 | namespace starter { |
| 19 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 20 | class StarterdTest : public ::testing::Test { |
| 21 | public: |
| 22 | StarterdTest() : shm_dir_(aos::testing::TestTmpDir() + "/aos") { |
| 23 | FLAGS_shm_base = shm_dir_; |
| 24 | |
| 25 | // Nuke the shm dir: |
Austin Schuh | fc0bca4 | 2021-12-31 21:49:41 -0800 | [diff] [blame] | 26 | aos::util::UnlinkRecursive(shm_dir_); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | protected: |
| 30 | gflags::FlagSaver flag_saver_; |
| 31 | std::string shm_dir_; |
| 32 | }; |
| 33 | |
| 34 | struct TestParams { |
| 35 | std::string config; |
| 36 | std::string hostname; |
| 37 | }; |
| 38 | |
| 39 | class StarterdConfigParamTest |
| 40 | : public StarterdTest, |
| 41 | public ::testing::WithParamInterface<TestParams> {}; |
| 42 | |
| 43 | TEST_P(StarterdConfigParamTest, MultiNodeStartStopTest) { |
| 44 | gflags::FlagSaver flag_saver; |
| 45 | FLAGS_override_hostname = GetParam().hostname; |
| 46 | const std::string config_file = ArtifactPath(GetParam().config); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 47 | |
| 48 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 49 | aos::configuration::ReadConfig(config_file); |
| 50 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 51 | auto new_config = aos::configuration::MergeWithConfig( |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 52 | &config.message(), |
| 53 | absl::StrFormat( |
| 54 | R"({"applications": [ |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 55 | { |
| 56 | "name": "ping", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 57 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 58 | "nodes": ["pi1"], |
| 59 | "args": ["--shm_base", "%s", "--config", "%s", "--override_hostname", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 60 | }, |
| 61 | { |
| 62 | "name": "pong", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 63 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 64 | "nodes": ["pi1"], |
| 65 | "args": ["--shm_base", "%s", "--config", "%s", "--override_hostname", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 66 | } |
| 67 | ]})", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 68 | ArtifactPath("aos/events/ping"), shm_dir_, config_file, |
| 69 | GetParam().hostname, ArtifactPath("aos/events/pong"), shm_dir_, |
| 70 | config_file, GetParam().hostname)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 71 | |
| 72 | const aos::Configuration *config_msg = &new_config.message(); |
| 73 | |
| 74 | // Set up starter with config file |
| 75 | aos::starter::Starter starter(config_msg); |
| 76 | |
| 77 | // Create an event loop to watch for ping messages, verifying it actually |
| 78 | // started. |
| 79 | aos::ShmEventLoop watcher_loop(config_msg); |
| 80 | watcher_loop.SkipAosLog(); |
| 81 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 82 | aos::ShmEventLoop client_loop(config_msg); |
| 83 | client_loop.SkipAosLog(); |
| 84 | StarterClient client(&client_loop); |
| 85 | client.SetTimeoutHandler( |
| 86 | []() { FAIL() << ": Command should not have timed out."; }); |
| 87 | bool success = false; |
| 88 | client.SetSuccessHandler([&success, &client_loop]() { |
| 89 | client_loop.Exit(); |
| 90 | success = true; |
| 91 | }); |
| 92 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 93 | watcher_loop |
| 94 | .AddTimer([&watcher_loop] { |
| 95 | watcher_loop.Exit(); |
| 96 | FAIL(); |
| 97 | }) |
| 98 | ->Setup(watcher_loop.monotonic_now() + std::chrono::seconds(7)); |
| 99 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 100 | std::atomic<int> test_stage = 0; |
| 101 | // Watch on the client loop since we need to interact with the StarterClient. |
| 102 | client_loop.MakeWatcher("/test", [&test_stage, &client, |
| 103 | &client_loop](const aos::examples::Ping &) { |
| 104 | switch (test_stage) { |
| 105 | case 1: { |
| 106 | test_stage = 2; |
| 107 | break; |
| 108 | } |
| 109 | case 2: { |
| 110 | { |
| 111 | client.SendCommands({{Command::STOP, "ping", {client_loop.node()}}}, |
| 112 | std::chrono::seconds(3)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 113 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 114 | test_stage = 3; |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 119 | |
| 120 | watcher_loop.MakeWatcher( |
| 121 | "/aos", [&test_stage, &watcher_loop](const aos::starter::Status &status) { |
| 122 | const aos::starter::ApplicationStatus *app_status = |
| 123 | FindApplicationStatus(status, "ping"); |
| 124 | if (app_status == nullptr) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | switch (test_stage) { |
| 129 | case 0: { |
| 130 | if (app_status->has_state() && |
| 131 | app_status->state() == aos::starter::State::RUNNING) { |
| 132 | test_stage = 1; |
| 133 | } |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | case 3: { |
| 138 | if (app_status->has_state() && |
| 139 | app_status->state() == aos::starter::State::STOPPED) { |
| 140 | watcher_loop.Exit(); |
| 141 | SUCCEED(); |
| 142 | } |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | }); |
| 147 | |
| 148 | std::thread starterd_thread([&starter] { starter.Run(); }); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 149 | std::thread client_thread([&client_loop] { client_loop.Run(); }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 150 | watcher_loop.Run(); |
| 151 | |
| 152 | starter.Cleanup(); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 153 | client_thread.join(); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 154 | starterd_thread.join(); |
| 155 | } |
| 156 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 157 | INSTANTIATE_TEST_SUITE_P( |
| 158 | StarterdConfigParamTest, StarterdConfigParamTest, |
| 159 | ::testing::Values(TestParams{"aos/events/pingpong_config.json", ""}, |
| 160 | TestParams{"aos/starter/multinode_pingpong_config.json", |
| 161 | "pi1"})); |
| 162 | |
| 163 | TEST_F(StarterdTest, DeathTest) { |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 164 | const std::string config_file = |
| 165 | ArtifactPath("aos/events/pingpong_config.json"); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 166 | |
| 167 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 168 | aos::configuration::ReadConfig(config_file); |
| 169 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 170 | auto new_config = aos::configuration::MergeWithConfig( |
| 171 | &config.message(), absl::StrFormat( |
| 172 | R"({"applications": [ |
| 173 | { |
| 174 | "name": "ping", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 175 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 176 | "args": ["--shm_base", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 177 | }, |
| 178 | { |
| 179 | "name": "pong", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 180 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 181 | "args": ["--shm_base", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 182 | } |
| 183 | ]})", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 184 | ArtifactPath("aos/events/ping"), shm_dir_, |
| 185 | ArtifactPath("aos/events/pong"), shm_dir_)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 186 | |
| 187 | const aos::Configuration *config_msg = &new_config.message(); |
| 188 | |
| 189 | // Set up starter with config file |
| 190 | aos::starter::Starter starter(config_msg); |
| 191 | |
| 192 | // Create an event loop to watch for ping messages, verifying it actually |
| 193 | // started. |
| 194 | aos::ShmEventLoop watcher_loop(config_msg); |
| 195 | watcher_loop.SkipAosLog(); |
| 196 | |
| 197 | watcher_loop |
| 198 | .AddTimer([&watcher_loop] { |
| 199 | watcher_loop.Exit(); |
| 200 | FAIL(); |
| 201 | }) |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 202 | ->Setup(watcher_loop.monotonic_now() + std::chrono::seconds(11)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 203 | |
| 204 | int test_stage = 0; |
| 205 | uint64_t id; |
| 206 | |
| 207 | watcher_loop.MakeWatcher("/aos", [&test_stage, &watcher_loop, |
| 208 | &id](const aos::starter::Status &status) { |
| 209 | const aos::starter::ApplicationStatus *app_status = |
| 210 | FindApplicationStatus(status, "ping"); |
| 211 | if (app_status == nullptr) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | switch (test_stage) { |
| 216 | case 0: { |
| 217 | if (app_status->has_state() && |
| 218 | app_status->state() == aos::starter::State::RUNNING) { |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 219 | LOG(INFO) << "Ping is running"; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 220 | test_stage = 1; |
| 221 | ASSERT_TRUE(app_status->has_pid()); |
| 222 | ASSERT_TRUE(kill(app_status->pid(), SIGINT) != -1); |
| 223 | ASSERT_TRUE(app_status->has_id()); |
| 224 | id = app_status->id(); |
| 225 | } |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | case 1: { |
| 230 | if (app_status->has_state() && |
| 231 | app_status->state() == aos::starter::State::RUNNING && |
| 232 | app_status->has_id() && app_status->id() != id) { |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 233 | LOG(INFO) << "Ping restarted"; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 234 | watcher_loop.Exit(); |
| 235 | SUCCEED(); |
| 236 | } |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | }); |
| 241 | |
| 242 | std::thread starterd_thread([&starter] { starter.Run(); }); |
| 243 | watcher_loop.Run(); |
| 244 | |
| 245 | starter.Cleanup(); |
| 246 | starterd_thread.join(); |
| 247 | } |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 248 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 249 | TEST_F(StarterdTest, Autostart) { |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 250 | const std::string config_file = |
| 251 | ArtifactPath("aos/events/pingpong_config.json"); |
| 252 | |
| 253 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 254 | aos::configuration::ReadConfig(config_file); |
| 255 | |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 256 | auto new_config = aos::configuration::MergeWithConfig( |
| 257 | &config.message(), absl::StrFormat( |
| 258 | R"({"applications": [ |
| 259 | { |
| 260 | "name": "ping", |
| 261 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 262 | "args": ["--shm_base", "%s"], |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 263 | "autostart": false |
| 264 | }, |
| 265 | { |
| 266 | "name": "pong", |
| 267 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 268 | "args": ["--shm_base", "%s"] |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 269 | } |
| 270 | ]})", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 271 | ArtifactPath("aos/events/ping"), shm_dir_, |
| 272 | ArtifactPath("aos/events/pong"), shm_dir_)); |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 273 | |
| 274 | const aos::Configuration *config_msg = &new_config.message(); |
| 275 | |
| 276 | // Set up starter with config file |
| 277 | aos::starter::Starter starter(config_msg); |
| 278 | |
| 279 | // Create an event loop to watch for the application starting up. |
| 280 | aos::ShmEventLoop watcher_loop(config_msg); |
| 281 | watcher_loop.SkipAosLog(); |
| 282 | |
| 283 | watcher_loop |
| 284 | .AddTimer([&watcher_loop] { |
| 285 | watcher_loop.Exit(); |
| 286 | FAIL(); |
| 287 | }) |
| 288 | ->Setup(watcher_loop.monotonic_now() + std::chrono::seconds(7)); |
| 289 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 290 | watcher_loop.MakeWatcher("/aos", [&watcher_loop]( |
| 291 | const aos::starter::Status &status) { |
| 292 | const aos::starter::ApplicationStatus *ping_app_status = |
| 293 | FindApplicationStatus(status, "ping"); |
| 294 | const aos::starter::ApplicationStatus *pong_app_status = |
| 295 | FindApplicationStatus(status, "pong"); |
| 296 | if (ping_app_status == nullptr || pong_app_status == nullptr) { |
| 297 | return; |
| 298 | } |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 299 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 300 | if (ping_app_status->has_state() && |
| 301 | ping_app_status->state() != aos::starter::State::STOPPED) { |
| 302 | watcher_loop.Exit(); |
| 303 | FAIL(); |
| 304 | } |
| 305 | if (pong_app_status->has_state() && |
| 306 | pong_app_status->state() == aos::starter::State::RUNNING) { |
| 307 | ASSERT_TRUE(pong_app_status->has_process_info()); |
| 308 | ASSERT_EQ("pong", pong_app_status->process_info()->name()->string_view()); |
| 309 | ASSERT_EQ(pong_app_status->pid(), pong_app_status->process_info()->pid()); |
| 310 | ASSERT_LT(0.0, pong_app_status->process_info()->cpu_usage()); |
| 311 | watcher_loop.Exit(); |
| 312 | SUCCEED(); |
| 313 | } |
| 314 | }); |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 315 | |
| 316 | std::thread starterd_thread([&starter] { starter.Run(); }); |
| 317 | watcher_loop.Run(); |
| 318 | |
| 319 | starter.Cleanup(); |
| 320 | starterd_thread.join(); |
| 321 | } |
| 322 | |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 323 | // Tests that starterd respects autorestart. |
| 324 | TEST_F(StarterdTest, DeathNoRestartTest) { |
| 325 | const std::string config_file = |
| 326 | ArtifactPath("aos/events/pingpong_config.json"); |
| 327 | |
| 328 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 329 | aos::configuration::ReadConfig(config_file); |
| 330 | |
| 331 | const std::string test_dir = aos::testing::TestTmpDir(); |
| 332 | |
| 333 | auto new_config = aos::configuration::MergeWithConfig( |
| 334 | &config.message(), absl::StrFormat( |
| 335 | R"({"applications": [ |
| 336 | { |
| 337 | "name": "ping", |
| 338 | "executable_name": "%s", |
| 339 | "args": ["--shm_base", "%s/aos"], |
| 340 | "autorestart": false |
| 341 | }, |
| 342 | { |
| 343 | "name": "pong", |
| 344 | "executable_name": "%s", |
| 345 | "args": ["--shm_base", "%s/aos"] |
| 346 | } |
| 347 | ]})", |
| 348 | ArtifactPath("aos/events/ping"), test_dir, |
| 349 | ArtifactPath("aos/events/pong"), test_dir)); |
| 350 | |
| 351 | const aos::Configuration *config_msg = &new_config.message(); |
| 352 | |
| 353 | // Set up starter with config file |
| 354 | aos::starter::Starter starter(config_msg); |
| 355 | |
| 356 | // Create an event loop to watch for the Status message to watch the state |
| 357 | // transitions. |
| 358 | aos::ShmEventLoop watcher_loop(config_msg); |
| 359 | watcher_loop.SkipAosLog(); |
| 360 | |
| 361 | watcher_loop |
| 362 | .AddTimer([&watcher_loop] { |
| 363 | watcher_loop.Exit(); |
| 364 | SUCCEED(); |
| 365 | }) |
| 366 | ->Setup(watcher_loop.monotonic_now() + std::chrono::seconds(11)); |
| 367 | |
| 368 | int test_stage = 0; |
| 369 | uint64_t id; |
| 370 | |
| 371 | watcher_loop.MakeWatcher("/aos", [&test_stage, &watcher_loop, |
| 372 | &id](const aos::starter::Status &status) { |
| 373 | const aos::starter::ApplicationStatus *app_status = |
| 374 | FindApplicationStatus(status, "ping"); |
| 375 | if (app_status == nullptr) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | switch (test_stage) { |
| 380 | case 0: { |
| 381 | if (app_status->has_state() && |
| 382 | app_status->state() == aos::starter::State::RUNNING) { |
| 383 | LOG(INFO) << "Ping is running"; |
| 384 | test_stage = 1; |
| 385 | ASSERT_TRUE(app_status->has_pid()); |
| 386 | ASSERT_TRUE(kill(app_status->pid(), SIGINT) != -1); |
| 387 | ASSERT_TRUE(app_status->has_id()); |
| 388 | id = app_status->id(); |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | case 1: { |
| 394 | if (app_status->has_state() && |
| 395 | app_status->state() == aos::starter::State::RUNNING && |
| 396 | app_status->has_id() && app_status->id() != id) { |
| 397 | LOG(INFO) << "Ping restarted, it shouldn't..."; |
| 398 | watcher_loop.Exit(); |
| 399 | FAIL(); |
| 400 | } |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | }); |
| 405 | |
| 406 | std::thread starterd_thread([&starter] { starter.Run(); }); |
| 407 | watcher_loop.Run(); |
| 408 | |
| 409 | starter.Cleanup(); |
| 410 | starterd_thread.join(); |
| 411 | } |
| 412 | |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 413 | } // namespace starter |
| 414 | } // namespace aos |