1
0
mirror of https://github.com/e107inc/e107.git synced 2025-04-22 05:31:58 +02:00

Partial support for cPanel acceptance tests

via lib/prepare_cpanel.php
This commit is contained in:
Deltik 2018-02-07 05:04:28 -06:00
parent da69eb4d8c
commit ee84c3de76
No known key found for this signature in database
GPG Key ID: 1167C5F9C9897637
12 changed files with 252 additions and 2835 deletions

8
.gitignore vendored Normal file

@ -0,0 +1,8 @@
/secrets.yml
/tests/_output/*
/tests/_support/_generated/*
error_log
*.swp
*.swo

@ -5,6 +5,10 @@ paths:
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
settings:
bootstrap: _bootstrap.php
params:
- secrets.yml
extensions:
enabled:
- Codeception\Extension\RunFailed

187
lib/prepare_cpanel.php Normal file

@ -0,0 +1,187 @@
<?php
include_once(__DIR__ . "/cpaneluapi/cpaneluapi.class.php");
define('ACCEPTANCE_TEST_PREFIX', 'acceptance-test-');
class Prepare_cPanel
{
protected $credentials;
protected $cPanel;
protected $domain;
protected $run_id;
function __construct($credentials)
{
$this->credentials = $credentials;
}
public function getDomain()
{
return $this->domain;
}
public function start()
{
self::println();
self::println("=== Prepare cPanel ===");
$creds = $this->credentials;
if (!$creds['hostname'] ||
!$creds['username'] ||
!$creds['password'])
{
self::println("Cannot deploy cPanel environment because credentials are missing. Falling back to manual mode…");
return false;
}
$username = &$creds['username'];
$password = &$creds['password'];
$hostname = &$creds['hostname'];
$this->run_id = $run_id = uniqid(ACCEPTANCE_TEST_PREFIX);
self::println("Test run ID: ".$this->run_id);
$this->cPanel = $cPanel = new cpanelAPI($username, $password, $hostname);
self::println("Connecting to cPanel at \"${hostname}\" with username \"${username}\"");
$domains_data = $cPanel->uapi->DomainInfo->domains_data();
if (!$domains_data)
{
throw new Exception("Cannot connect to cPanel at \"${hostname}\" with username \"${username}\" and password \"${password}\"");
}
$userdata = $domains_data->{'data'};
$homedir = $userdata->{'main_domain'}->{'homedir'};
$docroot = $userdata->{'main_domain'}->{'documentroot'};
$this->domain = $domain = $userdata->{'main_domain'}->{'domain'};
self::println("Obtained domain name from cPanel: " . $this->domain);
self::println("Retrieving existing registered tests from cPanel account…");
$acceptance_tests = self::get_active_acceptance_tests($cPanel, $homedir);
$run_time = microtime(true);
array_push($acceptance_tests,
['id' => $run_id,
'time' => $run_time
]);
self::println("Writing this test to registered tests list in cPanel account…");
self::write_acceptance_tests($cPanel, $homedir, $acceptance_tests);
$valid_acceptance_test_ids = self::get_acceptance_test_ids($acceptance_tests);
self::println("Current unexpired tests: [".implode(", ", $valid_acceptance_test_ids)."]");
self::println("Pruning expired tests…");
self::prune_inactive_acceptance_test_resources($cPanel, $valid_acceptance_test_ids);
$db_id = "${username}_${run_id}";
self::println("Creating new MySQL database \"${db_id}\"");
$cPanel->uapi->Mysql->create_database(['name' => $db_id]);
self::println("Creating new MySQL user \"${db_id}\" with password \"${run_id}\"");
$cPanel->uapi->Mysql->create_user(['name' => $db_id, 'password' => $run_id]);
self::println("Granting ALL PRIVILEGES to MySQL user \"${db_id}\"");
$cPanel->uapi->Mysql->set_privileges_on_database(['user' => "${username}_${run_id}",
'database' => "${username}_${run_id}",
'privileges' => 'ALL PRIVILEGES'
]);
# TODO: Upload software to test
return true;
}
public function stop()
{
$acceptance_tests = self::get_active_acceptance_tests($cPanel, $homedir);
$acceptance_tests = self::prune_acceptance_tests($acceptance_tests, $this->run_id);
self::write_acceptance_tests($cPanel, $homedir, $acceptance_tests);
}
private static function println($text = '')
{
echo($text."\n");
}
private static function prune_inactive_acceptance_test_resources($cPanel, $valid_acceptance_test_ids)
{
$listdbs = $cPanel->api2->MysqlFE->listdbs()->{'cpanelresult'}->{'data'};
self::prune_mysql_databases($listdbs, $valid_acceptance_test_ids, $cPanel);
$listdbusers = $cPanel->api2->MysqlFE->listusers()->{'cpanelresult'}->{'data'};
self::prune_mysql_users($listdbusers, $valid_acceptance_test_ids, $cPanel);
}
private static function get_active_acceptance_tests($cPanel, $homedir)
{
$acceptance_tests = [];
$acceptance_tests_apiresponse = $cPanel->uapi->Fileman->get_file_content(['dir' => $homedir, 'file' => 'acceptance_tests.status.txt']);
if (!is_null($acceptance_tests_apiresponse->{'data'}))
{
$acceptance_tests_raw = $acceptance_tests_apiresponse->{'data'}->{'content'};
$acceptance_tests = json_decode($acceptance_tests_raw, true);
self::prune_acceptance_tests($acceptance_tests);
}
return $acceptance_tests;
}
private static function prune_acceptance_tests(array &$list, $id_to_remove = null)
{
foreach ($list as $key => $item)
{
$time = $item['time'];
if ($item['id'] === $id_to_remove || $time <= strtotime("now - 10 seconds"))
{
unset($list[$key]);
}
}
$list = array_values($list);
}
private static function get_acceptance_test_ids(array $list)
{
$ids = [];
foreach ($list as $item)
{
$ids[] = $item['id'];
}
return $ids;
}
private static function write_acceptance_tests($cPanel, $homedir, $acceptance_tests)
{
$acceptance_tests_json = json_encode($acceptance_tests, JSON_PRETTY_PRINT);
$cPanel->uapi->Fileman->save_file_content(['dir' => $homedir, 'file' => 'acceptance_tests.status.txt', 'content' => $acceptance_tests_json]);
}
private static function prune_mysql_databases($dbs, $ids, $cPanel)
{
foreach ($dbs as $db)
{
$db = (array) $db;
$offset = strpos($db['db'], ACCEPTANCE_TEST_PREFIX);
$questionable_db = substr($db['db'], $offset);
if (!in_array($questionable_db, $ids))
{
$cPanel->uapi->Mysql->delete_database(['name' => $db['db']]);
}
}
}
private static function prune_mysql_users($users, $ids, $cPanel)
{
foreach ($users as $user)
{
$user = (array) $user;
$offset = strpos($user['user'], ACCEPTANCE_TEST_PREFIX);
$questionable_user = substr($user['user'], $offset);
if (!in_array($questionable_user, $ids))
{
$cPanel->uapi->Mysql->delete_user(['name' => $user['user']]);
}
}
}
}

8
secrets.yml Normal file

@ -0,0 +1,8 @@
---
# Configure this section for automated cPanel acceptance testing
cpanel:
enabled: false
hostname: ''
username: ''
password: ''

1
tests/_bootstrap.php Normal file

@ -0,0 +1 @@
<?php

@ -1,10 +1,24 @@
<?php
namespace Helper;
include_once(__DIR__ . "/../../../lib/prepare_cpanel.php");
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Acceptance extends \Codeception\Module
{
public function _beforeSuite($settings = array())
{
$secrets = $settings['secrets'];
if ($secrets['cpanel']['enabled'] === '1')
{
$prepare = new \Prepare_cPanel($secrets['cpanel']);
$retcode = $prepare->start();
if ($retcode === true)
{
$domain = $prepare->getDomain();
$this->getModule('PhpBrowser')->_reconfigure(array('url' => "http://${domain}"));
}
}
}
}

File diff suppressed because it is too large Load Diff

@ -1,16 +0,0 @@
<?php //[STAMP] 8f438bd57792c93435e1e1cfc7771c71
namespace _generated;
// This class was automatically generated by build task
// You should not change it manually as it will be overwritten on next build
// @codingStandardsIgnoreFile
trait FunctionalTesterActions
{
/**
* @return \Codeception\Scenario
*/
abstract protected function getScenario();
}

