forked from google/centipede
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_test.cc
152 lines (133 loc) · 5.16 KB
/
command_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2022 The Centipede Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "./command.h"
#include <signal.h>
#include <sys/wait.h> // NOLINT(for WTERMSIG)
#include <cstdlib>
#include <string>
#include "googletest/include/gtest/gtest.h"
#include "absl/strings/substitute.h"
#include "./logging.h"
#include "./test_util.h"
#include "./util.h"
namespace centipede {
namespace {
TEST(CommandTest, ToString) {
EXPECT_EQ(Command("x").ToString(), "x");
EXPECT_EQ(Command("path", {"arg1", "arg2"}).ToString(),
"path \\\narg1 \\\narg2");
EXPECT_EQ(Command("x", {}, {"K1=V1", "K2=V2"}).ToString(),
"K1=V1 \\\nK2=V2 \\\nx");
EXPECT_EQ(Command("x", {}, {}, "out").ToString(), "x \\\n> out");
EXPECT_EQ(Command("x", {}, {}, "", "err").ToString(), "x \\\n2> err");
EXPECT_EQ(Command("x", {}, {}, "out", "err").ToString(),
"x \\\n> out \\\n2> err");
EXPECT_EQ(Command("x", {}, {}, "out", "out").ToString(),
"x \\\n> out \\\n2>&1");
}
TEST(CommandTest, Execute) {
// Check for default exit code.
Command echo("echo");
EXPECT_EQ(echo.Execute(), 0);
EXPECT_FALSE(EarlyExitRequested());
// Check for exit code 7.
Command exit7("bash -c 'exit 7'");
EXPECT_EQ(exit7.Execute(), 7);
EXPECT_FALSE(EarlyExitRequested());
}
TEST(CommandDeathTest, Execute) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
// Test for interrupt handling.
const auto self_sigint_lambda = []() {
Command self_sigint("bash -c 'kill -SIGINT $$'");
self_sigint.Execute();
if (EarlyExitRequested()) {
LOG(INFO) << "Early exit requested";
exit(ExitCode());
}
};
EXPECT_DEATH(self_sigint_lambda(), "Early exit requested");
}
TEST(CommandTest, InputFileWildCard) {
std::string_view command_line = "foo bar @@ baz";
Command cmd(command_line, {}, {}, "", "", absl::Seconds(2), "TEMP_FILE");
EXPECT_EQ(cmd.ToString(), "foo bar TEMP_FILE baz");
}
TEST(CommandTest, ForkServer) {
const std::string test_tmpdir = GetTestTempDir(test_info_->name());
const std::string helper = GetDataDependencyFilepath("command_test_helper");
// TODO(ussuri): Dedupe these testcases.
{
const std::string input = "success";
const std::string log = std::filesystem::path{test_tmpdir} / input;
Command cmd(helper, {input}, {}, log, log);
EXPECT_TRUE(cmd.StartForkServer(test_tmpdir, "ForkServer"));
EXPECT_EQ(cmd.Execute(), EXIT_SUCCESS);
std::string log_contents;
ReadFromLocalFile(log, log_contents);
EXPECT_EQ(log_contents, absl::Substitute("Got input: $0", input));
}
{
const std::string input = "fail";
const std::string log = std::filesystem::path{test_tmpdir} / input;
Command cmd(helper, {input}, {}, log, log);
EXPECT_TRUE(cmd.StartForkServer(test_tmpdir, "ForkServer"));
EXPECT_EQ(cmd.Execute(), EXIT_FAILURE);
std::string log_contents;
ReadFromLocalFile(log, log_contents);
EXPECT_EQ(log_contents, absl::Substitute("Got input: $0", input));
}
{
const std::string input = "ret42";
const std::string log = std::filesystem::path{test_tmpdir} / input;
Command cmd(helper, {input}, {}, log, log);
EXPECT_TRUE(cmd.StartForkServer(test_tmpdir, "ForkServer"));
EXPECT_EQ(cmd.Execute(), 42);
std::string log_contents;
ReadFromLocalFile(log, log_contents);
EXPECT_EQ(log_contents, absl::Substitute("Got input: $0", input));
}
{
const std::string input = "abort";
const std::string log = std::filesystem::path{test_tmpdir} / input;
Command cmd(helper, {input}, {}, log, log);
EXPECT_TRUE(cmd.StartForkServer(test_tmpdir, "ForkServer"));
EXPECT_EQ(WTERMSIG(cmd.Execute()), SIGABRT);
std::string log_contents;
ReadFromLocalFile(log, log_contents);
EXPECT_EQ(log_contents, absl::Substitute("Got input: $0", input));
}
// TODO(kcc): [impl] test what happens if the child is interrupted.
}
TEST(CommandDeathTest, ForkServerHangingBinary) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
const std::string test_tmpdir = GetTestTempDir(test_info_->name());
const std::string input = "hang";
const std::string log = std::filesystem::path{test_tmpdir} / input;
EXPECT_DEATH(
{
const std::string helper =
GetDataDependencyFilepath("command_test_helper");
constexpr auto kTimeout = absl::Seconds(2);
Command hang(helper, {input}, {}, log, log, kTimeout);
ASSERT_TRUE(hang.StartForkServer(test_tmpdir, "ForkServer"));
hang.Execute();
},
"Timeout while waiting for fork server");
std::string log_contents;
ReadFromLocalFile(log, log_contents);
EXPECT_EQ(log_contents, absl::Substitute("Got input: $0", input));
}
} // namespace
} // namespace centipede