ClickHouse/src/Common/tests/gtest_shell_command.cpp

70 lines
1.5 KiB
C++
Raw Normal View History

2019-06-29 23:23:53 +00:00
#include <iostream>
2020-09-15 09:55:57 +00:00
#include <common/types.h>
2019-06-29 23:23:53 +00:00
#include <Common/ShellCommand.h>
#include <IO/copyData.h>
#include <IO/WriteBufferFromFileDescriptor.h>
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>
#include <chrono>
#include <thread>
#include <gtest/gtest.h>
using namespace DB;
TEST(ShellCommand, Execute)
{
auto command = ShellCommand::execute("echo 'Hello, world!'");
std::string res;
readStringUntilEOF(res, command->out);
command->wait();
EXPECT_EQ(res, "Hello, world!\n");
}
TEST(ShellCommand, ExecuteDirect)
{
2021-08-25 19:30:22 +00:00
ShellCommand::Config config("/bin/echo");
config.arguments = {"Hello, world!"};
auto command = ShellCommand::executeDirect(config);
2019-06-29 23:23:53 +00:00
std::string res;
readStringUntilEOF(res, command->out);
command->wait();
EXPECT_EQ(res, "Hello, world!\n");
}
TEST(ShellCommand, ExecuteWithInput)
{
auto command = ShellCommand::execute("cat");
String in_str = "Hello, world!\n";
ReadBufferFromString in(in_str);
copyData(in, command->in);
command->in.close();
std::string res;
readStringUntilEOF(res, command->out);
command->wait();
EXPECT_EQ(res, "Hello, world!\n");
}
TEST(ShellCommand, AutoWait)
{
// <defunct> hunting:
for (int i = 0; i < 1000; ++i)
{
auto command = ShellCommand::execute("echo " + std::to_string(i));
//command->wait(); // now automatic
}
2020-05-23 19:35:08 +00:00
// std::cerr << "inspect me: ps auxwwf\n";
2019-06-29 23:23:53 +00:00
// std::this_thread::sleep_for(std::chrono::seconds(100));
}