1 | #include <iostream> |
2 | #include <Core/Types.h> |
3 | #include <Common/ShellCommand.h> |
4 | #include <IO/copyData.h> |
5 | #include <IO/WriteBufferFromFileDescriptor.h> |
6 | #include <IO/ReadBufferFromString.h> |
7 | #include <IO/ReadHelpers.h> |
8 | |
9 | #include <chrono> |
10 | #include <thread> |
11 | |
12 | #include <gtest/gtest.h> |
13 | |
14 | |
15 | using namespace DB; |
16 | |
17 | |
18 | TEST(ShellCommand, Execute) |
19 | { |
20 | auto command = ShellCommand::execute("echo 'Hello, world!'" ); |
21 | |
22 | std::string res; |
23 | readStringUntilEOF(res, command->out); |
24 | command->wait(); |
25 | |
26 | EXPECT_EQ(res, "Hello, world!\n" ); |
27 | } |
28 | |
29 | TEST(ShellCommand, ExecuteDirect) |
30 | { |
31 | auto command = ShellCommand::executeDirect("/bin/echo" , {"Hello, world!" }); |
32 | |
33 | std::string res; |
34 | readStringUntilEOF(res, command->out); |
35 | command->wait(); |
36 | |
37 | EXPECT_EQ(res, "Hello, world!\n" ); |
38 | } |
39 | |
40 | TEST(ShellCommand, ExecuteWithInput) |
41 | { |
42 | auto command = ShellCommand::execute("cat" ); |
43 | |
44 | String in_str = "Hello, world!\n" ; |
45 | ReadBufferFromString in(in_str); |
46 | copyData(in, command->in); |
47 | command->in.close(); |
48 | |
49 | std::string res; |
50 | readStringUntilEOF(res, command->out); |
51 | command->wait(); |
52 | |
53 | EXPECT_EQ(res, "Hello, world!\n" ); |
54 | } |
55 | |
56 | TEST(ShellCommand, AutoWait) |
57 | { |
58 | // <defunct> hunting: |
59 | for (int i = 0; i < 1000; ++i) |
60 | { |
61 | auto command = ShellCommand::execute("echo " + std::to_string(i)); |
62 | //command->wait(); // now automatic |
63 | } |
64 | |
65 | // std::cerr << "inspect me: ps auxwwf" << "\n"; |
66 | // std::this_thread::sleep_for(std::chrono::seconds(100)); |
67 | } |
68 | |