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
|
#include "TestSupport.h"
#include "ServerInstanceDir.h"
using namespace Passenger;
using namespace std;
namespace tut {
struct ServerInstanceDirTest {
string parentDir;
TempDir tmpDir;
string nobodyGroup;
ServerInstanceDirTest(): tmpDir("server_instance_dir_test.tmp") {
parentDir = "server_instance_dir_test.tmp";
nobodyGroup = getPrimaryGroupName("nobody");
}
void createGenerationDir(const string &instanceDir, unsigned int number) {
string command = "mkdir " + instanceDir + "/generation-" + toString(number);
system(command.c_str());
}
};
DEFINE_TEST_GROUP(ServerInstanceDirTest);
TEST_METHOD(1) {
// The (pid_t, string) constructor creates a server instance directory
// in the given parent directory, and this server instance directory
// name contains the major and minor structure versions and the given PID.
ServerInstanceDir dir(1234, parentDir);
vector<string> contents = listDir(parentDir);
ensure_equals(contents.size(), 1u);
ensure_equals(contents[0],
"passenger." +
toString(ServerInstanceDir::DIR_STRUCTURE_MAJOR_VERSION) +
"." +
toString(ServerInstanceDir::DIR_STRUCTURE_MINOR_VERSION) +
".1234");
}
TEST_METHOD(2) {
// The (string) constructor creates a ServerInstanceDir object that's
// associated with the given directory, and creates the directory
// if it doesn't exist.
ServerInstanceDir dir(1234, parentDir);
ServerInstanceDir dir2(dir.getPath());
ServerInstanceDir dir3(parentDir + "/foo");
ensure_equals(dir2.getPath(), dir.getPath());
ensure_equals(dir3.getPath(), parentDir + "/foo");
ensure_equals(getFileType(dir3.getPath()), FT_DIRECTORY);
}
TEST_METHOD(3) {
// A ServerInstanceDir object removes the server instance directory
// upon destruction, but only if there are no more generations in it.
{
ServerInstanceDir dir(1234, parentDir);
}
ensure_equals(listDir(parentDir).size(), 0u);
{
ServerInstanceDir dir(1234, parentDir);
createGenerationDir(dir.getPath(), 1);
}
ensure_equals(listDir(parentDir).size(), 1u);
}
TEST_METHOD(4) {
// The destructor does not throw any exceptions if the server instance
// directory doesn't exist anymore.
ServerInstanceDir dir(1234, parentDir);
removeDirTree(dir.getPath());
}
TEST_METHOD(5) {
// The destructor doesn't remove the server instance directory if it
// wasn't created with the ownership flag or if it's been detached.
string path, path2;
makeDirTree(parentDir + "/passenger-test.1234");
makeDirTree(parentDir + "/passenger-test.5678");
{
ServerInstanceDir dir(1234, parentDir, false);
ServerInstanceDir dir2(5678, parentDir);
dir2.detach();
path = dir.getPath();
path2 = dir2.getPath();
}
ensure_equals(getFileType(path), FT_DIRECTORY);
ensure_equals(getFileType(path2), FT_DIRECTORY);
}
TEST_METHOD(6) {
// If there are no existing generations, newGeneration() creates a new
// generation directory with number 0.
ServerInstanceDir dir(1234, parentDir);
unsigned int ncontents = listDir(dir.getPath()).size();
ServerInstanceDir::GenerationPtr generation = dir.newGeneration(true,
"nobody", nobodyGroup, 0, 0);
ensure_equals(generation->getNumber(), 0u);
ensure_equals(getFileType(generation->getPath()), FT_DIRECTORY);
ensure_equals(listDir(dir.getPath()).size(), ncontents + 1);
}
TEST_METHOD(7) {
// A Generation object returned by newGeneration() deletes the associated
// generation directory upon destruction.
ServerInstanceDir dir(1234, parentDir);
ServerInstanceDir::GenerationPtr generation = dir.newGeneration(true,
"nobody", nobodyGroup, 0, 0);
string path = generation->getPath();
generation.reset();
ensure_equals(getFileType(path), FT_NONEXISTANT);
}
TEST_METHOD(8) {
// getNewestGeneration() returns the newest generation.
ServerInstanceDir dir(1234, parentDir);
ServerInstanceDir::GenerationPtr generation0 = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
ServerInstanceDir::GenerationPtr generation1 = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
ServerInstanceDir::GenerationPtr generation2 = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
ServerInstanceDir::GenerationPtr generation3 = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
generation2.reset();
ensure_equals(dir.getNewestGeneration()->getNumber(), 3u);
generation3.reset();
ensure_equals(dir.getNewestGeneration()->getNumber(), 1u);
}
TEST_METHOD(9) {
// getNewestGeneration returns null if there are no generations.
ServerInstanceDir dir(1234, parentDir);
ensure(dir.getNewestGeneration() == NULL);
}
TEST_METHOD(10) {
// A Generation object returned by getNewestGeneration() doesn't delete
// the associated generation directory upon destruction.
ServerInstanceDir dir(1234, parentDir);
ServerInstanceDir::GenerationPtr generation = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
ServerInstanceDir::GenerationPtr newestGeneration = dir.getNewestGeneration();
newestGeneration.reset();
ensure_equals(getFileType(generation->getPath()), FT_DIRECTORY);
}
TEST_METHOD(11) {
// getGeneration() returns the given generation.
ServerInstanceDir dir(1234, parentDir);
ServerInstanceDir::GenerationPtr generation0 = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
ServerInstanceDir::GenerationPtr generation1 = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
ServerInstanceDir::GenerationPtr generation2 = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
ServerInstanceDir::GenerationPtr generation3 = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
ensure_equals(dir.getGeneration(0)->getNumber(), 0u);
ensure_equals(dir.getGeneration(3)->getNumber(), 3u);
}
TEST_METHOD(12) {
// A Generation object returned by getGeneration() doesn't delete the
// associated generation directory upon destruction.
ServerInstanceDir dir(1234, parentDir);
ServerInstanceDir::GenerationPtr generation0 = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
ServerInstanceDir::GenerationPtr generation1 = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
dir.getGeneration(0).reset();
dir.getGeneration(1).reset();
ensure_equals(getFileType(generation0->getPath()), FT_DIRECTORY);
ensure_equals(getFileType(generation1->getPath()), FT_DIRECTORY);
}
TEST_METHOD(13) {
// A detached Generation doesn't delete the associated generation
// directory upon destruction.
ServerInstanceDir dir(1234, parentDir);
ServerInstanceDir::GenerationPtr generation = dir.newGeneration(true, "nobody", nobodyGroup, 0, 0);
string path = generation->getPath();
generation->detach();
generation.reset();
ensure_equals(getFileType(path), FT_DIRECTORY);
}
TEST_METHOD(14) {
// It's possible to have two ServerInstanceDir objects constructed
// with the same (pid_t, string) constructor arguments.
ServerInstanceDir dir1(1234, parentDir);
ServerInstanceDir dir2(1234, parentDir);
}
}
|