blob: 71f701a4d1dc2125f97dc68d5e99505dcb709072 [file] [log] [blame]
James Kuszmauld42edb42022-01-07 18:00:16 -08001#include "aos/starter/subprocess.h"
2
3#include "aos/events/shm_event_loop.h"
4#include "aos/testing/path.h"
5#include "aos/testing/tmpdir.h"
6#include "aos/util/file.h"
7#include "gtest/gtest.h"
8
9namespace aos::starter::testing {
10
11class SubprocessTest : public ::testing::Test {
12 protected:
13 SubprocessTest() : shm_dir_(aos::testing::TestTmpDir() + "/aos") {
14 FLAGS_shm_base = shm_dir_;
15
16 // Nuke the shm dir:
17 aos::util::UnlinkRecursive(shm_dir_);
18 }
19
20 gflags::FlagSaver flag_saver_;
21 std::string shm_dir_;
22};
23
24TEST_F(SubprocessTest, CaptureOutputs) {
25 const std::string config_file =
26 ::aos::testing::ArtifactPath("aos/events/pingpong_config.json");
27
28 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
29 aos::configuration::ReadConfig(config_file);
30 aos::ShmEventLoop event_loop(&config.message());
31 bool observed_stopped = false;
32 Application echo_stdout(
33 "echo", "echo", &event_loop, [&observed_stopped, &echo_stdout]() {
34 if (echo_stdout.status() == aos::starter::State::STOPPED) {
35 observed_stopped = true;
36 }
37 });
38 ASSERT_FALSE(echo_stdout.autorestart());
39 echo_stdout.set_args({"abcdef"});
40 echo_stdout.set_capture_stdout(true);
41 echo_stdout.set_capture_stderr(true);
42
43 echo_stdout.Start();
44 aos::TimerHandler *exit_timer =
45 event_loop.AddTimer([&event_loop]() { event_loop.Exit(); });
46 event_loop.OnRun([&event_loop, exit_timer]() {
Austin Schuh63851a42022-05-16 13:31:37 -070047 // Note: we are using the backup poll in this test to capture SIGCHLD. This
48 // runs at 1 hz, so make sure we let it run at least once.
49 exit_timer->Setup(event_loop.monotonic_now() +
50 std::chrono::milliseconds(1500));
James Kuszmauld42edb42022-01-07 18:00:16 -080051 });
52
53 event_loop.Run();
54
James Kuszmauld42edb42022-01-07 18:00:16 -080055 ASSERT_EQ("abcdef\n", echo_stdout.GetStdout());
56 ASSERT_TRUE(echo_stdout.GetStderr().empty());
57 EXPECT_TRUE(observed_stopped);
58 EXPECT_EQ(aos::starter::State::STOPPED, echo_stdout.status());
59
60 observed_stopped = false;
61
62 // Run again, the output should've been cleared.
63 echo_stdout.set_args({"ghijkl"});
64 echo_stdout.Start();
65 event_loop.Run();
66 ASSERT_EQ("ghijkl\n", echo_stdout.GetStdout());
67 EXPECT_TRUE(observed_stopped);
68}
69
70TEST_F(SubprocessTest, CaptureStderr) {
71 const std::string config_file =
72 ::aos::testing::ArtifactPath("aos/events/pingpong_config.json");
73
74 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
75 aos::configuration::ReadConfig(config_file);
76 aos::ShmEventLoop event_loop(&config.message());
77 bool observed_stopped = false;
78 Application echo_stderr(
79 "echo", "sh", &event_loop, [&observed_stopped, &echo_stderr]() {
80 if (echo_stderr.status() == aos::starter::State::STOPPED) {
81 observed_stopped = true;
82 }
83 });
84 echo_stderr.set_args({"-c", "echo abcdef >&2"});
85 echo_stderr.set_capture_stdout(true);
86 echo_stderr.set_capture_stderr(true);
87
88 echo_stderr.Start();
Austin Schuh63851a42022-05-16 13:31:37 -070089 // Note: we are using the backup poll in this test to capture SIGCHLD. This
90 // runs at 1 hz, so make sure we let it run at least once.
James Kuszmauld42edb42022-01-07 18:00:16 -080091 event_loop.AddTimer([&event_loop]() { event_loop.Exit(); })
Austin Schuh63851a42022-05-16 13:31:37 -070092 ->Setup(event_loop.monotonic_now() + std::chrono::milliseconds(1500));
James Kuszmauld42edb42022-01-07 18:00:16 -080093
94 event_loop.Run();
95
96 ASSERT_EQ("abcdef\n", echo_stderr.GetStderr());
97 ASSERT_TRUE(echo_stderr.GetStdout().empty());
98 ASSERT_TRUE(observed_stopped);
99 ASSERT_EQ(aos::starter::State::STOPPED, echo_stderr.status());
100}
101
102} // namespace aos::starter::testing