-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFeatureContext.php
More file actions
487 lines (411 loc) · 12.5 KB
/
FeatureContext.php
File metadata and controls
487 lines (411 loc) · 12.5 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
* Behat tests.
*
* @package ee-cli
*/
/* Start: Loading required files to enable EE::launch() in tests. */
define( 'EE_ROOT', __DIR__ . '/../..' );
include_once( EE_ROOT . '/php/class-ee.php' );
include_once( EE_ROOT . '/php/EE/Runner.php' );
include_once( EE_ROOT . '/php/utils.php' );
define( 'EE', true );
define( 'EE_VERSION', trim( file_get_contents( EE_ROOT . '/VERSION' ) ) );
$root_alias = '/opt';
if ( 'Darwin' === php_uname( 's' ) ) {
$sys_name = posix_getpwuid(posix_geteuid())['name'];
if ( ! empty( $sys_name ) ) {
$root_alias = '/Users/' . $sys_name;
}
}
define( 'EE_ROOT_DIR', $root_alias . '/easyengine' );
require_once EE_ROOT . '/php/bootstrap.php';
if ( ! class_exists( 'EE\Runner' ) ) {
require_once EE_ROOT . '/php/EE/Runner.php';
}
if ( ! class_exists( 'EE\Configurator' ) ) {
require_once EE_ROOT . '/php/EE/Configurator.php';
}
$logger_dir = EE_ROOT . '/php/EE/Loggers';
$iterator = new \DirectoryIterator( $logger_dir );
// Make sure the base class is declared first.
include_once "$logger_dir/Base.php";
foreach ( $iterator as $filename ) {
if ( '.php' !== pathinfo( $filename, PATHINFO_EXTENSION ) ) {
continue;
}
include_once "$logger_dir/$filename";
}
$runner = \EE::get_runner();
$runner->init_logger();
/* End. Loading required files to enable EE::launch() in tests. */
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\AfterFeatureScope;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
define( 'EE_SITE_ROOT', EE_ROOT_DIR . '/sites' );
class FeatureContext implements Context
{
public $command;
public $webroot_path;
public $ee_path;
/**
* Contain all test sites name.
*
* @var array
*/
public static $test_sites = [
'php.test',
'example.test',
'www.example1.test',
'labels.test',
'php-local-db.test',
'php-local-redis.test',
'php-local-db-redis.test',
];
/**
* Variable contain all fail sites details.
*
* @var array
*/
public $fail_sites = [];
/**
* Initializes context.
*/
public function __construct()
{
$this->commands = [];
$this->ee_path = getcwd();
}
/**
* @Given ee phar is generated
*/
public function eePharIsPresent()
{
// Checks if phar already exists, replaces it
if( file_exists( 'ee-old.phar' ) ) {
// Below exec call is required as currenly `ee cli update` is ran with root
// which updates ee.phar with root privileges.
exec( "sudo rm ee.phar" );
copy( 'ee-old.phar','ee.phar' );
return 0;
}
exec( "php -dphar.readonly=0 utils/make-phar.php ee.phar", $output, $return_status );
if ( 0 !== $return_status ) {
throw new Exception( "Unable to generate phar" . $return_status );
}
// Cache generaed phar as it is expensive to generate one
copy( 'ee.phar','ee-old.phar' );
}
/**
* @Given :command is installed
*/
public function isInstalled( $command )
{
exec( "type " . $command, $output, $return_status );
if ( 0 !== $return_status ) {
throw new Exception( $command . " is not installed! Exit code is:" . $return_status );
}
}
/**
* @When /I run '(.*)'|"(.*)"/
*/
public function iRun( $command )
{
$this->commands[] = EE::launch($command, false, true);
}
/**
* @When I try :command
*/
public function iTry( $command )
{
$this->commands[] = EE::launch($command, false, true);
}
/**
* @Then After delay of :time seconds
*/
public function afterDelayOfSeconds( $time )
{
sleep( $time );
}
/**
* @Then /(STDOUT|STDERR) should return exactly/
*/
public function stdoutShouldReturnExactly( $output_stream, PyStringNode $expected_output )
{
$command_output = $output_stream === "STDOUT" ? $this->commands[0]->stdout : $this->commands[0]->stderr;
$command_output = str_replace(["\033[1;31m","\033[0m"],'',$command_output);
if ( $expected_output->getRaw() !== trim($command_output)) {
throw new Exception("Actual output is:\n" . $command_output);
}
}
/**
* @Then /(STDOUT|STDERR) should return something like/
*/
public function stdoutShouldReturnSomethingLike( $output_stream, PyStringNode $expected_output )
{
$command_output = $output_stream === "STDOUT" ? $this->commands[0]->stdout : $this->commands[0]->stderr;
$expected_out = isset( $expected_output->getStrings()[0] ) ? $expected_output->getStrings()[0] : '';
if ( strpos( $command_output, $expected_out ) === false ) {
throw new Exception( "Actual output is:\n" . $command_output );
}
}
/**
* @Then The :site db entry should be removed
*/
public function theDbEntryShouldBeRemoved( $site )
{
$out = shell_exec( "sudo bin/ee site list" );
if ( false !== strpos( $out, $site ) ) {
throw new Exception( "$site db entry not been removed!" );
}
}
/**
* @Then The :site webroot should be removed
*/
public function theWebrootShouldBeRemoved( $site )
{
if ( file_exists( EE_SITE_ROOT . '/' . $site ) ) {
throw new Exception( "Webroot has not been removed!" );
}
}
/**
* @Then Following containers of site :site should be removed:
*/
public function followingContainersOfSiteShouldBeRemoved( $site, TableNode $table )
{
$containers = $table->getHash();
$site_name = implode( explode( '.', $site ) );
foreach ( $containers as $container ) {
$sevice = $container['container'];
$container_name = $site_name . '_' . $sevice . '_1';
exec( "docker inspect -f '{{.State.Running}}' $container_name > /dev/null 2>&1", $exec_out, $return );
if ( ! $return ) {
throw new Exception( "$container_name has not been removed!" );
}
}
}
/**
* @Then The site :site should have webroot
*/
public function theSiteShouldHaveWebroot( $site )
{
if ( ! file_exists( EE_SITE_ROOT . '/' . $site ) ) {
throw new Exception( "Webroot has not been created!" );
}
}
/**
* @Then The site :site should have index file
*/
public function theSiteShouldHaveIndexFile( $site )
{
if ( ! file_exists( EE_SITE_ROOT . '/' . $site . "/app/htdocs/index.php" ) ) {
throw new Exception( "PHP site data not found!" );
}
}
/**
* @Then Request on :site should contain following headers:
*/
public function requestOnShouldContainFollowingHeaders( $site, TableNode $table )
{
$url = 'http://' . $site;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_NOBODY, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_VERBOSE, true );
$headers = curl_exec( $ch );
curl_close($ch);
$rows = $table->getHash();
foreach ( $rows as $row ) {
if ( strpos( $headers, $row['header'] ) === false ) {
throw new Exception( "Unable to find " . $row['header'] . "\nActual output is : " . $headers );
}
}
}
/**
* @AfterScenario
*/
public function cleanupScenario( AfterScenarioScope $scope )
{
$this->commands = [];
chdir( $this->ee_path );
}
/**
* @AfterFeature
*/
public static function cleanup( AfterFeatureScope $scope )
{
$result = EE::launch( 'sudo bin/ee site list --format=text', false, true );
$running_sites = explode( "\n", $result->stdout );
$sites_to_delete = array_intersect( self::$test_sites, $running_sites );
foreach ( $sites_to_delete as $site ) {
exec( "sudo bin/ee site delete $site --yes" );
}
if( file_exists( 'ee.phar' ) ) {
unlink( 'ee.phar' );
}
if( file_exists( 'ee-old.phar' ) ) {
unlink( 'ee-old.phar' );
}
}
/**
* @Then There should be :expected_running_containers containers with labels
*/
public function thereShouldBeContainersWithLabel($expected_running_containers, PyStringNode $pyStringNode)
{
$labels = $pyStringNode->getStrings();
$label_string = implode($labels, ' -f label=');
$result = EE::launch( "docker ps -qf label=$label_string | wc -l", false, true );
$running_containers = (int) trim($result->stdout);
if($expected_running_containers === $running_containers) {
throw new Exception("Expected $expected_running_containers running containers. Found: $running_containers");
}
}
/**
* @Then Request on :host with header :header should contain following headers:
*/
public function requestOnWithHeaderShouldContainFollowingHeaders($host, $header, TableNode $table)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [ $header ]);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$headers = curl_exec($ch);
curl_close($ch);
$rows = $table->getHash();
foreach ($rows as $row) {
if (strpos($headers, $row['header']) === false) {
throw new Exception("Unable to find " . $row['header'] . "\nActual output is : " . $headers);
}
}
}
/**
* @Then Check global redis cache for :site
*/
public function checkGlobalRedisCacheOfSite($site)
{
exec( sprintf( "docker exec -it %s redis-cli set 'easyengine' 'rocks'", GLOBAL_REDIS_CONTAINER ) );
$output = exec( sprintf( "docker exec -it %s redis-cli get 'easyengine'", GLOBAL_REDIS_CONTAINER ) );
if ( '"rocks"' !== $output ) {
throw new Exception("Global redis not working for $site site. Getting '$output' instead of 'rock'");
}
exec( sprintf( "docker exec -it %s redis-cli del 'easyengine'", GLOBAL_REDIS_CONTAINER ) );
}
/**
* @Then Check local redis cache for :site
*/
public function checkLocalRedisCacheOfSite($site)
{
$site_root_folder = EE_SITE_ROOT . '/' . $site;
exec("cd $site_root_folder && docker-compose exec redis redis-cli set 'easyengine' 'rocks'");
$output = exec("cd $site_root_folder && docker-compose exec redis redis-cli get 'easyengine'");
if ( '"rocks"' !== $output ) {
throw new Exception("Local redis not working for $site site. Getting '$output' instead of 'rock'.");
}
exec("cd $site_root_folder && docker-compose exec redis redis-cli del 'easyengine'");
}
/**
* @Then Check local mysql database connection for :site
*/
public function checkLocalDbConnection(string $site)
{
$this->checkMysqlConnection( $site, 'db' );
}
/**
* @Then Check global mysql database connection for :site
*/
public function checkGlobalDbConnection(string $site)
{
$this->checkMysqlConnection( $site, 'global-db' );
}
/**
* Helper function to check global and local database connection.
*
* @param string $site
* @param string $db_type
*
* @return void
*/
public function checkMysqlConnection(string $site, string $db_type)
{
$db = new SQLite3( EE_ROOT_DIR . '/db/ee.sqlite' );
$query = "SELECT `db_user`, `db_password` FROM sites WHERE site_url='".$site."' LIMIT 1;";
$exec_query = $db->query( $query );
$site_info = $exec_query->fetchArray( SQLITE3_ASSOC );
if ( empty( $site_info['db_user'] ) || empty( $site_info['db_password'] ) ) {
throw new exception( 'Database credentials not found.' );
}
$mysql_query = sprintf(
'mysql -h"%s" -u"%s" -p"%s" -e"exit"',
$db_type,
$site_info['db_user'],
$site_info['db_password']
);
$cmd_command = sprintf(
'ee shell %s --command="%s"',
$site,
$mysql_query
);
$db_connection = exec( $cmd_command );
if ( ! empty( $db_connection ) ) {
throw new Exception( $db_connection );
}
}
/**
* @when Create :nth php site to fix docker issue
*/
public function createNNumberOfSite($nth)
{
for ( $i=1; $i <= $nth; $i++ ) {
$domain = 'example'.$i.'.test';
self::$test_sites[] = $domain; // Add site name to cleanup.
$this->createPhpSite($domain); // Create html site.
}
// Show list of fail sites.
foreach( $this->fail_sites as $site_name => $fail_site ) {
echo $site_name . "\n" . $fail_site . "\n";
}
}
/**
* Function to create n number of html sites
*
* @param string $domain The site name to create site.
* @return void
*/
public function createPhpSite($domain)
{
$command = sprintf(
'bin/ee site create %s --type=php',
$domain
);
EE::launch($command, false, true);
$this->checkSiteIsRunningOrNot($domain);
}
/**
* Function to check site is running or not
*
* @param string $site Site domain name.
* @return void
*/
public function checkSiteIsRunningOrNot($site)
{
$url = 'http://' . $site;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$headers = curl_exec($ch);
curl_close($ch);
if ( false === strpos( $headers, 'HTTP/1.1 200 OK' ) ) {
$this->fail_sites[$site] = "Unable to find `HTTP/1.1 200 OK` \nActual output is : " . $headers;
}
}
}