blob: ede39f83d33f72754a319a99f76e1e9855121e32 [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]() {
47 exit_timer->Setup(event_loop.monotonic_now() + std::chrono::seconds(1));
48 });
49
50 event_loop.Run();
51
Tyler Chatowd5db0422022-03-02 20:56:15 -080052 EXPECT_TRUE(echo_stdout.full_path() == "/bin/echo" ||
53 echo_stdout.full_path() == "/usr/bin/echo")
54 << echo_stdout.full_path();
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();
89 event_loop.AddTimer([&event_loop]() { event_loop.Exit(); })
90 ->Setup(event_loop.monotonic_now() + std::chrono::seconds(1));
91
92 event_loop.Run();
93
94 ASSERT_EQ("abcdef\n", echo_stderr.GetStderr());
95 ASSERT_TRUE(echo_stderr.GetStdout().empty());
96 ASSERT_TRUE(observed_stopped);
97 ASSERT_EQ(aos::starter::State::STOPPED, echo_stderr.status());
98}
99
100} // namespace aos::starter::testing