-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackendTest.php
85 lines (78 loc) · 3.47 KB
/
backendTest.php
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
<?php
/*
* @file
* We choose to test the backend system in two parts.
* - Origin. These tests assure that we are generate a proper ssh command
* when a backend invoke is needed.
* - Target. These tests assure that drush generates a delimited JSON array
* when called with --backend option.
*
* Advantages of this approach:
* - No network calls and thus more robust.
* - No network calls and thus faster.
*/
class backendCase extends Drush_TestCase {
const DRUSH_BACKEND_OUTPUT_DELIMITER = 'DRUSH_BACKEND_OUTPUT_START>>>%s<<<DRUSH_BACKEND_OUTPUT_END';
/*
* Covers the following origin responsibilities.
* - A remote host is recognized in site specification.
* - Generates expected ssh command.
*
* General handling of site aliases will be in sitealiasTest.php.
*/
function testOrigin() {
$exec = sprintf('%s %s version --simulate --ssh-options=%s | grep ssh', escapeshellcmd(UNISH_DRUSH), escapeshellarg('user@server/path/to/drupal#sitename'), escapeshellarg('-i mysite_dsa'));
$this->execute($exec);
// $expected might be different on non unix platforms. We shall see.
$expected = "Simulating backend invoke: ssh -i mysite_dsa 'user'@'server' 'drush --uri='\''sitename'\'' --root='\''/path/to/drupal'\'' --simulate version --backend 2>&1' 2>&1";
$output = $this->getOutput();
$this->assertEquals($expected, $output, 'Expected ssh command was built');
}
/*
* Covers the following target responsibilities.
* - Interpret stdin as options as per REST API.
* - Successfully execute specified command.
* - JSON object has expected contents (including errors).
* - JSON object is wrapped in expected delimiters.
*/
function testTarget() {
$stdin = json_encode(array('filter'=>'sql'));
$exec = sprintf('echo %s | %s help --backend', escapeshellarg($stdin), escapeshellcmd(UNISH_DRUSH));
$this->execute($exec);
$parsed = $this->parse($this->getOutput());
$this->assertTrue((bool) $parsed, 'Successfully parsed backend output');
$this->assertArrayHasKey('log', $parsed);
$this->assertArrayHasKey('output', $parsed);
$this->assertArrayHasKey('object', $parsed);
$this->assertEquals(self::EXIT_SUCCESS, $parsed['error_status']);
// This assertion shows that `help` was called and that stdin options were respected.
$this->assertStringStartsWith('SQL commands', $parsed['output']);
$this->assertEquals('Bootstrap to phase 0.', $parsed['log'][0]['message']);
// Check error propogation by requesting an invalid command (missing Drupal site).
$exec = sprintf('%s core-cron --backend', escapeshellcmd(UNISH_DRUSH));
$this->execute($exec, self::EXIT_ERROR);
$parsed = $this->parse($this->getOutput());
$this->assertEquals(1, $parsed['error_status']);
$this->assertArrayHasKey('DRUSH_NO_DRUPAL_ROOT', $parsed['error_log']);
}
/*
* A slightly less functional copy of drush_backend_parse_output().
*/
function parse($string) {
$regex = sprintf(self::DRUSH_BACKEND_OUTPUT_DELIMITER, '(.*)');
preg_match("/$regex/s", $string, $match);
if ($match[1]) {
// we have our JSON encoded string
$output = $match[1];
// remove the match we just made and any non printing characters
$string = trim(str_replace(sprintf(self::DRUSH_BACKEND_OUTPUT_DELIMITER, $match[1]), '', $string));
}
if ($output) {
$data = json_decode($output, TRUE);
if (is_array($data)) {
return $data;
}
}
return $string;
}
}