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
|
#include "TestSupport.h"
#include "SpawnManager.h"
#include <sys/types.h>
#include <signal.h>
#include <cstring>
#include <unistd.h>
#include "valgrind.h"
using namespace Passenger;
namespace tut {
struct SpawnManagerTest {
ServerInstanceDirPtr serverInstanceDir;
ServerInstanceDir::GenerationPtr generation;
SpawnManagerPtr manager;
AccountsDatabasePtr accountsDatabase;
PoolOptions rackOptions;
SpawnManagerTest() {
createServerInstanceDirAndGeneration(serverInstanceDir, generation);
rackOptions.appRoot = "stub/rack";
rackOptions.appType = "rack";
}
void initialize() {
manager = ptr(new SpawnManager("../helper-scripts/passenger-spawn-server", generation,
accountsDatabase));
}
void sendTestRequest(SessionPtr &session, bool authenticate = true, const char *uri = "/foo/new") {
string headers;
#define ADD_HEADER(name, value) \
headers.append(name); \
headers.append(1, '\0'); \
headers.append(value); \
headers.append(1, '\0')
ADD_HEADER("HTTP_HOST", "www.test.com");
ADD_HEADER("QUERY_STRING", "");
ADD_HEADER("REQUEST_URI", uri);
ADD_HEADER("REQUEST_METHOD", "GET");
ADD_HEADER("REMOTE_ADDR", "localhost");
ADD_HEADER("SCRIPT_NAME", "");
ADD_HEADER("PATH_INFO", uri);
if (authenticate) {
ADD_HEADER("PASSENGER_CONNECT_PASSWORD", session->getConnectPassword());
}
session->sendHeaders(headers);
}
};
DEFINE_TEST_GROUP(SpawnManagerTest);
TEST_METHOD(1) {
// Spawning an application should return a valid Application object.
initialize();
ProcessPtr process = manager->spawn(rackOptions);
SessionPtr session = process->newSession();
sendTestRequest(session);
session->shutdownWriter();
string result = readAll(session->getStream());
ensure(result.find("hello <b>world</b>") != string::npos);
}
TEST_METHOD(2) {
// If something goes wrong during spawning, the spawn manager
// should be restarted and another (successful) spawn should be attempted.
initialize();
pid_t old_pid = manager->getServerPid();
manager->killSpawnServer();
// Give the spawn server the time to properly terminate.
usleep(500000);
ProcessPtr process = manager->spawn(rackOptions);
SessionPtr session = process->newSession();
sendTestRequest(session);
session->shutdownWriter();
string result = readAll(session->getStream());
ensure(result.find("hello <b>world</b>") != string::npos);
// The following test will fail if we're inside Valgrind, but that's normal.
// Killing the spawn server doesn't work there.
if (!RUNNING_ON_VALGRIND) {
ensure("The spawn server was restarted", manager->getServerPid() != old_pid);
}
}
class BuggySpawnManager: public SpawnManager {
protected:
virtual void spawnServerStarted() {
if (nextRestartShouldFail) {
nextRestartShouldFail = false;
killSpawnServer();
usleep(25000);
}
}
public:
bool nextRestartShouldFail;
BuggySpawnManager(const ServerInstanceDir::GenerationPtr &generation)
: SpawnManager("stub/spawn_server.rb", generation)
{
nextRestartShouldFail = false;
}
};
TEST_METHOD(3) {
// If the spawn server dies after a restart, a SpawnException should be thrown.
// This test fails in Valgrind, but that's normal.
// Killing the spawn server doesn't work there.
if (!RUNNING_ON_VALGRIND) {
BuggySpawnManager manager(generation);
manager.killSpawnServer();
// Give the spawn server the time to properly terminate.
usleep(250000);
try {
manager.nextRestartShouldFail = true;
ProcessPtr process = manager.spawn(rackOptions);
fail("SpawnManager did not throw a SpawnException");
} catch (const SpawnException &e) {
// Success.
}
}
}
TEST_METHOD(4) {
// The connect password is passed to the spawned application, which rejects
// sessions that aren't authenticated with the right password.
initialize();
ProcessPtr process = manager->spawn(rackOptions);
SessionPtr session = process->newSession();
sendTestRequest(session, false);
session->shutdownWriter();
string result = readAll(session->getStream());
ensure_equals(result, "");
}
TEST_METHOD(5) {
// It automatically creates a unique account for the application,
// which is deleted when no longer needed.
accountsDatabase = ptr(new AccountsDatabase());
initialize();
ProcessPtr process1 = manager->spawn(rackOptions);
vector<string> usernames1 = accountsDatabase->listUsernames();
ensure_equals(accountsDatabase->size(), 1u);
ProcessPtr process2 = manager->spawn(rackOptions);
vector<string> usernames2 = accountsDatabase->listUsernames();
ensure_equals(accountsDatabase->size(), 2u);
process1.reset();
ensure_equals(accountsDatabase->size(), 1u);
ensure_equals(accountsDatabase->get(usernames1[0]), AccountPtr());
process2.reset();
ensure_equals(accountsDatabase->size(), 0u);
}
}
|