blob: 93fbf6a6b54dd3b2c2bd930c0180878451f97bd3 [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
52 ASSERT_EQ("abcdef\n", echo_stdout.GetStdout());
53 ASSERT_TRUE(echo_stdout.GetStderr().empty());
54 EXPECT_TRUE(observed_stopped);
55 EXPECT_EQ(aos::starter::State::STOPPED, echo_stdout.status());
56
57 observed_stopped = false;
58
59 // Run again, the output should've been cleared.
60 echo_stdout.set_args({"ghijkl"});
61 echo_stdout.Start();
62 event_loop.Run();
63 ASSERT_EQ("ghijkl\n", echo_stdout.GetStdout());
64 EXPECT_TRUE(observed_stopped);
65}
66
67TEST_F(SubprocessTest, CaptureStderr) {
68 const std::string config_file =
69 ::aos::testing::ArtifactPath("aos/events/pingpong_config.json");
70
71 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
72 aos::configuration::ReadConfig(config_file);
73 aos::ShmEventLoop event_loop(&config.message());
74 bool observed_stopped = false;
75 Application echo_stderr(
76 "echo", "sh", &event_loop, [&observed_stopped, &echo_stderr]() {
77 if (echo_stderr.status() == aos::starter::State::STOPPED) {
78 observed_stopped = true;
79 }
80 });
81 echo_stderr.set_args({"-c", "echo abcdef >&2"});
82 echo_stderr.set_capture_stdout(true);
83 echo_stderr.set_capture_stderr(true);
84
85 echo_stderr.Start();
86 event_loop.AddTimer([&event_loop]() { event_loop.Exit(); })
87 ->Setup(event_loop.monotonic_now() + std::chrono::seconds(1));
88
89 event_loop.Run();
90
91 ASSERT_EQ("abcdef\n", echo_stderr.GetStderr());
92 ASSERT_TRUE(echo_stderr.GetStdout().empty());
93 ASSERT_TRUE(observed_stopped);
94 ASSERT_EQ(aos::starter::State::STOPPED, echo_stderr.status());
95}
96
97} // namespace aos::starter::testing