Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 1 | #include <signal.h> |
| 2 | |
| 3 | #include <future> |
| 4 | #include <thread> |
| 5 | |
| 6 | #include "aos/events/ping_generated.h" |
| 7 | #include "aos/events/pong_generated.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" |
| 10 | #include "gtest/gtest.h" |
| 11 | #include "starter_rpc_lib.h" |
| 12 | #include "starterd_lib.h" |
| 13 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame^] | 14 | using aos::testing::ArtifactPath; |
| 15 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 16 | TEST(StarterdTest, StartStopTest) { |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame^] | 17 | const std::string config_file = |
| 18 | ArtifactPath("aos/events/pingpong_config.json"); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 19 | |
| 20 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 21 | aos::configuration::ReadConfig(config_file); |
| 22 | |
| 23 | const std::string test_dir = aos::testing::TestTmpDir(); |
| 24 | |
| 25 | auto new_config = aos::configuration::MergeWithConfig( |
| 26 | &config.message(), absl::StrFormat( |
| 27 | R"({"applications": [ |
| 28 | { |
| 29 | "name": "ping", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame^] | 30 | "executable_name": "%s", |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 31 | "args": ["--shm_base", "%s/aos"] |
| 32 | }, |
| 33 | { |
| 34 | "name": "pong", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame^] | 35 | "executable_name": "%s", |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 36 | "args": ["--shm_base", "%s/aos"] |
| 37 | } |
| 38 | ]})", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame^] | 39 | ArtifactPath("aos/events/ping"), test_dir, |
| 40 | ArtifactPath("aos/events/pong"), test_dir)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 41 | |
| 42 | const aos::Configuration *config_msg = &new_config.message(); |
| 43 | |
| 44 | // Set up starter with config file |
| 45 | aos::starter::Starter starter(config_msg); |
| 46 | |
| 47 | // Create an event loop to watch for ping messages, verifying it actually |
| 48 | // started. |
| 49 | aos::ShmEventLoop watcher_loop(config_msg); |
| 50 | watcher_loop.SkipAosLog(); |
| 51 | |
| 52 | watcher_loop |
| 53 | .AddTimer([&watcher_loop] { |
| 54 | watcher_loop.Exit(); |
| 55 | FAIL(); |
| 56 | }) |
| 57 | ->Setup(watcher_loop.monotonic_now() + std::chrono::seconds(7)); |
| 58 | |
| 59 | int test_stage = 0; |
| 60 | watcher_loop.MakeWatcher( |
| 61 | "/test", [&test_stage, config_msg](const aos::examples::Ping &) { |
| 62 | switch (test_stage) { |
| 63 | case 1: { |
| 64 | test_stage = 2; |
| 65 | break; |
| 66 | } |
| 67 | case 2: { |
| 68 | std::thread([config_msg] { |
| 69 | LOG(INFO) << "Send command"; |
| 70 | ASSERT_TRUE(aos::starter::SendCommandBlocking( |
| 71 | aos::starter::Command::STOP, "ping", config_msg, |
| 72 | std::chrono::seconds(3))); |
| 73 | }).detach(); |
| 74 | test_stage = 3; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | watcher_loop.MakeWatcher( |
| 81 | "/aos", [&test_stage, &watcher_loop](const aos::starter::Status &status) { |
| 82 | const aos::starter::ApplicationStatus *app_status = |
| 83 | FindApplicationStatus(status, "ping"); |
| 84 | if (app_status == nullptr) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | switch (test_stage) { |
| 89 | case 0: { |
| 90 | if (app_status->has_state() && |
| 91 | app_status->state() == aos::starter::State::RUNNING) { |
| 92 | test_stage = 1; |
| 93 | } |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | case 3: { |
| 98 | if (app_status->has_state() && |
| 99 | app_status->state() == aos::starter::State::STOPPED) { |
| 100 | watcher_loop.Exit(); |
| 101 | SUCCEED(); |
| 102 | } |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | }); |
| 107 | |
| 108 | std::thread starterd_thread([&starter] { starter.Run(); }); |
| 109 | watcher_loop.Run(); |
| 110 | |
| 111 | starter.Cleanup(); |
| 112 | starterd_thread.join(); |
| 113 | } |
| 114 | |
| 115 | TEST(StarterdTest, DeathTest) { |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame^] | 116 | const std::string config_file = |
| 117 | ArtifactPath("aos/events/pingpong_config.json"); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 118 | |
| 119 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 120 | aos::configuration::ReadConfig(config_file); |
| 121 | |
| 122 | const std::string test_dir = aos::testing::TestTmpDir(); |
| 123 | |
| 124 | auto new_config = aos::configuration::MergeWithConfig( |
| 125 | &config.message(), absl::StrFormat( |
| 126 | R"({"applications": [ |
| 127 | { |
| 128 | "name": "ping", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame^] | 129 | "executable_name": "%s", |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 130 | "args": ["--shm_base", "%s/aos"] |
| 131 | }, |
| 132 | { |
| 133 | "name": "pong", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame^] | 134 | "executable_name": "%s", |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 135 | "args": ["--shm_base", "%s/aos"] |
| 136 | } |
| 137 | ]})", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame^] | 138 | ArtifactPath("aos/events/ping"), test_dir, |
| 139 | ArtifactPath("aos/events/pong"), test_dir)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 140 | |
| 141 | const aos::Configuration *config_msg = &new_config.message(); |
| 142 | |
| 143 | // Set up starter with config file |
| 144 | aos::starter::Starter starter(config_msg); |
| 145 | |
| 146 | // Create an event loop to watch for ping messages, verifying it actually |
| 147 | // started. |
| 148 | aos::ShmEventLoop watcher_loop(config_msg); |
| 149 | watcher_loop.SkipAosLog(); |
| 150 | |
| 151 | watcher_loop |
| 152 | .AddTimer([&watcher_loop] { |
| 153 | watcher_loop.Exit(); |
| 154 | FAIL(); |
| 155 | }) |
| 156 | ->Setup(watcher_loop.monotonic_now() + std::chrono::seconds(7)); |
| 157 | |
| 158 | int test_stage = 0; |
| 159 | uint64_t id; |
| 160 | |
| 161 | watcher_loop.MakeWatcher("/aos", [&test_stage, &watcher_loop, |
| 162 | &id](const aos::starter::Status &status) { |
| 163 | const aos::starter::ApplicationStatus *app_status = |
| 164 | FindApplicationStatus(status, "ping"); |
| 165 | if (app_status == nullptr) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | switch (test_stage) { |
| 170 | case 0: { |
| 171 | if (app_status->has_state() && |
| 172 | app_status->state() == aos::starter::State::RUNNING) { |
| 173 | test_stage = 1; |
| 174 | ASSERT_TRUE(app_status->has_pid()); |
| 175 | ASSERT_TRUE(kill(app_status->pid(), SIGINT) != -1); |
| 176 | ASSERT_TRUE(app_status->has_id()); |
| 177 | id = app_status->id(); |
| 178 | } |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | case 1: { |
| 183 | if (app_status->has_state() && |
| 184 | app_status->state() == aos::starter::State::RUNNING && |
| 185 | app_status->has_id() && app_status->id() != id) { |
| 186 | watcher_loop.Exit(); |
| 187 | SUCCEED(); |
| 188 | } |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | }); |
| 193 | |
| 194 | std::thread starterd_thread([&starter] { starter.Run(); }); |
| 195 | watcher_loop.Run(); |
| 196 | |
| 197 | starter.Cleanup(); |
| 198 | starterd_thread.join(); |
| 199 | } |