Update Subprocess to throw if exec() fails
[folly.git] / folly / test / SubprocessTest.cpp
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "folly/Subprocess.h"
18
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <dirent.h>
22
23 #include <boost/container/flat_set.hpp>
24 #include <glog/logging.h>
25 #include <gtest/gtest.h>
26
27 #include "folly/Exception.h"
28 #include "folly/Format.h"
29 #include "folly/experimental/Gen.h"
30 #include "folly/experimental/FileGen.h"
31 #include "folly/experimental/StringGen.h"
32 #include "folly/experimental/io/FsUtil.h"
33
34 using namespace folly;
35
36 TEST(SimpleSubprocessTest, ExitsSuccessfully) {
37   Subprocess proc(std::vector<std::string>{ "/bin/true" });
38   EXPECT_EQ(0, proc.wait().exitStatus());
39 }
40
41 TEST(SimpleSubprocessTest, ExitsSuccessfullyChecked) {
42   Subprocess proc(std::vector<std::string>{ "/bin/true" });
43   proc.waitChecked();
44 }
45
46 TEST(SimpleSubprocessTest, ExitsWithError) {
47   Subprocess proc(std::vector<std::string>{ "/bin/false" });
48   EXPECT_EQ(1, proc.wait().exitStatus());
49 }
50
51 TEST(SimpleSubprocessTest, ExitsWithErrorChecked) {
52   Subprocess proc(std::vector<std::string>{ "/bin/false" });
53   EXPECT_THROW(proc.waitChecked(), CalledProcessError);
54 }
55
56 #define EXPECT_SPAWN_ERROR(err, errMsg, cmd, ...) \
57   do { \
58     try { \
59       Subprocess proc(std::vector<std::string>{ (cmd), ## __VA_ARGS__ }); \
60       ADD_FAILURE() << "expected an error when running " << (cmd); \
61     } catch (const SubprocessSpawnError& ex) { \
62       EXPECT_EQ((err), ex.errnoValue()); \
63       if (StringPiece(ex.what()).find(errMsg) == StringPiece::npos) { \
64         ADD_FAILURE() << "failed to find \"" << (errMsg) << \
65           "\" in exception: \"" << ex.what() << "\""; \
66       } \
67     } \
68   } while (0)
69
70 TEST(SimpleSubprocessTest, ExecFails) {
71   EXPECT_SPAWN_ERROR(ENOENT, "failed to execute /no/such/file:",
72                      "/no/such/file");
73   EXPECT_SPAWN_ERROR(EACCES, "failed to execute /etc/passwd:",
74                      "/etc/passwd");
75   EXPECT_SPAWN_ERROR(ENOTDIR, "failed to execute /etc/passwd/not/a/file:",
76                      "/etc/passwd/not/a/file");
77 }
78
79 TEST(SimpleSubprocessTest, ShellExitsSuccesssfully) {
80   Subprocess proc("true");
81   EXPECT_EQ(0, proc.wait().exitStatus());
82 }
83
84 TEST(SimpleSubprocessTest, ShellExitsWithError) {
85   Subprocess proc("false");
86   EXPECT_EQ(1, proc.wait().exitStatus());
87 }
88
89 namespace {
90 boost::container::flat_set<int> getOpenFds() {
91   auto pid = getpid();
92   auto dirname = to<std::string>("/proc/", pid, "/fd");
93
94   boost::container::flat_set<int> fds;
95   for (fs::directory_iterator it(dirname);
96        it != fs::directory_iterator();
97        ++it) {
98     int fd = to<int>(it->path().filename().native());
99     fds.insert(fd);
100   }
101   return fds;
102 }
103
104 template<class Runnable>
105 void checkFdLeak(const Runnable& r) {
106   // Get the currently open fds.  Check that they are the same both before and
107   // after calling the specified function.  We read the open fds from /proc.
108   // (If we wanted to work even on systems that don't have /proc, we could
109   // perhaps create and immediately close a socket both before and after
110   // running the function, and make sure we got the same fd number both times.)
111   auto fdsBefore = getOpenFds();
112   r();
113   auto fdsAfter = getOpenFds();
114   EXPECT_EQ(fdsAfter.size(), fdsBefore.size());
115 }
116 }
117
118 // Make sure Subprocess doesn't leak any file descriptors
119 TEST(SimpleSubprocessTest, FdLeakTest) {
120   // Normal execution
121   checkFdLeak([] {
122     Subprocess proc("true");
123     EXPECT_EQ(0, proc.wait().exitStatus());
124   });
125   // Normal execution with pipes
126   checkFdLeak([] {
127     Subprocess proc("echo foo; echo bar >&2",
128                     Subprocess::pipeStdout() | Subprocess::pipeStderr());
129     auto p = proc.communicate(Subprocess::readStdout() |
130                               Subprocess::readStderr());
131     EXPECT_EQ("foo\n", p.first);
132     EXPECT_EQ("bar\n", p.second);
133     proc.waitChecked();
134   });
135
136   // Test where the exec call fails()
137   checkFdLeak([] {
138     EXPECT_SPAWN_ERROR(ENOENT, "failed to execute", "/no/such/file");
139   });
140   // Test where the exec call fails() with pipes
141   checkFdLeak([] {
142     try {
143       Subprocess proc(std::vector<std::string>({"/no/such/file"}),
144                       Subprocess::pipeStdout().stderr(Subprocess::PIPE));
145       ADD_FAILURE() << "expected an error when running /no/such/file";
146     } catch (const SubprocessSpawnError& ex) {
147       EXPECT_EQ(ENOENT, ex.errnoValue());
148     }
149   });
150 }
151
152 TEST(ParentDeathSubprocessTest, ParentDeathSignal) {
153   // Find out where we are.
154   static constexpr size_t pathLength = 2048;
155   char buf[pathLength + 1];
156   int r = readlink("/proc/self/exe", buf, pathLength);
157   CHECK_ERR(r);
158   buf[r] = '\0';
159
160   fs::path helper(buf);
161   helper.remove_filename();
162   helper /= "subprocess_test_parent_death_helper";
163
164   fs::path tempFile(fs::temp_directory_path() / fs::unique_path());
165
166   std::vector<std::string> args {helper.string(), tempFile.string()};
167   Subprocess proc(args);
168   // The helper gets killed by its child, see details in
169   // SubprocessTestParentDeathHelper.cpp
170   ASSERT_EQ(SIGKILL, proc.wait().killSignal());
171
172   // Now wait for the file to be created, see details in
173   // SubprocessTestParentDeathHelper.cpp
174   while (!fs::exists(tempFile)) {
175     usleep(20000);  // 20ms
176   }
177
178   fs::remove(tempFile);
179 }
180
181 TEST(PopenSubprocessTest, PopenRead) {
182   Subprocess proc("ls /", Subprocess::pipeStdout());
183   int found = 0;
184   gen::byLine(proc.stdout()) |
185     [&] (StringPiece line) {
186       if (line == "etc" || line == "bin" || line == "usr") {
187         ++found;
188       }
189     };
190   EXPECT_EQ(3, found);
191   proc.waitChecked();
192 }
193
194 TEST(CommunicateSubprocessTest, SimpleRead) {
195   Subprocess proc(std::vector<std::string>{ "/bin/echo", "-n", "foo", "bar"},
196                   Subprocess::pipeStdout());
197   auto p = proc.communicate();
198   EXPECT_EQ("foo bar", p.first);
199   proc.waitChecked();
200 }
201
202 TEST(CommunicateSubprocessTest, BigWrite) {
203   const int numLines = 1 << 20;
204   std::string line("hello\n");
205   std::string data;
206   data.reserve(numLines * line.size());
207   for (int i = 0; i < numLines; ++i) {
208     data.append(line);
209   }
210
211   Subprocess proc("wc -l", Subprocess::pipeStdin() | Subprocess::pipeStdout());
212   auto p = proc.communicate(Subprocess::writeStdin() | Subprocess::readStdout(),
213                             data);
214   EXPECT_EQ(folly::format("{}\n", numLines).str(), p.first);
215   proc.waitChecked();
216 }
217
218 TEST(CommunicateSubprocessTest, Duplex) {
219   // Take 10MB of data and pass them through a filter.
220   // One line, as tr is line-buffered
221   const int bytes = 10 << 20;
222   std::string line(bytes, 'x');
223
224   Subprocess proc("tr a-z A-Z",
225                   Subprocess::pipeStdin() | Subprocess::pipeStdout());
226   auto p = proc.communicate(Subprocess::writeStdin() | Subprocess::readStdout(),
227                             line);
228   EXPECT_EQ(bytes, p.first.size());
229   EXPECT_EQ(std::string::npos, p.first.find_first_not_of('X'));
230   proc.waitChecked();
231 }