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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
#include <TestSupport.h>
#include <ApplicationPool2/SmartSpawner.h>
#include <Logging.h>
#include <json/json.h>
#include <unistd.h>
#include <climits>
#include <signal.h>
#include <fcntl.h>
using namespace std;
using namespace Passenger;
using namespace Passenger::ApplicationPool2;
namespace tut {
struct ApplicationPool2_SmartSpawnerTest {
ServerInstanceDirPtr serverInstanceDir;
ServerInstanceDir::GenerationPtr generation;
ProcessPtr process;
PipeWatcher::DataCallback gatherOutput;
string gatheredOutput;
boost::mutex gatheredOutputSyncher;
ApplicationPool2_SmartSpawnerTest() {
createServerInstanceDirAndGeneration(serverInstanceDir, generation);
PipeWatcher::onData = PipeWatcher::DataCallback();
gatherOutput = boost::bind(&ApplicationPool2_SmartSpawnerTest::_gatherOutput, this, _1, _2);
setLogLevel(LVL_ERROR); // TODO: should be LVL_WARN
setPrintAppOutputAsDebuggingMessages(true);
}
~ApplicationPool2_SmartSpawnerTest() {
setLogLevel(DEFAULT_LOG_LEVEL);
setPrintAppOutputAsDebuggingMessages(false);
unlink("stub/wsgi/passenger_wsgi.pyc");
PipeWatcher::onData = PipeWatcher::DataCallback();
}
boost::shared_ptr<SmartSpawner> createSpawner(const Options &options, bool exitImmediately = false) {
char buf[PATH_MAX + 1];
getcwd(buf, PATH_MAX);
vector<string> command;
command.push_back("ruby");
command.push_back(string(buf) + "/support/placebo-preloader.rb");
if (exitImmediately) {
command.push_back("exit-immediately");
}
return boost::make_shared<SmartSpawner>(
generation,
command,
options,
make_shared<SpawnerConfig>(*resourceLocator));
}
Options createOptions() {
Options options;
options.spawnMethod = "smart";
options.loadShellEnvvars = false;
return options;
}
void _gatherOutput(const char *data, unsigned int size) {
boost::lock_guard<boost::mutex> l(gatheredOutputSyncher);
gatheredOutput.append(data, size);
}
};
DEFINE_TEST_GROUP_WITH_LIMIT(ApplicationPool2_SmartSpawnerTest, 90);
#include "SpawnerTestCases.cpp"
TEST_METHOD(80) {
// If the preloader has crashed then SmartSpawner will
// restart it and try again.
Options options = createOptions();
options.appRoot = "stub/rack";
options.startCommand = "ruby\t" "start.rb";
options.startupFile = "start.rb";
boost::shared_ptr<SmartSpawner> spawner = createSpawner(options);
setLogLevel(LVL_CRIT);
process = spawner->spawn(options);
process->requiresShutdown = false;
kill(spawner->getPreloaderPid(), SIGTERM);
// Give it some time to exit.
usleep(300000);
// No exception at next spawn.
process = spawner->spawn(options);
process->requiresShutdown = false;
}
TEST_METHOD(81) {
// If the preloader still crashes after the restart then
// SmartSpawner will throw an exception.
Options options = createOptions();
options.appRoot = "stub/rack";
options.startCommand = "ruby\t" "start.rb";
options.startupFile = "start.rb";
setLogLevel(LVL_CRIT);
boost::shared_ptr<SmartSpawner> spawner = createSpawner(options, true);
try {
process = spawner->spawn(options);
process->requiresShutdown = false;
fail("SpawnException expected");
} catch (const SpawnException &) {
// Pass.
}
}
TEST_METHOD(82) {
// If the preloader didn't start within the timeout
// then it's killed and an exception is thrown, with
// whatever stderr output as error page.
Options options = createOptions();
options.appRoot = "stub/rack";
options.startCommand = "ruby\t" "start.rb";
options.startupFile = "start.rb";
options.startTimeout = 300;
vector<string> preloaderCommand;
preloaderCommand.push_back("bash");
preloaderCommand.push_back("-c");
preloaderCommand.push_back("echo hello world >&2; sleep 60");
SmartSpawner spawner(
generation,
preloaderCommand,
options,
make_shared<SpawnerConfig>(*resourceLocator));
setLogLevel(LVL_CRIT);
try {
process = spawner.spawn(options);
process->requiresShutdown = false;
fail("SpawnException expected");
} catch (const SpawnException &e) {
ensure_equals(e.getErrorKind(),
SpawnException::PRELOADER_STARTUP_TIMEOUT);
ensure(e.getErrorPage().find("hello world\n") != string::npos);
}
}
TEST_METHOD(83) {
// If the preloader crashed during startup without returning
// a proper error response, then its stderr output is used
// as error response instead.
Options options = createOptions();
options.appRoot = "stub/rack";
options.startCommand = "ruby\t" "start.rb";
options.startupFile = "start.rb";
vector<string> preloaderCommand;
preloaderCommand.push_back("bash");
preloaderCommand.push_back("-c");
preloaderCommand.push_back("echo hello world >&2");
SmartSpawner spawner(
generation,
preloaderCommand,
options,
make_shared<SpawnerConfig>(*resourceLocator));
setLogLevel(LVL_CRIT);
try {
process = spawner.spawn(options);
process->requiresShutdown = false;
fail("SpawnException expected");
} catch (const SpawnException &e) {
ensure_equals(e.getErrorKind(),
SpawnException::PRELOADER_STARTUP_ERROR);
ensure(e.getErrorPage().find("hello world\n") != string::npos);
}
}
TEST_METHOD(84) {
// If the preloader encountered an error, then the resulting SpawnException
// takes note of the process's environment variables.
Options options = createOptions();
options.appRoot = "stub/rack";
options.startCommand = "ruby\t" "start.rb";
options.startupFile = "start.rb";
options.environmentVariables.push_back(make_pair("PASSENGER_FOO", "foo"));
vector<string> preloaderCommand;
preloaderCommand.push_back("bash");
preloaderCommand.push_back("-c");
preloaderCommand.push_back("echo hello world >&2");
SmartSpawner spawner(
generation,
preloaderCommand,
options,
make_shared<SpawnerConfig>(*resourceLocator));
setLogLevel(LVL_CRIT);
try {
process = spawner.spawn(options);
process->requiresShutdown = false;
fail("SpawnException expected");
} catch (const SpawnException &e) {
ensure(containsSubstring(e["envvars"], "PASSENGER_FOO=foo\n"));
}
}
TEST_METHOD(85) {
// Test that the spawned process can still write to its stderr
// after the SmartSpawner has been destroyed.
DeleteFileEventually d("tmp.output");
PipeWatcher::onData = gatherOutput;
Options options = createOptions();
options.appRoot = "stub/rack";
{
vector<string> preloaderCommand;
preloaderCommand.push_back("ruby");
preloaderCommand.push_back(resourceLocator->getHelperScriptsDir() + "/rack-preloader.rb");
SmartSpawner spawner(
generation,
preloaderCommand,
options,
make_shared<SpawnerConfig>(*resourceLocator));
process = spawner.spawn(options);
process->requiresShutdown = false;
}
SessionPtr session = process->newSession();
session->initiate();
const char header[] =
"REQUEST_METHOD\0GET\0"
"PATH_INFO\0/print_stderr\0";
string data(header, sizeof(header) - 1);
data.append("PASSENGER_CONNECT_PASSWORD");
data.append(1, '\0');
data.append(process->connectPassword);
data.append(1, '\0');
writeScalarMessage(session->fd(), data);
shutdown(session->fd(), SHUT_WR);
readAll(session->fd());
EVENTUALLY(2,
boost::lock_guard<boost::mutex> l(gatheredOutputSyncher);
result = gatheredOutput.find("hello world!\n") != string::npos;
);
}
}
|