@ -1,545 +0,0 @@
<?php //[STAMP] 944b755085a310113b19d30e9d04d255
namespace _generated;
// This class was automatically generated by build task
// You should not change it manually as it will be overwritten on next build
// @codingStandardsIgnoreFile
trait UnitTesterActions
{
/**
* @return \Codeception\Scenario
*/
abstract protected function getScenario();
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that two variables are equal. If you're comparing floating-point values,
* you can specify the optional "delta" parameter which dictates how great of a precision
* error are you willing to tolerate in order to consider the two values equal.
*
* Regular example:
* ```php
* <?php
* $I->assertEquals($element->getChildrenCount(), 5);
* ```
*
* Floating-point example:
* ```php
* <?php
* $I->assertEquals($calculator->add(0.1, 0.2), 0.3, 'Calculator should add the two numbers correctly.', 0.01);
* ```
*
* @param $expected
* @param $actual
* @param string $message
* @param float $delta
* @see \Codeception\Module\Asserts::assertEquals()
*/
public function assertEquals($expected, $actual, $message = null, $delta = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertEquals', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that two variables are not equal. If you're comparing floating-point values,
* you can specify the optional "delta" parameter which dictates how great of a precision
* error are you willing to tolerate in order to consider the two values not equal.
*
* Regular example:
* ```php
* <?php
* $I->assertNotEquals($element->getChildrenCount(), 0);
* ```
*
* Floating-point example:
* ```php
* <?php
* $I->assertNotEquals($calculator->add(0.1, 0.2), 0.4, 'Calculator should add the two numbers correctly.', 0.01);
* ```
*
* @param $expected
* @param $actual
* @param string $message
* @param float $delta
* @see \Codeception\Module\Asserts::assertNotEquals()
*/
public function assertNotEquals($expected, $actual, $message = null, $delta = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotEquals', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that two variables are same
*
* @param $expected
* @param $actual
* @param string $message
* @see \Codeception\Module\Asserts::assertSame()
*/
public function assertSame($expected, $actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertSame', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that two variables are not same
*
* @param $expected
* @param $actual
* @param string $message
* @see \Codeception\Module\Asserts::assertNotSame()
*/
public function assertNotSame($expected, $actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotSame', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that actual is greater than expected
*
* @param $expected
* @param $actual
* @param string $message
* @see \Codeception\Module\Asserts::assertGreaterThan()
*/
public function assertGreaterThan($expected, $actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterThan', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that actual is greater or equal than expected
*
* @param $expected
* @param $actual
* @param string $message
* @see \Codeception\Module\Asserts::assertGreaterThanOrEqual()
*/
public function assertGreaterThanOrEqual($expected, $actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterThanOrEqual', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that actual is less than expected
*
* @param $expected
* @param $actual
* @param string $message
* @see \Codeception\Module\Asserts::assertLessThan()
*/
public function assertLessThan($expected, $actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertLessThan', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that actual is less or equal than expected
*
* @param $expected
* @param $actual
* @param string $message
* @see \Codeception\Module\Asserts::assertLessThanOrEqual()
*/
public function assertLessThanOrEqual($expected, $actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertLessThanOrEqual', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that haystack contains needle
*
* @param $needle
* @param $haystack
* @param string $message
* @see \Codeception\Module\Asserts::assertContains()
*/
public function assertContains($needle, $haystack, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertContains', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that haystack doesn't contain needle.
*
* @param $needle
* @param $haystack
* @param string $message
* @see \Codeception\Module\Asserts::assertNotContains()
*/
public function assertNotContains($needle, $haystack, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotContains', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that string match with pattern
*
* @param string $pattern
* @param string $string
* @param string $message
* @see \Codeception\Module\Asserts::assertRegExp()
*/
public function assertRegExp($pattern, $string, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertRegExp', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that string not match with pattern
*
* @param string $pattern
* @param string $string
* @param string $message
* @see \Codeception\Module\Asserts::assertNotRegExp()
*/
public function assertNotRegExp($pattern, $string, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotRegExp', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that a string starts with the given prefix.
*
* @param string $prefix
* @param string $string
* @param string $message
* @see \Codeception\Module\Asserts::assertStringStartsWith()
*/
public function assertStringStartsWith($prefix, $string, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertStringStartsWith', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that a string doesn't start with the given prefix.
*
* @param string $prefix
* @param string $string
* @param string $message
* @see \Codeception\Module\Asserts::assertStringStartsNotWith()
*/
public function assertStringStartsNotWith($prefix, $string, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertStringStartsNotWith', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that variable is empty.
*
* @param $actual
* @param string $message
* @see \Codeception\Module\Asserts::assertEmpty()
*/
public function assertEmpty($actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertEmpty', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that variable is not empty.
*
* @param $actual
* @param string $message
* @see \Codeception\Module\Asserts::assertNotEmpty()
*/
public function assertNotEmpty($actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotEmpty', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that variable is NULL
*
* @param $actual
* @param string $message
* @see \Codeception\Module\Asserts::assertNull()
*/
public function assertNull($actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNull', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that variable is not NULL
*
* @param $actual
* @param string $message
* @see \Codeception\Module\Asserts::assertNotNull()
*/
public function assertNotNull($actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotNull', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that condition is positive.
*
* @param $condition
* @param string $message
* @see \Codeception\Module\Asserts::assertTrue()
*/
public function assertTrue($condition, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertTrue', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that condition is negative.
*
* @param $condition
* @param string $message
* @see \Codeception\Module\Asserts::assertFalse()
*/
public function assertFalse($condition, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertFalse', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks if file exists
*
* @param string $filename
* @param string $message
* @see \Codeception\Module\Asserts::assertFileExists()
*/
public function assertFileExists($filename, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertFileExists', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks if file doesn't exist
*
* @param string $filename
* @param string $message
* @see \Codeception\Module\Asserts::assertFileNotExists()
*/
public function assertFileNotExists($filename, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertFileNotExists', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $expected
* @param $actual
* @param $description
* @see \Codeception\Module\Asserts::assertGreaterOrEquals()
*/
public function assertGreaterOrEquals($expected, $actual, $description = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterOrEquals', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $expected
* @param $actual
* @param $description
* @see \Codeception\Module\Asserts::assertLessOrEquals()
*/
public function assertLessOrEquals($expected, $actual, $description = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertLessOrEquals', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $actual
* @param $description
* @see \Codeception\Module\Asserts::assertIsEmpty()
*/
public function assertIsEmpty($actual, $description = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertIsEmpty', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $key
* @param $actual
* @param $description
* @see \Codeception\Module\Asserts::assertArrayHasKey()
*/
public function assertArrayHasKey($key, $actual, $description = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertArrayHasKey', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $key
* @param $actual
* @param $description
* @see \Codeception\Module\Asserts::assertArrayNotHasKey()
*/
public function assertArrayNotHasKey($key, $actual, $description = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertArrayNotHasKey', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Checks that array contains subset.
*
* @param array $subset
* @param array $array
* @param bool $strict
* @param string $message
* @see \Codeception\Module\Asserts::assertArraySubset()
*/
public function assertArraySubset($subset, $array, $strict = null, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertArraySubset', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $expectedCount
* @param $actual
* @param $description
* @see \Codeception\Module\Asserts::assertCount()
*/
public function assertCount($expectedCount, $actual, $description = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertCount', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $class
* @param $actual
* @param $description
* @see \Codeception\Module\Asserts::assertInstanceOf()
*/
public function assertInstanceOf($class, $actual, $description = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertInstanceOf', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $class
* @param $actual
* @param $description
* @see \Codeception\Module\Asserts::assertNotInstanceOf()
*/
public function assertNotInstanceOf($class, $actual, $description = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotInstanceOf', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @param $type
* @param $actual
* @param $description
* @see \Codeception\Module\Asserts::assertInternalType()
*/
public function assertInternalType($type, $actual, $description = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertInternalType', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Fails the test with message.
*
* @param $message
* @see \Codeception\Module\Asserts::fail()
*/
public function fail($message) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('fail', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Handles and checks exception called inside callback function.
* Either exception class name or exception instance should be provided.
*
* ```php
* <?php
* $I->expectException(MyException::class, function() {
* $this->doSomethingBad();
* });
*
* $I->expectException(new MyException(), function() {
* $this->doSomethingBad();
* });
* ```
* If you want to check message or exception code, you can pass them with exception instance:
* ```php
* <?php
* // will check that exception MyException is thrown with "Don't do bad things" message
* $I->expectException(new MyException("Don't do bad things"), function() {
* $this->doSomethingBad();
* });
* ```
*
* @param $exception string or \Exception
* @param $callback
* @see \Codeception\Module\Asserts::expectException()
*/
public function expectException($exception, $callback) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('expectException', func_get_args()));
}
}

@ -8,5 +8,11 @@ actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://localhost/myapp
- \Helper\Acceptance
url: http://set-this-if-running-acceptance-tests-manually.local
- \Helper\Acceptance:
secrets:
cpanel:
enabled: '%cpanel.enabled%'
hostname: '%cpanel.hostname%'
username: '%cpanel.username%'
password: '%cpanel.password%'

@ -0,0 +1,20 @@
<?php
class InstallCest
{
public function _before(AcceptanceTester $I)
{
}
public function _after(AcceptanceTester $I)
{
}
// tests
public function tryToTest(AcceptanceTester $I)
{
$I->amOnPage('/');
$I->see("Home");
}
}

@ -0,0 +1 @@
<?php