-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathRunner.php
More file actions
162 lines (137 loc) · 5.66 KB
/
Runner.php
File metadata and controls
162 lines (137 loc) · 5.66 KB
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
<?php
namespace OpenStack\Integration;
class Runner
{
private $testsDir;
private $samplesDir;
private $logger;
private $tests;
private $namespace;
public function __construct($samplesDir, $testsDir, $testNamespace)
{
$this->samplesDir = $samplesDir;
$this->testsDir = $testsDir;
$this->namespace = $testNamespace;
$this->logger = new DefaultLogger();
$this->assembleTestFiles();
}
private function traverse(string $path): \DirectoryIterator
{
return new \DirectoryIterator($path);
}
private function assembleTestFiles()
{
foreach ($this->traverse($this->testsDir) as $servicePath) {
if ($servicePath->isDir()) {
$serviceBn = $servicePath->getBasename();
foreach ($this->traverse($servicePath->getPathname()) as $versionPath) {
$versionBn = $versionPath->getBasename();
if ($servicePath->isDir() && $versionBn[0] == 'v') {
foreach ($this->traverse($versionPath->getPathname()) as $testPath) {
if (strpos($testPath->getFilename(), 'Test.php')) {
$testBn = strtolower(substr($testPath->getBasename(), 0, -8));
$this->tests[strtolower($serviceBn)][strtolower($versionBn)][] = $testBn;
}
}
}
}
}
}
}
private function getOpts()
{
$opts = getopt('s:v:m:t:', ['service:', 'version:', 'module::', 'test::', 'debug::', 'help::']);
$getOpt = function (array $keys, $default) use ($opts) {
$value = $default;
foreach ($keys as $key) {
if (isset($opts[$key])) {
$value = $opts[$key];
break;
}
}
return strtolower($value);
};
return [
$getOpt(['s', 'service'], 'all'),
$getOpt(['v', 'version'], 'all'),
$getOpt(['m', 'module'], 'core'),
$getOpt(['t', 'test'], ''),
isset($opts['debug']) ? (int)$opts['debug'] : 0,
];
}
private function getRunnableServices($service, $version, $module)
{
$tests = $this->tests;
if ($service != 'all') {
if (!isset($tests[$service])) {
$this->logger->critical(sprintf("%s is not a valid service", $service));
exit(1);
}
$serviceArray = $tests[$service];
$tests = [$service => $serviceArray];
if ($version != 'all') {
if (!isset($serviceArray[$version])) {
$this->logger->critical(sprintf("%s is not a valid version for the %s service", $version, $service));
exit(1);
}
$versionArray = $serviceArray[$version];
if ($module != 'core') {
if (!in_array($module, $serviceArray[$version])) {
$this->logger->critical(sprintf("%s is not a valid test class for the %s %s service", $module, $version, $service));
exit(1);
}
$versionArray = [$module];
}
$tests = [$service => [$version => $versionArray]];
}
}
return $tests;
}
/**
* @return TestInterface
*/
private function getTest($service, $version, $test, $verbosity)
{
$className = sprintf("%s\\%s\\%s\\%sTest", $this->namespace, Utils::toCamelCase($service), $version, ucfirst($test));
if (!class_exists($className)) {
throw new \RuntimeException(sprintf("%s does not exist", $className));
}
$basePath = $this->samplesDir . DIRECTORY_SEPARATOR . $service . DIRECTORY_SEPARATOR . $version;
$smClass = sprintf("%s\\SampleManager", $this->namespace);
$class = new $className($this->logger, new $smClass($basePath, $verbosity), $verbosity);
if (!($class instanceof TestInterface)) {
throw new \RuntimeException(sprintf("%s does not implement TestInterface", $className));
}
return $class;
}
public function runServices()
{
list($serviceOpt, $versionOpt, $moduleOpt, $testMethodOpt, $verbosityOpt) = $this->getOpts();
foreach ($this->getRunnableServices($serviceOpt, $versionOpt, $moduleOpt) as $serviceName => $serviceArray) {
foreach ($serviceArray as $versionName => $versionArray) {
foreach ($versionArray as $testName) {
$this->logger->info(str_repeat('=', 49));
$this->logger->info("Starting %s %v %m integration test(s)", [
'%s' => $serviceName,
'%v' => $versionName,
'%m' => $moduleOpt,
]);
$this->logger->info(str_repeat('=', 49));
$testRunner = $this->getTest($serviceName, $versionName, $testName, $verbosityOpt);
try {
if ($testMethodOpt) {
$testRunner->runOneTest($testMethodOpt);
} else {
$testRunner->runTests();
}
} finally {
$this->logger->info(str_repeat('=', 11));
$this->logger->info('Cleaning up');
$this->logger->info(str_repeat('=', 11));
$testRunner->teardown();
}
}
}
}
}
}