/** * Test that a command with input arguments returns an expected result * * @param \Magento\Framework\Shell $shell * @param string $command * @param array $commandArgs * @param string $expectedResult */ protected function _testExecuteCommand(\Magento\Framework\Shell $shell, $command, $commandArgs, $expectedResult) { $this->expectOutputString(''); // nothing is expected to be ever printed to the standard output $actualResult = $shell->execute($command, $commandArgs); $this->assertEquals($expectedResult, $actualResult); }
/** * Retrieve the current process' memory usage using Windows command line interface * * @link http://technet.microsoft.com/en-us/library/bb491010.aspx * @param int $pid * @return int Memory usage in bytes */ protected function _getWinProcessMemoryUsage($pid) { $output = $this->_shell->execute('tasklist.exe /fi %s /fo CSV /nh', ["PID eq {$pid}"]); $arr = str_getcsv($output); $memory = $arr[4]; return self::convertToBytes($memory); }
/** * Run scenario and optionally write results to report file * * @param \Magento\TestFramework\Performance\Scenario $scenario * @param string|null $reportFile Report file to write results to, NULL disables report creation * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\TestFramework\Performance\Scenario\FailureException */ public function run(\Magento\TestFramework\Performance\Scenario $scenario, $reportFile = null) { $this->_validateScenarioExecutable(); $cmd = $this->_buildScenarioCmd($scenario, $reportFile); list($scenarioCmd, $scenarioCmdArgs) = $cmd; $this->_shell->execute($scenarioCmd, $scenarioCmdArgs); if ($reportFile) { if (!file_exists($reportFile)) { throw new \Magento\Framework\Exception\LocalizedException(new Phrase("Report file '%1' for '%2' has not been created.", [$reportFile, $scenario->getTitle()])); } $reportErrors = $this->_getReportErrors($reportFile); if ($reportErrors) { throw new \Magento\TestFramework\Performance\Scenario\FailureException($scenario, new Phrase(implode(PHP_EOL, $reportErrors))); } } }
/** * Install an application * * @return void * @throws \Magento\Framework\Exception\LocalizedException */ public function install() { $dirs = \Magento\Framework\App\Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS; $this->_ensureDirExists($this->installDir); $this->_ensureDirExists($this->_configDir); $this->_ensureDirExists($this->_initParams[$dirs][DirectoryList::PUB][DirectoryList::PATH]); $this->_ensureDirExists($this->_initParams[$dirs][DirectoryList::MEDIA][DirectoryList::PATH]); $this->_ensureDirExists($this->_initParams[$dirs][DirectoryList::STATIC_VIEW][DirectoryList::PATH]); $this->_ensureDirExists($this->_initParams[$dirs][DirectoryList::VAR_DIR][DirectoryList::PATH]); $this->copyAppConfigFiles(); $installParams = $this->getInstallCliParams(); // performance optimization: restore DB from last good dump to make installation on top of it (much faster) $db = $this->getDbInstance(); if ($db->isDbDumpExists()) { $db->restoreFromDbDump(); } // run install script $this->_shell->execute(PHP_BINARY . ' -f %s setup:install -vvv ' . implode(' ', array_keys($installParams)), array_merge([BP . '/bin/magento'], array_values($installParams))); // enable only specified list of caches $initParamsQuery = $this->getInitParamsQuery(); $this->_shell->execute(PHP_BINARY . ' -f %s cache:disable -vvv --bootstrap=%s', [BP . '/bin/magento', $initParamsQuery]); $this->_shell->execute(PHP_BINARY . ' -f %s cache:enable -vvv %s %s %s %s --bootstrap=%s', [BP . '/bin/magento', \Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER, \Magento\Framework\App\Cache\Type\Layout::TYPE_IDENTIFIER, \Magento\Framework\App\Cache\Type\Translate::TYPE_IDENTIFIER, \Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER, $initParamsQuery]); // right after a clean installation, store DB dump for future reuse in tests or running the test suite again if (!$db->isDbDumpExists()) { $this->getDbInstance()->storeDbDump(); } }
/** * Test DI compiler * * @depends testConfigurationOfInstanceParameters * @depends testConstructorIntegrity * @depends testPluginInterfaces */ public function testCompiler() { try { $this->_shell->execute($this->_command, [$this->_generationDir, $this->_compilationDir]); } catch (\Magento\Framework\Exception $exception) { $this->fail($exception->getPrevious()->getMessage()); } }
/** * Test DI compiler * * @depends testConfigurationOfInstanceParameters * @depends testConstructorIntegrity * @depends testPluginInterfaces */ public function testCompiler() { try { $this->_shell->execute($this->_command); } catch (\Magento\Framework\Exception\LocalizedException $exception) { $this->fail($exception->getPrevious()->getMessage()); } }
/** * Check if composer command is available in the environment * * @return bool */ private static function isComposerAvailable() { try { self::$shell->execute(self::$composerPath . ' --version'); } catch (LocalizedException $e) { return false; } return true; }
/** * Execute scenario and return measurement results * * @param \Magento\TestFramework\Performance\Scenario $scenario * @return array */ protected function _executeScenario(\Magento\TestFramework\Performance\Scenario $scenario) { list($scenarioCmd, $scenarioCmdArgs) = $this->_buildScenarioCmd($scenario); $result = ['title' => $scenario->getTitle(), 'timestamp' => time(), 'success' => true, 'time' => null, 'exit_code' => 0, 'output' => '']; $executionTime = microtime(true); try { $result['output'] = $this->_shell->execute($scenarioCmd, $scenarioCmdArgs); } catch (\Magento\Framework\Exception $e) { $result['success'] = false; $result['exit_code'] = $e->getPrevious()->getCode(); $result['output'] = $e->getPrevious()->getMessage(); } $executionTime = microtime(true) - $executionTime; $executionTime *= 1000; // second -> millisecond $result['time'] = (int) round($executionTime); return $result; }
/** * Apply fixtures test with no reinstall */ public function testApplyFixturesIncompatibleSetReinstallation() { $this->_shell->expects($this->at(0))->method('execute')->with($this->anything(), $this->contains($this->_script)); $this->_shell->expects($this->at(1))->method('execute')->with($this->anything(), $this->contains($this->_script)); $fixtures = $this->_getFixtureFiles(['fixture1', 'fixture2']); $this->_object->applyFixtures($fixtures); $incompatibleSet = $this->_getFixtureFiles(['fixture1']); $this->_object->applyFixtures($incompatibleSet); }
/** * Retrieve the current process' memory usage using Windows command line interface * * @link http://technet.microsoft.com/en-us/library/bb491010.aspx * @param int $pid * @return int Memory usage in bytes */ protected function _getWinProcessMemoryUsage($pid) { $output = $this->_shell->execute('tasklist.exe /fi %s /fo CSV /nh', array("PID eq {$pid}")); /** @link http://www.php.net/manual/en/wrappers.data.php */ $csvStream = 'data://text/plain;base64,' . base64_encode($output); $csvHandle = fopen($csvStream, 'r'); $stats = fgetcsv($csvHandle); fclose($csvHandle); $result = $stats[4]; return self::convertToBytes($result); }
/** * Apply fixtures test with no reinstall */ public function testApplyFixturesIncompatibleSetReinstallation() { $this->_shell->expects($this->at(0))->method('execute')->with($this->stringContains('--uninstall'), $this->contains($this->_installerScript)); $this->_shell->expects($this->at(1))->method('execute')->with($this->logicalNot($this->stringContains('--uninstall')), $this->contains($this->_installerScript)); $this->_shell->expects($this->at(6))->method('execute')->with($this->stringContains('--uninstall'), $this->contains($this->_installerScript)); $this->_shell->expects($this->at(7))->method('execute')->with($this->logicalNot($this->stringContains('--uninstall')), $this->contains($this->_installerScript)); $fixtures = $this->_getFixtureFiles(array('fixture1', 'fixture2')); $this->_object->applyFixtures($fixtures); $incompatibleSet = $this->_getFixtureFiles(array('fixture1')); $this->_object->applyFixtures($incompatibleSet); }
/** * Runs code multi-tenant compiler to generate code and DI information * * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function compile(\Symfony\Component\Console\Output\OutputInterface $output) { $output->writeln('Start compilation'); $this->cleanupFilesystem([DirectoryList::CACHE, DirectoryList::GENERATION, DirectoryList::DI]); $cmd = $this->functionCallPath . 'setup:di:compile'; /** * exec command is necessary for now to isolate the autoloaders in the compiler from the memory state * of this process, which would prevent some classes from being generated * * @todo build a solution that does not depend on exec */ $execOutput = $this->shell->execute($cmd); $output->writeln($execOutput); $output->writeln('Compilation complete'); }
/** * Reinstall Db for source and destination * * @param string $fixturePath * @throws \Exception * @throws \Magento\Framework\Exception\LocalizedException */ protected function reinstallDb($fixturePath) { $mysqlConfigPath = dirname(__DIR__) . '/etc/mysql.php'; if (!is_file($mysqlConfigPath)) { throw new \Exception('Database configuration file does not exist: ' . $mysqlConfigPath); } $resourceSource = $fixturePath . '/source.sql'; $resourceDestination = $fixturePath . '/dest.sql'; if (file_exists($this->dbFixturePath . $fixturePath)) { $resourceSource = $this->dbFixturePath . $fixturePath . '/source.sql'; $resourceDestination = $this->dbFixturePath . $fixturePath . '/dest.sql'; } elseif (!file_exists($fixturePath)) { throw new \Exception('Database fixture not found: ' . $fixturePath); } $config = (include $mysqlConfigPath); $this->shell->execute('mysql --host=%s --user=%s --password=%s -e %s', [$config['source_db_host'], $config['source_db_user'], $config['source_db_pass'], "DROP DATABASE IF EXISTS `{$config['source_db_name']}`"]); $this->shell->execute('mysql --host=%s --user=%s --password=%s -e %s', [$config['source_db_host'], $config['source_db_user'], $config['source_db_pass'], "CREATE DATABASE IF NOT EXISTS `{$config['source_db_name']}`"]); $this->shell->execute('mysql --host=%s --user=%s --password=%s --database=%s < %s', [$config['source_db_host'], $config['source_db_user'], $config['source_db_pass'], $config['source_db_name'], $resourceSource]); $this->shell->execute('mysql --host=%s --user=%s --password=%s -e %s', [$config['dest_db_host'], $config['dest_db_user'], $config['dest_db_pass'], "DROP DATABASE IF EXISTS `{$config['dest_db_name']}`"]); $this->shell->execute('mysql --host=%s --user=%s --password=%s -e %s', [$config['dest_db_host'], $config['dest_db_user'], $config['dest_db_pass'], "CREATE DATABASE `{$config['dest_db_name']}`"]); $this->shell->execute('mysql --host=%s --user=%s --password=%s --database=%s < %s', [$config['dest_db_host'], $config['dest_db_user'], $config['dest_db_pass'], $config['dest_db_name'], $resourceDestination]); }
/** * Install application according to installation options * * @return \Magento\TestFramework\Application * @throws \Magento\Framework\Exception */ protected function _install() { $installOptions = $this->_config->getInstallOptions(); $installOptionsNoValue = $this->_config->getInstallOptionsNoValue(); if (!$installOptions) { throw new \Magento\Framework\Exception('Trying to install Magento, but installation options are not set'); } // Populate install options with global options $baseUrl = 'http://' . $this->_config->getApplicationUrlHost() . $this->_config->getApplicationUrlPath(); $installOptions = array_merge($installOptions, ['base_url' => $baseUrl, 'base_url_secure' => $baseUrl]); $installCmd = 'php -f %s install'; $installCmdArgs = [$this->_script]; foreach ($installOptions as $optionName => $optionValue) { $installCmd .= " --{$optionName}=%s"; $installCmdArgs[] = $optionValue; } foreach ($installOptionsNoValue as $optionName) { $installCmd .= " --{$optionName}"; } $this->_shell->execute($installCmd, $installCmdArgs); $this->_isInstalled = true; $this->_fixtures = []; return $this; }
/** * Install application according to installation options * * @return \Magento\TestFramework\Application * @throws \Magento\Framework\Exception */ protected function _install() { $installOptions = $this->_config->getInstallOptions(); if (!$installOptions) { throw new \Magento\Framework\Exception('Trying to install Magento, but installation options are not set'); } // Populate install options with global options $baseUrl = 'http://' . $this->_config->getApplicationUrlHost() . $this->_config->getApplicationUrlPath(); $installOptions = array_merge($installOptions, array('url' => $baseUrl, 'secure_base_url' => $baseUrl)); $adminOptions = $this->_config->getAdminOptions(); foreach ($adminOptions as $key => $val) { $installOptions['admin_' . $key] = $val; } $installCmd = 'php -f %s --'; $installCmdArgs = array($this->_installerScript); foreach ($installOptions as $optionName => $optionValue) { $installCmd .= " --{$optionName} %s"; $installCmdArgs[] = $optionValue; } $this->_shell->execute($installCmd, $installCmdArgs); $this->_isInstalled = true; $this->_fixtures = array(); return $this; }
/** * Run reindex * * @return Application */ public function reindex() { $this->_shell->execute('php -f ' . $this->_applicationBaseDir . '/dev/shell/indexer.php -- reindexall'); return $this; }
/** * @expectedException \Magento\TestFramework\Performance\Scenario\FailureException * @expectedExceptionMessage command failure message */ public function testRunException() { $failure = new \Magento\Framework\Exception('Command returned non-zero exit code.', 0, new \Exception('command failure message', 1)); $this->_shell->expects($this->any())->method('execute')->will($this->throwException($failure)); $this->_object->run($this->_scenario); }
public function testRunReport() { $this->_shell->expects($this->once())->method('execute')->with('jmeter -n -t %s -l %s %s %s %s %s', [$this->_scenarioFile, $this->_reportFile, '-Jhost=127.0.0.1', '-Jpath=/', '-Jusers=2', '-Jloops=3']); $this->_object->run($this->_scenario, $this->_reportFile); }