diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c45d10bf..9ffae2b1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project starting with the 0.6.0 release will be docu - Fixed `site upstream-info`. (#1224) - Fixed the notices emitting from `sites list`. (#1231) - Fixed the memberships column in `sites list` to accurately reflect when your user is a both a team and organizational member of a site. (#1231) +- Fixed `wp` and `drush` commands. (#1237) ## [0.13.1] - 2016-09-19 ### Changed diff --git a/php/Terminus/Commands/CommandWithSSH.php b/php/Terminus/Commands/CommandWithSSH.php index 4fbd1dfa4..d07d3c74f 100755 --- a/php/Terminus/Commands/CommandWithSSH.php +++ b/php/Terminus/Commands/CommandWithSSH.php @@ -3,7 +3,6 @@ namespace Terminus\Commands; use Terminus\Collections\Sites; -use Terminus\Config; /** * Base class for Terminus commands that deal with sending SSH commands @@ -14,12 +13,18 @@ abstract class CommandWithSSH extends TerminusCommand * @var string Name of the client to run a command on the platform */ protected $client = ''; - /** * @var string Name of the command to be run as it will be used on server */ protected $command = ''; - + /** + * @var Environment + */ + protected $environment; + /** + * @var string + */ + protected $ssh_command; /** * @var string[] A hash of commands which do not work in Terminus. The key * is the Drush command, and the value is the Terminus equivalent, and @@ -39,182 +44,80 @@ public function __construct(array $options = []) parent::__construct($options); } + /** + * Parent invoke function + * + * @param array $args Parameters from the command line + * @param array $assoc_args Options from the command line + * @return void + */ + public function __invoke($args, $assoc_args) + { + $command = array_shift($args); + $this->ensureCommandIsPermitted($command); + + $sites = new Sites(); + $site = $sites->get($this->input()->siteName(['args' => $assoc_args,])); + $this->environment = $site->environments->get( + $this->input()->env(['args' => $assoc_args, 'site' => $site,]) + ); + $this->checkConnectionMode($this->environment); + + $this->ssh_command = "{$this->command} $command"; + $this->log()->info( + 'Running {command} on {site}-{env}', + [ + 'command' => $this->ssh_command, + 'site' => $site->get('name'), + 'env' => $this->environment->id, + ] + ); + $this->log()->debug( + 'Command "{command}" is being run.', + ['command' => escapeshellarg($this->ssh_command),] + ); + } + /** * Checks to see if the command is not available in Terminus and, if not, * it will refer you to an equivalent Terminus command, if such exists. * - * @param string[] $args Command-line arguments - * @param string[] $assoc_args Command-line parameters and flags + * @param string $command The command to be sent to Pantheon via SSH * @return void */ - protected function checkCommand($args, $assoc_args) + protected function ensureCommandIsPermitted($command) { - $command_array = explode(' ', $args[0]); + $command_array = explode(' ', $command); foreach ($command_array as $element) { - if ((strpos($element, '--') === 0) - || !isset($this->unavailable_commands[$element]) - ) { + if ((strpos($element, '--') === 0) || !isset($this->unavailable_commands[$element])) { continue; } - $alternative = $this->unavailable_commands[$element]; - $error_message = "$element is not available via Terminus. " - . 'Please run it via ' . $this->client; - if (!empty($alternative)) { - $command = sprintf( - '%s %s%s', - 'terminus', - $alternative, - $this->helpers->launch->assocArgsToStr($assoc_args) + $message = "That command is not available via Terminus. Please run it via {client}"; + if (!empty($alternative = $this->unavailable_commands[$element])) { + $this->failure( + "$message, or you can use `{suggestion}` to the same effect.", + ['client' => $this->client, 'suggestion' => "terminus $alternative",] ); - $error_message .= ', or you can use `{command}` to the same effect'; + } else { + $this->failure("$message.", ['client' => $this->client,]); } - $error_message .= '.'; - $this->failure($error_message, compact('command')); } } /** * Checks the site's mode and suggests SFTP if it is not set. * - * @param Environment $environment Environment object to check mode of + * @param Environment $env Environment object to check mode of * @return void */ - protected function checkConnectionMode($environment) - { - if ($environment->info('connection_mode') != 'sftp') { - $message = 'Note: This environment is in read-only Git mode. If you '; - $message .= 'want to make changes to the codebase of this site '; - $message .= '(e.g. updating modules or plugins), you will need to '; - $message .= 'toggle into read/write SFTP mode first.'; - $this->log()->warning($message); - } - } - - /** - * Verifies that there is only one argument given and no extaneous params - * - * @param string[] $args Command(s) given in the command line - * @param string[] $assoc_args Arguments and flags passed into the former - * @return bool True if correct - */ - protected function ensureQuotation($args, $assoc_args) + protected function checkConnectionMode($env) { - unset($assoc_args['site']); - unset($assoc_args['env']); - if (!empty($assoc_args) || (count($args) !== 1)) { - $message = 'Your {client} subcommands and arguments must be in '; - $message .= "quotation marks.\n Example: terminus {command} "; - $message .= '"subcommand --arg=value" --site= --env='; - - $this->failure( - $message, - ['client' => $this->client, 'command' => $this->command] + if ($env->info('connection_mode') != 'sftp') { + $this->log()->warning( + "Note: This environment is in read-only Git mode. If you want to make changes to the + codebase of this site (e.g. updating modules or plugins), you will need to toggle into + read/write SFTP mode first." ); } - return true; - } - - /** - * Parses server information for connections - * - * @param array $site_info Elements as follows: - * [string] site Site UUID - * [string] environment Environment name - * @return array Connection info - */ - protected function getAppserverInfo(array $site_info = []) - { - $config = Config::getAll(); - $site_id = $site_info['site']; - $env_id = $site_info['environment']; - $server = [ - 'user' => "$env_id.$site_id", - 'host' => "appserver.$env_id.$site_id.drush.in", - 'port' => '2222', - ]; - if (!is_null($ssh_host = $config['ssh_host'])) { - $server['user'] = "appserver.$env_id.$site_id"; - $server['host'] = $ssh_host; - } elseif (strpos($config['host'], 'onebox') !== false) { - $server['user'] = "appserver.$env_id.$site_id"; - $server['host'] = $config['host']; - } - return $server; - } - - /** - * Parent function to SSH-based command invocations - * - * @param string[] $args Command(s) given in the command line - * @param string[] $assoc_args Arguments and flags passed into the former - * @return array Elements as follow: - * Site site Site being invoked - * string env_id Name of the environment being invoked - * string command Command to run remotely - * string server Server connection info - */ - protected function getElements($args, $assoc_args) - { - $this->ensureQuotation($args, $assoc_args); - $this->checkCommand($args, $assoc_args); - - $sites = new Sites(); - $site = $sites->get($this->input()->siteName(['args' => $assoc_args,])); - if (!$site) { - $this->failure('Command could not be completed. Unknown site specified.'); - } - - $env_id = $this->input()->env(['args' => $assoc_args, 'site' => $site,]); - if (!in_array($env_id, ['test', 'live',])) { - $this->checkConnectionMode($site->environments->get($env_id)); - } - - $elements = [ - 'site' => $site, - 'env_id' => $env_id, - 'command' => $args[0], - 'server' => $this->getAppserverInfo( - ['site' => $site->id, 'environment' => $env_id,] - ), - ]; - return $elements; - } - - /** - * Sends command through SSH - * - * @param array $options Elements as follow: - * Site site Site being invoked - * string env_id Name of the environment being invoked - * string command Command to run remotely - * string server Server connection info - * @return array - */ - protected function sendCommand(array $options = []) - { - $this->log()->info( - sprintf('Running %s {cmd} on {site}-{env}', $this->command), - [ - 'cmd' => $options['command'], - 'site' => $options['site']->get('name'), - 'env' => $options['env_id'], - ] - ); - $server = $options['server']; - - $cmd = 'ssh -T ' . $server['user'] . '@' . $server['host'] . ' -p ' - . $server['port'] . ' -o "AddressFamily inet"' . " " - . escapeshellarg( - $this->command . ' ' . $options['command'] . ' ' - ); - $this->log()->debug( - 'Command "{command}" is being run.', - ['command' => escapeshellarg($cmd),] - ); - - ob_start(); - passthru($cmd, $exit_code); - $result = ob_get_clean(); - return $result; } } diff --git a/php/Terminus/Commands/DrushCommand.php b/php/Terminus/Commands/DrushCommand.php index 08d9126fc..b8fb01171 100755 --- a/php/Terminus/Commands/DrushCommand.php +++ b/php/Terminus/Commands/DrushCommand.php @@ -8,17 +8,15 @@ class DrushCommand extends CommandWithSSH { /** - * {@inheritdoc} + * @inheritdoc */ protected $client = 'Drush'; - /** - * {@inheritdoc} + * @inheritdoc */ protected $command = 'drush'; - /** - * {@inheritdoc} + * @inheritdoc */ protected $unavailable_commands = [ 'sql-connect' => 'site connection-info --field=mysql_command', @@ -40,11 +38,13 @@ class DrushCommand extends CommandWithSSH */ public function __invoke($args, $assoc_args) { - $elements = $this->getElements($args, $assoc_args); + parent::__invoke($args, $assoc_args); + $command = $this->ssh_command; if ($this->log()->getOptions('logFormat') != 'normal') { - $elements['command'] .= ' --pipe'; + $command .= ' --pipe'; } - $result = $this->sendCommand($elements); - $this->output()->outputDump($result); + $result = $this->environment->sendCommandViaSsh($command); + $this->output()->outputDump($result['output']); + exit($result['exit_code']); } } diff --git a/php/Terminus/Commands/WpCommand.php b/php/Terminus/Commands/WpCommand.php index 6e636a15e..7075ed53e 100755 --- a/php/Terminus/Commands/WpCommand.php +++ b/php/Terminus/Commands/WpCommand.php @@ -8,17 +8,17 @@ class WpCommand extends CommandWithSSH { /** - * {@inheritdoc} + * @inheritdoc */ protected $client = 'WP-CLI'; /** - * {@inheritdoc} + * @inheritdoc */ protected $command = 'wp'; /** - * {@inheritdoc} + * @inheritdoc */ protected $unavailable_commands = [ 'db' => '', @@ -27,7 +27,7 @@ class WpCommand extends CommandWithSSH /** * Invoke `wp` commands on a Pantheon development site * - * ... + * * : The WP-CLI command you intend to run with its arguments, in quotes * * [--site=] @@ -39,8 +39,9 @@ class WpCommand extends CommandWithSSH */ public function __invoke($args, $assoc_args) { - $elements = $this->getElements($args, $assoc_args); - $results = $this->sendCommand($elements); - $this->output()->outputDump($results); + parent::__invoke($args, $assoc_args); + $result = $this->environment->sendCommandViaSsh($this->ssh_command); + $this->output()->outputDump($result['output']); + exit($result['exit_code']); } } diff --git a/php/Terminus/Models/Environment.php b/php/Terminus/Models/Environment.php index 949dee1b5..db9e6182a 100755 --- a/php/Terminus/Models/Environment.php +++ b/php/Terminus/Models/Environment.php @@ -4,6 +4,7 @@ use GuzzleHttp\TransferStats as TransferStats; use Terminus\Exceptions\TerminusException; +use Terminus\Config; use Terminus\Collections\Backups; use Terminus\Collections\Bindings; use Terminus\Collections\Commits; @@ -69,6 +70,39 @@ public function applyUpstreamUpdates($updatedb = true, $xoption = false) return $workflow; } + /** + * Gives cacheserver connection info for this environment + * + * @return array + */ + public function cacheserverConnectionInfo() + { + $cacheserver_binding = (array)$this->bindings->getByType('cacheserver'); + if (!empty($cacheserver_binding)) { + do { + $next_binding = array_shift($cacheserver_binding); + if (is_null($next_binding)) { + break; + } + $cache_binding = $next_binding; + } while (!is_null($cache_binding) && $cache_binding->get('environment') != $this->id); + + $password = $cache_binding->get('password'); + $hostname = $cache_binding->get('host'); + $port = $cache_binding->get('port'); + $url = "redis://pantheon:$password@$hostname:$port"; + $command = "redis-cli -h $hostname -p $port -a $password"; + $info = [ + 'password' => $password, + 'host' => $hostname, + 'port' => $port, + 'url' => $url, + 'command' => $command, + ]; + return $info; + } + } + /** * Changes connection mode * @@ -170,153 +204,42 @@ public function commitChanges($commit = null) */ public function connectionInfo() { - $info = []; - - $sftp_username = sprintf( - '%s.%s', - $this->id, - $this->site->id - ); - $sftp_password = 'Use your account password'; - $sftp_host = sprintf( - 'appserver.%s.%s.drush.in', - $this->id, - $this->site->id - ); - $sftp_port = 2222; - $sftp_url = sprintf( - 'sftp://%s@%s:%s', - $sftp_username, - $sftp_host, - $sftp_port - ); - $sftp_command = sprintf( - 'sftp -o Port=%s %s@%s', - $sftp_port, - $sftp_username, - $sftp_host + $sftp_info = $this->sftpConnectionInfo(); + $mysql_info = $this->databaseConnectionInfo(); + $redis_info = $this->cacheserverConnectionInfo(); + $info = array_merge( + array_combine( + array_map(function ($key) { + return "sftp_$key"; + }, array_keys($sftp_info)), + array_values($sftp_info) + ), + array_combine( + array_map(function ($key) { + return "mysql_$key"; + }, array_keys($mysql_info)), + array_values($mysql_info) + ), + array_combine( + array_map(function ($key) { + return "redis_$key"; + }, array_keys($redis_info)), + array_values($redis_info) + ) ); - $sftp_params = [ - 'sftp_username' => $sftp_username, - 'sftp_host' => $sftp_host, - 'sftp_password' => $sftp_password, - 'sftp_url' => $sftp_url, - 'sftp_command' => $sftp_command, - ]; - $info = array_merge($info, $sftp_params); - // Can only Use Git on dev/multidev environments + // Can only Use Git on dev/multidev environments if (!in_array($this->id, ['test', 'live',])) { - $git_username = sprintf( - 'codeserver.dev.%s', - $this->site->id - ); - $git_host = sprintf( - 'codeserver.dev.%s.drush.in', - $this->site->id - ); - $git_port = 2222; - $git_url = sprintf( - 'ssh://%s@%s:%s/~/repository.git', - $git_username, - $git_host, - $git_port - ); - $git_command = sprintf( - 'git clone %s %s', - $git_url, - $this->site->get('name') - ); - $git_params = [ - 'git_username' => $git_username, - 'git_host' => $git_host, - 'git_port' => $git_port, - 'git_url' => $git_url, - 'git_command' => $git_command, - ]; - $info = array_merge($info, $git_params); - } - - $dbserver_binding = (array)$this->bindings->getByType('dbserver'); - if (!empty($dbserver_binding)) { - do { - $db_binding = array_shift($dbserver_binding); - } while ($db_binding->get('environment') != $this->id); - - $mysql_username = 'pantheon'; - $mysql_password = $db_binding->get('password'); - $mysql_host = sprintf( - 'dbserver.%s.%s.drush.in', - $this->id, - $this->site->id - ); - $mysql_port = $db_binding->get('port'); - $mysql_database = 'pantheon'; - $mysql_url = sprintf( - 'mysql://%s:%s@%s:%s/%s', - $mysql_username, - $mysql_password, - $mysql_host, - $mysql_port, - $mysql_database - ); - $mysql_command = sprintf( - 'mysql -u %s -p%s -h %s -P %s %s', - $mysql_username, - $mysql_password, - $mysql_host, - $mysql_port, - $mysql_database - ); - $mysql_params = [ - 'mysql_host' => $mysql_host, - 'mysql_username' => $mysql_username, - 'mysql_password' => $mysql_password, - 'mysql_port' => $mysql_port, - 'mysql_database' => $mysql_database, - 'mysql_url' => $mysql_url, - 'mysql_command' => $mysql_command, - ]; - - $info = array_merge($info, $mysql_params); - } - - $cacheserver_binding = (array)$this->bindings->getByType('cacheserver'); - if (!empty($cacheserver_binding)) { - do { - $next_binding = array_shift($cacheserver_binding); - if (is_null($next_binding)) { - break; - } - $cache_binding = $next_binding; - } while (!is_null($cache_binding) - && $cache_binding->get('environment') != $this->id - ); - - $redis_password = $cache_binding->get('password'); - $redis_host = $cache_binding->get('host'); - $redis_port = $cache_binding->get('port'); - $redis_url = sprintf( - 'redis://pantheon:%s@%s:%s', - $redis_password, - $redis_host, - $redis_port - ); - $redis_command = sprintf( - 'redis-cli -h %s -p %s -a %s', - $redis_host, - $redis_port, - $redis_password + $git_info = $this->gitConnectionInfo(); + $info = array_merge( + array_combine( + array_map(function ($key) { + return "git_$key"; + }, array_keys($git_info)), + array_values($git_info) + ), + $info ); - $redis_params = [ - 'redis_password' => $redis_password, - 'redis_host' => $redis_host, - 'redis_port' => $redis_port, - 'redis_url' => $redis_url, - 'redis_command' => $redis_command, - ]; - - $info = array_merge($info, $redis_params); } return $info; @@ -353,6 +276,39 @@ public function countDeployableCommits() return $number_of_commits; } + /** + * Gives database connection info for this environment + * + * @return array + */ + public function databaseConnectionInfo() + { + $dbserver_binding = (array)$this->bindings->getByType('dbserver'); + if (!empty($dbserver_binding)) { + do { + $db_binding = array_shift($dbserver_binding); + } while ($db_binding->get('environment') != $this->id); + + $username = 'pantheon'; + $password = $db_binding->get('password'); + $hostname = "dbserver.{$this->id}.{$this->site->id}.drush.in"; + $port = $db_binding->get('port'); + $database = 'pantheon'; + $url = "mysql://$username:$password@$hostname:$port/$database"; + $command = "mysql -u $username -p$password -h $hostname -P $port $database"; + $info = [ + 'host' => $hostname, + 'username' => $username, + 'password' => $password, + 'port' => $port, + 'database' => $database, + 'url' => $url, + 'command' => $command, + ]; + } + return $info; + } + /** * Delete a multidev environment * @@ -463,6 +419,28 @@ public function getParentEnvironment() return $environment; } + /** + * Gives Git connection info for this environment + * + * @return array + */ + public function gitConnectionInfo() + { + $username = "codeserver.dev.{$this->site->id}"; + $hostname = "codeserver.dev.{$this->site->id}.drush.in"; + $port = '2222'; + $url = "ssh://$username@$hostname:$port/~/repository.git"; + $command = "git clone $url {$this->site->get('name')}"; + $info = [ + 'username' => $username, + 'host' => $hostname, + 'port' => $port, + 'url' => $url, + 'command' => $command, + ]; + return $info; + } + /** * Decides if the environment has changes to deploy * @@ -707,6 +685,27 @@ public function mergeToDev($options = []) return $workflow; } + /** + * Sends a command to an environment via SSH. + * + * @param string $command The command to be run on the platform + * @return string[] $response Elements as follow: + * string output The output from the command run + * string exit_code The status code returned by the command run + */ + public function sendCommandViaSsh($command) + { + $sftp = $this->sftpConnectionInfo(); + $ssh_command = vsprintf( + 'ssh -T %s@%s -p %s -o "AddressFamily inet" %s', + [$sftp['username'], $sftp['host'], $sftp['port'], escapeshellarg($command),] + ); + ob_start(); + passthru($ssh_command, $exit_code); + $response = ['output' => ob_get_clean(), 'exit_code' => $exit_code,]; + return $response; + } + /** * Add/replace an HTTPS certificate on the environment * @@ -743,6 +742,38 @@ function ($param) { return $workflow; } + /** + * Gives SFTP connection info for this environment + * + * @return array + */ + public function sftpConnectionInfo() + { + if (!empty($ssh_host = Config::get('ssh_host'))) { + $username = "appserver.{$this->id}.{$this->site->id}"; + $hostname = $ssh_host; + } elseif (strpos(Config::get('host'), 'onebox') !== false) { + $username = "appserver.{$this->id}.{$this->site->id}"; + $hostname = Config::get('host'); + } else { + $username = "{$this->id}.{$this->site->id}"; + $hostname = "appserver.{$this->id}.{$this->site->id}.drush.in"; + } + $password = 'Use your account password'; + $port = '2222'; + $url = "sftp://$username@$hostname:$port"; + $command = "sftp -o Port=$port $username@$hostname"; + $info = [ + 'username' => $username, + 'host' => $hostname, + 'port' => $port, + 'password' => $password, + 'url' => $url, + 'command' => $command, + ]; + return $info; + } + /** * Disable HTTP Basic Access authentication on the web environment * diff --git a/tests/features/drush.feature b/tests/features/drush.feature index 1940bf7f4..d90adb271 100644 --- a/tests/features/drush.feature +++ b/tests/features/drush.feature @@ -7,10 +7,10 @@ Feature: Running Drush commands Given I am authenticated And a site named "[[test_site_name]]" - @vcr drush_unavailable + @vcr site_lookup Scenario: Running a command that is not available via Terminus When I run "terminus drush 'sql-connect' --site=[[test_site_name]] --env=dev" Then I should get: """ - sql-connect is not available via Terminus. Please run it via Drush, or you can use `terminus site connection-info --field=mysql_connection` to complete the same task. + That command is not available via Terminus. Please run it via Drush, or you can use `terminus site connection-info --field=mysql_connection` to complete the same task. """ diff --git a/tests/features/wp.feature b/tests/features/wp.feature index b1fb1b5b1..ba0c29cde 100644 --- a/tests/features/wp.feature +++ b/tests/features/wp.feature @@ -7,10 +7,10 @@ Feature: Running WP-CLI commands Given I am authenticated And a site named "[[test_site_name]]" - @vcr wp_unavailable + @vcr site_lookup Scenario: Running a command that is not available via Terminus When I run "terminus wp 'db' --site=[[test_site_name]] --env=dev" Then I should get: """ - db is not available via Terminus. Please run it via WP-CLI. + That command is not available via Terminus. Please run it via WP-CLI. """ diff --git a/tests/unit_tests/models/test-auth.php b/tests/unit_tests/Models/AuthTest.php similarity index 82% rename from tests/unit_tests/models/test-auth.php rename to tests/unit_tests/Models/AuthTest.php index df7aa098e..229351af1 100644 --- a/tests/unit_tests/models/test-auth.php +++ b/tests/unit_tests/Models/AuthTest.php @@ -3,14 +3,13 @@ namespace Terminus\UnitTests\Models; use Terminus\Caches\TokensCache; -use Terminus\Exceptions\TerminusException; use Terminus\Models\Auth; -use Terminus\Runner; +use Terminus\UnitTests\TerminusTest; /** * Testing class for Terminus\Models\Auth */ -class AuthTest extends \PHPUnit_Framework_TestCase +class AuthTest extends TerminusTest { /** @@ -46,8 +45,12 @@ public function testGetMachineTokenCreationUrl() $this->assertInternalType('integer', strpos($url, 'machine-token/create')); } + /** + * @vcr auth_login + */ public function testLoggedIn() { + $this->logInWithVCRCredentials(); $this->assertTrue($this->auth->loggedIn()); } @@ -56,18 +59,20 @@ public function testLoggedIn() */ public function testLogInViaMachineToken() { - $passed = $this->auth->logInViaMachineToken($this->getBehatCredentials()); + $creds = $this->getVCRCredentials(); + $creds['token'] = $creds['machine_token']; + $passed = $this->auth->logInViaMachineToken($creds); $this->assertTrue($passed); $this->setDummyCredentials(); } /** * @expectedException \Terminus\Exceptions\TerminusException - * @expectedExceptionMessage Login unsuccessful + * @expectedExceptionMessage Login unsuccessful for devuser@pantheon.io */ public function testLogInViaUsernameAndPassword() { - $creds = $this->getBehatCredentials(); + $creds = $this->getVCRCredentials(); $this->assertTrue( $this->auth->logInViaUsernameAndPassword( $creds['username'], @@ -83,7 +88,7 @@ public function testTokenExistsForEmail() { $tokens_cache = new TokensCache(); $tokens_dir = $tokens_cache->getCacheDir(); - $creds = $this->getBehatCredentials(); + $creds = $this->getVCRCredentials(); $file_name = $tokens_dir . '/' . $creds['username']; $this->setOutputDestination($file_name); $this->assertTrue($this->auth->tokenExistsForEmail($creds['username'])); diff --git a/tests/unit_tests/models/test-environment.php b/tests/unit_tests/Models/EnvironmentTest.php similarity index 60% rename from tests/unit_tests/models/test-environment.php rename to tests/unit_tests/Models/EnvironmentTest.php index 4cad701eb..87368d498 100644 --- a/tests/unit_tests/models/test-environment.php +++ b/tests/unit_tests/Models/EnvironmentTest.php @@ -3,11 +3,12 @@ namespace Terminus\UnitTests\Models; use Terminus\Collections\Sites; +use Terminus\UnitTests\TerminusTest; /** * Testing class for Terminus\Models\Environment */ -class EnvironmentTest extends \PHPUnit_Framework_TestCase +class EnvironmentTest extends TerminusTest { /** @@ -21,6 +22,37 @@ public function setUp() $this->sites = new Sites(); } + /** + * Ensure correct cacheserver connection info for environments + * + * @vcr site_connection-info + */ + public function testCacheserverConnectionInfo() + { + $this->logInWithVCRCredentials(); + $site = $this->sites->get('behat-tests'); + $env = $site->environments->get('dev'); + + $connection_info = $env->cacheserverConnectionInfo(); + + // Cache server connection connection_info + $pass = 'bf19fbfd2f584df591aa1c8666a8f126'; + $host = '23.253.39.24'; + $port = '11279'; + $user = "pantheon"; + + $cache_info_expected = [ + 'password' => $pass, + 'host' => $host, + 'port' => $port, + 'url' => "redis://$user:$pass@$host:$port", + 'command' => "redis-cli -h $host -p $port -a $pass", + ]; + $this->assertArraySubset($cache_info_expected, $connection_info); + + $this->setDummyCredentials(); + } + /** * Ensure correct connection info for development environments * Development environment connection info includes git parameters @@ -29,7 +61,7 @@ public function setUp() */ public function testConnectionInfoDev() { - $this->$this->logInWithVCRCredentials(); + $this->logInWithVCRCredentials(); $site = $this->sites->get('behat-tests'); $env = $site->environments->get('dev'); @@ -151,6 +183,80 @@ public function testCountNoDeployableCommits() $this->setDummyCredentials(); } + /** + * Ensure correct database connection info for environments + * + * @vcr site_connection-info + */ + public function testDatabaseConnectionInfo() + { + $this->logInWithVCRCredentials(); + $site = $this->sites->get('behat-tests'); + $env = $site->environments->get('dev'); + $connection_info = $env->databaseConnectionInfo(); + + // Database Connection Info + $host = "dbserver.{$env->id}.{$site->id}.drush.in"; + $user = 'pantheon'; + $database = 'pantheon'; + $pass = 'ad7e59695d264b3782c2a9fd959d6a40'; + $port = '16698'; + + $db_info_expected = [ + 'host' => $host, + 'username' => $user, + 'password' => $pass, + 'port' => $port, + 'database' => $database, + 'url' => "mysql://{$user}:{$pass}@{$host}:{$port}/{$database}", + 'command' => "mysql -u $user -p$pass -h $host -P $port $database", + ]; + $this->assertArraySubset($db_info_expected, $connection_info); + $this->setDummyCredentials(); + } + + /** + * @vcr site_deploy + */ + public function testGetParentEnvironment() + { + $this->logInWithVCRCredentials(); + $site = $this->sites->get('behat-tests'); + $test_env = $site->environments->get('test'); + $dev_env = $test_env->getParentEnvironment(); + $this->assertEquals($dev_env->get('id'), 'dev'); + $this->setDummyCredentials(); + } + + /** + * Ensure correct Git connection info for environments + * + * @vcr site_connection-info + */ + public function testGitConnectionInfo() + { + $this->logInWithVCRCredentials(); + $site = $this->sites->get('behat-tests'); + $env = $site->environments->get('dev'); + $connection_info = $env->gitConnectionInfo(); + + // Git Connection Info + $user = "codeserver.{$env->id}.{$site->id}"; + $host = "$user.drush.in"; + $port = '2222'; + $url = "ssh://$user@$host:$port/~/repository.git"; + + $git_info_expected = [ + 'username' => $user, + 'host' => $host, + 'port' => $port, + 'url' => $url, + 'command' => "git clone $url {$site->get('name')}", + ]; + $this->assertArraySubset($git_info_expected, $connection_info); + $this->setDummyCredentials(); + } + /** * @vcr site_deploy */ @@ -176,15 +282,30 @@ public function testHasNoDeployableCode() } /** - * @vcr site_deploy + * Ensure correct SFTP connection info for environments + * + * @vcr site_connection-info */ - public function testGetParentEnvironment() + public function testSftpConnectionInfo() { $this->logInWithVCRCredentials(); - $site = $this->sites->get('behat-tests'); - $test_env = $site->environments->get('test'); - $dev_env = $test_env->getParentEnvironment(); - $this->assertEquals($dev_env->get('id'), 'dev'); + $site = $this->sites->get('behat-tests'); + $env = $site->environments->get('dev'); + $connection_info = $env->sftpConnectionInfo(); + + // SFTP Connection Info + $user = "{$env->id}.{$site->id}"; + $host = "appserver.{$user}.drush.in"; + $port = "2222"; + + $sftp_info_expected = [ + 'username' => $user, + 'host' => $host, + 'password' => 'Use your account password', + 'url' => "sftp://{$env->id}.{$site->id}@$host:$port", + 'command' => "sftp -o Port=$port {$env->id}.{$site->id}@$host", + ]; + $this->assertArraySubset($sftp_info_expected, $connection_info); $this->setDummyCredentials(); } } diff --git a/tests/unit_tests/models/test-upstream.php b/tests/unit_tests/Models/UpstreamTest.php similarity index 98% rename from tests/unit_tests/models/test-upstream.php rename to tests/unit_tests/Models/UpstreamTest.php index 3f51f5695..dd67d416d 100644 --- a/tests/unit_tests/models/test-upstream.php +++ b/tests/unit_tests/Models/UpstreamTest.php @@ -4,11 +4,12 @@ use Terminus\Models\Site; use Terminus\Models\Upstream; +use Terminus\UnitTests\TerminusTest; /** * Testing class for Terminus\Models\Upstream */ -class UpstreamTest extends \PHPUnit_Framework_TestCase +class UpstreamTest extends TerminusTest { /** @@ -16,7 +17,7 @@ class UpstreamTest extends \PHPUnit_Framework_TestCase */ private $site; - /** + /** * @inheritdoc * * @vcr site_upstream-info diff --git a/tests/unit_tests/TerminusTest.php b/tests/unit_tests/TerminusTest.php index 4ccad33fa..3c5afed75 100755 --- a/tests/unit_tests/TerminusTest.php +++ b/tests/unit_tests/TerminusTest.php @@ -47,8 +47,8 @@ public function setUp() */ public function getVCRCredentials() { - $vcr_config = $this->config->get('root') . '/behat.yml'; - return Yaml::parse(file_get_contents($vcr_config)); + $vcr_config = Yaml::parse(file_get_contents($this->config->get('root') . '/tests/config/behat.yml')); + return $vcr_config['default']['suites']['default']['contexts'][0]['Terminus\FeatureTests\FeatureContext']['parameters']; } /** @@ -58,7 +58,8 @@ public function getVCRCredentials() */ public function logInWithVCRCredentials() { - $creds = getVCRCredentials(); + $creds = $this->getVCRCredentials(); + $creds['token'] = $creds['machine_token']; $auth = new Auth(); $auth->logInViaMachineToken($creds); }